novella icon indicating copy to clipboard operation
novella copied to clipboard

OpenAIStream Not Working

Open pritishmishra703 opened this issue 1 year ago • 0 comments

This is my route.ts code:

import { StreamingTextResponse, OpenAIStream } from "ai";
export const runtime = "edge";

export async function POST(req: Request): Promise<Response> {
  const { prompt } = await req.json();
  const api_url = "<endpoint>";

  const data = {
    prompt,
    max_new_tokens: 200,
    temperature: 0.8,
  };

  try {
    const response = await fetch(api_url, {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
      },
      body: JSON.stringify(data),
    });

    if (response.ok) {
      const result = await response.json();
      const generated_text = result.generated_text;
      const textWithoutPrompt = generated_text.replace(prompt, "").trim();
      const stream = OpenAIStream(textWithoutPrompt);
      return new StreamingTextResponse(stream);
    } else {
      return new Response(`Error: ${response.status} - ${response.statusText}`, {
        status: response.status,
      });
    }
  } catch (error: any) {
    return new Response(`Error: ${error.message}`, {
      status: 500,
    });
  }
}

The OpenAIStream is not working in my case, I think the reason is my API response JSON is like this:

{
    'generated_text': 'the LLM text from my API'
}

So, what is the possible fix for this?

pritishmishra703 avatar Dec 14 '23 08:12 pritishmishra703