matt HOFFNER
fix local and move to original
87495f8
raw
history blame
830 Bytes
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) {
let { messages, systemMessage } = await req.json();
// Prepend the system message if it's not already there
if (messages.length === 0 || messages[0].role !== "system") {
messages = [{
role: "system",
content: systemMessage
}, ...messages];
}
const response = await openai.createChatCompletion({
model: 'gpt-4',
stream: true,
messages
})
const stream = OpenAIStream(response);
return new StreamingTextResponse(stream);
}