SillyTavern/src/openai-vectors.js

93 lines
2.6 KiB
JavaScript
Raw Normal View History

2023-09-08 13:57:27 +03:00
const fetch = require('node-fetch').default;
2023-12-03 08:52:43 -05:00
const { SECRET_KEYS, readSecret } = require('./endpoints/secrets');
2023-09-08 13:57:27 +03:00
2023-12-17 02:56:47 +02:00
const SOURCES = {
'togetherai': {
secretKey: SECRET_KEYS.TOGETHERAI,
url: 'api.together.xyz',
model: 'togethercomputer/m2-bert-80M-32k-retrieval',
},
2023-12-17 02:56:47 +02:00
'mistral': {
2024-01-24 13:56:13 +02:00
secretKey: SECRET_KEYS.MISTRALAI,
2023-12-17 02:56:47 +02:00
url: 'api.mistral.ai',
model: 'mistral-embed',
},
'openai': {
secretKey: SECRET_KEYS.OPENAI,
url: 'api.openai.com',
model: 'text-embedding-ada-002',
},
};
2023-09-08 13:57:27 +03:00
/**
2024-01-24 13:56:13 +02:00
* Gets the vector for the given text batch from an OpenAI compatible endpoint.
* @param {string[]} texts - The array of texts to get the vector for
2023-12-17 02:56:47 +02:00
* @param {string} source - The source of the vector
2024-03-02 23:18:34 +02:00
* @param {string} model - The model to use for the embedding
2024-01-24 13:56:13 +02:00
* @returns {Promise<number[][]>} - The array of vectors for the texts
2023-09-08 13:57:27 +03:00
*/
async function getOpenAIBatchVector(texts, source, model = '') {
2023-12-17 02:56:47 +02:00
const config = SOURCES[source];
if (!config) {
console.log('Unknown source', source);
throw new Error('Unknown source');
}
const key = readSecret(config.secretKey);
2023-09-08 13:57:27 +03:00
if (!key) {
2023-12-17 02:56:47 +02:00
console.log('No API key found');
throw new Error('No API key found');
2023-09-08 13:57:27 +03:00
}
2023-12-17 02:56:47 +02:00
const url = config.url;
const response = await fetch(`https://${url}/v1/embeddings`, {
2023-09-08 13:57:27 +03:00
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${key}`,
},
body: JSON.stringify({
2024-01-24 13:56:13 +02:00
input: texts,
2024-03-02 23:16:18 +02:00
model: model || config.model,
2023-12-02 22:06:57 +02:00
}),
2023-09-08 13:57:27 +03:00
});
if (!response.ok) {
2023-09-09 22:15:47 +03:00
const text = await response.text();
2023-12-17 02:56:47 +02:00
console.log('API request failed', response.statusText, text);
throw new Error('API request failed');
2023-09-08 13:57:27 +03:00
}
const data = await response.json();
2024-01-24 13:56:13 +02:00
if (!Array.isArray(data?.data)) {
2023-12-17 02:56:47 +02:00
console.log('API response was not an array');
throw new Error('API response was not an array');
2023-09-08 13:57:27 +03:00
}
2024-01-24 13:56:13 +02:00
// Sort data by x.index to ensure the order is correct
data.data.sort((a, b) => a.index - b.index);
const vectors = data.data.map(x => x.embedding);
return vectors;
}
/**
* Gets the vector for the given text from an OpenAI compatible endpoint.
* @param {string} text - The text to get the vector for
* @param {string} source - The source of the vector
* @param model
2024-01-24 13:56:13 +02:00
* @returns {Promise<number[]>} - The vector for the text
*/
async function getOpenAIVector(text, source, model = '') {
const vectors = await getOpenAIBatchVector([text], source, model);
2024-01-24 13:56:13 +02:00
return vectors[0];
2023-09-08 13:57:27 +03:00
}
module.exports = {
getOpenAIVector,
2024-01-24 13:56:13 +02:00
getOpenAIBatchVector,
2023-09-08 13:57:27 +03:00
};