import { OpenAIStream, StreamingTextResponse } from "ai"; import { Configuration, OpenAIApi } from "openai-edge"; const config = new Configuration({ apiKey: process.env.OPENAI_API_KEY, }); const openai = new OpenAIApi(config); export const runtime = "edge"; export default async function(req: Request) { const { messages } = await req.json(); const response = await openai.createChatCompletion({ model: 'gpt-4', stream: true, messages }) const stream = OpenAIStream(response); return new StreamingTextResponse(stream); }