为什么我的代码在具有生成 UI 的 Google Gemini API 上发送 429 配额超出?

问题描述 投票:0回答:1

这是我第一次尝试使用 AI SDK 的新 Vercel 生成 UI,我使用的是 Google 的 Gemini AI 和 Gemini-1.5-pro-最新模型,它在我的本地运行完美,但部署时返回以下响应:

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 密钥,但无济于事,问题是,它仍然可以在我的本地运行:< Any suggestions will be very appreciated, thanks!

next.js artificial-intelligence server-side-rendering gemini generative
1个回答
0
投票

如果您看到 429 配额错误,则说明您已用完配额。

您可以在此处查看默认配额:https://ai.google.dev/pricing(确保选择您正在使用的型号)。

由于您正在使用

gemini-1.5-pro-latest
,并且我假设您使用的是免费套餐,因此您的配额为每分钟 2 个请求。如果您在本地尝试 2 轮请求,然后将相同的 API 密钥部署到另一个环境并在一分钟内运行它,您将没有任何剩余配额。

尝试等待一分钟,看看错误是否仍然存在。否则尝试使用具有更多配额的模型,例如

gemini-1.5-flash-latest

© www.soinside.com 2019 - 2024. All rights reserved.