-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Python: feat: add Amazon Bedrock Knowledge Base tool and context provider #8173
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
Open
Vidyadhar Pogul (PVidyadhar)
wants to merge
19
commits into
microsoft:main
Choose a base branch
from
PVidyadhar:bmkb-managed-kb-support
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 all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
5d81286
feat: add Amazon Bedrock Knowledge Base tool and context provider
PVidyadhar 2f9edd2
Merge branch 'main' into bmkb-managed-kb-support
PVidyadhar 3d1ae25
fix: inject KB context as instructions to avoid consecutive user roles
PVidyadhar 9f8ea75
fix: address Copilot review on #8173
PVidyadhar dfd0d2f
fix: correct agentic result parsing + scope user-message coalescing
PVidyadhar 8da7c32
fix: disable response generation in agentic retrieve
PVidyadhar db70b32
fix: address 4th Copilot review + eavanvalkenburg feedback on #8173
PVidyadhar a639086
samples: move Bedrock KB samples to canonical provider tree
PVidyadhar 46fb12e
samples: read BEDROCK_REGION for KB client region
PVidyadhar ef6bbb0
test: fix stale coalescing comment in KB provider test
PVidyadhar 3af22d9
Python: fix bedrock KB CI — pyright, pyupgrade, and test typing
PVidyadhar 376c616
Python: address bedrock KB review feedback (IAM, SQL ROW, sample output)
PVidyadhar 37d1d52
Python: consolidate standard Retrieve behind _retrieve_standard_passages
PVidyadhar 773cece
Python: apply ruff format to bedrock KB (single-line comprehensions)
PVidyadhar af64bdd
Python: address Copilot re-review (IMAGE content, security wording, s…
PVidyadhar 6900e99
Python: log KB context-provider retrieval failure at WARNING not DEBUG
PVidyadhar 736d27e
Python: fix BedrockChatClient usage in KB examples
PVidyadhar dafe97c
Python: handle AUDIO/VIDEO retrieval content, not just IMAGE
PVidyadhar 60b3361
Python: align KB client with Bedrock settings/session; drop file-leve…
PVidyadhar 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 hidden or 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 hidden or 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,77 @@ | ||
| # Bedrock Managed Knowledge Base Support | ||
|
|
||
| ## Overview | ||
| Adds an Agent Framework tool that queries Amazon Bedrock Knowledge Bases for managed retrieval within agent pipelines. | ||
|
|
||
| ## Usage | ||
| ```python | ||
| from agent_framework import Agent | ||
| from agent_framework_bedrock import BedrockKnowledgeBaseTool, BedrockChatClient, BedrockChatOptions | ||
|
|
||
| tool = BedrockKnowledgeBaseTool( | ||
| knowledge_base_id="YOUR_KB_ID", | ||
| region_name="us-east-1", | ||
| ) | ||
|
|
||
| # As a FunctionTool, pass directly to an Agent: | ||
| agent = Agent(client=BedrockChatClient(model="..."), tools=[tool]) | ||
|
|
||
| # Or invoke directly for testing: | ||
| import asyncio | ||
| result = asyncio.run(tool.invoke(arguments={"query": "What are the compliance requirements?"})) | ||
| print(result) # List of Content items with retrieval results | ||
| ``` | ||
|
|
||
| ## Configuration | ||
|
|
||
| All configuration is via constructor parameters: | ||
|
|
||
| | Parameter | Description | Default | | ||
| |---|---|---| | ||
| | `knowledge_base_id` | Bedrock Knowledge Base ID (required) | — | | ||
| | `region_name` | AWS region for the KB | `us-east-1` | | ||
| | `number_of_results` | Maximum retrieval results | `5` | | ||
| | `use_agentic_retrieval` | Enable agentic multi-hop retrieval | `True` | | ||
| | `client` | Pre-configured boto3 client (optional) | Auto-created | | ||
|
|
||
| ## Features | ||
| - Managed search (no vector store needed) | ||
| - **BedrockKnowledgeBaseTool**: Agentic retrieval with query decomposition + reranking, automatic fallback to standard Retrieve | ||
| - **BedrockKnowledgeBaseProvider**: Standard managed retrieval injected as context before each agent run | ||
| - Multi-source support (S3, Web, Confluence, SharePoint) | ||
| - Compatible with Agent Framework FunctionTool and ContextProvider interfaces | ||
|
|
||
| ## SDK Requirements | ||
| - boto3 >= 1.43.32 | ||
|
|
||
| ## Required IAM Permissions | ||
| ```json | ||
| { | ||
| "Version": "2012-10-17", | ||
| "Statement": [ | ||
| { | ||
| "Effect": "Allow", | ||
| "Action": [ | ||
| "bedrock:Retrieve", | ||
| "bedrock:GetDocumentContent" | ||
| ], | ||
| "Resource": "arn:aws:bedrock:<region>:<account-id>:knowledge-base/<kb-id>" | ||
| }, | ||
| { | ||
| "Effect": "Allow", | ||
| "Action": [ | ||
| "bedrock:AgenticRetrieveStream", | ||
| "bedrock:InvokeModelWithResponseStream" | ||
| ], | ||
| "Resource": "*" | ||
| } | ||
| ] | ||
| } | ||
| ``` | ||
|
|
||
| > Note: `bedrock:AgenticRetrieveStream` and `bedrock:InvokeModelWithResponseStream` have no resource-level permission type, so they must be granted with `Resource: "*"`. Scoping `AgenticRetrieveStream` to a Knowledge Base ARN implicitly denies the call and silently forces a fallback to standard `Retrieve`, so the default tool never performs the advertised query decomposition. `bedrock:Retrieve` and `bedrock:GetDocumentContent` remain scoped to the Knowledge Base ARN — `GetDocumentContent` is required because agentic retrieval calls it during a `FullDocumentExpansion` step, and a policy without it fails partway through a query. This matches the [AWS agentic-retrieval permissions reference](https://docs.aws.amazon.com/bedrock/latest/userguide/kb-test-agentic-retrieve.html). `AgenticRetrieveStream`/`GetDocumentContent`/`InvokeModelWithResponseStream` are only required when using `use_agentic_retrieval=True`. | ||
|
|
||
| ## References | ||
| - [Build a Managed Knowledge Base](https://docs.aws.amazon.com/bedrock/latest/userguide/kb-build-managed.html) | ||
| - [Retrieve API](https://docs.aws.amazon.com/bedrock/latest/userguide/kb-test-retrieve.html) | ||
| - [Agentic Retrieval](https://docs.aws.amazon.com/bedrock/latest/userguide/kb-test-agentic.html) | ||
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
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.
Could we use AWS's complete agentic-retrieval policy here? The calling identity also needs
bedrock:GetDocumentContenton the knowledge base andbedrock:InvokeModelWithResponseStream; with this policy,AgenticRetrieveStreamcan fail authorization and_retrievesilently falls back to single-passRetrieve, so the default tool never provides the advertised query decomposition. The policy inpython/samples/02-agents/providers/amazon/README.md:31-42needs the same additions.