What’s Function Calling

Function Calling 是OpenAI于2024年3月更新中引入的新特性,最开始仅能在gpt-3.5-turbo-0613 和 gpt-4-0613 中支持,目前除了OpenAI外,国内外各大主流模型也均支持了Function Calling。

LLMs通过Function Calling与外部工具建立了连接,拓展了LLMs的能力边界,使得能够更加灵活地应对各种复杂任务和场景。
其本质是实现了准确识别用户的语义,将其转为结构化的指令。这里的结构化的指令其实指的就是要调用的函数的名称和入参,有了函数名称和调用参数,开发者就可以调用函数并把结果发给LLM,让LLM给出更人性化的回答。

因此,Function Calling 特性并不是指模型主动调用函数,而是指会根据用户意图和提供的函数列表选择合适的函数并返回调用函数所需的参数。 而真正调用函数的动作,需要开发者通过解析模型返回调用函数所需的Json数据包后自行调用。

Function Calling Demo

接下来就以一个具体示例来介绍下Function Calling 的具体应用。假设您想使用 OpenAI 模型来获取特定城市的当前天气。

Function Calling

0. 定义函数

假设代码中已经定义了一个函数get_current_weather,其方法签名如下:public string get_current_weather(string city) { ....}

1. 初始请求

发送到 OpenAI 的请求的 JSON 数据包可能如下所示:

{
    "messages": [
        {
            "role": "system",
            "content": "你是一个聊天助手,请使用提供的工具协助用户。"
        },
        {
            "role": "user",
            "content": "深圳今天的天气怎样"
        }
    ],
    "tools": [
        {
            "type": "function",
            "function": {
                "name": "get_current_weather",
                "description": "获取指定城市的天气",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "city": {
                            "type": "string",
                            "description": "城市名"
                        }
                    }
                },
                "required": ["city"]
            }
        }
    ]
}

核心参数解释

以上的这个 tools 部分定义了一个名为 get_current_weather 的函数,它需要一个名为 city 的字符串参数,用于指定想要查询天气的城市。当模型需要调用这个函数时,它将使用这个参数来获取相应的天气信息。

  • tools:在 tools 部分定义了一个函数,这个函数可以被 OpenAI 的模型调用。以下是 tools 部分参数的简单解释:

  • type: 指定了这个工具的类型,这里是 “function”,表示这是一个函数调用。

  • function: 包含函数的详细信息,是一个对象。

    • name: 函数的名称,这里是 “get_current_weather”,这是调用时使用的函数名。

    • description: 函数的描述,这里是 “获取指定城市的天气”,用于说明这个函数的作用。

    • parameters: 定义了函数调用时需要的参数,是一个对象。

      • type: 参数对象的类型,这里是 “object”,表示参数是一个对象类型。

      • properties: 包含具体的参数定义,是一个对象,每个属性对应一个参数。

        • city: 这是一个参数的名称,表示城市名称。
          • type: 此参数的类型,这里是 “string”,表示参数应该是一个字符串。
          • description: 参数的描述,这里是 “城市名”,用于解释这个参数的意义。
    • required: 用于指定哪些参数是必填项

2. 大模型返回函数调用

初始请求的返回结果如下所示,最核心的是tool_calls 中返回了获取天气的函数名称("name": "get_current_weather")及参数("arguments": "{\n \"city\": \"深圳\"\n}):

{
  "id": "chatcmpl-xxxxxxx",
  "object": "chat.completion",
  "created": 1766794123,
  "model": "gpt-4",
  "choices": [
      {
          "index": 0,
          "message": {
              "role": "assistant",
              "content": null,
              "tool_calls": [
                  {
                      "id": "call_xxxxxxxx",
                      "type": "function",
                      "function": {
                          "name": "get_current_weather",
                          "arguments": "{\n  \"city\": \"深圳\"\n}"
                      }
                  }
              ]
          },
          "logprobs": null,
          "finish_reason": "tool_calls"
      }
  ],
  "usage": {
      "prompt_tokens": 83,
      "completion_tokens": 20,
      "total_tokens": 103
  },
  "system_fingerprint": null
}

3. 解析要调用的函数并调用

通过解析choices[0].message.tool_calls[0],即可调用本地函数get_current_weather("深圳"),假设返回结果为:36℃,小雨

4. 回传函数调用结果给大模型

紧接着将函数调用的结果通过message回传回大模型,即下面"role": "function"的message:

{
    "messages": [
        {
            "role": "system",
            "content": "你是一个聊天助手,请使用提供的工具协助用户。"
        },
        {
            "role": "user",
            "content": "深圳今天的天气怎样"
        },
        {
            "role": "assistant",
            "function_call": {
                "name": "get_current_weather",
                "arguments": "{\n  \"city\": \"深圳\"\n}"
            }
        },
        {
            "role": "function",
            "name": "get_current_weather",
            "content": "36℃,小雨"
        }
    ],
    "tools": [
        {
            "type": "function",
            "function": {
                "name": "get_current_weather",
                "description": "获取指定城市的天气",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "city": {
                            "type": "string",
                            "description": "城市名"
                        }
                    }
                },
                "required": ["city"]
            }
        }
    ]
}

5. 大模型返回最终响应

大模型根据回传的信息,返回最终响应,即深圳今天小雨,气温36摄氏度。响应数据包如下:

{
  "id": "chatcmpl-xxx",
  "object": "chat.completion",
  "created": 1766804505,
  "model": "gpt-4",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "深圳今天小雨,气温36摄氏度。"
      },
      "logprobs": null,
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 133,
    "completion_tokens": 19,
    "total_tokens": 152
  },
  "system_fingerprint": null
}
Logo

开源鸿蒙跨平台开发社区汇聚开发者与厂商,共建“一次开发,多端部署”的开源生态,致力于降低跨端开发门槛,推动万物智联创新。

更多推荐