是什么

函数调用是将 LLM 可靠地连接到外部工具以实现有效使用工具以及与外部 API 交互的能力。 LLM 经过微调,可以检测何时需要调用函数,从 prompt 中提取出参数和结构,输出 JSON 以调用该函数。函数调用调用的函数将充当 AI 应用程序中的工具,

有什么用

  1. 可以有效地使用外部工具回答问题的对话代理
  2. 帮助将自然语言转换为 API 调用或有效数据库查询的应用程序

怎么用(Demo)

  1. 首先我们按格式定义一组 tools.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
tools = [
    {
        "type": "function",
        "function": {
            "name": "get_current_weather",
            "description": "Get the current weather in a given location",
            "parameters": {
                "type": "object",
                "properties": {
                    "location": {
                        "type": "string",
                        "description": "The city and state, e.g. San Francisco, CA",
                    },
                    "unit": {
                        "type": "string",
                        "enum": ["celsius", "fahrenheit"]},
                },
                "required": ["location"],
            },
        },
    }
]
  1. name 就是 function name
  2. description LLM会根据 de scription来判断 你的promtp 是不是和这个函数相关, (怎么判断的? 2 个文本量化计算相关性)
  3. parameters 就是 function 的入参.
  1. 然后, 我们有个prompt “What is the weather like in London?” 构建一个 messages
1
2
3
4
5
6
messages = [
  4. {
        "role": "user",
        "content": "What is the weather like in London?"
    }
]

最后把 messages 和 tools 都发给LLM, 生成 response