
Qwen not recognizing function / tools
快速结论:该问题发生在调用 Qwen2.5:14b-instruct 模型时,模型不会执行工具/函数调用,即使客户端代码中正确传入了 tools 参数。优先排查是否将 tools 参数传入了模型的 chat 接口,并检查模型模板中是否包含了函数调用的 XML 标签结构。
问题场景
用户在 Chainlit 框架中使用 Python 的 ollama.chat() 调用 Qwen2.5:14b-instruct 模型,并传入了 tools 参数(包含 toggle_lights、set_temperature 等自定义函数)。模型载入成功,但模型不会实际调用这些工具,也不会在对话中引用 tools 参数中的函数信息。用户也尝试了其他模型(majx13/test),问题同样存在,因此初步排除了单一模型兼容性问题。
报错原文
Qwen2.5:14b-instruct has no access to tools / functions
服务端日志未见错误(无 error 级别输出),仅输出正常的启动和模型加载日志。
原因分析
可能原因:Ollama 在为 Qwen2.5:14b-instruct 生成的模板中,# Tools 的说明和工具调用的格式使用了 XML 标签 <tool_call> 和 <tools>,但客户端(如 ollama.chat)在发送消息时,必须确保 tools 参数被正确嵌入到系统消息的模板之中——否则模型不会主动解析或调用它们。该 Issue 中用户并未提供客户端调用代码(仅展示了函数定义),无法确认 tools 是否被正确传递到 chat API 的 tools 字段。
环境排查
- Ollama 版本:0.5.4
- 模型:qwen2.5:14b-instruct(可能包含其他变体,如 qwen2.5:14b)
- 显卡:NVIDIA GeForce RTX 4070 (CUDA 12.7, Driver 12.7)
- Python SDK:ollama (Python client),版本未知
- 客户端框架:Chainlit
- 操作系统:Windows
解决步骤
- 确认客户端调用方式:在调用
ollama.chat(model='qwen2.5:14b-instruct', messages=[...], tools=your_tools_list)时,确保tools参数是以正确的 Python dict 列表形式传递,且每个 tool 的名称、描述、parameters 结构符合 Ollama 的要求(注意:strict: true在某些版本中可能不被支持,可尝试移除)。 - 查看模型默认模板:运行
ollama show --template qwen2.5:14b-instruct,检查模板中是否包含工具调用的 XML 渲染逻辑(如<tools>和<tool_call>)。模板若缺失或格式异常,可能会导致工具参数被忽略。 - 测试简化调用:在不使用 Chainlit 的情况下,用最简的 Python 脚本直接调用
ollama.chat,看工具是否能被识别。例如:from ollama import chat response = chat(model='qwen2.5:14b-instruct', messages=[{'role': 'user', 'content': 'Turn on the lights in the living room'}], tools=tools) print(response) - 调整工具 format:尝试移除
strict: true和additionalProperties: false,并在parameters中使用 OpenAPI 标准的 JSON Schema 格式(如{"type": "object", "properties": {...}})。 - 检查模型支持:确认所使用模型明确支持 function calling。Qwen2.5 系列通常支持,但建议查阅其官方文档确认。如果模型本身对工具调用的能力有限,可能需要更换为 Qwen 模型对应的
-instruct变体。 - 更新 Ollama:Ollama 版本 0.5.4 可能存在工具调用兼容性问题,建议升级到最新版本。
验证方法
正确调用后,模型应该返回带有 tool_calls 字段的响应,而不是自然语言回复。例如:
response = chat(model='qwen2.5:14b-instruct', messages=[...], tools=tools)
print(response['tool_calls'])
# 期望输出:包含函数名称和参数的列表,而非 None 或空列表
此外,观察 Ollama 服务端日志(默认 OLLAMA_DEBUG=false 时可能不输出工具调用的详细日志,可设置环境变量 OLLAMA_DEBUG=1 后重启服务端获取更详细的信息)。



