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

使用LiteLLM与Gemini-2.0-Flash构建终端助手时工具调用不足的问题排查

使用LiteLLM与Gemini-2.0-Flash构建终端助手时工具调用不足的问题排查

看起来你的终端助手遇到了工具调用不积极的问题,我帮你从系统提示和工具定义两个核心方向排查一下,先揪出最关键的问题:

一、最致命的问题:系统提示被意外覆盖了!

看你代码里的这段:

messages = [{"role": "system", "content": """You are a Linux terminal assistant. 
             Use 'ls', 'cd', and 'cat' to locate files. 
             ...(省略后续提示内容)"""}]
messages = [{"role": "user", "content": prompt}]

你先定义了系统提示的消息,结果下一行直接把messages重新赋值成只有用户提问的列表了!这就导致模型完全没收到你写的系统指令,根本不知道自己需要调用工具来完成任务,自然只会输出“你应该用cat命令”这类建议,而不是触发工具调用。

修复方法:

把两行合并,同时保留系统提示和用户提问:

messages = [
    {"role": "system", "content": """You are a Linux terminal assistant. 
                 Use 'ls', 'cd', and 'cat' to locate files. 
                 You will be prompted to solve a simple terminal query. 
                 Use the provided tools do to so. 
                 Always first list the CURRENT directory using the 'ls' tool to find out which files and directories are present inside it. 
                 Once the query is solved, do not use any tool and respond with 'solved'. 
                 Do not request further input from the user.
                 When you are asked to print the contents of a file, execute the cat command to retrieve its contents and then return those.
                 Do NOT list the root directory or any parent directories. Work from the current directory and its children directories."""},
    {"role": "user", "content": prompt}
]

二、系统提示可以进一步强化工具调用的要求

即使修复了覆盖问题,也可以优化提示语,让模型更明确地知道必须调用工具,而不是输出命令建议:

  • 在提示开头加上强制要求:你必须使用提供的工具函数(ls/cd/cat)来执行所有操作,绝对不能直接向用户输出终端命令。所有操作都要通过工具调用完成,再基于工具返回的结果继续处理。
  • 把“Use the provided tools do to so.”改成更明确的表述:你只能通过调用提供的工具来完成任务,不能自行模拟命令执行结果。

三、工具定义的潜在优化点(次要)

你的工具定义整体符合LiteLLM的格式要求,但可以做一些细节优化,让模型更容易理解如何调用:

  • 给每个工具的参数加上required字段,明确告诉模型哪些参数是必须的,比如cat工具的filename:
"parameters": {
    "type": "object", 
    "properties": {
        "filename": {
            "type": "string",
            "description": "The target file to be read, e.g. cat.txt",
        }
    },
    "required": ["filename"]  # 新增这一行
}
  • 工具描述里可以补充更具体的场景示例,比如ls的描述可以加上"例如:当前目录下有哪些文件,就调用ls工具查看",帮助模型关联场景和工具。

四、额外的调试建议

  1. 调整tool_choice参数:把tool_choice="auto"改成tool_choice="required",强制模型必须考虑调用工具(适合测试阶段,确认工具能被触发后再改回auto)。
  2. 降低temperature值:你当前设置的是0.8,这个值偏高,模型可能更倾向于发散输出,改成0.1或0.2能让模型更严格遵循系统提示。

先把系统提示的覆盖问题修好,这应该就能解决大部分工具不调用的情况,再逐步优化提示和工具定义,应该就能让助手正常工作了!

备注:内容来源于stack exchange,提问作者d0cmen

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.13 20:09:29