开发工具

AWS CDK 构建 Bedrock 知识库代理

Build enterprise agentic retrieval on a Managed Amazon Bedrock Knowledge Base with the AWS CDK: an agent that routes across two knowledge bases, retrieves through the AgentCore Gat

返回指南列表 · 正文以簡體中文為主;下方提供 English summary 供國際讀者與 AI 引用。

AWS CDK 构建 Bedrock 知识库代理

在企业环境中,单一检索增强生成(RAG)系统常因问题跨多个知识库而返回不完整答案,或无法根据问题语义选择正确来源。AWS CDK 提供了一种基础设施即代码方式,直接构建代理式检索系统:利用托管的 Amazon Bedrock Knowledge Base,支持跨两个知识库的多轮路由、AgentCore Gateway 工具调用,以及通过 AgenticRetrieveStream API 返回带引用的合成回答。

这套解决方案特别适合需要高可观测性和可靠质量的企业场景(如金融文档或天气数据处理),帮助团队一次性部署所有资源并实时监控代理行为,避免后期补丁。

下面是完整指南,重点说明如何用 Python CDK 实现,附带决策表格和边界说明。

为什么选择代理式检索,而非传统 RAG

经典 RAG 流程是:嵌入问题后从一个索引拉取片段,送给模型生成答案。这一设计适合简单查询,但对复杂问题(答案分散于多个语料库、或需二次确认)会失效。

代理式检索则将推理代理放入循环:代理决定是否检索、选择哪个知识库、是否需二次检索,再合成带引用的最终回答。托管 Knowledge Base 让这一能力成为原生服务特性,无需自行维护向量数据库或索引生命周期。

架构概览与工作流程

整个系统由四个 CDK 栈组成,依赖关系由 CDK 自动推导,无需手动顺序脚本。

1. 用户向 Bedrock AgentCore Runtime 发送问题。

2. 代理模型规划并路由到对应知识库(金融或天气示例)。

3. AgentCore Gateway 通过 MCP 协议转发工具调用,授权后触发该知识库的 AgenticRetrieveStream API。

4. AgenticRetrieveStream 执行内部多轮检索、重新排序,并合成带引用的回答。

5. 代理检查上下文是否足够;不足则循环返回,否则返回最终答案。

每一步通过 OpenTelemetry 流向 CloudWatch 和 X-Ray,实现七层可观测性。

主要服务组件对照

组件 功能描述 托管 Knowledge Base 优势
Knowledge Base 管理数据摄取、存储、索引和检索 无需自行维护向量数据库或 OCU 计费
AgentCore Gateway 将知识库暴露为 MCP 工具 支持工具路由,无需额外编排
AgentCore Runtime 托管推理代理 内置 OTEL 自动观测代理循环
AgenticRetrieveStream 执行多轮规划、检索并合成回答 原生支持,返回带引用的答案

代码实现要点(Python CDK)

应用在单个 CDK 项目中,核心是四个栈(kb、gateway、agent、dashboards)。示例代码片段(精简):


# dashboards_stack.py

from aws_cdk import (

 Dashboard,

 GraphWidget,

 ...

)



# 自动生成可观测图表,包含七层指标

dashboard.add_widgets(

 GraphWidget(

 title="Layer 3 Quality (Financial KB)",

 left=[...grounded_coverage...],

 right=[...retrieval_utilization...]

 )

)

AgentCore Gateway 和 Runtime 栈通过 IAM 角色和 SigV4 签名授权工具调用。托管 KB 栈自动处理摄取,无需 OpenSearch Serverless。

完整示例已同步至 AWS 官方 GitHub 仓库(需查阅最新版本)。

成本与可观测性

托管方式下,空闲时无 OCU 费用,仅存储成本。代理每步消耗 Token 和调用次数,可在 CloudWatch 实时查看。Dashboard A 覆盖端到端代理行为,Dashboard B 拆分到每个 KB 的检索量、工具调用和 Token 使用。

延伸阅读

风险与边界

此解决方案适用于企业级生产环境,但必须遵循本地法规与合规要求。托管服务由 AWS 提供,代理质量依赖模型选择和提示词工程。空闲成本仅为存储,查询量按实际 Token 和调用计费。以官方定价为准。

非法律意见声明:本文仅为技术演示,供参考使用。实际部署请咨询 AWS 专业服务或法律顾问,不构成任何承诺。

English summary

This guide shows how to deploy an enterprise agentic retrieval system on a Managed Amazon Bedrock Knowledge Base using the AWS Cloud Development Kit (CDK) in Python. The solution routes questions across two knowledge bases via the AgentCore Gateway, performs multi-turn retrieval through the AgenticRetrieveStream API, and returns grounded, cited answers. Observability is built-in with two CloudWatch dashboards spanning seven telemetry layers plus on-demand and continuous evaluation.

The CDK approach automatically handles cross-stack dependencies, eliminating manual ordering scripts. Ideal for complex queries where a single RAG call falls short, such as financial or weather data processing. Key benefits include zero infrastructure management for storage/indexing, native tool integration, and continuous monitoring of agent behavior, latency, and quality scores.

Clone the official GitHub repo to deploy in one command. Review the full AWS blog for the complete codebase and screenshots. This pattern scales to VPC deployments and additional knowledge bases while keeping costs predictable at idle (only storage).

This is not legal advice. Always verify current pricing and compliance with AWS documentation and your organization’s policies.