你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

Azure OpenAI 助手函数调用

助手 API 支持函数调用,允许你向助手描述函数的结构,然后返回需要调用的函数及其参数。

注意

  • 文件搜索可为每个助手最多引入 10,000 个文件 - 比之前多 500 倍。 它速度快,支持通过多线程搜索进行并行查询,并具有增强的重新排序和查询重写功能。
    • 矢量存储是 API 中的新对象。 文件一旦添加到矢量存储中,就会自动进行分析、分块和嵌入,以便随时搜索。 矢量存储可跨助手和线程使用,简化了文件管理和计费。
  • 我们添加了对 tool_choice 参数的支持,该参数可用于在特定运行中强制使用特定工具(如文件搜索、代码解释器或函数)。

函数调用支持

支持的模型

模型页包含有关支持助手的区域/模型的最新信息。

若要使用函数调用的所有功能(包括并行函数),则需要使用在 2023 年 11 月 6 日之后发布的模型。

API 版本

  • 2024-02-15-preview
  • 2024-05-01-preview

示例函数定义

注意

  • 我们添加了对 tool_choice 参数的支持,该参数可用于在特定运行中强制使用特定工具(如 file_searchcode_interpreterfunction)。
  • 创建 10 分钟后运行将过期。 请确保在此过期之前提交工具输出。
  • 还可以使用 Azure 逻辑应用执行函数调用
from openai import AzureOpenAI
    
client = AzureOpenAI(
    api_key=os.getenv("AZURE_OPENAI_API_KEY"),  
    api_version="2024-02-15-preview",
    azure_endpoint = os.getenv("AZURE_OPENAI_ENDPOINT")
    )

assistant = client.beta.assistants.create(
  instructions="You are a weather bot. Use the provided functions to answer questions.",
  model="gpt-4-1106-preview", #Replace with model deployment name
  tools=[{
      "type": "function",
    "function": {
      "name": "getCurrentWeather",
      "description": "Get the weather in location",
      "parameters": {
        "type": "object",
        "properties": {
          "location": {"type": "string", "description": "The city and state e.g. San Francisco, CA"},
          "unit": {"type": "string", "enum": ["c", "f"]}
        },
        "required": ["location"]
      }
    }
  }, {
    "type": "function",
    "function": {
      "name": "getNickname",
      "description": "Get the nickname of a city",
      "parameters": {
        "type": "object",
        "properties": {
          "location": {"type": "string", "description": "The city and state e.g. San Francisco, CA"},
        },
        "required": ["location"]
      }
    } 
  }]
)

读取函数

使用触发函数的用户消息启动“运行”时,“运行”将进入挂起状态。 处理完成后,运行将进入 require_action 状态,可以通过检索“运行”进行验证

{
  "id": "run_abc123",
  "object": "thread.run",
  "assistant_id": "asst_abc123",
  "thread_id": "thread_abc123",
  "status": "requires_action",
  "required_action": {
    "type": "submit_tool_outputs",
    "submit_tool_outputs": {
      "tool_calls": [
        {
          "id": "call_abc123",
          "type": "function",
          "function": {
            "name": "getCurrentWeather",
            "arguments": "{\"location\":\"San Francisco\"}"
          }
        },
        {
          "id": "call_abc456",
          "type": "function",
          "function": {
            "name": "getNickname",
            "arguments": "{\"location\":\"Los Angeles\"}"
          }
        }
      ]
    }
  },
...

提交函数输出

然后,可以通过提交调用的函数的工具输出来完成“运行”。 传递上面 required_action 对象中引用的 tool_call_id,以匹配每个函数调用的输出。

from openai import AzureOpenAI
    
client = AzureOpenAI(
    api_key=os.getenv("AZURE_OPENAI_API_KEY"),  
    api_version="2024-02-15-preview",
    azure_endpoint = os.getenv("AZURE_OPENAI_ENDPOINT")
    )


run = client.beta.threads.runs.submit_tool_outputs(
  thread_id=thread.id,
  run_id=run.id,
  tool_outputs=[
      {
        "tool_call_id": call_ids[0],
        "output": "22C",
      },
      {
        "tool_call_id": call_ids[1],
        "output": "LA",
      },
    ]
)

提交工具输出后,“运行”将进入 queued 状态,然后继续执行

另请参阅