参考文档:Tool support · Ollama Blog
示例代码:ollama/docs/api.md at main · ollama/ollama
实现原理
这里以支持工具调用的qwen3为例,介绍实现原理。
1、安装ollama,下载并启动qwen3模型。
ollama run qwen3
2、向ollama发送对话请求。
POST http://localhost:11434/api/chat
body
{
"model": "qwen3:latest",
"messages": [
{
"role": "user",
"content": "今天北京的天气怎么样?"
}
],
"stream": true,
"tools": [
{
"type": "function",
"function": {
"name": "get_current_weather",
"description": "获取当前天气",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "城市名称",
"enum": [
"北京",
"上海",
"广州"
]
}
},
"required": [
"location"
]
}
}
}
]
}
模型返回tool_calls消息,要求调用get_current_weather工具。
{
"model": "qwen3:latest",
"created_at": "2025-05-04T14:16:37.5915052Z",
"message": {
"role": "assistant",
"content": "",
"tool_calls": [
{
"function": {
"name": "get_current_weather",
"arguments": {
"location": "北京"
}
}
}
]
},
"done": false
}
3、把聊天历史和工具返回的结果一起发送给模型即可。
POST http://localhost:11434/api/chat
body
{
"model": "qwen3:latest",
"messages": [
{
"role": "user",
"content": "今天北京的天气怎么样?"
},
{
"role": "tool",
"content": "北京的天气是晴朗,25摄氏度",
"name": "get_current_weather"
}
],
"stream": true,
"tools": [
{
"type": "function",
"function": {
"name": "get_current_weather",
"description": "获取当前天气",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "城市名称",
"enum": [
"北京",
"上海",
"广州"
]
}
},
"required": [
"location"
]
}
}
}
]
}
模型返回结果。
<think>
好的,用户问今天北京的天气怎么样。我需要先调用get_current_weather函数来获取天气信息。根据工具中的定义,这个函数需要location参数,且北京是允许的选项之一。所以,我应该生成一个工具调用,指定北京作为地点。然后,假设函数返回了晴朗和25度的数据,我需要用自然的中文回复用户,说明天气情况。确保回答简洁明了,符合用户的需求。
</think>
今天北京的天气是晴朗的,气温25摄氏度,适合外出活动。
Python源码
import requests
api_base = "http://localhost:11434"
def get_current_weather(location: str):
if location == "北京":
return "北京的天气是晴朗,25摄氏度"
elif location == "上海":
return "上海的天气是多云,20摄氏度"
else:
return "广州的天气是晴朗,25摄氏度"
message = {
"model": "qwen3:latest",
"messages": [{"role": "user", "content": "今天北京的天气怎么样?"}],
"stream": False,
"tools": [
{
"type": "function",
"function": {
"name": "get_current_weather",
"description": "获取当前天气",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "城市名称",
"enum": ["北京", "上海", "广州"],
}
},
"required": ["location"],
},
},
}
],
}
# 第一轮对话
result = requests.post(f"{api_base}/api/chat", json=message)
print(result.text)
message["messages"].append(
{
"role": "tool",
"content": get_current_weather("北京"),
"name": "get_current_weather",
}
)
# 第二轮对话
result = requests.post(f"{api_base}/api/chat", json=message)
print(result.text)
返回结果:
{"model":"qwen3:latest","created_at":"2025-05-04T14:40:17.5745628Z","message":{"role":"assistant","content":"","tool_calls":[{"function":{"name":"get_current_weather","arguments":{"location":"北京"}}}]},"done_reason":"stop","done":true,"total_duration":12648830200,"load_duration":12336100,"prompt_eval_count":152,"prompt_eval_duration":354411300,"eval_count":108,"eval_duration":12280590400}
{"model":"qwen3:latest","created_at":"2025-05-04T14:40:35.5741283Z","message":{"role":"assistant","content":"\u003cthink\u003e\n好的,用户问今天北京的天气怎么样。我需要先调用get_current_weather函数来获取天气信息
。根据工具里的函数定义,参数是location,必须是北京、上海或广州中的一个。用户提到的是北京,所以参数没问题。
调用函数后,假设返回的结果是晴朗,25度。然后我要组织回答,用自然的中文告诉用户天气情况,包括天气状况和温度
,并保持口语化,避免使用专业术语或格式。确认没有其他参数需要处理,直接回答即可。\n\u003c/think\u003e\n\n今
天北京的天气是晴朗的,气温在25摄氏度左右,非常适合外出活动哦!"},"done_reason":"stop","done":true,"total_duration":17993911600,"load_duration":13278100,"prompt_eval_count":173,"prompt_eval_duration":685325300,"eval_count":139,"eval_duration":17292244200}
转载请注明转自www.hylab.cn,原文地址:使用Ollama实现工具调用的原理及Python代码实现