ai icon indicating copy to clipboard operation
ai copied to clipboard

Missing tool_call_id

Open iGmainC opened this issue 7 months ago • 2 comments

Description

error message:

{
  url: 'https://api.ai-gaochao.cn/v1/chat/completions',
  requestBodyValues: {
    model: 'gpt-3.5-turbo-0125',
    logit_bias: undefined,
    logprobs: undefined,
    top_logprobs: undefined,
    user: undefined,
    parallel_tool_calls: undefined,
    max_tokens: undefined,
    temperature: 0.5,
    top_p: undefined,
    frequency_penalty: undefined,
    presence_penalty: undefined,
    seed: undefined,
    messages: [ [Object], [Object], [Object], [Object], [Object] ],
    tools: [ [Object] ],
    tool_choice: 'auto',
    stream: true,
    stream_options: undefined
  },
  statusCode: 400,
  responseHeaders: {
    connection: 'keep-alive',
    'content-length': '282',
    'content-type': 'application/json; charset=utf-8',
    date: 'Thu, 04 Jul 2024 04:34:00 GMT',
    server: 'nginx',
    'x-cache-lookup': 'Cache Miss, Cache Miss, Cache Miss',
    'x-nws-log-uuid': '13820491355925190028',
    'x-oneapi-request-id': '2024070412335989301426876979553'
  },
  responseBody: `{"error":{"message":"Missing parameter 'tool_call_id': messages with role 'tool' must have a 'tool_call_id'. (request id: 2024070404335997779905546548020) (request id: 20240704123359926885698l4WWc0Dk)","type":"invalid_request_error","param":"messages.[3].tool_call_id","code":null}}`,
  cause: undefined,
  isRetryable: false,
  data: {
    error: {
      message: "Missing parameter 'tool_call_id': messages with role 'tool' must have a 'tool_call_id'. (request id: 2024070404335997779905546548020) (request id: 20240704123359926885698l4WWc0Dk)",
      type: 'invalid_request_error',
      param: 'messages.[3].tool_call_id',
      code: null
    }
  }
}

Code example

server code:

import { createOpenAI } from "@ai-sdk/openai";
import { convertToCoreMessages, streamText } from "ai";
import { OPENAI_API_KEY, OPENAI_BASE_URL } from "@/config/constants";
import { z } from "zod";

const openai = createOpenAI({
  apiKey: OPENAI_API_KEY,
  baseURL: OPENAI_BASE_URL,
});

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

  let tmp = convertToCoreMessages(messages);
  const result = await streamText({
    model: openai(config.model),
    system: config.prompt ? config.prompt : undefined,
    temperature: config.temperature,
    messages: tmp,
    tools: {
      searchDB: {
        description: "xxx",
        parameters: z.object({
          query: z.string().describe("xxx"),
        }),
        execute: async ({ query }) => {
          return JSON.stringify(["xxx", "xxx"]);
        },
      },
    },
  });
  return result.toAIStreamResponse();
}

Additional context

"@ai-sdk/openai": "^0.0.34", "ai": "^3.2.16",

let tmp = convertToCoreMessages(messages);

[
  {
    role: "user",
    content: "xxx",
  },
  {
    role: "assistant",
    content: [
      {
        type: "text",
        text: "",
      },
      {
        type: "tool-call",
        toolCallId: "call_cnrX1hmJQfZjWs6WxbPRiKV8",
        toolName: "searchDB",
        args: {
          query: "xxx",
        },
      },
    ],
  },
  {
    role: "tool",
    content: [
      {
        type: "tool-result",
        toolCallId: "call_cnrX1hmJQfZjWs6WxbPRiKV8",
        toolName: "searchDB",
        args: {
          query: "xxx",
        },
        result: '["xxx"]',
      },
    ],
  },
];

iGmainC avatar Jul 04 '24 04:07 iGmainC