手把手教你用Ollama实现工具调用,三行Python代码看懂MCP原理
本文介绍了使用Ollama实现工具调用的原理,并以qwen3模型为例,用Python进行了最简单的实现演示。文章首先说明了如何安装Ollama并启动qwen3模型,然后详细展示了工具调用的完整流程:发送包含工具定义的对话请求、模型返回tool_calls要求调用工具、将工具返回结果与聊天历史再次发送给模型,最终获得自然语言回答。通过一个查询北京天气的实例,Python源码清晰展示了两次API调用的交互过程,帮助读者理解MCP工具调用的基本原理。
本文介绍了使用Ollama实现工具调用的原理,并使用Python做了最简单的实现,方便大家理解MCP工具调用的原理。
实现原理
这里以支持工具调用的qwen3为例,介绍实现原理。
1、下载并安装Ollama。
下载地址:https://ollama.com/
2、打开命令行工具,执行以下命令下载并启动qwen3模型服务。
ollama run qwen3
3、向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"
]
}
}
}
]
}
4、模型返回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
}
5、把聊天历史和工具返回的结果一起发送给模型即可。
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}
参考文档
Tool support · Ollama Blog:https://ollama.com/blog/tool-support
Ollama github文档:https://github.com/ollama/ollama/blob/main/docs/api.md#chat-request-with-tools
最后更新于1小时前
本文由人工编写,AI优化,转载请注明原文地址: 手把手教你用Ollama实现工具调用,三行Python代码看懂MCP原理
推荐阅读
XWiki只允许本机访问:Jetty绑定127.0.0.1配置方法
1282026-04-28
从非交互到交互式备案,手把手教你一周内搞定公安安全评估,轻松解锁网站互动功能
2392026-04-11
Windows系统PyTorch安装教程:CUDA 12.1环境配置与TorchText版本兼容性指南
30012024-06-21
OpenVPN安装配置完整指南:从零搭建安全VPN服务器与客户端
28802024-06-21
Claude Mythos Preview称霸AI编程榜:16项全冠,昂贵且危险的性能怪兽
1712026-04-21
GeoServer适配达梦数据库完整教程:从账号创建到图层发布
1292026-04-14
评论 (2)
请 登录 后发表评论
感谢分享!原理讲得很清晰,Python示例也很实用。请问如果我想自定义工具函数,参数格式有特别要注意的地方吗?
有的,参数格式的固定的,你可以参考我的示例,也可以看ollama官方文档。