765  
查询码: 00000561
Qwen3.5-27B在 vLLM 中开启 tool_calls(sdk4.4.0+x86)
专家 2026年04月14日 发布 ,于 2026年09月29日 编辑

前提

Qwen3.5-27B启动环境,可以参考

mr100_bi150+x86容器上运行Qwen3.5(SDK4.4.0)-天数智芯知识库系统

这里默认环境都已经安装好,已经进入到容器中,下边的操作默认在容器中进行。

启动服务

export VLLM_RPC_TIMEOUT=50000
export VLLM_ENFORCE_CUDA_GRAPH=1
export VLLM_KV_DISABLE_CROSS_GROUP_SHARE=1


vllm serve /data/model/Qwen3.5-27B \
  --served-model-name qwen35 \
  --trust-remote-code \
  -tp 4 \
  --max-num-seqs 16 \
  --enable-chunked-prefill \
  --max-model-len 16384 \
  --host 0.0.0.0 \
  --port 1234 \
  --gpu-memory-utilization 0.90 \
  --compilation-config '{"cudagraph_mode": "FULL_DECODE_ONLY", "level": 0}' \
  --enable-auto-tool-choice \
  --tool-call-parser qwen3_coder

--enable-auto-tool-choice

开启自动工具选择能力,使模型能够在请求中根据  tools  自动决定是否发起工具调用。

 --tool-call-parser qwen3_coder

指定 Qwen3.5 系列对应的工具调用解析器。

注意可用名称是:

  • qwen3_coder

不是:

  • qwen3coder

启动测试脚本

创建测试脚本

test_tool_call_full.py

import json
from openai import OpenAI

BASE_URL = "http://127.0.0.1:1234/v1"
MODEL_NAME = "qwen35"

client = OpenAI(
    base_url=BASE_URL,
    api_key="EMPTY"
)

tools = [
    {
        "type": "function",
        "function": {
            "name": "get_weather",
            "description": "获取指定城市的天气信息",
            "parameters": {
                "type": "object",
                "properties": {
                    "city": {
                        "type": "string",
                        "description": "城市名,比如北京、上海"
                    }
                },
                "required": ["city"]
            }
        }
    }
]

def get_weather(city: str):
    fake_db = {
        "北京": {"weather": "晴", "temperature": "28C"},
        "上海": {"weather": "多云", "temperature": "31C"},
        "深圳": {"weather": "小雨", "temperature": "29C"},
    }
    return fake_db.get(city, {"weather": "未知", "temperature": "未知"})

messages = [
    {"role": "user", "content": "请帮我查询北京天气,然后告诉我结果"}
]

extra_body = {
    "chat_template_kwargs": {
        "enable_thinking": False
    }
}

print("\n================ 第1轮:请求模型决定是否调用工具 ================\n")

resp1 = client.chat.completions.create(
    model=MODEL_NAME,
    messages=messages,
    tools=tools,
    tool_choice="auto",
    temperature=0,
    extra_body=extra_body
)

msg1 = resp1.choices[0].message

print("=== assistant 原始返回 ===")
print(msg1)
print("\n=== content ===")
print(repr(msg1.content))
print("\n=== tool_calls ===")
print(msg1.tool_calls)

if not msg1.tool_calls:
    print("\n模型没有发起标准 tool_calls,测试结束。")
    exit(0)

assistant_message = {
    "role": "assistant",
    "content": msg1.content or "",
    "tool_calls": []
}

for tc in msg1.tool_calls:
    assistant_message["tool_calls"].append({
        "id": tc.id,
        "type": "function",
        "function": {
            "name": tc.function.name,
            "arguments": tc.function.arguments
        }
    })

messages.append(assistant_message)

print("\n================ 第2步:本地执行工具 ================\n")

for tc in msg1.tool_calls:
    tool_name = tc.function.name
    args = json.loads(tc.function.arguments)

    print(f"调用工具: {tool_name}")
    print(f"工具参数: {args}")

    if tool_name == "get_weather":
        tool_result = get_weather(**args)
    else:
        tool_result = {"error": f"unknown tool: {tool_name}"}

    print(f"工具返回: {tool_result}")

    messages.append({
        "role": "tool",
        "tool_call_id": tc.id,
        "content": json.dumps(tool_result, ensure_ascii=False)
    })

print("\n================ 第3轮:把工具结果喂回模型 ================\n")

resp2 = client.chat.completions.create(
    model=MODEL_NAME,
    messages=messages,
    tools=tools,
    tool_choice="auto",
    temperature=0,
    extra_body=extra_body
)

msg2 = resp2.choices[0].message

print("=== 最终 assistant 返回 ===")
print(msg2)
print("\n=== 最终 content ===")
print(repr(msg2.content))
print("\n=== 最终 tool_calls ===")
print(msg2.tool_calls)


执行

python3 test_tool_call_full.py

预期测试结果

================ 第1轮:请求模型决定是否调用工具 ================

=== assistant 原始返回 ===
ChatCompletionMessage(content=None, refusal=None, role='assistant', annotations=None, audio=None, function_call=None, tool_calls=[ChatCompletionMessageFunctionToolCall(id='chatcmpl-tool-59b020b4ee7f4eb5a51684a96e0c75f7', function=Function(arguments='{"city": "北京"}', name='get_weather'), type='function')], reasoning=None, reasoning_content=None)

=== content ===
None

=== tool_calls ===
[ChatCompletionMessageFunctionToolCall(id='chatcmpl-tool-59b020b4ee7f4eb5a51684a96e0c75f7', function=Function(arguments='{"city": "北京"}', name='get_weather'), type='function')]

================ 第2步:本地执行工具 ================

调用工具: get_weather
工具参数: {'city': '北京'}
工具返回: {'weather': '晴', 'temperature': '28C'}

================ 第3轮:把工具结果喂回模型 ================

=== 最终 assistant 返回 ===
ChatCompletionMessage(content='北京现在的天气是晴,气温28摄氏度。', refusal=None, role='assistant', annotations=None, audio=None, function_call=None, tool_calls=[], reasoning=None, reasoning_content=None)

=== 最终 content ===
'北京现在的天气是晴,气温28摄氏度。'

=== 最终 tool_calls ===
[]


笔记



  目录
    天数智芯知识库系统 -V 5.2.7 -wcp