closevector-web
Version:
CloseVector is fundamentally a vector database. We have made dedicated libraries available for both browsers and node.js, aiming for easy integration no matter your platform. One feature we've been working on is its potential for scalability. Instead of b
65 lines (64 loc) • 2.46 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.CloseVectorEmbeddingsWeb = void 0;
const lib_1 = require("./lib");
class CloseVectorEmbeddingsWeb {
batchSize = 512;
stripNewLines = true;
timeout;
config;
constructor(fields) {
this.batchSize = fields.batchSize || this.batchSize;
this.stripNewLines = fields.stripNewLines || this.stripNewLines;
this.timeout = fields.timeout;
this.config = {
key: fields.key,
secret: fields.secret,
};
}
async embedDocuments(texts) {
const subPrompts = (0, lib_1.chunkArray)(this.stripNewLines ? texts.map(t => t.replace(/\n/g, ' ')) : texts, this.batchSize);
const embeddings = [];
for (let i = 0; i < subPrompts.length; i += 1) {
const input = subPrompts[i];
const { data } = await this.embeddingWithRetry(input);
for (let j = 0; j < input.length; j += 1) {
embeddings.push(data[j].embedding);
}
}
return embeddings;
}
async embedQuery(text) {
const { data } = await this.embeddingWithRetry(this.stripNewLines ? [text.replace(/\n/g, ' ')] : [text]);
return data[0].embedding;
}
async embeddingWithRetry(textList) {
let accessKey = this.config.key;
let secret = this.config.secret;
const fetchUsingToken = async function fetchUsingToken(url, options) {
let res = await fetch(url, {
...options,
headers: {
...options.headers,
Authorization: `Bearer ${accessKey}:${await (0, lib_1.encryptToken)({ accessKey }, secret)}`,
},
});
if (res.status !== 200) {
let json = await res.json();
throw new Error(await json.message);
}
return res;
};
async function createEmbeddings(textList) {
// let resp = await fetchUsingToken(`https://vector-kv.mega-ug.uk/vectorEmbedding`, {
let resp = await fetchUsingToken(`${lib_1.END_POINT}/embeddings`, {
method: 'POST',
body: JSON.stringify(textList),
});
let data = await resp.json();
return data;
}
return createEmbeddings(textList);
}
}
exports.CloseVectorEmbeddingsWeb = CloseVectorEmbeddingsWeb;
;