import { OpenAIStream, StreamingTextResponse } from ‘ai’;
import OpenAI from ‘openai’;
// Create an OpenAI API client (that’s edge friendly!)
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY || ‘’,
});
// IMPORTANT! Set the runtime to edge
export const runtime = ‘edge’;
export default async function handler(req: Request, res: Response) {
const { messages } = await req.json();
// Ask OpenAI for a streaming chat completion given the prompt
const response = await openai.chat.completions.create({
model: ‘gpt-3.5-turbo’,
messages: messages,
stream: true,
});
// Convert the response into a friendly text-stream
const stream = OpenAIStream(response);
// Respond with the stream
return new StreamingTextResponse(stream);
}
⨯ node_modules.pnpm\openai@4.16.1\node_modules\openai\core.mjs (272:18) @ OpenAI.makeRequest
⨯ Connection error.