-
Notifications
You must be signed in to change notification settings - Fork 0
/
ollama.js
64 lines (53 loc) · 1.58 KB
/
ollama.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
const express = require('express');
const cors = require('cors');
const app = express();
const bodyParser = require('body-parser');
const axios = require('axios');
const myprompt = "Resume this text in english";
// Utiliser cors pour accepter les requêtes cross-origin
app.use(cors());
// Utiliser bodyParser pour parser les requêtes JSON
app.use(bodyParser.json({ limit: '50mb' })); // Vous pouvez ajuster cette valeur en fonction de vos besoins
app.post('/summarize', async (req, res) => {
const content = req.body.content;
try {
const summary = await generateSummary(content);
res.json({ summary });
} catch (error) {
console.error('Error generating summary:', error);
res.status(500).json({ error: 'Failed to generate summary' });
}
});
async function generateSummary(text) {
const url = 'http://localhost:11434/api/generate';
const model = 'llama3:8B';
const prompt = myprompt + text;
const responseStream = await axios.post(url, {
model,
prompt
}, {
responseType: 'stream'
});
return new Promise((resolve, reject) => {
let summary = '';
responseStream.data.on('data', (chunk) => {
try {
const data = JSON.parse(chunk.toString());
if (data.response) {
summary += data.response;
}
} catch (e) {
console.error('Error parsing chunk', e);
}
});
responseStream.data.on('end', () => {
resolve(summary);
});
responseStream.data.on('error', (err) => {
reject(err);
});
});
}
app.listen(8080, () => {
console.log('Server running on port 8080');
});