-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
39 lines (35 loc) · 1.28 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
// node server.js
const express = require('express');
const axios = require('axios');
const app = express();
const port = 3002;
const ollamaModel = 'mistral'; // The model to use
app.use(express.json());
app.use(express.static('public'));
// API route to send a request to Ollama
app.post('/api/query', async (req, res) => {
const prompt = req.body.prompt;
if (!prompt) {
return res.status(400).json({ error: 'Prompt is required' });
}
try {
console.log(`Received prompt: ${prompt}`);
const response = await axios.post('http://localhost:11434/api/generate', {
prompt,
model: ollamaModel
});
console.log('Ollama response:', response.data); // Log the response from Ollama
res.json(response.data); // Send the response back to the client
} catch (error) {
console.error('Error communicating with Ollama:', error.message);
if (error.response) {
console.error('Ollama API response:', error.response.data);
res.status(500).json({ error: error.response.data });
} else {
res.status(500).json({ error: 'Failed to communicate with Ollama' });
}
}
});
app.listen(port, () => {
console.log(`Server listening at http://localhost:${port}`);
});