人工智能
用 CDK 在管理中的 Amazon Bedrock 知识库上建立代理检索
使用AWS Cloud Development Kit(AWS CDK),研究为何值得建造,大多数RAG系统都以同样的方式开始:它们嵌入了问题,从一个索引取出最接近的块,把它们交给一个模型,然后返回答案。 一个检索,一个世代。 对于一个大类的问题来说,这是正确的设计,并且没有理由制造更复杂的问题。
它的失败是显而易见的。 问一个答案跨出两个Corpora的问题,一个检索返回模型需要的一半,这比不返回更糟糕,因为答案来得流利而不完整。 问一个问题,右出处取决于问题,单一索引没有选择机制。 问一个问题,在看到第一个结果后需要再看一眼尸体,也没有第二个结果。
企业代理检索通过将推理agent放入回路中来回答所有三个。 agent决定是否取回,哪些知识库需要咨询,以及是否在做出答案之前返回更多的知识库。 这就是Amazon Bedrock知识库的发展方向。 与"管理知识基地"的发布相接后,检索不再是一通嵌入式和相接式的呼叫。 它已成为一种代理能力:多回合规划、由工具驱动的检索、以及基于事实的答案。 Agentic RetrieveStream API提供这个作为一等服务功能,而不是你自己组装的东西。 这行得通,推出也是一个真正更难操作的问题。 单发管是一种功能,你可以通过读取它来解释。 agent在循环中解释和检索在每个问题上都走不同的路径。 默认,你无法看到它走过的路,也无法看到它所产生的答案是否很好。 观察力不再是一个好人,成为了解系统运作的唯一方法。
2024年8月,使用知识库为Amazon Bedrock和AWS CDK构建一端到端的RAG解决方案,将单发设计作为CDK应用程序部署:Amazon简单存储服务(Amazon S3)ecurity,Amazon OpenSearch Serverles Collection and Index,一个知识库,以及一个数据源,作为CDK堆提供,其顺序由你按手顺序排列。 出版后有三事有变。
- Amazon Bedrock现在提供了一个管理下的知识库(Type: MANAGED),拥有摄取,存储,索引和检索本身。 OpenSearch 服务器收藏和索引显示,邮资已不再是您需要用 OpenSearch 计算单位(OCU) 创建、规模或付费的东西。
- Amazon Bedrock Agent Core提供代理半,一个将每个知识库暴露为"模型背景协议"(MCP)工具的Gateway,一个存储推理agent的运行时间,以及AgenticRetrieve Stream,它内部重新排序并返回一个被合成的被引用的答案。 代理检索只在管理下的知识库中提供。
- 观察和评价的故事是你可以提供的东西,而不是你锁定的东西。 这个解决方案搭载了两台Amazon Cloud Watch仪表板,横跨了七层遥测,加上来自同一部署的两种评价形式,即点播和连续。
这篇文章构建了这个系统:agent通过两个知识基地,通过"Gateway"检索,并返回一个被基于,被引用的答案,其行为的每层都有仪器,作为Python的单一AWS CDK应用程序。 一个命令部署全部,CDK获得命令。
如果您读取了2024个帖子,从Amazon S3桶到知识库的路径会显得很熟悉,之后的一切都是新的:矢量商店已经走光了,检索是代理的,大约一半的应用条款存在来告诉你agent做了什么。 若你没有
游戏中的服务以及一个问题所经历的路径:agent决定是否检索以及哪些知识库可以查询,"网关"将每个知识库暴露为它可以调用的工具,"管理的知识库"搜索并引用,循环重复的次数是问题需要的多倍。 agent在Amazon Cloud Watch中沿途所做的一切。
背景情况
在行走之前,它有助于建立三件事:是什么使得检索代理,为什么管理的知识库是它是正确的基础,为什么可观察性和评价属于设计而不是在后期被栓上。
从RAG到企业代理检索
经典检索增强生成(RAG)进行一检索和一代。 企业代理检索将一个推理agent放入了循环中:它决定了是否和什么检索,可以检索几次来精炼,选择哪些知识基础是相关的(语义路由),然后才用引用来作一个有根据的答案。 有了"管理知识库",这是一个一等的服务能力,AgenticRetrieveStream API执行多回合规划,执行检索,并返回被引用的答案,而不是通过原始矢量搜索来构建和维护的管弦系统层。 这对复杂的问题产生更好的答案,但也产生一个更复杂的操作系统,这正是从一开始就建立可观察性和评价性的原因。
与自己知识库相比的管理
Amazon Bedrock现在提供管理知识库(Type:MANAGED):Amazon Bedrock 管理您的摄入,存储,索引和检索,包括默认情况下嵌入和重排服务管理模型,因此没有向量数据库来提供,缩放,或补丁。 这与客户管理的(DIY)知识库不同,您自己选择并运行了向量商店(例如Amazon OpenSearch Serverless).
两种差异对于这个解决方案最为重要:
由于代理检索和代理Core网关连接器只在管理的知识库中提供,因此它是企业代理检索的自然基础。 这也意味着Amazon Bedrock为您管理存储和索引,所以在这个设计中没有OpenSearch Serverless收藏或OCU能力提供。 客户管理选项在您想要选择和运行自己的向量存储时仍然是正确的选择,例如重新使用现有的 OpenSearch 服务器集,或者满足特定的数据滞留,调取,或遵守要求。 就企业代理检索而言,管理的知识库是使能力成为可能的因素。
为什么观察和评价
返回答案的代理系统不足以生产。 你需要看到它的表现方式(纬度,调用音量,符号用法),检索和答案的好坏程度,你需要连续的这些信号。 这个解决方案搭载了两个云表仪表板,跨越了七层遥测,外加两种评价形式(按需和连续),都由同一种CDK应用程序提供。 定位、引用和评价层也支持负责任的使用。 被引用的答案可以追溯到源的段落,所以答案仍然可以验证,而忠诚和正确的分数让你能够捕捉从源被漂移的答案,而不是在生产中发现。
为何要用CDK来解决这个问题
任何基础设施即用代码工具都可以创建这些资源。 CDK在这里获得其位置是因为这个架构的特定原因:这四个堆栈被密集地交叉引用。 网关需要两个知识库。 agent需要网关的URL和ARN. 仪表板需要KB ids,Gateway ARN,和 agent 运行时间 id,7个值跨越堆栈边界,每个值在一个度量器或连接器上的目标上一个维度。
被写成原始模板,即一组手持一致的堆栈导出和交叉堆栈取景,加上一个按正确顺序部署并线程输出向前的脚本,并同时漂移在任何事物被重新命名的时刻。 在CDK中,您通过构造,导出和依赖边缘来自通过它的行为。 2024年的溶液有三叠相并多为线性依赖链。 在有7个值的四叠堆之间,差异不再具有风格性。
解决办法概述
以下是一个问题通过系统,结束到结束的路径。 因为其他的邮递员从左到右走过。
以下图可追溯到端到端的代理检索工作流程。 一个用户的提问进入了Agent Core Runtime上的agent,通过Agent Core Gateway将它传送到两个Agentic Retrieve Stream工具中的一个,每个管理的知识库一个,财务和天气,每个工具从Amazon S3中摄取。 所选工具返回一个被禁用,被引用的答案,以及每个步骤流到Amazon Cloud Watch和AWS X-Ray. 编号的徽章映射到下面的行走。
图中到工作流程的编号步骤如下。
第0步——一次性部署。 在问到任何问题之前,Amazon S3中的每个实体都被吸收到自己的管理知识库中:Amazon Bedrock parses,块,嵌入物,并自动索引。 当堆栈部署时,这个问题就会发生,它不是每个问题路径的一部分,所以它被画成一分箭头和标记步骤 0。 然后,每个问题都遵循步骤1-6:
- 一个用户向Amazon Bedrock Agent Core Runtime上托管的agent发送了问题。 Runtime自动装置与OpenTeleometry(OTEL)的全因与动作回路相通,因此系统从最先调用时就可观察到了。
agent的推理模型计划和跨越知识基地的路线。 由于每个知识库都作为自己的检索工具被曝光,agent选择了与问题,财务或天气相匹配的工具,而不是搜索一个混合索引。
- 选定的工具呼叫由代理Core Gateway在MCP上进行中介,由agent的IAM任务角色授权,并与SigV4签署,SigV4援引了该知识库在管理的知识库上的AgenticRetrieveStream API.
—— Agentic Retrieve Stream 做内部知识基础的工作:它将问题分解为子库,从Amazon Bedrock管理的数据库中反复取回(取自Amazon S3中的Corpora),并合成一个被基于,被引用的答案,流回通过Gateway.
- agent检查返回的上下文是否足够。 如果不是,它会循环,回路和检索,然后编组最后的答案。 如果是,则返回引用的答案给用户。
- 在整个期间,运行时向Amazon Cloud Watch和AWS X-Ray发出信号、信号计数和度量表,充斥7个可观察层,并输入点播和连续评价的分数。
内部,该工作流程是一个CDK应用程序,app.py,宣布了四堆。 您不选择顺序,CDK 来源于您通过的构造引用:
这就是整条线。 这4个建筑师电话是用来取代进出口簿记和部署脚本的。
agent运行一个有原因而有作用的循环:它认为(一个大型语言模型(LLM)调用),决定哪个KB工具可以调用,读取通过"Gateway"返回的内容,并经常在编集其最终被引用的答案之前再次取回。 每一步由"跑道"自动接通,因此7个可观察层从实际流量中填充。
七层。 L1 KB-内置度量衡,L2摄入,L3代理取回质量,L4 Gateway/MCP 度量衡,L5 OTEL跨树,L6符号用量,和L7评分。 第1、第4和第5层自动发射。 第3,6和7层由驱动程序笔记本作为自定义的度量衡发布。
所有可捕鲸动物都生活在
本文由 AI宝库自动抓取并翻译,版权归原作者所有;如需查看原文请点下方链接。
Artificial Intelligence
Build agentic retrieval on a Managed Amazon Bedrock Knowledge Base with CDK
This post builds enterprise agentic retrieval on a Managed Amazon Bedrock Knowledge Base with the AWS Cloud Development Kit (AWS CDK). To see why that’s worth building, it helps to start with where ordinary Retrieval Augmented Generation (RAG) breaks down. Most RAG systems start the same way: they embed the question, fetch the nearest chunks from one index, hand them to a model, and return the answer. One retrieval, one generation. For a large class of questions that’s the right design, and there’s no reason to build anything more complicated.
It fails in a recognizable way. Ask a question whose answer spans two corpora, and a single retrieval returns half of what the model needs, which is worse than returning nothing, because the answer comes back fluent and incomplete. Ask a question where the right source depends on the question, and a single index has no mechanism to choose. Ask a question that needs a second look at the corpus after seeing the first result, and there is nowhere for that second look to happen.
Enterprise agentic retrieval answers all three by putting a reasoning agent in the loop. The agent decides whether to retrieve, which knowledge base to consult, and whether to go back for more before it composes an answer. This is the direction Amazon Bedrock Knowledge Bases have evolved. With the launch of the Managed Knowledge Base, retrieval is no longer a single embed-and-fetch call. It has become an agentic capability: multi-turn planning, tool-driven retrieval, and grounded, cited answers. The AgenticRetrieveStream API offers this as a first-class service feature rather than something you assemble yourself. That works, and it also introduces a genuinely harder operational problem. A single-shot pipeline is a function you can reason about by reading it. An agent that reasons and retrieves in a loop takes a different path on every question. By default you can see neither the path it took nor whether the answer it produced was any good. Observability stops being a nice-to-have and becomes the only way to know the system works.
In August 2024, Build an end-to-end RAG solution using Knowledge Bases for Amazon Bedrock and the AWS CDK deployed the single-shot design as a CDK application: an Amazon Simple Storage Service (Amazon S3) corpus, an Amazon OpenSearch Serverless collection and index, a knowledge base, and a data source, provisioned as CDK stacks whose order you sequenced by hand. Three things have changed since it was published.
- Amazon Bedrock now offers a managed knowledge base (Type: MANAGED), which owns the ingestion, storage, indexing, and retrieval itself. The OpenSearch Serverless collection and index that post provisions are no longer something you need to create, scale, or pay for with OpenSearch Compute Units (OCUs).
- Amazon Bedrock AgentCore provides the agentic half, a Gateway that exposes each knowledge base as a Model Context Protocol (MCP) tool, a Runtime that hosts the reasoning agent, and AgenticRetrieveStream, which reranks internally and returns a synthesized, cited answer. Agentic retrieval is available only on the Managed Knowledge Base.
- The observability and evaluation story is now something you can provision, not something you bolt on. This solution ships two Amazon CloudWatch dashboards spanning seven layers of telemetry, plus two forms of evaluation, on-demand and continuous, from the same deployment.
This post builds that system: an agent that routes across two knowledge bases, retrieves through the Gateway, and returns a grounded, cited answer, with every layer of its behavior instrumented, as a single AWS CDK application in Python. One command deploys all of it, and CDK derives the order.
If you read the 2024 post, the path from an Amazon S3 bucket to the knowledge base will look familiar, and everything after it is new: the vector store is gone, the retrieval is agentic, and roughly half of what the app provisions exists to tell you what the agent did. If you didn’t, this post stands on its own.
The services at play and the path one question takes through them: the agent decides whether to retrieve and which knowledge base to consult, the Gateway exposes each knowledge base as a tool it can call, the Managed Knowledge Bases search and cite, and the loop repeats as many times as the question needs. Everything the agent does along the way lands in Amazon CloudWatch.
Background
Before the walkthrough, it helps to establish three things: what makes retrieval agentic, why the Managed Knowledge Base is the right foundation for it, and why observability and evaluation belong in the design rather than bolted on later.
From RAG to enterprise agentic retrieval
Classic Retrieval Augmented Generation (RAG) does one retrieval and one generation. Enterprise agentic retrieval puts a reasoning agent in the loop: it decides whether and what to retrieve, can retrieve several times to refine, chooses which knowledge base is relevant (semantic routing), and only then composes a grounded answer with citations. With the Managed Knowledge Base, this is a first-class service capability, the AgenticRetrieveStream API performs the multi-turn planning, executes the retrieval, and returns a cited answer, rather than an orchestration layer you build and maintain over a raw vector search. That produces better answers on complex questions, but it also produces a more complex system to operate, which is exactly why observability and evaluation are built in from the start.
Managed compared to do-it-yourself Knowledge Bases
Amazon Bedrock now offers a Managed Knowledge Base (Type: MANAGED): Amazon Bedrock manages the ingestion, storage, indexing, and retrieval for you, including embedding and reranking with service-managed models by default, so there is no vector database to provision, scale, or patch. This differs from a Customer-managed (DIY) Knowledge Base, where you choose and operate the vector store (for example, Amazon OpenSearch Serverless) yourself.
Two differences matter most for this solution:
Because agentic retrieval and the AgentCore Gateway connector are available only on the managed knowledge base, it is the natural foundation for enterprise agentic retrieval. It also means Amazon Bedrock manages the storage and indexing for you, so there is no OpenSearch Serverless collection or OCU capacity to provision in this design. The customer-managed option remains the right choice when you want to select and operate your own vector store, for example to reuse an existing OpenSearch Serverless collection or to meet specific data-residency, tuning, or compliance requirements. For enterprise agentic retrieval, the managed knowledge base is what makes the capability possible.
Why observability and evaluation
An agentic system that returns an answer isn’t enough for production. You need to see how it behaves (latency, call volume, token usage), how good the retrieval and answers are, and you need those signals continuously. This solution ships two CloudWatch dashboards spanning seven layers of telemetry, plus two forms of evaluation (on-demand and continuous), all provisioned by the same CDK application. The grounding, citations, and evaluation layers also support responsible use. Cited answers trace back to their source passages so responses stay auditable, and the faithfulness and correctness scores let you catch answers drifting from their sources rather than finding out in production.
Why CDK for this particular solution
Any infrastructure-as-code tool can create these resources. CDK earns its place here for a reason specific to this architecture: the four stacks are densely cross-referenced. The Gateway needs both knowledge base ids. The agent needs the URL and ARN for the Gateway. The dashboards need the KB ids, the Gateway ARN, and the agent runtime id, seven values crossing stack boundaries, each one a dimension on a metric or a target on a connector.
Written as raw templates, that is a set of stack exports and cross-stack lookups kept in agreement by hand, plus a script that deploys in the right order and threads outputs forward, and both drift the moment anything is renamed. In CDK you pass the construct, and the export and the dependency edge are derived from that one act of passing it. The 2024 solution had three stacks and a mostly linear dependency chain. At four stacks with seven values crossing between them, the difference stops being stylistic.
Solution overview
Here is the path a single question takes through the system, end to end. It’s worth a slow look, because the rest of the post walks through it left to right.
The following diagram traces the end-to-end agentic retrieval workflow. A user’s question enters the agent on the AgentCore Runtime, which routes it through the AgentCore Gateway to one of two AgenticRetrieveStream tools, one per managed knowledge base, financial and weather, each ingested from Amazon S3. The selected tool returns a grounded, cited answer, and every step streams to Amazon CloudWatch and AWS X-Ray. The numbered badges map to the following walkthrough.
The numbered steps in the diagram map to the workflow, which is as follows.
Step 0 — one-time, at deploy. Before any question is asked, each corpus in Amazon S3 is ingested into its own Managed Knowledge Base: Amazon Bedrock parses, chunks, embeds, and indexes it automatically. This happens once when the stacks deploy, it isn’t part of the per-question path, so it is drawn as a dashed arrow and marked step 0. Every question then follows steps 1–6:
- A user sends a question to the agent hosted on the Amazon Bedrock AgentCore Runtime. The Runtime auto-instruments the whole reason-and-act loop with OpenTelemetry (OTEL) spans, so the system is observable from the very first call.
- The agent’s reasoning model plans and routes across knowledge bases. Because each knowledge base is exposed as its own retrieval tool, the agent selects the tool whose topic matches the question, financial or weather, rather than searching one blended index.
- The selected tool call is brokered by the AgentCore Gateway over MCP, authorized by the agent’s IAM task role and signed with SigV4, which invokes that knowledge base’s AgenticRetrieveStream API on the Managed Knowledge Base.
- AgenticRetrieveStream does the within-knowledge-base work: it decomposes the question into sub-queries, retrieves iteratively from the data store that Amazon Bedrock manages (ingested from the corpora in Amazon S3), and synthesizes a grounded, cited answer that streams back through the Gateway.
- The agent checks whether the returned context is sufficient. If not, it loops, routing and retrieving again, before composing its final answer. If so, it returns the cited answer to the user.
- Throughout, the Runtime emits spans, token counts, and metrics to Amazon CloudWatch and AWS X-Ray, populating the seven observability layers and feeding both the on-demand and continuous evaluation scores.
Internally, that workflow is one CDK app, app.py, that declares four stacks. You don’t choose the order, CDK derives it from the construct references you pass:
That is the whole wiring. Those four constructor calls are what replace the export/import bookkeeping and the deploy script.
The agent runs a reason-and-act loop: it thinks (a large language model (LLM) call), decides which KB tool to call, reads what came back through the Gateway, and often retrieves again before composing its final, cited answer. Every step is auto-instrumented by the Runtime, so the seven observability layers fill from real traffic.
The seven layers. L1 KB-native metrics, L2 ingestion, L3 agentic retrieval quality, L4 Gateway/MCP metrics, L5 OTEL span tree , L6 token usage, and L7 evaluation scores. Layers 1, 4, and 5 are emitted automatically. Layers 3, 6, and 7 are published as custom metrics by the driver notebook.
Everything tunable lives in
Auto-fetched and translated by AI Gem Box. All rights belong to the original author.