Skip to content
CrewAI 文档版本 跟随上游 源快照 25281f81 更新于 2026-10-18

Bedrock Knowledge Base 检索器

使用自然语言查询从 Amazon Bedrock Knowledge Bases 检索信息

BedrockKBRetrieverTool

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

安装

bash
uv pip install 'crewai[tools]'

要求

  • 已配置 AWS 凭证(通过环境变量或 AWS CLI)
  • boto3 和 python-dotenv 包
  • 对 Amazon Bedrock Knowledge Base 的访问权限

用量

以下是如何在 CrewAI 智能体中使用该工具:

python
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)

工具参数

参数类型是否必填默认值描述
knowledge_base_idstr是无知识库的唯一标识符(0-10 个字母数字字符)
number_of_resultsint否5返回结果的最大数量
retrieval_configurationdict否无知识库查询的自定义配置
guardrail_configurationdict否无内容过滤设置
next_tokenstr否无用于分页的 token

环境变量

bash
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 格式返回结果:

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"
}

高级用法

自定义检索配置

python
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
  • 网页
  • 自定义文档位置
  • Amazon Kendra
  • SQL 数据库

使用场景

企业知识集成

  • 让 CrewAI 智能体能够访问组织的专有知识,而无需暴露敏感数据
  • 让智能体能够基于公司特定的政策、流程和文档做出决策
  • 创建能够基于内部文档回答问题的智能体,同时保持数据安全

专业领域知识

  • 将 CrewAI 智能体连接到特定领域的知识库(法律、医疗、技术),而无需重新训练模型
  • 利用已在你的 AWS 环境中维护的现有知识库
  • 将 CrewAI 的推理能力与知识库中的领域特定信息相结合

数据驱动的决策

  • 让 CrewAI 智能体的回答基于你实际的公司数据,而不是通用知识
  • 确保智能体基于你特定的业务背景和文档提供建议
  • 通过从知识库中检索事实信息来减少幻觉

可扩展的信息访问

  • 访问数 TB 的组织知识,而无需将其全部嵌入到模型中
  • 动态查询特定任务所需的相关信息
  • 利用 AWS 的可扩展基础设施高效处理大型知识库

合规与治理

  • 确保 CrewAI 智能体提供的回复与你公司已批准的文档保持一致
  • 为智能体使用的信息来源创建可审计的记录
  • 保持对智能体可访问哪些信息来源的控制

BytesRouter 是社区翻译站点,所有译文仅供参考,一切以官方英文原文为准。