switch to ollama library

This commit is contained in:
legop3
2026-02-24 19:37:38 -05:00
parent 1d97756aef
commit 46de9b71f3
2 changed files with 20 additions and 32 deletions
+1
View File
@@ -15,6 +15,7 @@
"js-yaml": "^4.1.1", "js-yaml": "^4.1.1",
"morgan": "^1.10.0", "morgan": "^1.10.0",
"obscenity": "^0.4.6", "obscenity": "^0.4.6",
"ollama": "^0.6.3",
"sharp": "^0.33.5", "sharp": "^0.33.5",
"socket.io": "^4.7.5", "socket.io": "^4.7.5",
"uuid": "^9.0.1", "uuid": "^9.0.1",
+19 -32
View File
@@ -1,5 +1,6 @@
const fsp = require('fs/promises'); const fsp = require('fs/promises');
const path = require('path'); const path = require('path');
const { Ollama } = require('ollama');
const io = require('../globals/io'); const io = require('../globals/io');
const logger = require('../globals/logger').child('llmCommentary'); const logger = require('../globals/logger').child('llmCommentary');
const { loadConfig } = require('../helpers/configLoader'); const { loadConfig } = require('../helpers/configLoader');
@@ -15,7 +16,6 @@ const JITTER_MS = 30000;
const MAX_ROVERS = 8; const MAX_ROVERS = 8;
const MAX_CHAT_MESSAGES = 12; const MAX_CHAT_MESSAGES = 12;
const MAX_BOT_MESSAGES = 6; const MAX_BOT_MESSAGES = 6;
const REQUEST_TIMEOUT_MS = 12000;
const MAX_OUTPUT_CHARS = 140; const MAX_OUTPUT_CHARS = 140;
const SKIP_TOKEN = 'SKIP'; const SKIP_TOKEN = 'SKIP';
@@ -27,6 +27,7 @@ const ollamaUrl = String(
).trim(); ).trim();
const model = String(commentaryConfig.model || '').trim(); const model = String(commentaryConfig.model || '').trim();
const timezone = String(config.timezone || 'UTC'); const timezone = String(config.timezone || 'UTC');
const ollamaClient = ollamaUrl ? new Ollama({ host: ollamaUrl }) : null;
let timer = null; let timer = null;
let inFlight = false; let inFlight = false;
@@ -170,38 +171,24 @@ async function readSystemPrompt() {
} }
async function generateCommentary(systemPrompt, snapshot) { async function generateCommentary(systemPrompt, snapshot) {
const controller = new AbortController(); if (!ollamaClient) {
const timeout = setTimeout(() => controller.abort(), REQUEST_TIMEOUT_MS); throw new Error('Ollama client unavailable');
const url = `${ollamaUrl.replace(/\/+$/, '')}/api/chat`;
try {
const response = await fetch(url, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
signal: controller.signal,
body: JSON.stringify({
model,
stream: false,
keep_alive: -1,
options: {
temperature: 0.7,
top_p: 0.9,
num_predict: 80,
},
messages: [
{ role: 'system', content: systemPrompt },
{ role: 'user', content: JSON.stringify(snapshot) },
],
}),
});
if (!response.ok) {
const body = await response.text();
throw new Error(`Ollama error ${response.status}: ${body.slice(0, 200)}`);
}
const payload = await response.json();
return normalizeCommentary(payload?.message?.content);
} finally {
clearTimeout(timeout);
} }
const payload = await ollamaClient.chat({
model,
stream: false,
keep_alive: -1,
options: {
temperature: 0.7,
top_p: 0.9,
num_predict: 80,
},
messages: [
{ role: 'system', content: systemPrompt },
{ role: 'user', content: JSON.stringify(snapshot) },
],
});
return normalizeCommentary(payload?.message?.content);
} }
function scheduleNextTick() { function scheduleNextTick() {