UNPKG

n8n-nodes-tushare

Version:
67 lines 2.91 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SelfEmbeddings = void 0; const embeddings_1 = require("@langchain/core/embeddings"); class SelfEmbeddings extends embeddings_1.Embeddings { constructor(fields) { var _a, _b, _c, _d, _e; super({ maxConcurrency: 1, ...fields }); this.apiKey = (_a = fields.apiKey) !== null && _a !== void 0 ? _a : ''; this.baseURL = (_b = fields.baseURL) !== null && _b !== void 0 ? _b : 'http://llm.local.cn/ai'; this.model = (_c = fields.model) !== null && _c !== void 0 ? _c : 'm3e-base'; this.timeout = fields.timeout; this.stripNewLines = (_d = fields.stripNewLines) !== null && _d !== void 0 ? _d : true; this.dimensions = fields.dimensions; this.batchSize = (_e = fields.batchSize) !== null && _e !== void 0 ? _e : 1000; } async embedQuery(text) { const embeddings = await this.embedDocuments([text]); return embeddings[0]; } async embedDocuments(texts) { const results = []; for (let i = 0; i < texts.length; i += this.batchSize) { const batch = texts.slice(i, i + this.batchSize); const batchResults = await this.embedBatch(batch); results.push(...batchResults); } return results; } async embedBatch(texts) { const processedTexts = texts.map(text => this.stripNewLines ? text.replace(/\n/g, ' ') : text); try { const response = await fetch(`${this.baseURL}/embeddings`, { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${this.apiKey}`, }, body: JSON.stringify({ input: processedTexts, texts: processedTexts, model: this.model, ...(this.dimensions && { dimensions: this.dimensions }), }), ...(this.timeout && { signal: AbortSignal.timeout(this.timeout * 1000) }), }); if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } const data = await response.json(); if (data.data && Array.isArray(data.data)) { return data.data.map((item) => item.embedding); } else if (data.embeddings && Array.isArray(data.embeddings)) { return data.embeddings; } else { throw new Error('Invalid response format from Self API'); } } catch (error) { throw new Error(`Failed to embed documents: ${error instanceof Error ? error.message : String(error)}`); } } } exports.SelfEmbeddings = SelfEmbeddings; //# sourceMappingURL=SelfEmbeddings.js.map