Feature/AWS Bedrock Knowledge Bases retriever (#2905)

* AWS Bedrock Knowledge bases retriever added

* Moved @langchain/aws from root dependency to overrides

* pnpm-lock.yaml update

---------

Co-authored-by: Rok Pajk Kosec <rok.pajkkosec@gmail.com>
This commit is contained in:
Cosso 2024-07-30 17:40:59 +02:00 committed by GitHub
parent 7f668e6413
commit c899c05693
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 35690 additions and 34416 deletions

View File

@ -58,7 +58,8 @@
"sqlite3"
],
"overrides": {
"@langchain/core": "0.2.18"
"@langchain/core": "0.2.18",
"@langchain/aws": "^0.0.6"
}
},
"engines": {

View File

@ -0,0 +1 @@
<svg width="32" height="32" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M6 21c1.67 1.77 5.491 3 9.757 3 2.788 0 5.386-.525 7.317-1.383L25 21.65" stroke="#F90" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/><path d="m23 20 3 1.074L25.4 24" stroke="#F90" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/><path d="M24.564 9.44C23.677 8.76 21 8.633 21 10.796c0 2.396 4 .77 4 3.294 0 1.898-2.67 2.501-4 1.264M14 9v7l2-4 2 4V9" stroke="#252F3E" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/><path d="M7.053 8.597H8.21a2.526 2.526 0 0 1 2.526 2.526v4.842" stroke="#252F3E" stroke-width="2" stroke-linecap="round"/><circle cx="8.632" cy="13.86" stroke="#252F3E" stroke-width="2" r="2.105"/></svg>

After

Width:  |  Height:  |  Size: 753 B

View File

@ -0,0 +1,143 @@
import { AmazonKnowledgeBaseRetriever } from '@langchain/aws'
import { ICommonObject, INode, INodeData, INodeParams, INodeOptionsValue } from '../../../src/Interface'
import { getCredentialData, getCredentialParam } from '../../../src/utils'
import { RetrievalFilter } from '@aws-sdk/client-bedrock-agent-runtime'
import { MODEL_TYPE, getRegions } from '../../../src/modelLoader'
class AWSBedrockKBRetriever_Retrievers implements INode {
label: string
name: string
version: number
description: string
type: string
icon: string
category: string
baseClasses: string[]
credential: INodeParams
inputs: INodeParams[]
badge: string
constructor() {
this.label = 'AWS Bedrock Knowledge Base Retriever'
this.name = 'awsBedrockKBRetriever'
this.version = 1.0
this.type = 'AWSBedrockKBRetriever'
this.icon = 'AWSBedrockKBRetriever.svg'
this.category = 'Retrievers'
this.badge = 'NEW'
this.description = 'Connect to AWS Bedrock Knowledge Base API and retrieve relevant chunks'
this.baseClasses = [this.type, 'BaseRetriever']
this.credential = {
label: 'AWS Credential',
name: 'credential',
type: 'credential',
credentialNames: ['awsApi'],
optional: true
}
this.inputs = [
{
label: 'Region',
name: 'region',
type: 'asyncOptions',
loadMethod: 'listRegions',
default: 'us-east-1'
},
{
label: 'Knowledge Base ID',
name: 'knoledgeBaseID',
type: 'string'
},
{
label: 'Query',
name: 'query',
type: 'string',
description: 'Query to retrieve documents from retriever. If not specified, user question will be used',
optional: true,
acceptVariable: true
},
{
label: 'TopK',
name: 'topK',
type: 'number',
description: 'Number of chunks to retrieve',
optional: true,
additionalParams: true,
default: 5
},
{
label: 'SearchType',
name: 'searchType',
type: 'options',
description:
'Knowledge Base search type. Possible values are HYBRID and SEMANTIC. If not specified, default will be used. Consult AWS documentation for more',
options: [
{
label: 'HYBRID',
name: 'HYBRID',
description: 'Hybrid seach type'
},
{
label: 'SEMANTIC',
name: 'SEMANTIC',
description: 'Semantic seach type'
}
],
optional: true,
additionalParams: true,
default: undefined
},
{
label: 'Filter',
name: 'filter',
type: 'string',
description: 'Knowledge Base retrieval filter. Read documentation for filter syntax',
optional: true,
additionalParams: true
}
]
}
loadMethods = {
// Reuse the AWS Bedrock Embeddings region list as it should be same for all Bedrock functions
async listRegions(): Promise<INodeOptionsValue[]> {
return await getRegions(MODEL_TYPE.EMBEDDING, 'AWSBedrockEmbeddings')
}
}
async init(nodeData: INodeData, input: string, options: ICommonObject): Promise<any> {
const knoledgeBaseID = nodeData.inputs?.knoledgeBaseID as string
const region = nodeData.inputs?.region as string
const topK = nodeData.inputs?.topK as number
const overrideSearchType = (nodeData.inputs?.searchType != '' ? nodeData.inputs?.searchType : undefined) as 'HYBRID' | 'SEMANTIC'
const filter = (nodeData.inputs?.filter != '' ? JSON.parse(nodeData.inputs?.filter) : undefined) as RetrievalFilter
let credentialApiKey = ''
let credentialApiSecret = ''
let credentialApiSession = ''
const credentialData = await getCredentialData(nodeData.credential ?? '', options)
if (credentialData && Object.keys(credentialData).length !== 0) {
credentialApiKey = getCredentialParam('awsKey', credentialData, nodeData)
credentialApiSecret = getCredentialParam('awsSecret', credentialData, nodeData)
credentialApiSession = getCredentialParam('awsSession', credentialData, nodeData)
}
const retriever = new AmazonKnowledgeBaseRetriever({
topK: topK,
knowledgeBaseId: knoledgeBaseID,
region: region,
filter,
overrideSearchType,
clientOptions: {
credentials: {
accessKeyId: credentialApiKey,
secretAccessKey: credentialApiSecret,
sessionToken: credentialApiSession
}
}
})
return retriever
}
}
module.exports = { nodeClass: AWSBedrockKBRetriever_Retrievers }

File diff suppressed because it is too large Load Diff