-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathserver.cjs
More file actions
71 lines (59 loc) · 2.66 KB
/
server.cjs
File metadata and controls
71 lines (59 loc) · 2.66 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
59
60
61
62
63
64
65
66
67
68
69
70
71
const express = require('express');
const cors = require('cors');
const bodyParser = require('body-parser');
const OpenAI = require("openai");
const app = express();
const PORT = process.env.PORT || 7005;
const openai = new OpenAI({ apiKey: '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' });
app.use(cors({
origin: '*',
methods: 'GET,POST,PUT,DELETE',
credentials: true
}));
app.use(bodyParser.json());
app.post('/api/question', async (req, res) => {
const question = req.body.question;
let prompt = '';
if (question.includes('book') || question.includes('books')) {
prompt = `Recommend 5 books related to : '${question}' with a brief description of each in less than 3 sentences.`;
} else if (question.includes('movie') || question.includes('movies') || question.includes('documentary') || question.includes('documentaries')) {
prompt = `Give 5 African movies related to '${question}' with a brief description of each in 2 sentences.`;
} else if (question.includes('articles') || question.includes('article')) {
prompt = `Give 5 articles related to '${question}' with a brief description of each in 1 sentence.`;
} else {
prompt = `Rephrase the question, please.`;
}
try {
const completion = await getCompletionWithRetry(prompt);
res.json({ message: completion });
} catch (error) {
console.error('Error fetching data:', error);
res.status(500).json({ error: 'Internal server error' });
}
});
async function getCompletionWithRetry(prompt, max_retries = 3) {
let retries = max_retries;
while (retries > 0) {
try {
const response = await openai.chat.completions.create({
model: "gpt-3.5-turbo",
messages: [{ role: "system", content: "You are an expert in African culture and history. Provide insightful responses in 2 sentences." }, { role: "user", content: prompt }],
});
return response.choices[0].message["content"];
} catch (error) {
if (error.response && error.response.status === 502) {
console.log(`Received a 'Bad Gateway' error, retrying... (Attempts left: ${retries})`);
} else if (error.message.includes('read timeout')) {
console.log(`Read timed out, retrying... (Attempts left: ${retries})`);
} else {
throw error;
}
retries--;
await new Promise(resolve => setTimeout(resolve, 5000)); // Wait for a while before retrying
}
}
throw new Error('API is not responding');
}
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});