LLM 学习系列 2. Function Calling
介绍 对于典型的 LLM 交互,单个提示或几轮聊天通常足以获得所需的结果。但是,某些任务需要 LLM 访问其内部知识库之外的信息。例如,获取特定城市今天的天气信息或搜索特定的动漫,这些都需要调用外部函数。 什么是 Function Calling? LLM 中的 Function Calling(函数调用)使模型能够生成 JSON 对象,从而触发代码中的外部函数。此功能不仅使 LLM 能够连接到外部工具和 API,而且扩展了其执行各种任务的能力。 Function Calling 执行步骤 用户使用工具和用户提示词调用 LLM API: 用户提供提示并指定可用工具。 What is the weather like in San Francisco? 定义工具 Schema 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"], }, }, } ] 定义 Mock 函数 ...