ai icon indicating copy to clipboard operation
ai copied to clipboard

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

Open AdrianFerrer1 opened this issue 1 year ago • 3 comments

//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>

AdrianFerrer1 avatar Jun 18 '23 17:06 AdrianFerrer1

Possibly related to #144

karlbateman avatar Jun 18 '23 19:06 karlbateman

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

AdrianFerrer1 avatar Jun 18 '23 20:06 AdrianFerrer1

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

karlbateman avatar Jun 18 '23 20:06 karlbateman

Improved error handling was implemented in #163

MaxLeiter avatar Jul 15 '23 02:07 MaxLeiter