forked from ahmadbasyouni10/ctp-hackathon-24
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchat-openai.js
64 lines (53 loc) · 1.91 KB
/
chat-openai.js
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import dotenv from "dotenv";
import OpenAI from "openai";
import vectorSearchOpenAI from "./vectorSearchOpenAI.js";
dotenv.config();
export default async function handler(req, res) {
if (req.method === "POST") {
const { messages, school } = req.body;
const currentMessageContent = messages[messages.length - 1].content;
try {
const previousMessages = messages.slice(0, -1);
// Initialize the OpenAI chat model
const openai = new OpenAI({
apiKey: process.env.OPENAI_KEY,
});
// Perform the RAG search
const ragResults = await vectorSearchOpenAI(currentMessageContent);
// Ensure ragResults is an array of strings
const ragMessages = Array.isArray(ragResults) ? ragResults : [ragResults];
const formattedRagMessages = ragMessages.map((result) =>
typeof result === "string" ? result : JSON.stringify(result)
);
const completion = await openai.chat.completions.create({
model: "gpt-4o-mini",
messages: [
{
role: "system",
content: `You are a helpful ${school} guide who loves to assist students! If you are unsure say "I'm sorry, I don't have that specific information."`,
},
...previousMessages,
...formattedRagMessages.map((result) => ({
role: "assistant",
content: result,
})),
{
role: "user",
content: currentMessageContent,
},
],
});
const response = completion.choices[0].message.content;
// console.log(completion.choices[0].message);
res.status(200).json(response);
} catch (error) {
console.error("Error in chat:", error);
res.status(500).json({
error: "An error occurred during chat processing",
});
}
} else {
res.setHeader("Allow", ["POST"]);
res.status(405).end(`Method ${req.method} Not Allowed`);
}
}