How can handle continuous chat with Chatgpt in next Js

I created a voice assistant using ChatGPT. It is working fine, but I would like something like this. Please follow the example.
User - who is Sachin Tendulkar
GPT- Sachin Ramesh Tendulkar is an Indian former international cricketer who captained the Indian national team so on …
User - what is his age
GPT - he is 50 years old like so on …
so i want like this

i have code it in next js

async function gpt(prompt) {
        try {

            const response = await fetch("/api/chat", {
                method: "POST",
                headers: {
                    "Content-Type": "application/json",
                body: JSON.stringify({
            if (!response.ok) {
                toast.error("Error in gpt :", response.statusText);
            } else {

            const reader = response.body
                .pipeThrough(new TextDecoderStream())
            let done = false;
            try {
                while (true) {
                    if (streamingStoppedRef.current) {
                    const { value, done } = await;
                    if (done) break;
                    setText((prev) => prev + value);
            }   catch (error) {
            console.error("Error in gpt function:", error);
            toast.error("Error in gpt function:", error);


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

// Create an OpenAI API client (that's edge friendly!)
const config = new Configuration({
  apiKey: process.env.CHATGPT_API_KEY,
const openai = new OpenAIApi(config);
// Set the runtime to edge for best performance
export const runtime = 'edge';

export default async function handler(req, context) {
  const {prompt } = await req.json();

  // Ask OpenAI for a streaming completion given the prompt
  const response = await openai.createChatCompletion({
    model: 'gpt-3.5-turbo',
    n: 1,
    temperature: 0.3,
    stream: true,
    messages: [
        role: 'user',
        content: prompt,

  // Convert the response into a friendly text-stream
  const stream = OpenAIStream(response);
  // Respond with the stream
  return new StreamingTextResponse(stream);
 <div className="Gpt_response" ref={textRef}>
                                        <p id="chatgptId" dangerouslySetInnerHTML={{ __html: text }} />


here i am getting response