-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathollama-proxy.js
More file actions
58 lines (49 loc) · 1.91 KB
/
ollama-proxy.js
File metadata and controls
58 lines (49 loc) · 1.91 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
import express from 'express';
import { trimV1 } from '../util.js';
export const router = express.Router();
// This endpoint allows external access to Ollama through SillyTavern
router.post('/generate', async (request, response) => {
try {
console.log('⚡ Received generate request:', request.body);
const ollamaUrl = 'http://127.0.0.1:11434';
// Forward the request to Ollama
const ollamaResponse = await fetch(`${ollamaUrl}/api/generate`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(request.body),
});
if (!ollamaResponse.ok) {
const errorText = await ollamaResponse.text();
console.error('❌ Ollama proxy error:', ollamaResponse.status, errorText);
return response.status(ollamaResponse.status).send(errorText);
}
// Return the Ollama response
const data = await ollamaResponse.json();
console.log('✅ Ollama generate response received');
return response.send(data);
} catch (error) {
console.error('❌ Ollama proxy error:', error);
return response.status(500).send({ error: error.message });
}
});
// Get available models
router.get('/models', async (request, response) => {
try {
console.log('⚡ Received models request');
const ollamaUrl = 'http://127.0.0.1:11434';
const ollamaResponse = await fetch(`${ollamaUrl}/api/tags`);
if (!ollamaResponse.ok) {
const errorText = await ollamaResponse.text();
console.error('❌ Ollama models error:', ollamaResponse.status, errorText);
return response.status(ollamaResponse.status).send(errorText);
}
const data = await ollamaResponse.json();
console.log('✅ Ollama models response received');
return response.send(data);
} catch (error) {
console.error('❌ Ollama models error:', error);
return response.status(500).send({ error: error.message });
}
});