Update server.js
Browse files
server.js
CHANGED
|
@@ -1,11 +1,13 @@
|
|
| 1 |
const express = require('express');
|
| 2 |
-
const
|
| 3 |
const fs = require('fs');
|
| 4 |
const path = require('path');
|
| 5 |
|
| 6 |
const app = express();
|
|
|
|
|
|
|
| 7 |
const apiKey = 'AIzaSyCRpX4SnNQl__BNLcVWqgzKdbIlt06vod0';
|
| 8 |
-
const
|
| 9 |
const tmpDir = path.join(require('os').tmpdir(), 'gemini_sessions');
|
| 10 |
if (!fs.existsSync(tmpDir)) fs.mkdirSync(tmpDir);
|
| 11 |
|
|
@@ -15,17 +17,20 @@ function getSessionFile(sessionId) {
|
|
| 15 |
|
| 16 |
async function gemini(sessionId, prompt) {
|
| 17 |
const sessionFile = getSessionFile(sessionId);
|
| 18 |
-
let
|
| 19 |
if (fs.existsSync(sessionFile)) {
|
| 20 |
-
|
| 21 |
-
} else {
|
| 22 |
-
chatSession = await genAI.getGenerativeModel({ model: 'gemini-2.0-flash' }).startChat();
|
| 23 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
const
|
| 26 |
-
|
| 27 |
-
fs.writeFileSync(sessionFile, JSON.stringify(
|
| 28 |
-
return
|
| 29 |
}
|
| 30 |
|
| 31 |
app.get('/gemini', async (req, res) => {
|
|
@@ -33,9 +38,9 @@ app.get('/gemini', async (req, res) => {
|
|
| 33 |
const { question, sessionId } = req.query;
|
| 34 |
if (!question || !sessionId) return res.status(400).json({ error: 'Missing parameters' });
|
| 35 |
const reply = await gemini(sessionId, question);
|
| 36 |
-
res.json({ reply }
|
| 37 |
} catch (error) {
|
| 38 |
-
res.status(500).json({ error: error.message }
|
| 39 |
}
|
| 40 |
});
|
| 41 |
|
|
|
|
| 1 |
const express = require('express');
|
| 2 |
+
const axios = require('axios');
|
| 3 |
const fs = require('fs');
|
| 4 |
const path = require('path');
|
| 5 |
|
| 6 |
const app = express();
|
| 7 |
+
app.set('json spaces', 2);
|
| 8 |
+
|
| 9 |
const apiKey = 'AIzaSyCRpX4SnNQl__BNLcVWqgzKdbIlt06vod0';
|
| 10 |
+
const apiUrl = 'https://generativelanguage.googleapis.com/v1/models/gemini-2.0-flash:generateContent';
|
| 11 |
const tmpDir = path.join(require('os').tmpdir(), 'gemini_sessions');
|
| 12 |
if (!fs.existsSync(tmpDir)) fs.mkdirSync(tmpDir);
|
| 13 |
|
|
|
|
| 17 |
|
| 18 |
async function gemini(sessionId, prompt) {
|
| 19 |
const sessionFile = getSessionFile(sessionId);
|
| 20 |
+
let history = [];
|
| 21 |
if (fs.existsSync(sessionFile)) {
|
| 22 |
+
history = JSON.parse(fs.readFileSync(sessionFile, 'utf8'));
|
|
|
|
|
|
|
| 23 |
}
|
| 24 |
+
history.push({ role: 'user', content: prompt });
|
| 25 |
+
|
| 26 |
+
const response = await axios.post(`${apiUrl}?key=${apiKey}`, {
|
| 27 |
+
prompt: { parts: [{ text: prompt }] }
|
| 28 |
+
});
|
| 29 |
|
| 30 |
+
const reply = response.data.candidates?.[0]?.content?.parts?.[0]?.text || 'No response';
|
| 31 |
+
history.push({ role: 'assistant', content: reply });
|
| 32 |
+
fs.writeFileSync(sessionFile, JSON.stringify(history, null, 2));
|
| 33 |
+
return reply;
|
| 34 |
}
|
| 35 |
|
| 36 |
app.get('/gemini', async (req, res) => {
|
|
|
|
| 38 |
const { question, sessionId } = req.query;
|
| 39 |
if (!question || !sessionId) return res.status(400).json({ error: 'Missing parameters' });
|
| 40 |
const reply = await gemini(sessionId, question);
|
| 41 |
+
res.json({ reply });
|
| 42 |
} catch (error) {
|
| 43 |
+
res.status(500).json({ error: error.message });
|
| 44 |
}
|
| 45 |
});
|
| 46 |
|