-
Notifications
You must be signed in to change notification settings - Fork 218
/
Copy pathapp.js
84 lines (65 loc) · 1.94 KB
/
app.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
import express from 'express';
import { OpenAI } from 'openai';
import path from 'path';
import { fileURLToPath } from 'url';
import dotenv from 'dotenv';
import json from './public/characters.json' with { type: "json" };
let systemMessage = json[4].description;
let page = json[4].page;
console.log("SERVER systemMessage: ", systemMessage);
console.log("SERVER page: ", page);
dotenv.config();
const app = express();
const port = process.env.PORT || 3000;
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
app.use(express.json());
// Serve static files from the 'public' directory
app.use(express.static(path.join(__dirname, 'public')));
app.locals.delimiters = '{{ }}';
function getCharacterByName(name) {
for(let i=0; i< json.length; i++) {
if(json[i].name == name) {
return json[i];
}
}
return null;
}
// Route to send the prompt
app.post('/send', async (req, res) => {
const { message, character } = req.body;
systemMessage = character.description;
const prompt = message;
const messages = [
{
"role": "system",
"content": systemMessage,
},
{
"role": "user",
"content": prompt
}
];
const openai = new OpenAI({
baseURL: "https://models.inference.ai.azure.com", // might need to change to this url in the future: https://models.github.ai/inference
apiKey: process.env.GITHUB_TOKEN,
});
try {
console.log(`SERVER sending prompt ${prompt}`)
const completion = await openai.chat.completions.create({
model: 'gpt-4o-mini',
messages: messages,
});
console.log(`SERVER: ${completion.choices[0]?.message?.content}`);
res.json({
prompt: prompt,
answer: completion.choices[0]?.message?.content
});
} catch (error) {
console.log(`Error: ${error}`);
res.status(500).json({ error: error });
}
});
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});