This repository was archived by the owner on Jul 12, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
169 lines (148 loc) · 4.85 KB
/
Copy pathserver.js
File metadata and controls
169 lines (148 loc) · 4.85 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import express from 'express';
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';
import fetch from 'node-fetch';
import { exec } from 'child_process';
import { promisify } from 'util';
const execAsync = promisify(exec);
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const app = express();
const PORT = process.env.PORT || 3000;
const OLLAMA_API = 'http://localhost:11434';
// Serve static frontend files
app.use(express.static(join(__dirname, 'public')));
// Parse JSON bodies with increased limit for base64 images
app.use(express.json({ limit: '50mb' }));
// Get list of available models
app.get('/api/models', async (req, res) => {
try {
const response = await fetch(`${OLLAMA_API}/api/tags`);
const data = await response.json();
res.json(data);
} catch (error) {
console.error('Error fetching models:', error);
res.status(500).json({ error: error.message });
}
});
// Pull a new model
app.post('/api/models/pull', async (req, res) => {
try {
const { modelName } = req.body;
// Start the pull process
const pullProcess = exec(`ollama pull ${modelName}`);
// Stream the output back to the client
res.setHeader('Content-Type', 'text/event-stream');
res.setHeader('Cache-Control', 'no-cache');
res.setHeader('Connection', 'keep-alive');
pullProcess.stdout.on('data', (data) => {
res.write(`data: ${JSON.stringify({ type: 'progress', data: data.toString() })}\n\n`);
});
pullProcess.stderr.on('data', (data) => {
res.write(`data: ${JSON.stringify({ type: 'error', data: data.toString() })}\n\n`);
});
pullProcess.on('close', (code) => {
if (code === 0) {
res.write(
`data: ${JSON.stringify({ type: 'complete', data: 'Model pulled successfully' })}\n\n`
);
} else {
res.write(`data: ${JSON.stringify({ type: 'error', data: 'Failed to pull model' })}\n\n`);
}
res.end();
});
} catch (error) {
console.error('Error pulling model:', error);
res.status(500).json({ error: error.message });
}
});
// Get model info
app.get('/api/models/:name', async (req, res) => {
try {
const modelName = req.params.name;
const response = await fetch(`${OLLAMA_API}/api/show`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ name: modelName }),
});
const data = await response.json();
res.json(data);
} catch (error) {
console.error('Error fetching model info:', error);
res.status(500).json({ error: error.message });
}
});
// Delete a model
app.delete('/api/models/:name', async (req, res) => {
try {
const modelName = req.params.name;
const { stdout, stderr } = await execAsync(`ollama rm ${modelName}`);
res.json({ message: 'Model deleted successfully', stdout, stderr });
} catch (error) {
console.error('Error deleting model:', error);
res.status(500).json({ error: error.message });
}
});
// Custom proxy middleware for Ollama chat
app.post('/api/chat', async (req, res) => {
try {
if (!req.body.model) {
return res.status(400).json({ error: 'Model name is required' });
}
const ollamaResponse = await fetch(`${OLLAMA_API}/api/chat`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
model: req.body.model,
messages: req.body.messages,
stream: true,
options: {
temperature: 0.7,
top_k: 40,
top_p: 0.9,
},
}),
});
if (!ollamaResponse.ok) {
const errorText = await ollamaResponse.text();
throw new Error(`Ollama API error: ${ollamaResponse.statusText}. ${errorText}`);
}
// Set headers for streaming response
res.setHeader('Content-Type', 'text/plain');
res.setHeader('Cache-Control', 'no-cache');
res.setHeader('Connection', 'keep-alive');
// Stream the response line by line
const responseStream = ollamaResponse.body;
responseStream.on('data', (chunk) => {
try {
const lines = chunk
.toString()
.split('\n')
.filter((line) => line.trim());
for (const line of lines) {
const parsed = JSON.parse(line);
if (parsed.message?.content) {
res.write(parsed.message.content);
}
}
} catch (e) {
console.error('Error parsing chunk:', e);
}
});
responseStream.on('end', () => {
res.end();
});
responseStream.on('error', (error) => {
console.error('Stream error:', error);
res.status(500).json({ error: 'Stream error occurred' });
});
} catch (error) {
console.error('Error:', error);
res.status(500).json({ error: error.message });
}
});
app.listen(PORT, () => {
console.log(`Server listening: http://localhost:${PORT}`);
});