原理与优势

qwen3.5/3.6 tool_call注意

qwen3.5/3.6 官方jinja2 template有问题,无法调用tool,具体情况及其修正见 readfrog.ltd/index.php/archives/8/

示例代码

import requests
import json

BASE_URL = "http://192.168.1.112:8000/v1"
API_KEY = "EMPTY"

tools = [
    {
        "type": "function",
        "function": {
            "name": "get_weather",
            "description": "查询指定城市当前的天气情况",
            "parameters": {
                "type": "object",
                "properties": {
                    "city": {
                        "type": "string",
                        "description": "城市名称,例如:上海、北京"
                    },
                    "unit": {
                        "type": "string",
                        "enum": ["celsius", "fahrenheit"],
                        "description": "温度单位"
                    }
                },
                "required": ["city"]
            }
        }
    }
]

def call_model_with_tools(messages):
    url = f"{BASE_URL}/chat/completions"
    headers = {
        "Content-Type": "application/json",
        "Authorization": f"Bearer {API_KEY}",
    }
    payload = {
        "model": "models/qwen3.6",  # model即目录名字
        "messages": messages,
        "tools": tools,
        "tool_choice": "auto",
    }

    res = requests.post(url, headers=headers, data=json.dumps(payload))
    return res.json()["choices"][0]["message"]["content"] , [i['function'] for i in res.json()["choices"][0]["message"]["tool_calls"]]


messages = [
    {"role": "system", "content": "你是一个能调用工具查询天气的助手。"},
    {"role": "user", "content": "帮我查一下上海现在的天气,摄氏度。"}
]

text,tools = call_model_with_tools(messages)
for i in tools:
    print(i['name'],json.loads(i["arguments"]))
 # 注意arguments是json字符串,需要解析

vllm api参数与格式解析