-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
91 lines (76 loc) · 2.83 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
79
80
81
82
83
84
85
86
87
88
89
90
91
import express from 'express';
import bodyParser from 'body-parser';
import dotenv from 'dotenv';
dotenv.config();
import OpenAI from 'openai';
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
});
const app = express();
const port = 3000;
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static("public"));
//* openAIApi area (start)
// Make runPrompt function return the motivation instead of storing it in a global variable
const runPrompt = async (name, problem, retryCount = 0) => {
const maxRetries = 3;
const prompt = `
My name is ${name}, my problem is ${problem}.
Return response in the following parsable JSON format:
{
"A": "answer"
}
`;
try {
const response = await openai.chat.completions.create({
model: 'gpt-4',
messages: [
{
role: 'system',
content: "You are David Goggins. Provide harsh, one paragraph motivational speech relating with the user's struggles.",
},
{ role: 'user', content: prompt },
],
max_tokens: 200,
});
const result = response.choices[0].message.content;
try {
const jsonResponse = JSON.parse(result);
console.log(jsonResponse.A); // Output the parsed answer
return jsonResponse.A; // Return the motivation
} catch (error) {
console.error('Error parsing JSON:', error);
console.log('Original response:', result); // For debugging
// Retry if the parsing fails, up to maxRetries
if (retryCount < maxRetries) {
console.log(`Retrying... (${retryCount + 1}/${maxRetries})`);
return await runPrompt(name, problem, retryCount + 1);
} else {
console.error('Max retries reached. Could not parse JSON.');
return "Sorry, I couldn't generate a proper response. Try again!";
}
}
} catch (error) {
console.error('Error fetching completion:', error);
return "Sorry, there was an error fetching your motivation.";
}
};
//* openAIApi area (end)
app.get("/", (req, res) => {
res.render("index.ejs");
});
app.post("/", async (req, res) => {
console.log(req.body);
try {
const name = req.body.name;
const problem = req.body.problem;
const motivation = await runPrompt(name, problem); // Use user-specific data
res.render("index.ejs", { motivation: motivation }); // Send motivation to the template
} catch (error) {
console.log(error.message);
res.render("index.ejs", { motivation: "Error generating motivation. Please try again." });
}
});
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});