文章目录环境说明踩坑一llama-server 默认不支持 Function Calling问题现象问题原因解决方案踩坑二工具函数返回值类型错误问题现象错误代码问题原因解决方案踩坑三自作聪明写了一堆解析代码问题现象问题原因解决方案最终正确代码运行效果总结参考资料最近在尝试用 AgentScope 框架连接本地部署的 Qwen3.6 大模型通过 llama-server实现工具调用Function Calling功能。本以为按照 OpenAI 标准接口对接就行结果踩了一堆坑特此实录。环境说明本地模型: Qwen3.6-35B (GGUF 格式)推理服务: llama.cpp 的 llama-server智能体框架: AgentScope (阿里巴巴开源)操作系统: Windows 10踩坑一llama-server 默认不支持 Function Calling问题现象代码写好了工具也注册了但模型就是不调用工具而是自己瞎编答案。toolkitToolkit()toolkit.register_tool_function(get_current_time)agentReActAgent(name AI,sys_prompt你是智能助手可以调用工具。,modelchat_model,toolkittoolkit,)用户问现在几点了模型直接回答现在是2024年…完全没有调用get_current_time()工具。问题原因llama-server 默认使用的是普通聊天模式不支持 OpenAI 的 Function Calling 格式。即使你在请求中传了tools参数服务器也不会处理模型也收不到工具定义。解决方案启动 llama-server 时加上--jinja参数llama-server.exe ^ -m F:\models\Qwen3.6-35B-A3B-UD-IQ1_M.gguf ^ --n-gpu-layers 999 ^ --ctx-size 16384 ^ --jinja ^ --host 127.0.0.1 ^ --port 8080--jinja参数会启用 Jinja2 模板渲染让服务器能够正确处理 ChatML 格式的消息包括工具调用。注意: 不是所有模型都支持 Function Calling需要模型本身经过相关训练。Qwen 系列模型大多支持。踩坑二工具函数返回值类型错误问题现象加上--jinja后模型终于开始调用工具了但是报错TypeError: The tool function must return a ToolResponse object, or an AsyncGenerator/Generator of ToolResponse objects, but got class str错误代码defget_current_time()-str:获取当前时间fromdatetimeimportdatetimereturndatetime.now().strftime(%Y-%m-%d %H:%M:%S)问题原因AgentScope 的工具函数必须返回ToolResponse对象不能直接返回字符串。这是 AgentScope 的设计规范用于统一处理工具调用的结果。解决方案fromagentscope.toolimportToolResponsedefget_current_time()-ToolResponse:获取当前时间fromdatetimeimportdatetime resultdatetime.now().strftime(%Y-%m-%d %H:%M:%S)returnToolResponse(contentresult)所有工具函数都要改成返回ToolResponsedefcalculate(expression:str)-ToolResponse:计算数学表达式try:resulteval(expression,{__builtins__:{}},{})returnToolResponse(contentf{expression}{result})exceptExceptionase:returnToolResponse(contentf计算错误:{e})踩坑三自作聪明写了一堆解析代码问题现象一开始以为 AgentScope 不能正确处理工具调用于是写了一个LlamaServerModelWrapper里面有一大堆正则表达式来解析模型输出classLlamaServerModelWrapper(OpenAIChatModel):TOOL_PATTERNre.compile(r{{call:\s*(\w)\s*\}\})JSON_TOOL_PATTERNre.compile(r\[\s*\{\s*name\s*:\s*(\w)...)TOOL_CALL_PATTERNre.compile(rtool_call\s*\(\s*(\w)\s*\(\s*([^)]*)\s*\)\s*\))asyncdef__call__(self,messages,**kwargs):# 100多行的解析逻辑...responseawaitsuper().__call__(messages,**kwargs)# 各种正则匹配...# 手动构造 ToolUseBlock...returnresponse问题原因完全没必要AgentScope 原生就支持 OpenAI 格式的工具调用。只要 llama-server 正确返回tool_calls字段AgentScope 会自动处理一切。解决方案删除所有自定义解析代码只保留必要的参数修正classLlamaServerModelWrapper(OpenAIChatModel):包装 OpenAIChatModel修复 tool_choice 参数asyncdef__call__(self,messages,**kwargs):iftool_choiceinkwargsandkwargs[tool_choice]isNone:kwargs[tool_choice]autoreturnawaitsuper().__call__(messages,**kwargs)AgentScope 会自动把工具定义发给模型解析模型的tool_calls响应执行对应的工具函数把结果返回给模型让模型生成最终回复最终正确代码#!/usr/bin/env python3importasyncioimportagentscopefromagentscope.agentimportReActAgentfromagentscope.messageimportMsgfromagentscope.toolimportToolkit,ToolResponsefromagentscope.memoryimportInMemoryMemoryfromagentscope.formatterimportOpenAIChatFormatterfromagentscope.modelimportOpenAIChatModel# 工具函数 - 必须返回 ToolResponsedefget_current_time()-ToolResponse:获取当前日期和时间fromdatetimeimportdatetime resultdatetime.now().strftime(%Y-%m-%d %H:%M:%S)returnToolResponse(contentresult)defcalculate(expression:str)-ToolResponse:计算数学表达式try:allowed{abs:abs,max:max,min:min}resulteval(expression,{__builtins__:{}},allowed)returnToolResponse(contentf{expression}{result})exceptExceptionase:returnToolResponse(contentf计算错误:{e})# 自定义 Model 包装器 - 只需修复参数classLlamaServerModelWrapper(OpenAIChatModel):asyncdef__call__(self,messages,**kwargs):iftool_choiceinkwargsandkwargs[tool_choice]isNone:kwargs[tool_choice]autoreturnawaitsuper().__call__(messages,**kwargs)defcreate_agent():chat_modelLlamaServerModelWrapper(model_nameqwen3.6,api_keyany-key,client_kwargs{base_url:http://127.0.0.1:8080/v1},)toolkitToolkit()toolkit.register_tool_function(get_current_time)toolkit.register_tool_function(calculate)returnReActAgent(name AI,sys_prompt你是智能助手可以使用工具回答问题。,modelchat_model,toolkittoolkit,memoryInMemoryMemory(),max_iters3,)asyncdefmain():agentscope.init()agentcreate_agent()user_msgMsg(name用户,content现在几点了,roleuser)responseawaitagent(user_msg)print(fAI:{response.content})if__name____main__:asyncio.run(main())运行效果 你: 现在几点了 AI: 调用工具 get_current_time() 工具返回: 2026-04-27 02:49:27 AI: 现在是2026年4月27日02点49分27秒。总结问题原因解决方案模型不调用工具llama-server 默认不支持 FC启动时加--jinja参数TypeError: ToolResponse工具函数返回字符串返回ToolResponse(content...)代码复杂难维护自作聪明写解析逻辑信任框架用原生功能核心教训不要自作聪明框架能做的事就让框架做。遇到问题先查官方文档而不是自己造轮子。参考资料llama.cpp 官方文档AgentScope 官方文档Qwen 模型仓库如果这篇文章对你有帮助欢迎点赞收藏有问题欢迎评论区交流~