Ollama工具调用原理详解及Python代码实现教程

2025-05-04 李腾 182 次阅读 0 次点赞
本文全面介绍使用Ollama实现工具调用的技术原理,通过qwen3模型实例详细演示工具调用流程,包括模型请求、工具响应处理等关键步骤,并提供完整的Python代码实现,帮助开发者深入理解MCP工具调用机制并快速应用于实际项目开发。

本文介绍了使用Ollama实现工具调用的原理,并使用python做了最简单的实现,方便大家理解MCP工具调用的原理。

参考文档: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}
本文由人工编写,AI优化,转载请注明原文地址: Ollama工具调用原理详解及Python代码实现教程

评论 (2)

登录后发表评论
微笑的向日葵2025-11-28 08:46:48
感谢分享!原理讲得很清晰,Python示例也很实用。请问如果我想自定义工具函数,参数格式有特别要注意的地方吗?
超腾开源2025-11-28 20:00:48
有的,参数格式的固定的,你可以参考我的示例,也可以看ollama官方文档。