大模型在面对实时性问题、数学计算等问题时可能效果不佳。你可以使用Function Calling功能,通过引入外部工具,使得大模型可以与外部世界进行交互。
一、前置:
- 这里我们以阿里的通义千问-Plus(qwen-plus)为例
- 通过HTTP调用LLM时,需要做这些准备:
1 1. 获取API-KEY,如何获取?可进入阿里云百炼的控制台(bailian.console.aliyun.com/?tab=model#…)登录后可免费领取大额度的Tokens,点击左下角的API,创建即可
-
- 指定baseURL,此处我们选取DashScope聊天完成API的端点地址dashscope.aliyuncs.com/compatible-…,此URL用于创建HTTP请求的目标地址
二、Function Calling
- 简单介绍Function Calling是什么:
Function Calling(工具调用)是大语言模型(LLM)与外部工具/API交互的机制,通过自动化调用外部工具来增强模型处理实时或复杂任务的能力
- 工作流程:
-
- 定义工具(Tools)
-
-
- 开发者自己预先定义工具函数(比如查询天气、时间),并描述其功能、输入参数(JSON Schema格式)
-
// 工具示例
"tools": [
{
"type": "function",
"function": {
"name": "get_current_time",
"description": "当用户查询现在或者说当前时间时,请调用这个方法。",
"parameters": {}
}
},
{
"type": "function",
"function": {
"name": "get_current_weather",
"description": "当用户询问指定城市的天气时请使用这个方法。",
"parameters": {
"properties": {
"location": {
"description": "城市或县区,比如北京市、杭州市、余杭区等。",
"type": "string"
}
},
"required": [
"location"
],
"type": "object"
}
}
}
],
- 2. 创建对话上下文(Messages)
-
-
- Messages的json示例如下
-
"messages": [
{
"role": "system",
"content": "你是一个智能助手,能根据用户的询问自动识别需要调哪些function,比如天气、获取当前时间"
},
{
"role": "user",
"content": "余杭区今天天气怎么样"
}
],
- 3. 发起Function Calling请求
-
-
- 将工具描述(tools 参数)和对话上下文(messages)传给模型
- 支持Function Calling的模型就会判断是否需要调用工具,接着会返回工具名称及参数。具体各个流程构建、返回的参数会在下面以json格式列出
-
-
- 接着就是执行工具函数
-
-
- 工具函数是在本地编写的。需要本地代码解析上一步模型返回的工具名称和参数,用于执行对应的工具函数
-
-
- 返回结果
-
-
- 将工具函数返回的结果作为Tool Message追加到对话上下文,再次请求模型生成自然语言回复
- 最终回复示例:余杭区今天天气晴朗,非常适合外出!
-
三、各个流程中涉及到的结构体参数(这里以json格式给出示例)
- 第一次调用构建的完整请求参数;是的没错,请求体只需要model、messages、tools就可以请求了
{
"model": "qwen-plus",
"messages": [
{
"role": "system",
"content": "你是一个智能助手,能根据用户的询问自动识别需要调哪些function,比如天气、获取当前时间"
},
{
"role": "user",
"content": "余杭区今天天气怎么样"
}
],
"tools": [
{
"type": "function",
"function": {
"name": "get_current_time",
"description": "当用户查询现在或者说当前时间时,请调用这个方法。",
"parameters": {}
}
},
{
"type": "function",
"function": {
"name": "get_current_weather",
"description": "当用户询问指定城市的天气时请使用这个方法。",
"parameters": {
"properties": {
"location": {
"description": "城市或县区,比如北京市、杭州市、余杭区等。",
"type": "string"
}
},
"required": [
"location"
],
"type": "object"
}
}
}
],
"stream": false // 可选,默认为false
}
2. 第一次调用的返回结果, 因为支持Function Calling,qwen-plus会根据所给的工具描述自动决策,自动选择工具并提取参数,无需人工干预
{
"choices": [
{
"message": {
"content": "",
"role": "assistant",
"tool_calls": [
{
"index": 0,
"id": "call_9a3b9357026e4aba9ef56d",
"type": "function",
"function": {
"name": "get_current_weather",
"arguments": "{"location": "余杭区"}"
}
}
]
},
"finish_reason": "tool_calls",
"index": 0,
"logprobs": null
}
],
"object": "chat.completion",
"usage": {
"prompt_tokens": 248,
"completion_tokens": 22,
"total_tokens": 270,
"prompt_tokens_details": {
"cached_tokens": 0
}
},
"created": 1753460252,
"system_fingerprint": null,
"model": "qwen-plus",
"id": "chatcmpl-f4bf6007-f82c-95e0-9a6b-4c6f2f177274"
}
3. 此时调用工具函数并获取返回值 4. 第二次调用构建的完整请求参数, 此步需要将上一步的工具函数返回值加进上下文中
{
"model": "qwen-plus",
"messages": [
{
"role": "system",
"content": "你是一个智能助手,能根据用户的询问自动识别需要调哪些function,比如天气、获取当前时间"
},
{
"role": "user",
"content": "余杭区今天天气怎么样"
},
{
"role": "assistant",
"content": "",
"tool_calls": [
{
"id": "call_9a3b9357026e4aba9ef56d",
"type": "function",
"function": {
"name": "get_current_weather",
"arguments": "{"location": "余杭区"}"
}
}
]
},
{
"role": "tool",
"content": "",
"tool_call_id": "call_9a3b9357026e4aba9ef56d"
}
],
"tools": [
{
"type": "function",
"function": {
"name": "get_current_time",
"description": "当用户查询现在或者说当前时间时,请调用这个方法。",
"parameters": {}
}
},
{
"type": "function",
"function": {
"name": "get_current_weather",
"description": "当用户询问指定城市的天气时请使用这个方法。",
"parameters": {
"properties": {
"location": {
"description": "城市或县区,比如北京市、杭州市、余杭区等。",
"type": "string"
}
},
"required": [
"location"
],
"type": "object"
}
}
}
],
"stream": false
}
5. 第二次返回的结果(最后一次)
{
"choices": [
{
"message": {
"content": "余杭区今天的天气情况是:晴天,最高气温25℃,最低气温18℃,东南风2级。建议外出时注意防晒和补水哦!",
"role": "assistant"
},
"finish_reason": "stop",
"index": 0,
"logprobs": null
}
],
"object": "chat.completion",
"usage": {
"prompt_tokens": 283,
"completion_tokens": 37,
"total_tokens": 320,
"prompt_tokens_details": {
"cached_tokens": 0
}
},
"created": 1753460253,
"system_fingerprint": null,
"model": "qwen-plus",
"id": "chatcmpl-e6314cbd-bb5a-906e-a34d-5fa61a8771e2"
}