-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
47 lines (36 loc) · 1.41 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
const express = require('express');
const { GoogleGenerativeAI } = require('@google/generative-ai');
const dotenv = require('dotenv');
dotenv.config();
const app = express();
const port = 80;
const genAI = new GoogleGenerativeAI(process.env.API_KEY);
const model = genAI.getGenerativeModel({ model: 'gemini-1.5-pro' });
app.use(express.json());
app.use(express.static('public'));
app.post('/solve', async (req, res) => {
const { prompt, type } = req.body;
try {
let finalPrompt = '';
if (type === 'school-safe') {
finalPrompt = `Provide steps on how to solve the following algebraic expression without giving the final answer: ${prompt}`;
} else {
finalPrompt = `Solve the following algebraic expression and show all work: ${prompt}`;
}
const result = await model.generateContent(finalPrompt);
const response = await result.response;
let text = await response.text();
// Replace **text** with <b>text</b>
text = text.replace(/\*\*(.*?)\*\*/g, '<b>$1</b>');
res.json({ solution: text });
} catch (error) {
res.status(500).json({ error: error.message });
}
});
// Serve schoolsafe.html for the school-safe version
app.get('/schoolsafe', (req, res) => {
res.sendFile(__dirname + '/public/schoolsafe.html');
});
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});