Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bedrock[minor]: Allow knowledge bases to use filters and searchType #6189

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions libs/langchain-aws/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@
"author": "LangChain",
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey there! 👋 I noticed that this PR includes a change in the dependencies, specifically the update of "@aws-sdk/client-bedrock-agent-runtime" and "@aws-sdk/types". This is flagged for maintainers to review as it affects the peer/dev dependencies of the project. Keep up the great work! 🚀

"license": "MIT",
"dependencies": {
"@aws-sdk/client-bedrock-agent-runtime": "^3.583.0",
"@aws-sdk/client-bedrock-agent-runtime": "^3.616.0",
"@aws-sdk/client-bedrock-runtime": "^3.602.0",
"@aws-sdk/client-kendra": "^3.352.0",
"@aws-sdk/credential-provider-node": "^3.600.0",
"@langchain/core": ">=0.2.16 <0.3.0",
"zod-to-json-schema": "^3.22.5"
},
"devDependencies": {
"@aws-sdk/types": "^3.598.0",
"@aws-sdk/types": "^3.609.0",
"@jest/globals": "^29.5.0",
"@langchain/scripts": "~0.0.14",
"@langchain/standard-tests": "0.0.0",
Expand Down Expand Up @@ -97,4 +97,4 @@
"index.d.ts",
"index.d.cts"
]
}
}
68 changes: 58 additions & 10 deletions libs/langchain-aws/src/retrievers/bedrock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import {
RetrieveCommand,
BedrockAgentRuntimeClient,
type BedrockAgentRuntimeClientConfig,
type SearchType,
type RetrievalFilter,
} from "@aws-sdk/client-bedrock-agent-runtime";

import { BaseRetriever } from "@langchain/core/retrievers";
Expand All @@ -16,6 +18,8 @@ export interface AmazonKnowledgeBaseRetrieverArgs {
topK: number;
region: string;
clientOptions?: BedrockAgentRuntimeClientConfig;
filter?: RetrievalFilter;
overrideSearchType?: SearchType;
}

/**
Expand Down Expand Up @@ -51,15 +55,23 @@ export class AmazonKnowledgeBaseRetriever extends BaseRetriever {

bedrockAgentRuntimeClient: BedrockAgentRuntimeClient;

filter?: RetrievalFilter;

overrideSearchType?: SearchType;

constructor({
knowledgeBaseId,
topK = 10,
clientOptions,
region,
filter,
overrideSearchType,
}: AmazonKnowledgeBaseRetrieverArgs) {
super();

this.topK = topK;
this.filter = filter;
this.overrideSearchType = overrideSearchType;
this.bedrockAgentRuntimeClient = new BedrockAgentRuntimeClient({
region,
...clientOptions,
Expand All @@ -78,7 +90,12 @@ export class AmazonKnowledgeBaseRetriever extends BaseRetriever {
return res;
}

async queryKnowledgeBase(query: string, topK: number) {
async queryKnowledgeBase(
query: string,
topK: number,
filter?: RetrievalFilter,
overrideSearchType?: SearchType
) {
const retrieveCommand = new RetrieveCommand({
knowledgeBaseId: this.knowledgeBaseId,
retrievalQuery: {
Expand All @@ -87,6 +104,8 @@ export class AmazonKnowledgeBaseRetriever extends BaseRetriever {
retrievalConfiguration: {
vectorSearchConfiguration: {
numberOfResults: topK,
overrideSearchType,
filter,
},
},
});
Expand All @@ -96,19 +115,48 @@ export class AmazonKnowledgeBaseRetriever extends BaseRetriever {
);

return (
retrieveResponse.retrievalResults?.map((result) => ({
pageContent: this.cleanResult(result.content?.text || ""),
metadata: {
source: result.location?.s3Location?.uri,
score: result.score,
...result.metadata,
},
})) ?? ([] as Array<Document>)
retrieveResponse.retrievalResults?.map((result) => {
let source;
switch (result.location?.type) {
case "CONFLUENCE":
source = result.location?.confluenceLocation?.url;
break;
case "S3":
source = result.location?.s3Location?.uri;
break;
case "SALESFORCE":
source = result.location?.salesforceLocation?.url;
break;
case "SHAREPOINT":
source = result.location?.sharePointLocation?.url;
break;
case "WEB":
source = result.location?.webLocation?.url;
break;
default:
source = result.location?.s3Location?.uri;
break;
}

return {
pageContent: this.cleanResult(result.content?.text || ""),
metadata: {
source,
score: result.score,
...result.metadata,
},
};
}) ?? ([] as Array<Document>)
);
}

async _getRelevantDocuments(query: string): Promise<Document[]> {
const docs = await this.queryKnowledgeBase(query, this.topK);
const docs = await this.queryKnowledgeBase(
query,
this.topK,
this.filter,
this.overrideSearchType
);
return docs;
}
}
2 changes: 2 additions & 0 deletions libs/langchain-aws/src/retrievers/tests/bedrock.int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ test("AmazonKnowledgeBaseRetriever", async () => {
topK: 10,
knowledgeBaseId: process.env.AMAZON_KNOWLEDGE_BASE_ID || "",
region: process.env.BEDROCK_AWS_REGION,
overrideSearchType: "HYBRID",
filter: undefined,
clientOptions: {
credentials: {
accessKeyId: process.env.BEDROCK_AWS_ACCESS_KEY_ID,
Expand Down
Loading
Loading