vedaco commited on
Commit
b53253f
Β·
verified Β·
1 Parent(s): 3cd9b91

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +50 -22
server.js CHANGED
@@ -7,12 +7,17 @@ const app = express();
7
  app.use(cors());
8
  app.use(express.json({ limit: '50mb' }));
9
 
10
- const PORT = process.env.PORT || 5000;
11
 
12
- // Ollama Configuration
13
- const OLLAMA_URL = process.env.OLLAMA_URL || 'http://localhost:11434';
14
  const MODEL = process.env.MODEL || 'qwen3.5:0.8b';
15
 
 
 
 
 
 
16
  // Tera V2 System Prompt
17
  const SYSTEM_PROMPT = `You are Tera V2, an advanced AI document intelligence assistant.
18
 
@@ -32,7 +37,7 @@ console.log('╔═════════════════════
32
  console.log('β•‘ Tera V2 Starting... β•‘');
33
  console.log('β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•');
34
  console.log(`Model: ${MODEL}`);
35
- console.log(`Ollama: ${OLLAMA_URL}\n`);
36
 
37
  // Root route - API info
38
  app.get('/', (req, res) => {
@@ -41,6 +46,7 @@ app.get('/', (req, res) => {
41
  version: '2.0.0',
42
  model: MODEL,
43
  status: 'running',
 
44
  endpoints: {
45
  health: '/health',
46
  chat: '/api/chat',
@@ -52,7 +58,15 @@ app.get('/', (req, res) => {
52
  // Health check
53
  app.get('/health', async (req, res) => {
54
  try {
55
- const request = http.get(`${OLLAMA_URL}/api/tags`, (response) => {
 
 
 
 
 
 
 
 
56
  let data = '';
57
  response.on('data', chunk => data += chunk);
58
  response.on('end', () => {
@@ -63,29 +77,46 @@ app.get('/health', async (req, res) => {
63
  status: 'ok',
64
  service: 'Tera V2',
65
  model: MODEL,
66
- modelLoaded: hasModel
 
67
  });
68
  } catch (e) {
69
- res.status(503).json({ status: 'error', message: 'Ollama not responding' });
 
 
 
 
70
  }
71
  });
72
  });
73
 
74
- request.on('error', () => {
 
75
  res.status(503).json({
76
  status: 'error',
77
  message: 'Ollama not running',
78
- solution: `Run: ollama run ${MODEL}`
 
79
  });
80
  });
81
 
82
  request.setTimeout(5000, () => {
83
  request.destroy();
84
- res.status(503).json({ status: 'error', message: 'Ollama timeout' });
 
 
 
 
85
  });
 
 
86
 
87
  } catch (error) {
88
- res.status(503).json({ status: 'error', message: error.message });
 
 
 
 
89
  }
90
  });
91
 
@@ -116,8 +147,8 @@ app.post('/api/chat', async (req, res) => {
116
  });
117
 
118
  const options = {
119
- hostname: 'localhost',
120
- port: 11434,
121
  path: '/api/generate',
122
  method: 'POST',
123
  headers: {
@@ -153,7 +184,7 @@ app.post('/api/chat', async (req, res) => {
153
 
154
  proxyReq.on('error', (error) => {
155
  console.error('Ollama error:', error.message);
156
- res.write(`data: {"error": "${error.message}"}\n\n`);
157
  res.end();
158
  });
159
 
@@ -193,8 +224,8 @@ app.post('/api/generate-audio-script', async (req, res) => {
193
  });
194
 
195
  const options = {
196
- hostname: 'localhost',
197
- port: 11434,
198
  path: '/api/generate',
199
  method: 'POST',
200
  headers: {
@@ -233,12 +264,9 @@ app.listen(PORT, () => {
233
  console.log('β•‘ Tera V2 is Ready! β•‘');
234
  console.log('β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•');
235
  console.log(`Server: http://localhost:${PORT}`);
 
236
  console.log(`Model: ${MODEL}`);
237
  console.log('');
238
- console.log('Quick Start:');
239
- console.log(`1. Keep Ollama running: ollama run ${MODEL}`);
240
- console.log('2. Open tera-docs.html in browser');
241
- console.log('');
242
- console.log('No API keys - runs locally!');
243
  console.log('╔════════════════════════════════════════╗\n');
244
- });
 
7
  app.use(cors());
8
  app.use(express.json({ limit: '50mb' }));
9
 
10
+ const PORT = process.env.PORT || 7860;
11
 
12
+ // Ollama Configuration - use 127.0.0.1 instead of localhost
13
+ const OLLAMA_HOST = process.env.OLLAMA_URL || 'http://127.0.0.1:11434';
14
  const MODEL = process.env.MODEL || 'qwen3.5:0.8b';
15
 
16
+ // Parse OLLAMA_URL to get hostname and port
17
+ const OLLAMA_URL_OBJ = new URL(OLLAMA_HOST);
18
+ const OLLAMA_HOSTNAME = OLLAMA_URL_OBJ.hostname;
19
+ const OLLAMA_PORT = OLLAMA_URL_OBJ.port || 11434;
20
+
21
  // Tera V2 System Prompt
22
  const SYSTEM_PROMPT = `You are Tera V2, an advanced AI document intelligence assistant.
23
 
 
37
  console.log('β•‘ Tera V2 Starting... β•‘');
38
  console.log('β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•');
39
  console.log(`Model: ${MODEL}`);
40
+ console.log(`Ollama: ${OLLAMA_HOST}:${OLLAMA_PORT}\n`);
41
 
42
  // Root route - API info
43
  app.get('/', (req, res) => {
 
46
  version: '2.0.0',
47
  model: MODEL,
48
  status: 'running',
49
+ ollama: `${OLLAMA_HOST}:${OLLAMA_PORT}`,
50
  endpoints: {
51
  health: '/health',
52
  chat: '/api/chat',
 
58
  // Health check
59
  app.get('/health', async (req, res) => {
60
  try {
61
+ const options = {
62
+ hostname: OLLAMA_HOSTNAME,
63
+ port: OLLAMA_PORT,
64
+ path: '/api/tags',
65
+ method: 'GET',
66
+ timeout: 5000
67
+ };
68
+
69
+ const request = http.request(options, (response) => {
70
  let data = '';
71
  response.on('data', chunk => data += chunk);
72
  response.on('end', () => {
 
77
  status: 'ok',
78
  service: 'Tera V2',
79
  model: MODEL,
80
+ modelLoaded: hasModel,
81
+ ollamaConnected: true
82
  });
83
  } catch (e) {
84
+ res.status(503).json({
85
+ status: 'error',
86
+ message: 'Ollama not responding correctly',
87
+ ollamaConnected: false
88
+ });
89
  }
90
  });
91
  });
92
 
93
+ request.on('error', (err) => {
94
+ console.error('Health check error:', err.message);
95
  res.status(503).json({
96
  status: 'error',
97
  message: 'Ollama not running',
98
+ solution: 'Wait for container to finish starting',
99
+ ollamaConnected: false
100
  });
101
  });
102
 
103
  request.setTimeout(5000, () => {
104
  request.destroy();
105
+ res.status(503).json({
106
+ status: 'error',
107
+ message: 'Ollama timeout',
108
+ ollamaConnected: false
109
+ });
110
  });
111
+
112
+ request.end();
113
 
114
  } catch (error) {
115
+ res.status(503).json({
116
+ status: 'error',
117
+ message: error.message,
118
+ ollamaConnected: false
119
+ });
120
  }
121
  });
122
 
 
147
  });
148
 
149
  const options = {
150
+ hostname: OLLAMA_HOSTNAME,
151
+ port: OLLAMA_PORT,
152
  path: '/api/generate',
153
  method: 'POST',
154
  headers: {
 
184
 
185
  proxyReq.on('error', (error) => {
186
  console.error('Ollama error:', error.message);
187
+ res.write(`data: {"error": "Ollama connection failed: ${error.message}"}\n\n`);
188
  res.end();
189
  });
190
 
 
224
  });
225
 
226
  const options = {
227
+ hostname: OLLAMA_HOSTNAME,
228
+ port: OLLAMA_PORT,
229
  path: '/api/generate',
230
  method: 'POST',
231
  headers: {
 
264
  console.log('β•‘ Tera V2 is Ready! β•‘');
265
  console.log('β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•');
266
  console.log(`Server: http://localhost:${PORT}`);
267
+ console.log(`Ollama: ${OLLAMA_HOST}:${OLLAMA_PORT}`);
268
  console.log(`Model: ${MODEL}`);
269
  console.log('');
270
+ console.log('Waiting for Ollama to be ready...');
 
 
 
 
271
  console.log('╔════════════════════════════════════════╗\n');
272
+ });