import { CloseVectorEmbeddings, CloseVectorEmbeddingsParams, } from 'closevector-common'; import { encryptToken, chunkArray, END_POINT } from './lib'; import axios, { AxiosRequestConfig } from 'axios'; export { CloseVectorEmbeddings, CloseVectorEmbeddingsParams }; export class CloseVectorEmbeddingsNode implements CloseVectorEmbeddings { batchSize = 512; stripNewLines = true; timeout?: number; config: { key: string; secret: string; }; constructor( fields: Partial & { key: string; secret: string; } ) { 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: string[]): Promise { const subPrompts = chunkArray( this.stripNewLines ? texts.map(t => t.replace(/\n/g, ' ')) : texts, this.batchSize ); const embeddings: number[][] = []; 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: string): Promise { const { data } = await this.embeddingWithRetry( this.stripNewLines ? [text.replace(/\n/g, ' ')] : [text] ); return data[0].embedding; } private async embeddingWithRetry(textList: string[]): Promise { let accessKey = this.config.key; let secret = this.config.secret; const axiosInstance = axios.create({ baseURL: END_POINT, headers: { Authorization: `Bearer ${accessKey}:${await encryptToken({ accessKey }, secret)}`, }, }); const createEmbeddings = async (textList: string[]) => { const options: AxiosRequestConfig = { method: 'POST', url: '/embeddings', data: JSON.stringify(textList), }; const response = await axiosInstance.request(options); return response.data; }; return createEmbeddings(textList); } }