Skip to content

Commit

Permalink
Improve OpenSearchURL credential storing user and password in separat…
Browse files Browse the repository at this point in the history
…e fields from the URL (FlowiseAI#2549)

OpenSearch - Improve OpenSearchURL credential storing user and password in separate fields from the URL
  • Loading branch information
danieldabate authored Jun 4, 2024
1 parent 76abd20 commit a799ac8
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 9 deletions.
16 changes: 15 additions & 1 deletion packages/components/credentials/OpenSearchUrl.credential.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: '<OPENSEARCH_USERNAME>',
optional: true
},
{
label: 'Password',
name: 'password',
type: 'password',
placeholder: '<OPENSEARCH_PASSWORD>',
optional: true
}
]
}
Expand Down
29 changes: 21 additions & 8 deletions packages/components/nodes/vectorstores/OpenSearch/OpenSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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 = []
Expand All @@ -96,10 +100,6 @@ class OpenSearch_VectorStores implements INode {
}
}

const client = new Client({
nodes: [opensearchURL]
})

try {
await OpenSearchVectorStore.fromDocuments(finalDocs, embeddings, {
client,
Expand All @@ -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,
Expand All @@ -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 }

0 comments on commit a799ac8

Please sign in to comment.