hiw3c 日报精选
AWS ML Blog RSS 全文

在亚马逊 bedrock管理知识库中通过表格搜索获取答案

原文标题 · Get answers from tables with tabular search in Amazon Bedrock Managed Knowledge Base

Amazon Bedrock 托管知识库现在支持表格搜索,因此代理式检索可以针对 CSV、XLSX 和 XLS 文件回答问题。它会查找对应值并在所有相关行上执行计算,然后将结果与相关文档中的事实结合,从而回答多步分析性问题。

来源:AWS ML Blog · aws.amazon.com

正文为英文,可一键机器翻译(仅首次需要等待)

Amazon Bedrock Managed Knowledge Base now supports tabular search, so you can get answers from tables through agentic retrieval. When a question needs data from CSV, XLSX, or XLS files, tabular search looks up values and runs calculations across every relevant row in one or more tables or sheets. It then combines the results with facts from related documents to answer analytical questions that take several steps.

A finance team closing the quarter needs to know “Which departments must write variance commentary?” No single file has the answer. It depends on the variance policy’s threshold, the quarter-close memo’s budget revisions, and math over every department in the profit and loss (P&L) workbook.

Like this finance team, most teams keep their narratives and data in the same place. For example, a Microsoft SharePoint site holds workbooks next to policies, and an Amazon Simple Storage Service (Amazon S3) bucket holds CSV exports next to PDFs. Tabular search answers questions across these files through the same AgenticRetrieveStream request you already use for documents. It runs code over the original file in an isolated sandbox, so the answer uses the whole table instead of only the rows that retrieval returns. For the finance team, one request returns the departments that must write commentary. The answer cites the policy and the memo, and the trace names the workbook the code read.

In this post, you learn how tabular search answers questions that need both table data and facts from documents, and when another tool is a better fit. You also build a knowledge base with sample finance files and use the trace to see which file the code read.

Why tables are hard for retrieval

Retrieval Augmented Generation (RAG) returns a fixed number of passages that best match a question, which works well for prose. Tables lose information when they’re split into passages, in ways that depend on the table’s shape:

  • Long tables – A table with thousands of rows spans many passages, and retrieval returns only the few that best match the question. An answer built from them uses a handful of rows, so a total, a comparison, or a threshold check can be incomplete.
  • Wide tables – In a table with hundreds of columns, one row can hold more text than a passage or a search model takes in at once. As a result, values in later columns are harder to match. Long passages from wide tables can also match many unrelated questions and push more relevant passages out of the results.

Retrieval also has to match a question to the right table. Questions use business terms, such as “controllable spend,” but tables hold cost center codes, account names, and numbers. A draft and a final workbook look almost the same to search. The rule that gives the numbers meaning is often in a separate document, such as a policy.

Tabular search therefore splits the work. Retrieval only has to return one passage from the right table file, and code then reads the whole table. Because retrieval still has to find that file first, naming the file in your question helps.

Table 1 shows how plain retrieval and tabular search each handle the finance team’s question, “Which departments must write variance commentary?”

Compared Retrieve only Agentic retrieval with tabular search
What you get back Passages from the policy, memo, business review, and workbook, but no complete answer An answer naming Sales, Engineering, and Operations, with each department’s variance against its revised budget
Revised variance percentages Not available. No file stores them, so no passage can contain them Calculated from every department row in the final workbook
Sources Each passage links to its own file, but none of them contains the answer Citations to the finance policy and the close memo. The trace names the workbook the code read

Table 1: The same question with plain retrieval and with tabular search.

How tabular search works in agentic retrieval

In agentic retrieval, an agent uses retrieval to decide which documents matter and then writes the answer. Tabular search is part of that flow, so there’s nothing to turn on.

When a question needs the data in a table, the request moves through these stages:

  1. Retrieve – Passages come back, including passages from a table file.
  2. Plan – The agent uses the question and table structure to determine whether to run code over one or more retrieved tables.
  3. Stage – Tabular search copies the original file into an isolated sandbox with no network access.
  4. Compute – Model-written code runs over the whole table or sheet.
  5. Return – The agent gets back only what the code prints, such as totals or matching rows.
  6. Answer – The agent answers from the results and the passages, with citations.

Tabular search starts when retrieved passages come from a CSV, XLSX, or XLS file and the question needs a calculation across rows or a lookup of specific values.

Knowledge bases, connectors, and APIs

An AgenticRetrieveStream request can search one knowledge base or several. The Retrieve API keeps returning passages and doesn’t run code.

Tabular search works the same way whichever connector brings the files into the knowledge base, including Amazon S3, Microsoft SharePoint, Atlassian Confluence, and others.

Scope and trade-offs

Tabular search is built for questions that need calculations or lookups over the table files it retrieves. Running code adds at least one more agent turn, so answers that use table data take longer than text-only answers. Governed metrics, recurring dashboards, warehouse-scale joins, and reporting controls still belong in your business intelligence (BI) tools. Tabular search works on table files stored next to your documents, and you don’t define a schema.

Solution overview

In this solution, an Amazon Bedrock Managed Knowledge Base stores your CSV and Excel files together with related documents, such as PDF and Word files. Your application sends every question through agentic retrieval. When a question needs table data, the answer combines calculations over your tables with facts from your documents. It cites the passages it used, and the trace names the table files the code read.

In Figure 1, an Amazon S3 data source syncs these files into the knowledge base, which keeps embeddings, text, metadata, and the original files. When your application sends a question with AgenticRetrieveStream, agentic retrieval plans the work and retrieves passages from the knowledge base. If the question needs data from a table file, it sends the files to read to an isolated sandbox with no network access. There, model-written code runs over the original file and returns printed results. The answer, with citations and trace events, streams back to your application.

Your application, outside the AWS Cloud, sends a question to agentic retrieval and receives an answer with citations and trace events. Inside the AWS Cloud, agentic retrieval contains Retrieval, Planning, and an isolated sandbox. Retrieval gets passages from an Amazon Bedrock Managed Knowledge Base, Planning sends files to read to the sandbox and receives printed results, and the sandbox reads the original file from the knowledge base. Below, an Amazon S3 bucket of table files and documents syncs into the knowledge base.

Figure 1: Table files and documents in an Amazon Bedrock Managed Knowledge Base, with tabular search inside agentic retrieval.

The worked example uses fictitious AnyCompany Retail finance files:

  • Final P&L workbook (anycompany-pnl-fy2026.xlsx). It has Notes, Actuals, Budget, and Summary sheets, with one row per month, cost center, and account in the Actuals and Budget sheets.
  • Draft workbook. A preliminary copy with different Q3 numbers.
  • Finance variance policy (PDF). Defines controllable spend and approved budget, and sets review thresholds.
  • Q3 close memo (Word). Lists approved and rejected budget revisions.
  • Business review (Markdown). Explains the main variances.

These files are in the tabular-search folder of the sample-managed-knowledge-base-patterns GitHub repository. The folder also has an AWS Cloud Development Kit (AWS CDK) app that deploys the resources in this post, and Python scripts that ask these questions.

Prerequisites

You need the following:

  • Permissions to create AWS Identity and Access Management (IAM) roles, S3 buckets, and Amazon Bedrock knowledge bases.
  • For the identity that calls AgenticRetrieveStream, the permissions for agentic retrieval, including bedrock:GetDocumentContent. The agent reads the original table file with the caller’s credentials.
  • Amazon Bedrock in an AWS Region where tabular search is available. For Region availability, see Supported AWS Regions.
  • Your own CSV, XLSX, or XLS files and related documents.
  • To follow the walkthrough: Python 3.10 or later with the AWS SDK for Python (Boto3) version 1.43.123 or later. You can also ask questions from the knowledge base test window in the Amazon Bedrock console or with the AWS Command Line Interface (AWS CLI).

The calling identity needs this policy, from the agentic retrieval documentation:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": "bedrock:AgenticRetrieveStream",
      "Resource": "*"
    },
    {
      "Effect": "Allow",
      "Action": ["bedrock:Retrieve", "bedrock:GetDocumentContent"],
      "Resource": "arn:aws:bedrock:<region>:111122223333:knowledge-base/*"
    },
    {
      "Effect": "Allow",
      "Action": "bedrock:InvokeModelWithResponseStream",
      "Resource": "*"
    }
  ]
}

For costs, see Amazon Bedrock pricing.

Upload your files to Amazon S3

Put the table files and related documents under one prefix.

  1. Open the Amazon S3 console and choose Create bucket.
  2. Enter a globally unique bucket name (this post uses amzn-s3-demo-bucket), keep Block all public access selected, and choose Create bucket.
  3. Choose the bucket, choose Create folder, and enter finance.
  4. Open the finance folder and choose Upload.
  5. Choose Add files, select your table files and documents, and choose Upload.

Create the knowledge base and data source

The knowledge base needs an IAM role that Amazon Bedrock can assume. This trust policy allows only the bedrock.amazonaws.com service principal, and only for knowledge bases in your account.

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "BedrockKnowledgeBaseAssume",
      "Effect": "Allow",
      "Principal": {"Service": "bedrock.amazonaws.com"},
      "Action": "sts:AssumeRole",
      "Condition": {
        "StringEquals": {"aws:SourceAccount": "111122223333"},
        "ArnLike": {
          "aws:SourceArn": "arn:aws:bedrock:<region>:111122223333:knowledge-base/*"
        }
      }
    }
  ]
}

Next, set up the remaining resources:

  1. Create a role named kb-finance-role with the trust policy.
  2. Attach an inline policy that allows s3:ListBucket on the bucket and s3:GetObject on its objects.
  3. Create a knowledge base of type MANAGED with the managed embedding model.
  4. Create a MANAGED_KNOWLEDGE_BASE_CONNECTOR data source for the bucket, limited to the finance/ prefix.
  5. Set maxFileSizeInMegaBytes above your largest file.

The inline policy for step 2 allows reads on this bucket only:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "ListBucket",
      "Effect": "Allow",
      "Action": "s3:ListBucket",
      "Resource": "arn:aws:s3:::amzn-s3-demo-bucket",
      "Condition": {"StringEquals": {"aws:ResourceAccount": "111122223333"}}
    },
    {
      "Sid": "ReadObjects",
      "Effect": "Allow",
      "Action": "s3:GetObject",
      "Resource": "arn:aws:s3:::amzn-s3-demo-bucket/*",
      "Condition": {"StringEquals": {"aws:ResourceAccount": "111122223333"}}
    }
  ]
}

This script runs steps 3 to 5 and prints the IDs:

import time
import boto3

agent = boto3.client("bedrock-agent")

kb = agent.create_knowledge_base(
    name="finance-kb",
    roleArn="arn:aws:iam::111122223333:role/kb-finance-role",
    knowledgeBaseConfiguration={
        "type": "MANAGED",
        "managedKnowledgeBaseConfiguration": {"embeddingModelType": "MANAGED"},
    },
)
kb_id = kb["knowledgeBase"]["knowledgeBaseId"]

# Wait until the knowledge base is ACTIVE before adding a data source.
status = "CREATING"
while status in ("CREATING", "UPDATING"):
    time.sleep(10)
    status = agent.get_knowledge_base(knowledgeBaseId=kb_id)["knowledgeBase"]["status"]
if status != "ACTIVE":
    raise RuntimeError(f"Knowledge base status is {status}")

ds = agent.create_data_source(
    knowledgeBaseId=kb_id,
    name="finance-s3",
    dataSourceConfiguration={
        "type": "MANAGED_KNOWLEDGE_BASE_CONNECTOR",
        "managedKnowledgeBaseConnectorConfiguration": {
            "connectorParameters": {
                "type": "S3",
                "version": "1",
                "connectionConfiguration": {
                    "bucketName": "amzn-s3-demo-bucket",
                    "bucketOwnerAccountId": "111122223333",
                },
                "filterConfiguration": {
                    # In MB. Set this above your largest table.
                    "maxFileSizeInMegaBytes": "200",
                    "inclusionPrefixes": ["finance/"],
                },
                "aclEnabled": False,
            }
        },
    },
    vectorIngestionConfiguration={
        "parsingConfiguration": {"parsingStrategy": "SMART_PARSING"}
    },
)
print("knowledge base:", kb_id, "data source:", ds["dataSource"]["dataSourceId"])

Sync the data source and check file types

Tabular search can start only if the knowledge base assigns the right file type, so check the file type after the first sync.

  1. Start an ingestion job for the data source.
  2. Poll the job until its status is COMPLETE.
  3. Review the job statistics for failed documents.
  4. Run a Retrieve call with your question.
  5. Confirm that table passages show _file_type as CSV (for CSV files) or MS_EXCEL (for XLSX and XLS files).

The first script runs steps 1 to 3:

import time
import boto3

agent = boto3.client("bedrock-agent")
KB_ID, DS_ID = "<your-knowledge-base-id>", "<your-data-source-id>"

job_id = agent.start_ingestion_job(knowledgeBaseId=KB_ID, dataSourceId=DS_ID)[
    "ingestionJob"]["ingestionJobId"]
while True:
    job = agent.get_ingestion_job(
        knowledgeBaseId=KB_ID, dataSourceId=DS_ID, ingestionJobId=job_id
    )["ingestionJob"]
    if job["status"] in ("COMPLETE", "FAILED", "STOPPED"):
        break
    time.sleep(15)
print(job["status"], job.get("statistics"), job.get("failureReasons"))

The second script runs steps 4 and 5 so you can check the table file type before asking the question with agentic retrieval. With your own files, ask a question that pairs a rule in one of your documents with a calculation over your tables, and name both.

import boto3

runtime = boto3.client("bedrock-agent-runtime")
question = (
    "In fiscal Q2 of FY2026, which cost center had the largest overspend in "
    "controllable spend, as the finance policy defines it? Compare the Actuals "
    "and Budget sheets of the FY2026 P&L workbook and give the cost center and "
    "the overspend in US dollars."
)
response = runtime.retrieve(
    knowledgeBaseId="1234567890",
    retrievalQuery={"text": question},
    retrievalConfiguration={"managedSearchConfiguration": {"numberOfResults": 10}},
)
for result in response["retrievalResults"]:
    print(result["metadata"].get("_file_type"), result.get("documentId"))

Ask a question across tables and documents

Send the same question through AgenticRetrieveStream. The script prints the answer, each CodeExecution status, and the files the code read.

import boto3

runtime = boto3.client("bedrock-agent-runtime")
question = (
    "In fiscal Q2 of FY2026, which cost center had the largest overspend in "
    "controllable spend, as the finance policy defines it? Co