Overview
Concrete tool for retrieving information from Amazon Bedrock Knowledge Bases provided by CrewAI.
Description
The BedrockKBRetrieverTool class extends BaseTool to perform semantic search against Amazon Bedrock Knowledge Bases using the boto3 Bedrock Agent Runtime client. It accepts a natural language query and returns structured JSON results with content, metadata, relevance scores, and source provenance. The tool supports configurable number_of_results (default: 5), custom retrieval_configuration for vector search settings, guardrail_configuration for content safety, and pagination via next_token. The _process_retrieval_result() method handles multiple data source types including S3, Confluence, Salesforce, SharePoint, Web, Custom Documents, Kendra Documents, and SQL locations, extracting the appropriate URI or identifier for each. Parameter validation enforces constraints on knowledge base ID format (alphanumeric, max 10 characters) and optional token format. Configuration values can be provided via constructor or BEDROCK_KB_ID environment variable.
Usage
Import and instantiate BedrockKBRetrieverTool when CrewAI agents need to ground their responses in enterprise knowledge stored in Amazon Bedrock Knowledge Bases. This enables agents to perform semantic search across documents, wikis, databases, and other data sources indexed by Bedrock.
Code Reference
Source Location
- Repository: CrewAI
- File: lib/crewai-tools/src/crewai_tools/aws/bedrock/knowledge_base/retriever_tool.py
- Lines: 1-269
Signature
class BedrockKBRetrieverTool(BaseTool):
def __init__(
self,
knowledge_base_id: str | None = None,
number_of_results: int | None = 5,
retrieval_configuration: dict[str, Any] | None = None,
guardrail_configuration: dict[str, Any] | None = None,
next_token: str | None = None,
**kwargs,
):
Import
from crewai_tools.aws.bedrock.knowledge_base import BedrockKBRetrieverTool
I/O Contract
Inputs (Constructor)
| Name |
Type |
Required |
Description
|
| knowledge_base_id |
None |
No |
Unique ID of the Bedrock Knowledge Base (alphanumeric, max 10 chars); falls back to BEDROCK_KB_ID env var
|
| number_of_results |
None |
No |
Maximum number of results to return (default: 5)
|
| retrieval_configuration |
None |
No |
Custom retrieval configuration for vector search settings
|
| guardrail_configuration |
None |
No |
Guardrail settings for content safety
|
| next_token |
None |
No |
Token for paginating through results
|
Inputs (_run)
| Name |
Type |
Required |
Description
|
| query |
str |
Yes |
Natural language query to search the knowledge base
|
Outputs
| Name |
Type |
Description
|
| return |
str |
JSON string containing results array with content, content_type, source_type, source_uri, optional score and metadata; or a message if no results found; may include nextToken and guardrailAction fields
|
Usage Examples
Basic Usage
from crewai_tools.aws.bedrock.knowledge_base import BedrockKBRetrieverTool
tool = BedrockKBRetrieverTool(
knowledge_base_id="ABC1234567",
number_of_results=10,
)
# Use with a CrewAI agent
agent = Agent(
role="research_analyst",
tools=[tool],
)
With Custom Retrieval Configuration
from crewai_tools.aws.bedrock.knowledge_base import BedrockKBRetrieverTool
tool = BedrockKBRetrieverTool(
knowledge_base_id="ABC1234567",
retrieval_configuration={
"vectorSearchConfiguration": {
"numberOfResults": 10,
"overrideSearchType": "HYBRID",
}
},
guardrail_configuration={
"guardrailId": "my-guardrail",
"guardrailVersion": "1",
},
)
result = tool._run(query="What are the quarterly revenue figures?")
Related Pages