From 7449e10f9202233648f3b0b88a202546b7e33b60 Mon Sep 17 00:00:00 2001 From: Daniel D'Abate Date: Sun, 2 Jun 2024 01:19:45 +0200 Subject: [PATCH] OpenSearch - Improve OpenSearchURL credential storing user and password in separate fields from the URL --- .../credentials/OpenSearchUrl.credential.ts | 16 +++++++++- .../vectorstores/OpenSearch/OpenSearch.ts | 29 ++++++++++++++----- 2 files changed, 36 insertions(+), 9 deletions(-) diff --git a/packages/components/credentials/OpenSearchUrl.credential.ts b/packages/components/credentials/OpenSearchUrl.credential.ts index d5c54bfa80b..c941e61f32d 100644 --- a/packages/components/credentials/OpenSearchUrl.credential.ts +++ b/packages/components/credentials/OpenSearchUrl.credential.ts @@ -10,12 +10,26 @@ class OpenSearchUrl implements INodeCredential { constructor() { this.label = 'OpenSearch' this.name = 'openSearchUrl' - this.version = 1.0 + this.version = 2.0 this.inputs = [ { label: 'OpenSearch Url', name: 'openSearchUrl', type: 'string' + }, + { + label: 'User', + name: 'user', + type: 'string', + placeholder: '', + optional: true + }, + { + label: 'Password', + name: 'password', + type: 'password', + placeholder: '', + optional: true } ] } diff --git a/packages/components/nodes/vectorstores/OpenSearch/OpenSearch.ts b/packages/components/nodes/vectorstores/OpenSearch/OpenSearch.ts index df721c49031..5399ebc3a0c 100644 --- a/packages/components/nodes/vectorstores/OpenSearch/OpenSearch.ts +++ b/packages/components/nodes/vectorstores/OpenSearch/OpenSearch.ts @@ -23,7 +23,7 @@ class OpenSearch_VectorStores implements INode { constructor() { this.label = 'OpenSearch' this.name = 'openSearch' - this.version = 2.0 + this.version = 3.0 this.type = 'OpenSearch' this.icon = 'opensearch.svg' this.category = 'Vector Stores' @@ -87,6 +87,10 @@ class OpenSearch_VectorStores implements INode { const credentialData = await getCredentialData(nodeData.credential ?? '', options) const opensearchURL = getCredentialParam('openSearchUrl', credentialData, nodeData) + const user = getCredentialParam('user', credentialData, nodeData) + const password = getCredentialParam('password', credentialData, nodeData) + + const client = getOpenSearchClient(opensearchURL, user, password) const flattenDocs = docs && docs.length ? flatten(docs) : [] const finalDocs = [] @@ -96,10 +100,6 @@ class OpenSearch_VectorStores implements INode { } } - const client = new Client({ - nodes: [opensearchURL] - }) - try { await OpenSearchVectorStore.fromDocuments(finalDocs, embeddings, { client, @@ -121,10 +121,10 @@ class OpenSearch_VectorStores implements INode { const credentialData = await getCredentialData(nodeData.credential ?? '', options) const opensearchURL = getCredentialParam('openSearchUrl', credentialData, nodeData) + const user = getCredentialParam('user', credentialData, nodeData) + const password = getCredentialParam('password', credentialData, nodeData) - const client = new Client({ - nodes: [opensearchURL] - }) + const client = getOpenSearchClient(opensearchURL, user, password) const vectorStore = new OpenSearchVectorStore(embeddings, { client, @@ -142,4 +142,17 @@ class OpenSearch_VectorStores implements INode { } } +const getOpenSearchClient = (url: string, user?: string, password?: string): Client => { + if (user && password) { + const urlObj = new URL(url) + urlObj.username = user + urlObj.password = password + url = urlObj.toString() + } + + return new Client({ + nodes: [url] + }) +} + module.exports = { nodeClass: OpenSearch_VectorStores }