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

sdk.vercel.ai streamText调用工具异常:工具返回结果仍提示“Sorry, I don't know.”

问题排查与解决方案

核心原因分析

你遇到的问题大概率是系统提示词表述不够明确,导致LLM没有正确识别工具返回的结果为有效信息,反而触发了"Sorry, I don't know."的分支。当前提示词里的"If no relevant information is found after using the tools"表述模糊,LLM可能误判工具返回的临时结果为"无相关信息"。

具体修复方案

1. 优化系统提示词

修改system字段,明确告知LLM:只要工具返回结果,就必须用这个结果回答,只有工具返回空/无内容时才返回默认话术。示例:

system: `You are a helpful assistant with access to tools for retrieving information.
When answering user questions:
1. Always use the \`getInformation\` tool first to fetch relevant data.
2. If the tool returns any result (even a temporary one), use that result directly to answer the user's question. Do not ignore the tool's response.
3. Only respond with "Sorry, I don't know." if the tool returns an empty or null result.`,

2. 明确工具返回结构(可选)

将工具返回改为结构化数据,让LLM更容易识别有效信息。修改execute函数:

execute: async ({ question }) => ({
  answer: "A temporary result"
})

同时在提示词里补充说明工具返回结构:The \getInformation` tool returns an object with an `answer` field containing the information you need to use.`

3. 验证工具调用流程

确保messages参数正确传入了用户的问题,且工具确实被触发(可以在execute里加日志确认)。比如:

execute: async ({ question }) => {
  console.log("Tool called with question:", question);
  return "A temporary result";
}

修改后的完整代码示例

// route.ts
import { streamText, tool } from 'ai';
import { groq } from '@ai-sdk/groq';
import { z } from 'zod';

export async function POST(req: Request) {
  const { messages } = await req.json();

  const result = streamText({
    model: groq('llama-3.1-8b-instant'),
    messages,
    system: `You are a helpful assistant with access to tools for retrieving information.
When answering user questions:
1. Always use the \`getInformation\` tool first to fetch relevant data.
2. If the tool returns any result (even a temporary one), use that result directly to answer the user's question. Do not ignore the tool's response.
3. Only respond with "Sorry, I don't know." if the tool returns an empty or null result.`,
    maxSteps: 5,
    tools: {
      getInformation: tool({
        description: 'Retrieve information from the knowledge base to answer user questions.',
        parameters: z.object({
          question: z.string().describe('The exact question from the user'),
        }),
        execute: async ({ question }) => {
          console.log('Tool invoked with question:', question);
          return "A temporary result";
        }
      })
    }
  });

  return result.toDataStreamResponse();
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.13 09:58:16