Use official ollama npm client for all LLM calls
Replaces hand-rolled fetch to /api/chat with the ollama@0.6.3 package. The package supplies proper Accept/Content-Type/User-Agent headers and auto-handles the Ollama protocol, fixing the CDN 403s Node's bare fetch gets against ollama.com during filing. Both organize and search LLM ranking now route through controller/ollama.js. Signed-off-by: William Mantly <wmantly@gmail.com>
This commit is contained in:
+22
-28
@@ -1,7 +1,18 @@
|
||||
'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();
|
||||
@@ -19,35 +30,18 @@ function extractJSON(content){
|
||||
}
|
||||
}
|
||||
|
||||
// One-shot JSON chat against the Ollama cloud model.
|
||||
// One-shot JSON chat against the configured Ollama model.
|
||||
async function chatJSON(system, user){
|
||||
let res = await fetch(`${conf.ollama.url}/api/chat`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': `Bearer ${conf.ollama.apiKey}`,
|
||||
'User-Agent': 'tpbproxy/1.0 (organize; +https://github.com/wmantly/tpbproxy)',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
model: conf.ollama.model,
|
||||
stream: false,
|
||||
format: 'json',
|
||||
messages: [
|
||||
{ role: 'system', content: system },
|
||||
{ role: 'user', content: user },
|
||||
],
|
||||
}),
|
||||
let res = await client.chat({
|
||||
model: conf.ollama.model,
|
||||
stream: false,
|
||||
format: 'json',
|
||||
messages: [
|
||||
{ role: 'system', content: system },
|
||||
{ role: 'user', content: user },
|
||||
],
|
||||
});
|
||||
if(!res.ok){
|
||||
let body = await res.text().catch(() => '');
|
||||
console.error(`Ollama chat failed (${res.status}):`, body.slice(0, 500));
|
||||
let error = new Error('OllamaError');
|
||||
error.message = `Ollama chat failed( ${res.status})`;
|
||||
error.status = 502;
|
||||
throw error;
|
||||
}
|
||||
let data = await res.json();
|
||||
return extractJSON(data.message && data.message.content);
|
||||
return extractJSON(res.message && res.message.content);
|
||||
}
|
||||
|
||||
module.exports = { chatJSON, extractJSON };
|
||||
module.exports = { client, chatJSON, extractJSON };
|
||||
|
||||
Reference in New Issue
Block a user