-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Add Amazon Knowledge Bases retriever support #4035
Merged
bracesproul
merged 6 commits into
langchain-ai:main
from
jl4nz:3781-amazon-knowledge-bases
Jan 24, 2024
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
docs/core_docs/docs/integrations/retrievers/bedrock-knowledge-bases.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
--- | ||
hide_table_of_contents: true | ||
--- | ||
|
||
# Knowledge Bases for Amazon Bedrock | ||
|
||
Knowledge Bases for Amazon Bedrock is a fully managed support for end-to-end RAG workflow provided by Amazon Web Services (AWS). It provides an entire ingestion workflow of converting your documents into embeddings (vector) and storing the embeddings in a specialized vector database. Knowledge Bases for Amazon Bedrock supports popular databases for vector storage, including vector engine for Amazon OpenSearch Serverless, Pinecone, Redis Enterprise Cloud, Amazon Aurora (coming soon), and MongoDB (coming soon). | ||
|
||
## Setup | ||
|
||
import IntegrationInstallTooltip from "@mdx_components/integration_install_tooltip.mdx"; | ||
|
||
<IntegrationInstallTooltip></IntegrationInstallTooltip> | ||
|
||
```bash npm2yarn | ||
npm i @aws-sdk/client-bedrock-agent-runtime @langchain/community | ||
``` | ||
|
||
## Usage | ||
|
||
import CodeBlock from "@theme/CodeBlock"; | ||
import Example from "@examples/retrievers/amazon_knowledge_bases.ts"; | ||
|
||
<CodeBlock language="typescript">{Example}</CodeBlock> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { AmazonKnowledgeBaseRetriever } from "@langchain/community/retrievers/amazon_knowledge_base"; | ||
|
||
const retriever = new AmazonKnowledgeBaseRetriever({ | ||
topK: 10, | ||
knowledgeBaseId: "YOUR_KNOWLEDGE_BASE_ID", | ||
region: "us-east-2", | ||
clientOptions: { | ||
credentials: { | ||
accessKeyId: "YOUR_ACCESS_KEY_ID", | ||
secretAccessKey: "YOUR_SECRET_ACCESS_KEY", | ||
}, | ||
}, | ||
}); | ||
|
||
const docs = await retriever.getRelevantDocuments("How are clouds formed?"); | ||
|
||
console.log(docs); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
libs/langchain-community/src/retrievers/amazon_knowledge_base.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import { | ||
RetrieveCommand, | ||
BedrockAgentRuntimeClient, | ||
BedrockAgentRuntimeClientConfig, | ||
} from "@aws-sdk/client-bedrock-agent-runtime"; | ||
|
||
import { BaseRetriever } from "@langchain/core/retrievers"; | ||
import { Document } from "@langchain/core/documents"; | ||
|
||
/** | ||
* Interface for the arguments required to initialize an | ||
* AmazonKnowledgeBaseRetriever instance. | ||
*/ | ||
export interface AmazonKnowledgeBaseRetrieverArgs { | ||
knowledgeBaseId: string; | ||
topK: number; | ||
region: string; | ||
clientOptions?: BedrockAgentRuntimeClientConfig; | ||
} | ||
|
||
/** | ||
* Class for interacting with Amazon Bedrock Knowledge Bases, a RAG workflow oriented service | ||
* provided by AWS. Extends the BaseRetriever class. | ||
* @example | ||
* ```typescript | ||
* const retriever = new AmazonKnowledgeBaseRetriever({ | ||
* topK: 10, | ||
* knowledgeBaseId: "YOUR_KNOWLEDGE_BASE_ID", | ||
* region: "us-east-2", | ||
* clientOptions: { | ||
* credentials: { | ||
* accessKeyId: "YOUR_ACCESS_KEY_ID", | ||
* secretAccessKey: "YOUR_SECRET_ACCESS_KEY", | ||
* }, | ||
* }, | ||
* }); | ||
* | ||
* const docs = await retriever.getRelevantDocuments("How are clouds formed?"); | ||
* ``` | ||
*/ | ||
export class AmazonKnowledgeBaseRetriever extends BaseRetriever { | ||
static lc_name() { | ||
return "AmazonKnowledgeBaseRetriever"; | ||
} | ||
|
||
lc_namespace = ["langchain", "retrievers", "amazon_bedrock_knowledge_base"]; | ||
|
||
knowledgeBaseId: string; | ||
|
||
topK: number; | ||
|
||
bedrockAgentRuntimeClient: BedrockAgentRuntimeClient; | ||
|
||
constructor({ | ||
knowledgeBaseId, | ||
topK = 10, | ||
clientOptions, | ||
region, | ||
}: AmazonKnowledgeBaseRetrieverArgs) { | ||
super(); | ||
|
||
this.topK = topK; | ||
this.bedrockAgentRuntimeClient = new BedrockAgentRuntimeClient({ | ||
region, | ||
...clientOptions, | ||
}); | ||
this.knowledgeBaseId = knowledgeBaseId; | ||
} | ||
|
||
/** | ||
* Cleans the result text by replacing sequences of whitespace with a | ||
* single space and removing ellipses. | ||
* @param resText The result text to clean. | ||
* @returns The cleaned result text. | ||
*/ | ||
cleanResult(resText: string) { | ||
const res = resText.replace(/\s+/g, " ").replace(/\.\.\./g, ""); | ||
return res; | ||
} | ||
|
||
async queryKnowledgeBase(query: string, topK: number) { | ||
const retrieveCommand = new RetrieveCommand({ | ||
knowledgeBaseId: this.knowledgeBaseId, | ||
retrievalQuery: { | ||
text: query, | ||
}, | ||
retrievalConfiguration: { | ||
vectorSearchConfiguration: { | ||
numberOfResults: topK, | ||
}, | ||
}, | ||
}); | ||
|
||
const retrieveResponse = await this.bedrockAgentRuntimeClient.send( | ||
retrieveCommand | ||
); | ||
|
||
return ( | ||
retrieveResponse.retrievalResults?.map((result) => ({ | ||
pageContent: this.cleanResult(result.content?.text || ""), | ||
metadata: { | ||
source: result.location?.s3Location?.uri, | ||
score: result.score, | ||
}, | ||
})) ?? ([] as Array<Document>) | ||
); | ||
} | ||
|
||
async _getRelevantDocuments(query: string): Promise<Document[]> { | ||
const docs = await this.queryKnowledgeBase(query, this.topK); | ||
return docs; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
libs/langchain-community/src/retrievers/tests/amazon_knowledge_base.int.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* eslint-disable no-process-env */ | ||
/* eslint-disable @typescript-eslint/no-non-null-assertion */ | ||
import { test } from "@jest/globals"; | ||
import { AmazonKnowledgeBaseRetriever } from "../amazon_knowledge_base.js"; | ||
|
||
test("AmazonKnowledgeBaseRetriever", async () => { | ||
const retriever = new AmazonKnowledgeBaseRetriever({ | ||
topK: 10, | ||
knowledgeBaseId: process.env.AMAZON_KNOWLEDGE_BASE_ID || "", | ||
region: process.env.AWS_REGION || "us-east-1", | ||
clientOptions: { | ||
credentials: { | ||
accessKeyId: process.env.AWS_ACCESS_KEY_ID!, | ||
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY!, | ||
sessionToken: process.env.AWS_SESSION_TOKEN!, | ||
}, | ||
}, | ||
}); | ||
|
||
const docs = await retriever.getRelevantDocuments("How are clouds formed?"); | ||
expect(docs.length).toBeGreaterThan(0); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 a new peer dependency "@aws-sdk/client-bedrock-agent-runtime" has been added in this PR. This comment is to flag the change for maintainers to review. Keep up the great work! 🚀