记一次 LangChain Tool Calling 的踩坑实录:模型选错,工具调不动

1 阅读1分钟

一、背景:我想让 LLM 自动调用天气工具

最近在学习 LangChain 的 Tool Calling 功能,目标很简单:用户问"北京天气",模型能自动识别出需要调用 get_weather 工具,并传入正确的参数。

from langchain_core.tools import tool
from langchain_openai import ChatOpenAI
from langchain_core.messages import HumanMessage

@tool
def get_weather(city: str) -> str:
    """调用模型工具获取天气"""
    return f"模型调用结果:{city}的天气是晴朗的"

client = ChatOpenAI(
    api_key="sk-xxx",
    model_name="Qwen/Qwen3-VL-8B-Instruct",  # ← 注意这里
    base_url="https://api.siliconflow.cn/v1",
)

# 绑定工具
model_bind_tool = client.bind_tools([get_weather], tool_choice="auto")

# 测试调用
response = model_bind_tool.invoke("请调用get_weather工具查询北京的天气")
print(response.tool_calls)  # 期望能看到工具调用

预期结果:tool_calls 里应该有 get_weather 的调用请求
实际结果:tool_calls=[],空的。模型直接回了一堆 metadata,但就是不调工具。

二、排查过程:

询问ai,说是模型Qwen3-VL-xx-Instruct 是视觉模型,不支持工具调用。换Qwen/Qwen3-8B模型就可以了,

tool_calls=[{
    'name': 'get_weather', 
    'args': {'city': '北京'}, 
    'id': '019d1d890ded266d8b28938ec024817a', 
    'type': 'tool_call'
}]

但是硅基流动的官网标注Qwen3-VL-8B-Instruct支持 Tools,这也是奇了怪了