Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OpenAiStrem is depritiated #15

Open
Prabal-verma opened this issue Jun 15, 2024 · 2 comments
Open

OpenAiStrem is depritiated #15

Prabal-verma opened this issue Jun 15, 2024 · 2 comments

Comments

@Prabal-verma
Copy link

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
Copy link

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
Copy link

How much does it cost to use OpenAI API ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants