-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
77 lines (67 loc) · 2.13 KB
/
Copy pathserver.js
File metadata and controls
77 lines (67 loc) · 2.13 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
72
73
74
75
76
77
// node --version # Should be >= 18
// npm install @google/generative-ai express
const express = require('express');
const { GoogleGenerativeAI, HarmCategory, HarmBlockThreshold } = require('@google/generative-ai');
const dotenv = require('dotenv').config()
const app = express();
const port = process.env.PORT || 3000;
app.use(express.json());
const MODEL_NAME = "gemini-pro";
const API_KEY = process.env.API_KEY;
async function runChat(userInput) {
const genAI = new GoogleGenerativeAI(API_KEY);
const model = genAI.getGenerativeModel({ model: MODEL_NAME });
const generationConfig = {
temperature: 0.9,
topK: 1,
topP: 1,
maxOutputTokens: 1000,
};
const safetySettings = [
{
category: HarmCategory.HARM_CATEGORY_HARASSMENT,
threshold: HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE,
},
// ... other safety settings
];
const chat = model.startChat({
generationConfig,
safetySettings,
history: [
{
role: "user",
parts: [{ text: "You are an A.I. therapist, you have to listen to the user's input and problems and try to be helpful for him/her. If the user asks who are you, you have to say you are an A.I. therapist, DO NOT mention that you are gemini."}],
},
{
role: "model",
parts: [{ text: "OK,i will start the roleplay."}],
},
],
});
const result = await chat.sendMessage(userInput);
const response = result.response;
return response.text();
}
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
});
app.get('/loader.gif', (req, res) => {
res.sendFile(__dirname + '/loader.gif');
});
app.post('/chat', async (req, res) => {
try {
const userInput = req.body?.userInput;
console.log('incoming /chat req', userInput)
if (!userInput) {
return res.status(400).json({ error: 'Invalid request body' });
}
const response = await runChat(userInput);
res.json({ response });
} catch (error) {
console.error('Error in chat endpoint:', error);
res.status(500).json({ error: 'Internal Server Error' });
}
});
app.listen(port, () => {
console.log(`Server listening on port ${port}`);
});