为什么我的代码在使用Google Gemini API的生成式UI时返回429配额超限?

这是我第一次尝试使用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文件:

我尝试更换了新的账户的API密钥,但没有效果,奇怪的是,在本地仍然可以正常工作。任何建议都将非常受欢迎,谢谢!


回答:

如果你看到429配额错误,那么你的配额已经用完了。

你可以在这里查看默认配额:https://ai.google.dev/gemini-api/docs/rate-limits(确保选择你正在使用的模型)。

由于你使用的是gemini-1.5-pro-latest,我假设你是使用免费层级,你的配额将是每分钟2个请求(撰写时正确,但请检查定价页面)。如果你在本地尝试了两次请求,然后在同一分钟内将相同的API密钥部署到另一个环境并运行,你将没有剩余配额。

尝试等待一分钟,看看错误是否仍然存在。否则,尝试使用配额更多的模型,如gemini-1.5-flash-latest,或者启用计费以获得更高的限制。

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注