-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
58 lines (48 loc) · 1.78 KB
/
server.js
File metadata and controls
58 lines (48 loc) · 1.78 KB
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
import express from 'express';
import dotenv from 'dotenv';
import OpenAI from 'openai';
// Load environment variables from .env file
dotenv.config();
// Initialize the Express app
const app = express();
const port = 3000;
// Get the OpenAI API key from the environment variables
const apiKey = process.env.OPENAI_API_KEY; // DO NOT DIRECTLY ENTER YOUR KEY HERE!! USE dotenv!!!
// Check if the API key is provided
if (!apiKey) {
throw new Error('You forgot to add the API key in the .env file.');
}
// Set up OpenAI configuration
const openai = new OpenAI({
apiKey: apiKey // This is also the default, can be omitted
});
// Middleware to serve static files from the 'public' directory
app.use(express.static('public'));
// Middleware to parse JSON request bodies
app.use(express.json());
// Route to handle the generation of multiple choice questions
app.post('/generate', async (req, res) => {
const prompt = req.body.prompt;
try {
const response = await openai.completions.create({
model: "gpt-3.5-turbo-0125",
prompt: `Make 5 multiple choice questions based on the following: ${prompt}`,
max_tokens: 4096,
});
res.json(response.data);
} catch (error) {
if (error instanceof OpenAI.APIError) {
console.error(`Status: ${error.status}`);
console.error(`Message: ${error.message}`); // e.g. The authentication token you passed was invalid...
console.error(`Code: ${error.code}`);
console.error(`Type: ${error.type}`);
} else {
// Handle non-API errors
console.error(error);
}
}
});
// Start the server and listen on the specified port
app.listen(port, () => {
console.log(`Server is running at http://localhost:${port}`);
});