Upload server.js
Browse files
server.js
CHANGED
|
@@ -368,6 +368,13 @@ app.post('/v1/chat/completions', gssAuth, async (req, res) => {
|
|
| 368 |
? (process.env.OLLAMA_BASE_URL || 'https://ollama.com/api')
|
| 369 |
: (process.env.GROQ_BASE_URL || 'https://api.groq.com/openai/v1');
|
| 370 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 371 |
const poolKeys = (subscriberPools[subscriberId]?.[provider]) || (subscriberPools['env']?.groq) || [];
|
| 372 |
const maxRetries = Math.min(poolKeys.length, 10);
|
| 373 |
|
|
@@ -387,7 +394,7 @@ app.post('/v1/chat/completions', gssAuth, async (req, res) => {
|
|
| 387 |
try {
|
| 388 |
const ollamaUrl = process.env.OLLAMA_BASE_URL || 'https://ollama.com/api';
|
| 389 |
const ollamaBody = {
|
| 390 |
-
model: '
|
| 391 |
messages: req.body.messages || [],
|
| 392 |
stream: false,
|
| 393 |
};
|
|
@@ -411,12 +418,21 @@ app.post('/v1/chat/completions', gssAuth, async (req, res) => {
|
|
| 411 |
}
|
| 412 |
|
| 413 |
try {
|
| 414 |
-
const upstream = await fetch(
|
| 415 |
method: 'POST',
|
| 416 |
headers: { 'Authorization': `Bearer ${entry.key}`, 'Content-Type': 'application/json' },
|
| 417 |
body: JSON.stringify(req.body),
|
| 418 |
});
|
| 419 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 420 |
// 401/403 = invalid/expired key — cooldown and try next
|
| 421 |
if (upstream.status === 401 || upstream.status === 403) {
|
| 422 |
cooldownKey(entry);
|
|
@@ -429,9 +445,9 @@ app.post('/v1/chat/completions', gssAuth, async (req, res) => {
|
|
| 429 |
ollamaFallbackAttempted = true;
|
| 430 |
console.warn(`[GSS] All Groq keys invalid for sub ${subscriberId}, switching to Ollama cloud`);
|
| 431 |
const ollamaUrl = process.env.OLLAMA_BASE_URL || 'https://ollama.com/api';
|
| 432 |
-
//
|
| 433 |
const ollamaBody = {
|
| 434 |
-
model: '
|
| 435 |
messages: req.body.messages || [],
|
| 436 |
stream: false,
|
| 437 |
};
|
|
@@ -442,7 +458,6 @@ app.post('/v1/chat/completions', gssAuth, async (req, res) => {
|
|
| 442 |
});
|
| 443 |
if (ollamaUpstream.ok) {
|
| 444 |
const ollamaData = await ollamaUpstream.json();
|
| 445 |
-
// Convert Ollama response to OpenAI format
|
| 446 |
const content = ollamaData.message?.content || ollamaData.response || '';
|
| 447 |
return res.status(200).json({
|
| 448 |
choices: [{ message: { role: 'assistant', content }, finish_reason: 'stop' }],
|
|
@@ -461,7 +476,7 @@ app.post('/v1/chat/completions', gssAuth, async (req, res) => {
|
|
| 461 |
continue;
|
| 462 |
}
|
| 463 |
|
| 464 |
-
const data = await
|
| 465 |
return res.status(upstream.status).json(data);
|
| 466 |
|
| 467 |
} catch (err) {
|
|
|
|
| 368 |
? (process.env.OLLAMA_BASE_URL || 'https://ollama.com/api')
|
| 369 |
: (process.env.GROQ_BASE_URL || 'https://api.groq.com/openai/v1');
|
| 370 |
|
| 371 |
+
// For Ollama cloud, use native /chat endpoint with different model format
|
| 372 |
+
const isOllamaCloud = useOllama && baseUrl.includes('ollama.com');
|
| 373 |
+
const chatEndpoint = isOllamaCloud ? `${baseUrl}/chat` : `${baseUrl}/chat/completions`;
|
| 374 |
+
if (isOllamaCloud && !req.body.model?.includes(':')) {
|
| 375 |
+
req.body.model = 'gpt-oss:120b';
|
| 376 |
+
}
|
| 377 |
+
|
| 378 |
const poolKeys = (subscriberPools[subscriberId]?.[provider]) || (subscriberPools['env']?.groq) || [];
|
| 379 |
const maxRetries = Math.min(poolKeys.length, 10);
|
| 380 |
|
|
|
|
| 394 |
try {
|
| 395 |
const ollamaUrl = process.env.OLLAMA_BASE_URL || 'https://ollama.com/api';
|
| 396 |
const ollamaBody = {
|
| 397 |
+
model: 'gpt-oss:120b',
|
| 398 |
messages: req.body.messages || [],
|
| 399 |
stream: false,
|
| 400 |
};
|
|
|
|
| 418 |
}
|
| 419 |
|
| 420 |
try {
|
| 421 |
+
const upstream = await fetch(chatEndpoint, {
|
| 422 |
method: 'POST',
|
| 423 |
headers: { 'Authorization': `Bearer ${entry.key}`, 'Content-Type': 'application/json' },
|
| 424 |
body: JSON.stringify(req.body),
|
| 425 |
});
|
| 426 |
|
| 427 |
+
// For Ollama cloud responses, convert to OpenAI format
|
| 428 |
+
const processResponse = async (r) => {
|
| 429 |
+
const data = await r.json();
|
| 430 |
+
if (isOllamaCloud && data.message) {
|
| 431 |
+
return { choices: [{ message: { role: 'assistant', content: data.message.content || '' }, finish_reason: 'stop' }], model: req.body.model };
|
| 432 |
+
}
|
| 433 |
+
return data;
|
| 434 |
+
};
|
| 435 |
+
|
| 436 |
// 401/403 = invalid/expired key — cooldown and try next
|
| 437 |
if (upstream.status === 401 || upstream.status === 403) {
|
| 438 |
cooldownKey(entry);
|
|
|
|
| 445 |
ollamaFallbackAttempted = true;
|
| 446 |
console.warn(`[GSS] All Groq keys invalid for sub ${subscriberId}, switching to Ollama cloud`);
|
| 447 |
const ollamaUrl = process.env.OLLAMA_BASE_URL || 'https://ollama.com/api';
|
| 448 |
+
// Ollama cloud native format
|
| 449 |
const ollamaBody = {
|
| 450 |
+
model: 'gpt-oss:120b',
|
| 451 |
messages: req.body.messages || [],
|
| 452 |
stream: false,
|
| 453 |
};
|
|
|
|
| 458 |
});
|
| 459 |
if (ollamaUpstream.ok) {
|
| 460 |
const ollamaData = await ollamaUpstream.json();
|
|
|
|
| 461 |
const content = ollamaData.message?.content || ollamaData.response || '';
|
| 462 |
return res.status(200).json({
|
| 463 |
choices: [{ message: { role: 'assistant', content }, finish_reason: 'stop' }],
|
|
|
|
| 476 |
continue;
|
| 477 |
}
|
| 478 |
|
| 479 |
+
const data = await processResponse(upstream);
|
| 480 |
return res.status(upstream.status).json(data);
|
| 481 |
|
| 482 |
} catch (err) {
|