人工智能
用 CDK 在管理中的 Amazon Bedrock 知识库上建立代理检索
这个员额用AWS Cloud Development Kit(AWS CDK)在管理下的Amazon Bedrock知识库上建立企业代理检索。 为何这值得建设,大多数RAG系统都以同样的方式开始:它们嵌入了问题,从一个索引中获取最近的块,把它们交给模型,然后返回答案。 一个检索,一个世代。 对于一大批问题来说,这是正确的设计,没有理由再制造更复杂的问题。
以可辨认的方式失败。 问一个答案跨过两个蝎子的问题,单一个检索返回模型需要的一半,这比不返回更糟糕,因为答案来得流利且不完整。 问一个问题,正确的来源取决于问题,一个单一索引没有选择机制。 问一个问题,在看到第一个结果后,需要再看一眼尸体,而第二个样子也无处发生。
企业代理检索通过将推理agent放入循环来回答所有三个。 agent决定是否检索,哪些知识库可以咨询,以及是否在做出答案之前返回更多。 这就是Amazon贝德罗克知识库的发展方向。 有了管理知识库的发布,检索不再是单一的嵌入式和牵引式呼叫。 它已成为一种代理能力:多回合规划、工具驱动检索和基于事实的答案。 Agentic RetrieveStream API提供这个作为头等舱服务功能,而不是你自己组装的东西。 这有效,而且推出也是真正更难操作的问题。 单发管是一种功能,你可以通过读取它来解释。 agent,在循环中解释和检索在每个问题上都走不同的路径。 默认你无法看到它走过的路,也无法看到它所产生的答案是否很好。 观察力不再是一个好人,成为了解系统运作的唯一方法。
2024年8月,使用知识库为Amazon Bedrock和AWS CDK构建端到端的RAG解决方案,将单发设计作为CDK应用程序部署:Amazon简单存储服务(Amazon S3)ecurity,Amazon OpenSearch Serverless collection and Index,一个知识库,以及一个数据源,作为CDK堆提供,其顺序由你按手顺序排列。 出版以来,三件事发生了改变。
- Amazon Bedrock现在提供了一个管理的知识库(Type: MANAGED),拥有摄入,存储,索引和检索本身。 OpenSearch 服务器的收藏和索引显示,邮资已不再是您需要用 OpenSearch 计算单位(OCUs) 创建、规模或支付的东西。
- Amazon Bedrock Agent Core提供代理半,一个将每个知识库暴露为模型上下文协议(MCP)工具的Gateway,一个运行时间,主机推理agent,以及Agentic Retrieve Stream,在内部重新排序,并返回一个合成的,引用的答案。 代理检索仅在管理下的知识库中提供。
- 观察和评价的故事是你可以提供的东西,而不是你锁定的东西。 这个解决方案搭载了两台Amazon云表仪表板,横跨7层遥测仪表板,外加来自同一部署的两种评价形式,即点播式和连续式。
这篇文章构建了这个系统:agent通过两个知识基地,通过Gateway检索,并返回一个被禁的,引用的答案,每层行为都用仪器显示,作为Python的单一AWS CDK应用程序。 一个命令部署全部,CDK获得命令。
如果你读了2024帖子,从Amazon S3桶到知识库的路径会显得熟悉,之后的一切都是新的:矢量商店不见了,检索是代理的,大约一半的应用条款存在来告诉你agent做了什么。 如果你没有
游戏中的服务以及一个问题所经历的路径:agent决定是否检索以及哪些知识库可以查询,Gateway将每个知识库暴露为它可以调用的工具,管理知识库搜索和引用,循环重复的次数是问题需要的倍。 agent在Amazon云表中沿途所做的一切。
背景情况
在走过之前,它有助于建立三件事:什么使得检索代理,为什么管理的知识库是它正确的基础,为什么可观察性和评价属于设计,而不是在后面闪烁。
从 RAG 到企业代理检索
经典检索增强生成(RAG)进行一次检索和一代。 企业代理检索将推理agent放入循环中:它决定是否和什么检索,可以检索几次以提炼,选择哪些知识库是相关的(语义路由),然后只用引用来编曲一个有根据的答案。 有了管理知识库,这是一个一流的服务能力,AgenticRetrieveStream API执行多回合规划,执行检索,并返回一个引用的答案,而不是通过原始矢量搜索来构建和维护一个管弦图层。 这对复杂的问题提出了更好的答案,但也产生了一个更复杂的操作系统,这正是从一开始就建立可观察性和评价性的原因。
与自己的知识基础相比进行管理
Amazon Bedrock现在提供管理知识库(Type:MANAGED):Amazon Bedrock 管理您的摄入,存储,索引和检索,包括默认情况下嵌入和重排服务管理模型,所以没有向量数据库来提供,缩放,或补丁。 这与客户管理的(DIY)知识库不同,您自己选择并运行矢量商店(例如Amazon OpenSearch Serverless).
对于这个解决方案来说,两个差异最为重要:
由于代理检索和代理Core Gateway连接器只在管理的知识库中提供,因此它是企业代理检索的自然基础。 这也意味着Amazon Bedrock为您管理存储和索引,所以在这个设计中没有OpenSearch Serverless收藏或OCU提供能力。 客户管理的选项在您想要选择和运行自己的矢量存储时仍然是正确的选择,例如重新使用现有的 OpenSearch Server Server 收藏或满足特定的数据滞留,调制,或遵守要求。 就企业代理检索而言,管理的知识库是使能力成为可能的因素。
为什么是观察和评价
返回答案的代理系统不足以生产。 你需要看到它的表现(纬度,调用量,符号使用量),检索和答案的好坏程度,你需要连续的这些信号。 这个解决方案搭载了两台云表仪表板,横跨七层遥测,外加两种评价形式(按需和连续),均由同一CDK应用程序提供。 定位、引用和评价层也支持负责任的使用。 被引用的答案可以追溯到他们的源段落,所以答案仍然可以验证,而忠诚和正确的分数让你能够捕捉源头漂移的答案,而不是在生产中发现答案。
为什么用CDK来解决这个问题
任何基础设施现码工具都可以创造这些资源。 CDK在这里赚取其位置是因为这个架构的特定原因:四个堆栈是密集的交叉引用。 网关需要两个知识库。 agent需要网关的URL和ARN. 仪表板需要 KB ids, Gateway ARN,和 agent 运行时 id,七个值跨越堆栈边界,每个值在一个度量符上或一个连接器上的目标上一个维度。
以原始模板写成,即一组手持一致的堆栈导出和交叉堆栈浏览,外加一个按正确顺序部署的脚本和线程向前输出,并同时漂移在任何事物被重新命名的时刻。 在CDK中,您通过构造,导出和依赖边缘来自通过它的行为。 2024年的溶液有3个堆叠,且多为线性依赖链。 在四叠之间有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,块,嵌入物,以及自动索引。 当堆栈部署时,这个问题就会发生,它不是每个问题路径的一部分,因此它被画成一个破碎的箭头,然后,每个问题都遵循步骤1-6:
- 一个用户向Amazon Bedrock Agent Core Runtime上的agent发送一个问题。 Runtime自动装置与OpenTeleometter(OTEL)的全因与动作环路跨度,因此系统从最先调用即可观测到。
agent的推理模型计划和跨越知识基地的路线。 由于每个知识库都作为自己的检索工具被曝光,agent选择其主题与问题,财务或天气相符的工具,而不是搜索一个混合索引。
- 选定的工具呼叫由MCP的代理Core Gateway进行中介,由agent的IAM任务角色授权并与SigV4签署,SigV4援引了该知识库在管理知识库上的代理RetrieveStream API.
- Agentic RetrieveStream从事内基知识的工作:它将问题分解为子库,从Amazon Bedrock管理的数据库(从Amazon S3中的蝎子中获取)反复检索,合成一个被禁的,引用的答案,流回通过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 Atlas. All rights belong to the original author.