hiteshchoudhary / ama-app

https://truefeedback.in
316 stars 63 forks source link

OpenAiStrem is depritiated #15

Open Prabal-verma opened 5 months ago

Prabal-verma commented 5 months ago

there needs to be some changes in suggest-messages as openaistream has been depritiated also vercel has removed some of the providers and updated whole document

sankalpbarriar commented 5 months ago

use the given code - since openaistream has been removed use the ReadableStream object

import { OpenAI } from "openai";
import { NextResponse } from "next/server";

//USE CASE

//1. User will click on suggest-message
//2. we will go to open ai with some prompts
//3. we will show the response on frontend

const openai = new OpenAI({     
  apiKey: process.env.OPENAI_API_KEY,
});

export const runtime = "edge";

export async function POST(req: Request) {
  try {
    const prompt =
      "Create a list of three open-ended and engaging questions formatted as a single string. Each question should be separated by '||'. These questions are for an anonymous social messaging platform, like Qooh.me, and should be suitable for a diverse audience. Avoid personal or sensitive topics, focusing instead on universal themes that encourage friendly interaction. For example, your output should be structured like this: 'What’s a hobby you’ve recently started?||If you could have dinner with any historical figure, who would it be?||What’s a simple thing that makes you happy?'. Ensure the questions are intriguing, foster curiosity, and contribute to a positive and welcoming conversational environment.";

    const response = await openai.chat.completions.create({
      model: "gpt-3.5-turbo",
      messages: [{ role: "user", content: prompt }],
      max_tokens: 400,
      stream: true,
    });

    const stream = new ReadableStream({
      async start(controller) {
        for await (const chunk of response) {
          const { choices } = chunk;
          if (choices && choices.length > 0) {
            const text = choices[0].delta?.content || "";
            controller.enqueue(text);
          }
        }
        controller.close();
      },
    });

    return new Response(stream, {
      headers: { "Content-Type": "text/plain" },
    });
  } catch (error) {
    if (error instanceof OpenAI.APIError) {
      // OpenAI API error handling
      const { name, status, headers, message } = error;
      return NextResponse.json({ name, status, headers, message }, { status });
    } else {
      console.error("An unexpected error occurred:", error);
      throw error;
    }
  }
}

home it will work

LastMinuteLion commented 2 months ago

How much does it cost to use OpenAI API ?