-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
117 lines (92 loc) · 3.06 KB
/
server.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import express from 'express';
import path from 'path';
import dotenv from "dotenv";
import { GoogleGenerativeAI } from "@google/generative-ai";
import { DiffieHellmanGroup } from 'crypto';
const app = express();
app.set("view engine", "ejs");
const PORT = 3000;
dotenv.config();
//some strict mime type checking was enabled so used below code to tell .js is actually javascript code
app.use(express.static('public',{
setHeaders: (res, path, stat) => {
if (path.endsWith('.js')) {
res.set('Content-Type', 'text/javascript');
}
}
}));
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
const genAI = new GoogleGenerativeAI(process.env.API_key);
// let messageHistory = [];
let chat;
app.get("/",(req, res) => {
res.render("index.ejs")
});
app.get('/contact', (req, res) => {
res.render('contact');
});
app.get('/feedback', (req, res) => {
res.render('feedback');
});
// Store feedback messages in an array
let feedbackList = [];
app.post('/feedback', (req, res) => {
const feedbackMessage = req.body.message.trim();
feedbackList.push(feedbackMessage);
res.redirect('/'); // Redirect to homepage after submitting feedback
});
app.get('/services', (req, res) => {
res.render('services');
});
app.get("/depression",(req, res) => {
res.render("depression.ejs")
});
app.get("/anxiety",(req, res) => {
res.render("anxiety.ejs")
});
//start chat session
async function startChat(){
const model = await genAI.getGenerativeModel({model: 'gemini-pro'});
chat = model.startChat({
history: [],
generationConfig: {
maxOutputTokens: 500,
},
});
}
startChat();
let messageHistory = [];
app.get("/another",async (req, res) => {
const aiMessage = "Hello, It's your psycologist here. How can I help you?"
const userMessage = "please behave as a physocologist and your name is Dr. Dreamy Doodles. If anyone ask you that who are you then answer that I'm Doctor Dreamy Doodles and I'm you psycologist. If any one ask some thing like what do you do then reply that I help people recover their mental health. "
const airesp = await sendMessage(userMessage);
console.log(airesp);
messageHistory.push({ sender: 'Dr. Dreamy Doodles', message: aiMessage})
res.render('another', { messageHistory });
});
app.post('/blogs', async (req, res) => {
const userMessage = req.body.message.trim();
const aiResponse = await sendMessage(userMessage);
messageHistory.push({ sender: 'You', message: userMessage });
messageHistory.push({ sender: 'Dr. Dreamy Doodles', message: aiResponse });
res.setHeader('Content-Type', 'text/plain');
res.send(aiResponse);
});
async function sendMessage(userMessage) {
try {
let text = '';
const result = await chat.sendMessageStream(userMessage);
for await (const chunk of result.stream) {
const chunkText = chunk.text();
text += ((chunkText.replace(/\*/g, ''))+'\n');
}
return text;
} catch (error) {
console.error('Error:', error);
return null;
}
}
app.listen(PORT, ()=> {
console.log(`Server is running on port ${PORT}`)
})