I am using Vercel AI SDK with Open AI API to get the response as stream.
I am unable to figure out how can I get the finish_reason property from the response since it will tell me when the stream has ended or not.
I just want something to check if the stream has ended or not, before retuning the data to the frontend.
Here's my code.
import { Configuration, OpenAIApi } from "openai-edge";
import { OpenAIStream, StreamingTextResponse } from "ai";
export const runtime = "edge";
const config = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(config);
export async function POST(req: Request) {
try {
const { messages } = await req.json();
const response = await openai.createChatCompletion({
model: "gpt-3.5-turbo",
messages,
stream: true,
});
const stream = OpenAIStream(response);
return new StreamingTextResponse(stream);
} catch (error) {
console.log(error);
return "Error while generating response from Open AI.";
}
}
Please help me figure this out.