vercel / ai

Build AI-powered applications with React, Svelte, Vue, and Solid
https://sdk.vercel.ai/docs
Other
9.59k stars 1.41k forks source link

Error: Failed to convert the response to stream. Received status code: 429. Sveltekit #152

Closed AdrianFerrer1 closed 1 year ago

AdrianFerrer1 commented 1 year ago
//src/routes/api/completion/+server.ts
import { OPENAI_API_KEY } from "$env/static/private";

import { Configuration, OpenAIApi } from "openai-edge";
import { OpenAIStream, StreamingTextResponse } from "ai";

import type { RequestHandler } from "./$types";

const config = new Configuration({
  apiKey: OPENAI_API_KEY,
});
const openai = new OpenAIApi(config);

export const POST = (async ({ request }) => {
  const { prompt } = await request.json();

  const response = await openai.createCompletion({
    model: "text-davinci-003",
    max_tokens: 16,
    stream: true,
    prompt: prompt,
  });

  const stream = OpenAIStream(response);

  return new StreamingTextResponse(stream);
}) satisfies RequestHandler;
//src/routes/+page.svelte
<script>
  import { useCompletion } from "ai/svelte";

  const { input, handleSubmit, completion } = useCompletion({
    api: "/api/completion",
  });
</script>

<main>
  <form on:submit={handleSubmit}>
    <input
      type="text"
      bind:value={$input}
      placeholder="Describe your business..."
    />
    <button type="submit">Generate Slogan</button>
  </form>
  <p>{$completion}</p>
</main>
karlbateman commented 1 year ago

Possibly related to #144

AdrianFerrer1 commented 1 year ago

Possibly related to #144

omg I put the billing info and it works now lmao that is such a cashgrab move by "OPEN"ai xdxd insane

karlbateman commented 1 year ago

@AdrianFerrer1 Totally agree, I suspect this will be the source of many issues for those using the OpenAI integration.

MaxLeiter commented 1 year ago

Improved error handling was implemented in #163