使用Ollama实现工具调用的原理及Python代码实现

发布时间:2026/6/23 15:54:29

使用Ollama实现工具调用的原理及Python代码实现 实现原理这里以支持工具调用的qwen3为例介绍实现原理。1、安装ollama下载并启动qwen3模型。ollama run qwen32、向ollama发送对话请求。POST http://localhost:11434/api/chatbody{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/chatbody{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源码importrequests api_basehttp://localhost:11434defget_current_weather(location:str):iflocation北京:return北京的天气是晴朗25摄氏度eliflocation上海: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],},},}],}# 第一轮对话resultrequests.post(f{api_base}/api/chat,jsonmessage)print(result.text)message[messages].append({role:tool,content:get_current_weather(北京),name:get_current_weather,})# 第二轮对话resultrequests.post(f{api_base}/api/chat,jsonmessage)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}原文手把手教你用Ollama实现工具调用三行Python代码看懂MCP原理 - 超腾开源

相关新闻