51e6817393
New controller/retry.js wraps async calls with infinite retries, capped at 30s with jitter. Applied to: TPB search, TMDB, Emby, Ollama chat, Transmission RPC, and the status/status interval calls inherit it via a Proxy around the Transmission client. Also handles ollama package returning pre-parsed JSON objects. Signed-off-by: William Mantly <wmantly@gmail.com>
51 lines
1.6 KiB
JavaScript
51 lines
1.6 KiB
JavaScript
'use strict';
|
|
|
|
const { Ollama } = require('ollama');
|
|
const conf = require('>/conf');
|
|
|
|
// Shared Ollama client. The npm package adds its own Accept / Content-Type / User-Agent
|
|
// headers and handles the Ollama chat protocol, which avoids the CDN-level 403s Node's
|
|
// bare fetch gets against ollama.com.
|
|
const client = new Ollama({
|
|
host: conf.ollama.url,
|
|
headers: {
|
|
'Authorization': `Bearer ${conf.ollama.apiKey}`,
|
|
},
|
|
});
|
|
|
|
// Pull the first JSON object out of a model response( tolerates ```json fences / prose).
|
|
function extractJSON(content){
|
|
let text = String(content).replace(/```json/gi, '').replace(/```/g, '').trim();
|
|
let start = text.indexOf('{');
|
|
let end = text.lastIndexOf('}');
|
|
if(start === -1 || end === -1){
|
|
console.error('extractJSON: no JSON object found in:', content);
|
|
throw new Error('No JSON in model response');
|
|
}
|
|
try{
|
|
return JSON.parse(text.slice(start, end + 1));
|
|
}catch(error){
|
|
console.error('extractJSON: parse failed. extracted:', text.slice(start, end + 1));
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
// One-shot JSON chat against the configured Ollama model.
|
|
async function chatJSON(system, user){
|
|
let res = await client.chat({
|
|
model: conf.ollama.model,
|
|
stream: false,
|
|
format: 'json',
|
|
messages: [
|
|
{ role: 'system', content: system },
|
|
{ role: 'user', content: user },
|
|
],
|
|
});
|
|
// The ollama package may return message.content as a JSON string when format:'json' is used.
|
|
let content = res.message && res.message.content;
|
|
if(typeof content === 'string') return extractJSON(content);
|
|
return content;
|
|
}
|
|
|
|
module.exports = { client, chatJSON, extractJSON };
|