LLM 多智能体 AutoGen 教程 5:函数调用之避免参数捏造
1. 函数调用基础
OpenAI 的 Function Calling API 可以通过 HTTP 请求完成,需要在请求体中加入 tools 字段,它是一个列表,意味着它支持多个函数描述。函数描述采用 JSON 结构体,包括函数名、函数解释、参数列表,参数列表中每个字段都需要描述类型和解释。
由于 OpenAI Token 用完,测试了本地安装的 Llama.cpp 和 Ollama 安装的 Command R Plus 两个模型,它们明确不支持函数调用功能。因此测试了通义千问和月之暗面,其中通义千问模型 qwen-max 支持函数调用,只是它不支持 OpenAI 中提到的并发调用功能,而月之暗面是全面支持函数并发调用。
请求示例
curl --location 'https://api.moonshot.cn/v1/chat/completions' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer YOUR_API_KEY' \
--data '{
"model": "moonshot-v1-8k",
"messages": [
{
"role": "system",
"content": "你是一个强大的助手"
},
{
"role": "user",
"content": "今天南京天气怎么样?"
}
],
"tools": [
{
"type": "function",
"function": {
"name": "get_current_weather",
"description": "Get the current weather in a given location and unit of temperature",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA"
},
"unit": {
"type": "string",
"description": "the unit of temperature"
}
},
"required": [
"location",
"unit"
]
}
}
}
]
}'
响应示例
{
"id": "chatcmpl-bcb1b91facfb419488d85b88d55cbe0a",
"object": "chat.completion",
"created": 1717751056,
"model"


