import { createOpenAI } from "@ai-sdk/openai";import { streamText } from "ai";
// 1. 获取你的环境变量const apiKey = process.env.OPENAI_API_KEY;const baseUrl = process.env.OPENAI_BASE_URL;
const openai = createOpenAI({ apiKey: apiKey, baseURL: baseUrl,});
export async function POST(req: Request) { const { prompt, systemPrompt } = await req.json();
console.log("[/api/chat] 请求进入", { promptLength: typeof prompt === "string" ? prompt.length : 0, hasSystemPrompt: Boolean(systemPrompt), });
const result = await streamText({ model: openai("claude-3-5-haiku-20241022"), messages: [ { role: "system", content: systemPrompt ?? "你是一名专业的中文智能理财顾问,回答要通俗易懂、风险提示清晰。", }, { role: "user", content: prompt, }, ], });
return result.toTextStreamResponse();}
This is my code, and I am very sure that my apikey and baseURL can be used normally. I used
curl --location --request POST 'baseurl' \--header 'Content-Type: application/json' \--header 'Authorization: Bearer apikey ' \--data-raw '{ "model": "claude-3-5-haiku-20241022", "max_tokens": 512, "messages": [ { "role": "user", "content": "从上海火车站到陆家嘴怎么走" } ], "temperature": 0, "stream": true}'But now when using the Vercel AI SDK, it keeps reminding me
Error [AI_APICallError]: Bad Requestat ignore-listed frames {cause: undefined,url: ‘baseurl’,requestBodyValues: [Object],statusCode: 400,responseHeaders: [Object],responseBody: ‘{“message”:“Cannot invoke \“java.util.List.iterator()\” because the return value of \“com.zhongan.ai.gateway.protocol.adapter.llm.entity.Request.getMessages()\” is null”}’,isRetryable: false,data: undefined}
