-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathopenai.js
28 lines (23 loc) · 975 Bytes
/
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
// Import the necessary libraries and set up your OpenAI API key
const openai = require('openai');
const apiKey = process.env.OPENAI_API_KEY;
const openaiClient = new openai({ apiKey });
// Function to generate a schedule using OpenAI GPT-3
async function generateSchedule(classSchedule, tasks) {
try {
const prompt = `Generate a schedule for a student with the following class schedule:\n${classSchedule}\n\nTasks to complete:\n${tasks}`;
// Call the OpenAI API to generate the schedule
const response = await openaiClient.Completion.create({
engine: 'davinci',
prompt,
max_tokens: 100, // Adjust the max tokens as needed
});
// Extract and return the generated schedule from the response
const generatedSchedule = response.choices[0].text;
return generatedSchedule;
} catch (error) {
console.error('Error generating schedule from OpenAI:', error);
throw error;
}
}
module.exports = { generateSchedule };