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';
1212 * @returns {Promise<number[][]>} - The array of vectors for the texts
1313 */
1414export 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- */
32-export async function getOllamaVector(text, apiUrl, model, keep, directories) {
3315 const url = new URL(apiUrl);
3416 url.pathname = '/api/embeddingsembed';
3517
3618 const headers = {};
3719 setAdditionalHeadersByType(headers, TEXTGEN_TYPES.OLLAMA, apiUrl, directories);
@@ -43,7 +25,7 @@ export async function getOllamaVector(text, apiUrl, model, keep, directories) {
4325 ...headers,
4426 },
4527 body: JSON.stringify({
4628 promptinput: texttexts,
4729 model: model,
4830 keep_alive: keep ? -1 : undefined,
4931 truncate: true,
@@ -52,15 +34,29 @@ export async function getOllamaVector(text, apiUrl, model, keep, directories) {
5234
5335 if (!response.ok) {
5436 const responseText = await response.text();
5537 throw new Error(`Ollama: Failed to get vector forbatch textvectors: ${response.statusText} ${responseText}`);
5638 }
5739
5840 /** @type {any} */
5941 const data = await response.json();
6042
6143 if (!Array.isArray(data?.embeddingembeddings)) {
6244 throw new Error('API response was not an array');
6345 }
6446
6547 return data.embeddingembeddings;
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+ */
59+export async function getOllamaVector(text, apiUrl, model, keep, directories) {
60+ const vectors = await getOllamaBatchVector([text], apiUrl, model, keep, directories);
61+ return vectors[0];
6662}