这是我第一次尝试使用Vercel的新生成式UI与AI SDK,我使用的是Google的Gemini AI,模型为gemini-1.5-pro-latest。在本地运行时一切正常,但部署后返回以下响应:
responseBody: '{\n' +' "error": {\n' +' "code": 429,\n' +' "message": "Resource has been exhausted (e.g. check quota).",\n' +' "status": "RESOURCE_EXHAUSTED"\n' +' }\n' +'}\n',
我使用的是Next 14和Typescript,所以函数是在服务器端运行的(从Vercel的文档中复制),以下是调用SDK的函数:
export async function continueConversation( input: string): Promise<ClientMessage> { "use server"; const history = getMutableAIState(); const result = await streamUI({ model: google("models/gemini-1.5-pro-latest"), system: ` You are a general purpose assistant, you can help the user with a variety of tasks. You can tell jokes, give place and song recommendations, and much more. You are a professional, don't use emote. `, messages: [...history.get(), { role: "user", content: input }], text: ({ content, done }) => { if (done) { history.done((messages: ServerMessage[]) => [ ...messages, { role: "assistant", content }, ]); } return ( <article className="markdown-container"> <Markdown remarkPlugins={[remarkGfm]}>{content}</Markdown> </article> ); }, tools: { getJoke: { description: "A tool when the user wants a joke. The joke should make the user laugh.", parameters: z.object({ category: z.string().optional().describe("the category of the joke"), }), generate: async function* ({ category }) { yield <LoaderCircle />; const joke = await generateObject({ model: google("models/gemini-1.5-pro-latest"), schema: jokeSchema, prompt: "Generate a joke that will make the user laugh. The joke should be in the category of " + category + ". If no category is provided, ask the user for a category.", }); return <JokeComponent joke={joke.object} />; }, }, getPlaces: { description: "A tool when the user wants place recommendations based on the location and type.", parameters: z.object({ location: z.string().describe("the user's location"), type: z.string().optional().describe("the type of place"), }), generate: async function* ({ location, type }) { yield <LoaderCircle className="loader-circle" />; const places = await generateObject({ model: google("models/gemini-1.5-pro-latest"), schema: placeSchema, prompt: "Generate an array of places to visit in " + location + " with the type of " + (type || "any type") + ". The array should contain at least 5 places.", }); if (places && places.object && Array.isArray(places.object)) { return <PlaceComponent place={places.object} />; } else { return <p>Something went wrong, please try again later.</p>; } }, }, getSongs: { description: "A tool when the user wants song recommendations based on the genre.", parameters: z.object({ genre: z.string().optional().describe("the genre of the song"), singer: z.string().optional().describe("the singer of the song"), }), generate: async function* ({ genre, singer }) { yield <LoaderCircle />; const songs = await generateObject({ model: google("models/gemini-1.5-pro-latest"), schema: songSchema, prompt: "Generate songs recommendation in the genre of " + (genre || "any genres") + "or by the singer " + (singer || "any singer") + ". Return an array of 3 songs.", }); if (songs && songs.object && Array.isArray(songs.object)) { return <SongComponent song={songs.object} />; } else { return <p>Something went wrong, please try again later.</p>; } }, }, }, }); return { id: nanoid(), role: "assistant", display: result.value, };}
以下是完整的actions.tsx和page.tsx文件:
- actions.tsx : https://github.com/msafdev/msaf.pr/blob/main/lib/actions.tsx
- page : https://github.com/msafdev/msaf.pr/blob/main/app/page.tsx
我尝试更换了新的账户的API密钥,但没有效果,奇怪的是,在本地仍然可以正常工作。任何建议都将非常受欢迎,谢谢!
回答:
如果你看到429配额错误,那么你的配额已经用完了。
你可以在这里查看默认配额:https://ai.google.dev/gemini-api/docs/rate-limits(确保选择你正在使用的模型)。
由于你使用的是gemini-1.5-pro-latest
,我假设你是使用免费层级,你的配额将是每分钟2个请求(撰写时正确,但请检查定价页面)。如果你在本地尝试了两次请求,然后在同一分钟内将相同的API密钥部署到另一个环境并运行,你将没有剩余配额。
尝试等待一分钟,看看错误是否仍然存在。否则,尝试使用配额更多的模型,如gemini-1.5-flash-latest
,或者启用计费以获得更高的限制。