-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassistants-utils.js
32 lines (29 loc) · 1008 Bytes
/
assistants-utils.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
require("dotenv").config();
const OpenAI = require("openai");
const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
const BASIC_ASSISTANT_NAME = "Basic Assistant";
const BASIC_ASSISTANT_SYSTEM_MESSAGE =
"You are an AI assistant to help user with their queries.";
module.exports.getBasicAssistant = async () => {
let assistants = await openai.beta.assistants.list();
let assistant = assistants.data.find(
(assistant) => assistant.name == BASIC_ASSISTANT_NAME
);
if (!assistant) {
console.log(`${BASIC_ASSISTANT_NAME} Not found. Creating one now`);
assistant = await openai.beta.assistants.create({
name: BASIC_ASSISTANT_NAME,
instructions: BASIC_ASSISTANT_SYSTEM_MESSAGE,
model: "gpt-4o",
});
}
return assistant;
};
let threadId = null; // This will store the thread ID
module.exports.getOrCreateThread = async () => {
if (!threadId) {
const thread = await openai.beta.threads.create();
threadId = thread.id;
}
return threadId;
};