UNPKG

@n8n/n8n-nodes-langchain

Version:
195 lines • 9.27 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PineconeDocumentStore = void 0; exports.parseIndexModel = parseIndexModel; exports.classifyIndexShape = classifyIndexShape; exports.assertPineconeIndexAcceptsVectors = assertPineconeIndexAcceptsVectors; exports.getDocumentIndexScoring = getDocumentIndexScoring; exports.getPineconeVectorStore = getPineconeVectorStore; const node_crypto_1 = require("node:crypto"); const documents_1 = require("@langchain/core/documents"); const vectorstores_1 = require("@langchain/core/vectorstores"); const pinecone_1 = require("@langchain/pinecone"); const pinecone_2 = require("@pinecone-database/pinecone"); const is_record_1 = require("@n8n/utils/is-record"); const n8n_workflow_1 = require("n8n-workflow"); const API_VERSION = '2026-07'; const CONTROL_PLANE = 'https://api.pinecone.io'; const toRequest = ({ context, credentialType }) => async (options) => { const requestOptions = Object.assign({}, options, { headers: { ...options.headers, 'X-Pinecone-API-Version': API_VERSION }, json: true, }); return await context.helpers.httpRequestWithAuthentication.call(context, credentialType, requestOptions); }; function parseDataPlaneHost(host) { try { const url = new URL(/^[a-z]+:\/\//i.test(host) ? host : `https://${host}`); return url.protocol === 'https:' && url.hostname.endsWith('.pinecone.io') ? `https://${url.host}` : undefined; } catch { return undefined; } } function parseIndexModel(raw) { const host = (0, is_record_1.isRecord)(raw) && typeof raw.host === 'string' ? parseDataPlaneHost(raw.host) : undefined; if (!(0, is_record_1.isRecord)(raw) || typeof raw.name !== 'string' || !host) { throw new n8n_workflow_1.OperationalError('Unexpected Pinecone index description'); } const fields = (0, is_record_1.isRecord)(raw.schema) && (0, is_record_1.isRecord)(raw.schema.fields) ? raw.schema.fields : {}; return { name: raw.name, host, ready: (0, is_record_1.isRecord)(raw.status) && raw.status.ready === true, fields: Object.fromEntries(Object.entries(fields).filter((entry) => (0, is_record_1.isRecord)(entry[1]))), }; } const DESCRIBE_TTL_MS = 60_000; const describeCache = new Map(); async function describeIndex(connection, indexName) { const key = (0, node_crypto_1.createHash)('sha256').update(`${connection.apiKey}\0${indexName}`).digest('hex'); const now = Date.now(); const cached = describeCache.get(key); if (cached && cached.expires > now) return await cached.model; for (const [k, entry] of describeCache) if (entry.expires <= now) describeCache.delete(k); const model = toRequest(connection)({ method: 'GET', url: (0, n8n_workflow_1.requestUrl) `${CONTROL_PLANE}/indexes/${indexName}`, }).then(parseIndexModel); describeCache.set(key, { expires: now + DESCRIBE_TTL_MS, model }); try { return await model; } catch (error) { describeCache.delete(key); throw error; } } const tryDescribeIndex = async (connection, indexName) => await describeIndex(connection, indexName).catch(() => undefined); const typeOf = (field) => (typeof field.type === 'string' ? field.type : undefined); const isVectorField = (field) => typeOf(field) === 'dense_vector' || typeOf(field) === 'sparse_vector'; const RESERVED_VECTOR_FIELDS = new Set(['_values', '_sparse_values']); function classifyIndexShape(model) { const entries = Object.entries(model.fields); const vectors = entries.filter(([, f]) => isVectorField(f)); if (vectors.length === 0) { return entries.length === 0 && !model.ready ? 'indeterminate' : 'documents-api'; } const reserved = vectors.filter(([name]) => RESERVED_VECTOR_FIELDS.has(name)).length; if (reserved === vectors.length) return 'vectors-api'; return reserved === 0 ? 'documents-api' : 'indeterminate'; } function isDocumentIndex(model) { if (classifyIndexShape(model) !== 'documents-api') return false; return !Object.values(model.fields).some((f) => typeOf(f) === 'semantic_text'); } const INSERT_NOT_SUPPORTED = 'Inserting into a Pinecone index with a document schema is not supported yet. Use an index without a document schema to insert documents with n8n'; async function assertPineconeIndexAcceptsVectors(connection, indexName) { const model = await tryDescribeIndex(connection, indexName); if (model && isDocumentIndex(model)) throw new n8n_workflow_1.UserError(INSERT_NOT_SUPPORTED); } function getDocumentIndexScoring(model) { if (!isDocumentIndex(model)) return undefined; const fields = Object.entries(model.fields); const dense = fields.find(([, f]) => typeOf(f) === 'dense_vector'); if (dense) return { type: 'dense_vector', field: dense[0] }; const embed = fields.find(([, f]) => typeOf(f) === 'string' && (0, is_record_1.isRecord)(f.embed)); if (embed) return { type: 'embed', field: embed[0] }; throw new n8n_workflow_1.UserError(`Pinecone index "${model.name}" has no dense vector field or integrated embedding field to search`); } class PineconeDocumentStore extends vectorstores_1.VectorStore { constructor(embeddings, request, scoreBy, model, namespace, filter, textField) { super(embeddings, {}); this.request = request; this.scoreBy = scoreBy; this.filter = filter; this.textField = textField; this.vectorFields = new Set(Object.entries(model.fields) .filter(([, f]) => isVectorField(f)) .map(([name]) => name)); this.searchUrl = (0, n8n_workflow_1.requestUrl) `${model.host}/namespaces/${namespace || '__default__'}/documents/search`; } _vectorstoreType() { return 'pinecone-documents'; } async addVectors() { throw new n8n_workflow_1.UserError(INSERT_NOT_SUPPORTED); } async addDocuments() { await this.addVectors(); } async similaritySearch(query, k = 4, filter) { return (await this.similaritySearchWithScore(query, k, filter)).map(([doc]) => doc); } async similaritySearchWithScore(query, k = 4, filter) { if (this.scoreBy.type === 'embed') { return await this.search({ type: 'embed', field: this.scoreBy.field, query }, k, filter); } return await this.similaritySearchVectorWithScore(await this.embeddings.embedQuery(query), k, filter); } async similaritySearchVectorWithScore(values, k, filter) { if (this.scoreBy.type === 'embed') { throw new n8n_workflow_1.UserError(`Pinecone embeds queries for field "${this.scoreBy.field}" itself, so this operation needs the query text`); } return await this.search({ type: 'dense_vector', field: this.scoreBy.field, values }, k, filter); } async search(scoreBy, topK, filter) { const activeFilter = filter && Object.keys(filter).length > 0 ? filter : this.filter; const response = await this.request({ method: 'POST', url: this.searchUrl, body: { score_by: [scoreBy], top_k: topK, include_fields: ['*'], ...(activeFilter && Object.keys(activeFilter).length > 0 ? { filter: activeFilter } : {}), }, }); const matches = (0, is_record_1.isRecord)(response) && Array.isArray(response.matches) ? response.matches : []; return matches .filter(is_record_1.isRecord) .map((match) => [ this.toDocument(match), typeof match._score === 'number' ? match._score : 0, ]); } toDocument({ _id, _score, ...fields }) { const textKey = this.textField ?? (typeof fields.text === 'string' ? 'text' : Object.keys(fields).find((key) => typeof fields[key] === 'string' && !this.vectorFields.has(key))); const metadata = {}; for (const [key, value] of Object.entries(fields)) { if (key !== textKey && !this.vectorFields.has(key)) metadata[key] = value; } const content = textKey === undefined ? undefined : fields[textKey]; return new documents_1.Document({ id: typeof _id === 'string' ? _id : undefined, pageContent: typeof content === 'string' ? content : '', metadata, }); } } exports.PineconeDocumentStore = PineconeDocumentStore; async function getPineconeVectorStore(connection, embeddings, { indexName, namespace, filter, textField }) { const model = await tryDescribeIndex(connection, indexName); const scoreBy = model && getDocumentIndexScoring(model); if (!model || !scoreBy) { const pineconeIndex = new pinecone_2.Pinecone({ apiKey: connection.apiKey }).Index(indexName); return await pinecone_1.PineconeStore.fromExistingIndex(embeddings, { namespace, pineconeIndex, filter }); } return new PineconeDocumentStore(embeddings, toRequest(connection), scoreBy, model, namespace, filter, textField || undefined); } //# sourceMappingURL=pinecone.js.map