You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Llama模型能否自动选择工具?代码测试遇无内容输出异常

Llama模型自动工具选择问题排查

Llama 3.1系列模型支持自动工具/函数调用,但你的代码存在几个关键问题,导致输出不符合预期:

1. 对话消息格式违规

你的messages列表里有两条连续的user角色消息,不符合ChatML对话格式的规范——正常对话应该是user和assistant角色交替(有历史对话时),或单条user消息。这种连续的用户消息会让模型混淆上下文,无法触发工具调用逻辑。

修复方案:
保留需要触发工具的天气查询消息即可:

messages = [
    {"role": "user", "content": "Hello, what is the weather?"}
]

如果要保留两条消息,需补充模拟的assistant回复:

messages = [
    {"role": "user", "content": "Hello!"},
    {"role": "assistant", "content": "Hello! How can I help you today?"},
    {"role": "user", "content": "Hello, what is the weather?"}
]

2. 工具定义不完整

你定义的工具缺少parameters字段,这是函数调用格式的必填项。Llama需要明确知道函数接受哪些参数,才能生成有效的调用请求。

修复方案:
补充参数定义,比如天气查询需指定城市:

tools=[{
    "type": "function",
    "function": {
        "name": "get_current_weather",
        "description": "used whenever the model is being asked to find out the current weather",
        "parameters": {
            "type": "object",
            "properties": {
                "city": {
                    "type": "string",
                    "description": "The name of the city to get weather for"
                }
            },
            "required": ["city"]
        }
    }
}]

3. 生成参数配置不足

当前代码没有设置生成参数,模型可能过早停止输出。

修复方案:
添加max_tokens、temperature等参数,确保模型有足够空间生成工具调用内容:

result = llm.create_chat_completion(
    messages = [...],
    tools=[...],
    tool_choice="auto",
    max_tokens=200,
    temperature=0.1
)

修复后完整代码示例

from llama_cpp import Llama

llm = Llama(
    model_path="/home/s1ngle/.cache/huggingface/hub/models--bartowski--Meta-Llama-3.1-8B-Instruct-GGUF/snapshots/9a8dec50f04fa8fad1dc1e7bc20a84a512e2bb01/Meta-Llama-3.1-8B-Instruct-Q4_K_M.gguf",
    chat_format="chatml-function-calling",
    verbose=False
)

result = llm.create_chat_completion(
    messages = [
        {"role": "user", "content": "Hello, what is the weather in Beijing?"}
    ],
    tools=[{
        "type": "function",
        "function": {
            "name": "get_current_weather",
            "description": "used whenever the model is being asked to find out the current weather",
            "parameters": {
                "type": "object",
                "properties": {
                    "city": {
                        "type": "string",
                        "description": "The name of the city to get weather for"
                    }
                },
                "required": ["city"]
            }
        }
    }],
    tool_choice="auto",
    max_tokens=200,
    temperature=0.1
)

print(result)

预期输出示例

修复后会生成包含tool_calls的结构化输出,类似:

{
    'id': 'chatcmpl-xxx',
    'object': 'chat.completion',
    'created': 1727433781,
    'model': '...',
    'choices': [
        {
            'index': 0,
            'message': {
                'role': 'assistant',
                'content': '',
                'tool_calls': [
                    {
                        'id': 'call_xxx',
                        'type': 'function',
                        'function': {
                            'name': 'get_current_weather',
                            'arguments': '{"city": "Beijing"}'
                        }
                    }
                ]
            },
            'finish_reason': 'tool_call'
        }
    ],
    'usage': {...}
}

内容的提问来源于stack exchange,提问作者VanechikSpace

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 20:22:06