25 lines
875 B
JavaScript
25 lines
875 B
JavaScript
export class OllamaError extends Error {
|
|
constructor(status, body) { super(`ollama ${status}: ${body}`); this.status = status; }
|
|
}
|
|
|
|
export async function embedText(text, { model = 'nomic-embed-text', timeoutMs = 60_000 } = {}) {
|
|
const url = (process.env.OLLAMA_URL || 'http://192.168.1.185:11434') + '/api/embeddings';
|
|
const res = await fetch(url, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ model, prompt: text }),
|
|
signal: AbortSignal.timeout(timeoutMs)
|
|
});
|
|
if (!res.ok) throw new OllamaError(res.status, await res.text());
|
|
const j = await res.json();
|
|
return j.embedding;
|
|
}
|
|
|
|
export function padTo(vector, dim) {
|
|
if (vector.length === dim) return vector;
|
|
if (vector.length > dim) return vector.slice(0, dim);
|
|
const out = vector.slice();
|
|
while (out.length < dim) out.push(0);
|
|
return out;
|
|
}
|