Spaces:
Paused
Paused
File size: 1,624 Bytes
872630d cbb675f 872630d 110c969 872630d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
import OpenAI from 'openai';
import { OpenAIStream, StreamingTextResponse } from 'ai';
export const runtime = 'edge';
export async function POST(req: Request) {
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
});
const { messages, data } = await req.json();
const vectorStoreResultsString = data.vectorStoreResults;
const previousMessages = messages.slice(0, -1).map((message: any) => {
return message.role === "user" ? `User: ${message.content}\n` : `Assistant: ${message.content}\n`;
}).join("");
const lastMessage = messages[messages.length - 1]?.content || 'No message found';
const response = await openai.chat.completions.create({
model: "gpt-4o-mini",
stream: true,
messages: [
{
role: "system",
content:
"Use the following pieces of context (or previous conversaton if needed) to answer the users question in markdown format.",
},
{
role: "user",
content: `Use the following pieces of context (or previous conversaton if needed) to answer the users question in markdown format. \nIf you don't know the answer, just say that you don't know, don't try to make up an answer.
\n----------------\n
PREVIOUS CONVERSATION:
${previousMessages}
\n----------------\n
CONTEXT:
${vectorStoreResultsString}
USER INPUT: ${lastMessage}`,
},
],
});
const stream = OpenAIStream(response);
return new StreamingTextResponse(stream);
}
|