-
Notifications
You must be signed in to change notification settings - Fork 342
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 Bedrock Retriever to leverage Knowledge Base deployed onto AWS #1219
Open
ajohn-wick
wants to merge
7
commits into
run-llama:main
Choose a base branch
from
ajohn-wick:bedrock-retriever
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3a01606
Prettier bedrock
6a29f92
Deleted package-lock.json
ajohn-wick 2a26d36
Deleted pnpm-lock.yaml
ajohn-wick 8741bb7
Moved retrievers to src/
a07d345
Updated export + package.json dependencies
5e212e3
Rebased pnpm-lock.yaml
6c22b5b
Merge branch 'main' into bedrock-retriever
ajohn-wick 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
import { | ||
BedrockAgentRuntimeClient, | ||
RetrieveCommand, | ||
type BedrockAgentRuntimeClientConfig, | ||
type RetrievalFilter, | ||
type SearchType, | ||
} from "@aws-sdk/client-bedrock-agent-runtime"; | ||
|
||
import { BaseRetriever, Document } from "llamaindex"; | ||
|
||
/** | ||
* Interface for the arguments required to initialize an | ||
* AmazonKnowledgeBaseRetriever instance. | ||
*/ | ||
export interface AmazonKnowledgeBaseRetrieverArgs { | ||
knowledgeBaseId: string; | ||
topK: number; | ||
region: string; | ||
clientOptions?: BedrockAgentRuntimeClientConfig; | ||
filter?: RetrievalFilter; | ||
overrideSearchType?: SearchType; | ||
} | ||
|
||
/** | ||
* Class for interacting with Amazon Bedrock Knowledge Bases, a RAG workflow oriented service | ||
* 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.retrieve({query: "How are clouds formed?"}); | ||
* ``` | ||
*/ | ||
export class AmazonKnowledgeBaseRetriever extends BaseRetriever { | ||
static lc_name() { | ||
return "AmazonKnowledgeBaseRetriever"; | ||
} | ||
|
||
lc_namespace = ["llamaindex", "retrievers", "amazon_bedrock_knowledge_base"]; | ||
|
||
knowledgeBaseId: string; | ||
|
||
topK: number; | ||
|
||
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, | ||
}); | ||
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, | ||
filter?: RetrievalFilter, | ||
overrideSearchType?: SearchType, | ||
) { | ||
const retrieveCommand = new RetrieveCommand({ | ||
knowledgeBaseId: this.knowledgeBaseId, | ||
retrievalQuery: { | ||
text: query, | ||
}, | ||
retrievalConfiguration: { | ||
vectorSearchConfiguration: { | ||
numberOfResults: topK, | ||
overrideSearchType, | ||
filter, | ||
}, | ||
}, | ||
}); | ||
|
||
const retrieveResponse = | ||
await this.bedrockAgentRuntimeClient.send(retrieveCommand); | ||
|
||
return ( | ||
retrieveResponse.retrievalResults?.map((result: any) => { | ||
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 _retrieve(query: string): Promise<Document[]> { | ||
const docs = await this.queryKnowledgeBase( | ||
query, | ||
this.topK, | ||
this.filter, | ||
this.overrideSearchType, | ||
); | ||
return docs; | ||
} | ||
} |
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.
import Document from llamaindex/core/schema and copy BaseRetriever type here for now.
llamaindex
is the to end user level pacakge and shouldn't be imported from our other pacakge, so please removellamaindex
from depsremind me that we need move
BaseRetriever
into core module. If you are interested please follow python repo file structure and my other PR in this repo. Or I will do it tmrThere 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.
Thank you a lot for your review, please proceed with moving
BaseRetriever
into core module. Then, I will update code to import it if you are OKay @himself65?Otherwise, I will copy
BaseRetriever
type here.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.
too late to me, will do tmr