Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
76 lines (62 loc) · 2.2 KB
/
Copy pathserver.js
File metadata and controls
76 lines (62 loc) · 2.2 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
require('dotenv').config();
const express = require('express');
const { GoogleGenerativeAI } = require('@google/generative-ai');
const bodyParser = require('body-parser');
const path = require('path');
const http = require('http');
const socketIo = require('socket.io');
const app = express();
const server = http.createServer(app);
const io = socketIo(server);
const port = 3004;
app.use(express.static('public'));
app.use(bodyParser.json());
const genAI = new GoogleGenerativeAI(process.env.GEMINI_API_KEY);
io.on('connection', (socket) => {
console.log('User connected');
socket.on('typing', () => {
socket.broadcast.emit('userTyping');
});
socket.on('stopTyping', () => {
socket.broadcast.emit('userStoppedTyping');
});
socket.on('disconnect', () => {
console.log('User disconnected');
});
});
app.post('/chat', async (req, res) => {
try {
const model = genAI.getGenerativeModel({ model: "gemini-pro" });
const prompt = req.body.message;
const result = await model.generateContent(prompt);
const response = await result.response;
let text = response.text();
text = text.replace(/```(\w*)\n([\s\S]*?)```/g, (match, language, code) => {
code = code
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''');
return `<pre><code class="language-${language || 'plaintext'}">${code.trim()}</code></pre>`;
});
text = text
.replace(/\*\*([^*]+)\*\*/g, '<strong>$1</strong>')
.replace(/\*([^*]+)\*/g, '<em>$1</em>')
.replace(/`([^`]+)`/g, '<code>$1</code>')
.replace(/\n/g, '<br>');
res.json({
reply: text,
isHtml: true
});
} catch (error) {
console.error('Error:', error);
res.status(500).json({ error: 'Something went wrong' });
}
});
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
server.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});