跳转到内容

Bedrock Knowledge Base Retriever

BedrockKBRetrieverTool 让 CrewAI agents 能够使用自然语言查询从 Amazon Bedrock Knowledge Bases 检索信息。

Terminal window
uv pip install 'crewai[tools]'
  • 已配置 AWS 凭据(通过环境变量或 AWS CLI)
  • boto3python-dotenv
  • 可访问 Amazon Bedrock Knowledge Base

下面展示如何在 CrewAI agent 中使用该工具:

from crewai import Agent, Task, Crew
from crewai_tools.aws.bedrock.knowledge_base.retriever_tool import BedrockKBRetrieverTool
# Initialize the tool
kb_tool = BedrockKBRetrieverTool(
knowledge_base_id="your-kb-id",
number_of_results=5
)
# Create a CrewAI agent that uses the tool
researcher = Agent(
role='Knowledge Base Researcher',
goal='Find information about company policies',
backstory='I am a researcher specialized in retrieving and analyzing company documentation.',
tools=[kb_tool],
verbose=True
)
# Create a task for the agent
research_task = Task(
description="Find our company's remote work policy and summarize the key points.",
agent=researcher
)
# Create a crew with the agent
crew = Crew(
agents=[researcher],
tasks=[research_task],
verbose=2
)
# Run the crew
result = crew.kickoff()
print(result)
ArgumentTypeRequiredDefaultDescription
knowledge_base_idstrYesNone知识库的唯一标识符(0-10 个字母数字字符)
number_of_resultsintNo5返回结果的最大数量
retrieval_configurationdictNoNone针对知识库查询的自定义配置
guardrail_configurationdictNoNone内容过滤设置
next_tokenstrNoNone用于分页的 token
Terminal window
BEDROCK_KB_ID=your-knowledge-base-id # Alternative to passing knowledge_base_id
AWS_REGION=your-aws-region # Defaults to us-east-1
AWS_ACCESS_KEY_ID=your-access-key # Required for AWS authentication
AWS_SECRET_ACCESS_KEY=your-secret-key # Required for AWS authentication

工具会以 JSON 格式返回结果:

{
"results": [
{
"content": "Retrieved text content",
"content_type": "text",
"source_type": "S3",
"source_uri": "s3://bucket/document.pdf",
"score": 0.95,
"metadata": {
"additional": "metadata"
}
}
],
"nextToken": "pagination-token",
"guardrailAction": "NONE"
}
kb_tool = BedrockKBRetrieverTool(
knowledge_base_id="your-kb-id",
retrieval_configuration={
"vectorSearchConfiguration": {
"numberOfResults": 10,
"overrideSearchType": "HYBRID"
}
}
)
policy_expert = Agent(
role='Policy Expert',
goal='Analyze company policies in detail',
backstory='I am an expert in corporate policy analysis with deep knowledge of regulatory requirements.',
tools=[kb_tool]
)
  • Amazon S3
  • Confluence
  • Salesforce
  • SharePoint
  • Web pages
  • Custom document locations
  • Amazon Kendra
  • SQL databases
  • 让 CrewAI agents 可以访问组织的专有知识,而不暴露敏感数据
  • 让 agents 基于公司的具体政策、流程和文档做决策
  • 创建能够基于内部文档回答问题、同时保持数据安全的 agents
  • 将 CrewAI agents 连接到法律、医疗、技术等领域知识库,而无需重新训练模型
  • 利用已经在 AWS 环境中维护好的现有知识仓库
  • 将 CrewAI 的推理能力与知识库中的领域信息结合起来
  • 让 CrewAI agent 的响应基于真实公司数据,而不是通用知识
  • 确保 agents 基于你特定的业务上下文和文档给出建议
  • 通过从知识库中检索事实信息来减少幻觉
  • 无需把所有组织知识都嵌入模型,也能访问 TB 级别的信息
  • 只按需动态查询特定任务所需的相关信息
  • 利用 AWS 的可扩展基础设施高效处理大型知识库
  • 确保 CrewAI agents 的响应符合公司批准的文档
  • 为 agents 使用的信息源创建可审计轨迹
  • 控制你的 agents 可以访问哪些信息源