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>
Signed| @@ -12,26 +12,8 @@ import { TEXTGEN_TYPES } from '../constants.js'; | ||
| 12 | 12 | * @returns {Promise<number[][]>} - The array of vectors for the texts |
| 13 | 13 | */ |
| 14 | 14 | export 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) { | |
| 33 | 15 | const url = new URL(apiUrl); |
| 34 | 16 | url.pathname = '/api/embeddingsembed'; |
| 35 | 17 | |
| 36 | 18 | const headers = {}; |
| 37 | 19 | setAdditionalHeadersByType(headers, TEXTGEN_TYPES.OLLAMA, apiUrl, directories); |
| @@ -43,7 +25,7 @@ export async function getOllamaVector(text, apiUrl, model, keep, directories) { | ||
| 43 | 25 | ...headers, |
| 44 | 26 | }, |
| 45 | 27 | body: JSON.stringify({ |
| 46 | 28 | promptinput: texttexts, |
| 47 | 29 | model: model, |
| 48 | 30 | keep_alive: keep ? -1 : undefined, |
| 49 | 31 | truncate: true, |
| @@ -52,15 +34,29 @@ export async function getOllamaVector(text, apiUrl, model, keep, directories) { | ||
| 52 | 34 | |
| 53 | 35 | if (!response.ok) { |
| 54 | 36 | const responseText = await response.text(); |
| 55 | 37 | throw new Error(`Ollama: Failed to get vector forbatch textvectors: ${response.statusText} ${responseText}`); |
| 56 | 38 | } |
| 57 | 39 | |
| 58 | 40 | /** @type {any} */ |
| 59 | 41 | const data = await response.json(); |
| 60 | 42 | |
| 61 | 43 | if (!Array.isArray(data?.embeddingembeddings)) { |
| 62 | 44 | throw new Error('API response was not an array'); |
| 63 | 45 | } |
| 64 | 46 | |
| 65 | 47 | 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]; | |
| 66 | 62 | } |