function calling简介
拒绝废话
是什么 {#是什么}
函数调用是将 LLM 可靠地连接到外部工具以实现有效使用工具以及与外部 API 交互的能力。
LLM 经过微调,可以检测何时需要调用函数,从 prompt 中提取出参数和结构,输出 JSON 以调用该函数。函数调用调用的函数将充当 AI 应用程序中的工具,
有什么用 {#有什么用}
- 可以有效地使用外部工具回答问题的对话代理
- 帮助将自然语言转换为 API 调用或有效数据库查询的应用程序
怎么用(Demo) {#怎么用–demo}
- 首先我们按格式定义一组 tools.
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"],},},}
]
- name 就是 function name
- description LLM会根据 de scription来判断 你的promtp 是不是和这个函数相关, (怎么判断的? 2 个文本量化计算相关性)
- parameters 就是 function 的入参.
- 然后, 我们有个prompt “What is the weather like in London?” 构建一个 messages
messages = [4. {"role": "user","content": "What is the weather like in London?"}
]
最后把 messages 和 tools 都发给LLM, 生成 response