-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
78 lines (61 loc) · 2.44 KB
/
index.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import OpenAI from 'openai';
export default {
async fetch(request, env, ctx) {
const openai = new OpenAI({
apiKey: env.OPENAI_API_KEY,
baseURL: ' ', // Provide your own AI Gateway endpoint
});
// Define the CORS headers
const headers = new Headers({
'Access-Control-Allow-Origin': ' ', // Replace with your frontend URL
'Access-Control-Allow-Methods': 'POST',
'Access-Control-Allow-Headers': 'Content-Type',
});
// Check if the request method is OPTIONS (preflight request)
if (request.method === 'OPTIONS') {
return new Response(null, { headers });
}
try {
const requestBody = await request.json(); // Parse the request body as JSON
// Extract userQuery from the request body
const userQuery = requestBody.userQuery;
if (!userQuery) {
return new Response(JSON.stringify({ error: 'userQuery is missing in the request body' }), { status: 400 });
}
const assistant = await openai.beta.assistants.retrieve(' '); // Provide your assistant key
const thread = await openai.beta.threads.create();
const message = await openai.beta.threads.messages.create(thread.id, { role: 'user', content: userQuery });
const run = await openai.beta.threads.runs.create(thread.id, { assistant_id: assistant.id });
// Function to check run status and retrieve messages
const checkStatusAndPrintMessages = async (threadId, runId) => {
let runStatus;
let messages;
do {
runStatus = await openai.beta.threads.runs.retrieve(threadId, runId);
if (runStatus.status === 'completed') {
messages = await openai.beta.threads.messages.list(threadId);
} else {
await new Promise((resolve) => setTimeout(resolve, 5000)); // Wait for 5 seconds
}
} while (runStatus.status !== 'completed');
let assistantResponse = '';
// Find the Assistant's response
for (const msg of messages.data) {
if (msg.role === 'assistant') {
const content = msg.content[0].text.value;
assistantResponse += content + '\n';
}
}
// Return the response with the Assistant's message
return new Response(assistantResponse, { headers });
};
// Initial check and possibly waiting for completion
const response = await checkStatusAndPrintMessages(thread.id, run.id);
// Return the final response
return response;
} catch (e) {
// Handle any errors that may occur
return new Response(JSON.stringify({ error: e.message }), { status: 500, headers });
}
},
};