Use Ollama /api/embed endpoint for vector embeddings (#5221) * Use Ollama /api/embed endpoint for vector embeddings The deprecated /api/embeddings endpoint does not properly support the truncate parameter, causing "input length exceeds context length" errors when vectorizing files. Migrate to /api/embed which correctly handles truncation and supports native batch input. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * Wrap single Ollama vector calculation into batch Fixes https://github.com/SillyTavern/SillyTavern/pull/5221/changes#r2850052729 --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com> Co-authored-by: Cohee <18619528+Cohee1207@users.noreply.github.com>

d789efba0708097fb5716b6365ea3fcf830aa6f2

shifusen329 <quanhong329@gmail.com>

Signed
1 files changed, +19 -23Ignore whitespace
src/vectors/ollama-vectors.js+19 -23
@@ -12,26 +12,8 @@ import { TEXTGEN_TYPES } from '../constants.js';
12 * @returns {Promise<number[][]>} - The array of vectors for the texts12 * @returns {Promise<number[][]>} - The array of vectors for the texts
13 */13 */
14export async function getOllamaBatchVector(texts, apiUrl, model, keep, directories) {14export async function getOllamaBatchVector(texts, apiUrl, model, keep, directories) {
15 const result = [];
16 for (const text of texts) {
17 const vector = await getOllamaVector(text, apiUrl, model, keep, directories);
18 result.push(vector);
19 }
20 return result;
21}
22
23/**
24 * Gets the vector for the given text from Ollama
25 * @param {string} text - The text to get the vector for
26 * @param {string} apiUrl - The API URL
27 * @param {string} model - The model to use
28 * @param {boolean} keep - Keep the model loaded in memory
29 * @param {import('../users.js').UserDirectoryList} directories - The directories object for the user
30 * @returns {Promise<number[]>} - The vector for the text
31 */
32export async function getOllamaVector(text, apiUrl, model, keep, directories) {
33 const url = new URL(apiUrl);15 const url = new URL(apiUrl);
34 url.pathname = '/api/embeddings';16 url.pathname = '/api/embed';
3517
36 const headers = {};18 const headers = {};
37 setAdditionalHeadersByType(headers, TEXTGEN_TYPES.OLLAMA, apiUrl, directories);19 setAdditionalHeadersByType(headers, TEXTGEN_TYPES.OLLAMA, apiUrl, directories);
@@ -43,7 +25,7 @@ export async function getOllamaVector(text, apiUrl, model, keep, directories) {
43 ...headers,25 ...headers,
44 },26 },
45 body: JSON.stringify({27 body: JSON.stringify({
46 prompt: text,28 input: texts,
47 model: model,29 model: model,
48 keep_alive: keep ? -1 : undefined,30 keep_alive: keep ? -1 : undefined,
49 truncate: true,31 truncate: true,
@@ -52,15 +34,29 @@ export async function getOllamaVector(text, apiUrl, model, keep, directories) {
5234
53 if (!response.ok) {35 if (!response.ok) {
54 const responseText = await response.text();36 const responseText = await response.text();
55 throw new Error(`Ollama: Failed to get vector for text: ${response.statusText} ${responseText}`);37 throw new Error(`Ollama: Failed to get batch vectors: ${response.statusText} ${responseText}`);
56 }38 }
5739
58 /** @type {any} */40 /** @type {any} */
59 const data = await response.json();41 const data = await response.json();
6042
61 if (!Array.isArray(data?.embedding)) {43 if (!Array.isArray(data?.embeddings)) {
62 throw new Error('API response was not an array');44 throw new Error('API response was not an array');
63 }45 }
6446
65 return data.embedding;47 return data.embeddings;
48}
49
50/**
51 * Gets the vector for the given text from Ollama
52 * @param {string} text - The text to get the vector for
53 * @param {string} apiUrl - The API URL
54 * @param {string} model - The model to use
55 * @param {boolean} keep - Keep the model loaded in memory
56 * @param {import('../users.js').UserDirectoryList} directories - The directories object for the user
57 * @returns {Promise<number[]>} - The vector for the text
58 */
59export async function getOllamaVector(text, apiUrl, model, keep, directories) {
60 const vectors = await getOllamaBatchVector([text], apiUrl, model, keep, directories);
61 return vectors[0];
66}62}