support batch mode for Extras backend too

This commit is contained in:
Juha Jeronen
2024-01-24 15:48:14 +02:00
parent a1c7e2918b
commit 0a07161cf8
2 changed files with 25 additions and 10 deletions

View File

@ -30,16 +30,19 @@ async function getVector(source, sourceSettings, text) {
/** /**
* Gets the vector for the given text batch from the given source. * Gets the vector for the given text batch from the given source.
* @param {string} source - The source of the vector * @param {string} source - The source of the vector
* @param {Object} sourceSettings - Settings for the source, if it needs any
* @param {string[]} texts - The array of texts to get the vector for * @param {string[]} texts - The array of texts to get the vector for
* @returns {Promise<number[][]>} - The array of vectors for the texts * @returns {Promise<number[][]>} - The array of vectors for the texts
*/ */
async function getBatchVector(source, texts) { async function getBatchVector(source, sourceSettings, texts) {
switch (source) { switch (source) {
case 'mistral': case 'mistral':
case 'openai': case 'openai':
return require('../openai-vectors').getOpenAIBatchVector(texts, source); return require('../openai-vectors').getOpenAIBatchVector(texts, source);
case 'transformers': case 'transformers':
return require('../embedding').getTransformersBatchVector(texts); return require('../embedding').getTransformersBatchVector(texts);
case 'extras':
return require('../extras-vectors').getExtrasBatchVector(texts, sourceSettings.extrasUrl, sourceSettings.extrasKey);
case 'palm': case 'palm':
return require('../makersuite-vectors').getMakerSuiteBatchVector(texts); return require('../makersuite-vectors').getMakerSuiteBatchVector(texts);
} }
@ -76,7 +79,7 @@ async function insertVectorItems(collectionId, source, sourceSettings, items) {
await store.beginUpdate(); await store.beginUpdate();
const vectors = await getBatchVector(source, items.map(x => x.text)); const vectors = await getBatchVector(source, sourceSettings, items.map(x => x.text));
for (let i = 0; i < items.length; i++) { for (let i = 0; i < items.length; i++) {
const item = items[i]; const item = items[i];

View File

@ -2,10 +2,26 @@ const fetch = require('node-fetch').default;
/** /**
* Gets the vector for the given text from SillyTavern-extras * Gets the vector for the given text from SillyTavern-extras
* @param {string|Array} text - The text or texts to get the vector for * @param {string[]} texts - The array of texts to get the vector for
* @param {string} apiUrl - The Extras API URL * @param {string} apiUrl - The Extras API URL
* @param {string} - The Extras API key, or empty string if API key not enabled * @param {string} - The Extras API key, or empty string if API key not enabled
* @returns {Promise<number[]>} - The vector for a single text, or the array of vectors for multiple texts * @returns {Promise<number[][]>} - The array of vectors for the texts
*/
async function getExtrasBatchVector(texts, apiUrl, apiKey) {
return getExtrasVector(texts, apiUrl, apiKey); // The implementation supports batches transparently.
}
module.exports = {
getExtrasVector,
getExtrasBatchVector,
};
/**
* Gets the vector for the given text from SillyTavern-extras
* @param {string|string[]} text - The text or texts to get the vector for
* @param {string} apiUrl - The Extras API URL
* @param {string} - The Extras API key, or empty string if API key not enabled
* @returns {Promise<number[]>|Promise<number[][]>} - The vector for a single text, or the array of vectors for multiple texts
*/ */
async function getExtrasVector(text, apiUrl, apiKey) { async function getExtrasVector(text, apiUrl, apiKey) {
let url; let url;
@ -33,7 +49,7 @@ async function getExtrasVector(text, apiUrl, apiKey) {
method: 'POST', method: 'POST',
headers: headers, headers: headers,
body: JSON.stringify({ body: JSON.stringify({
text: text, // The backend accepts {string|Array} for one or multiple text items, respectively. text: text, // The backend accepts {string|string[]} for one or multiple text items, respectively.
}), }),
}); });
@ -44,11 +60,7 @@ async function getExtrasVector(text, apiUrl, apiKey) {
} }
const data = await response.json(); const data = await response.json();
const vector = data.embedding; // `embedding`: Array (one text item), or Array of Array (multiple text items). const vector = data.embedding; // `embedding`: number[] (one text item), or number[][] (multiple text items).
return vector; return vector;
} }
module.exports = {
getExtrasVector,
};