读见

从文件到带引用的答案,只需一次 API 调用:Agent Retrieval 中的托管式摄取与 SearchDocuments

原题From Files to Cited Answers in One API Call: Managed Ingestion and SearchDocuments in Agent Retrieval

约 7 分钟约 1,684 字原文英文已翻译

读前速览

3 个要点

本文介绍了 Google 在 Agent Retrieval 中推出的托管式摄取和 SearchDocuments 功能,开发者无需构建自定义摄取流水线,只需一次 API 调用,就能搜索 PDF、Office 文件和视频,并获得带引用的结果。

  1. 托管式摄取可自动完成文档和视频处理的整个流程

    开发者无需手动解析文件、拆分文本块、逐块生成嵌入并写入向量数据库,只需将数据存储指向 Cloud Storage 中的文件,服务便会自动完成解析、分段、使用 Gemini Embedding 2 生成嵌入,并创建向量 Collection。

  2. SearchDocuments 通过一次 API 调用返回带引用的结果

    SearchDocuments API 只需接收一个数据存储和文本查询,即可返回匹配段落、源文件 URI,以及文档的页码范围或视频的起止时间戳。智能体因此能够引用准确来源,无需管理 Collection 架构或向量字段。

  3. 启用音频的视频搜索无需转录文本,也能匹配语音内容

    在视频处理过程中启用音频嵌入后,即使画面呈现的内容与语音不同,查询也能匹配到说话者提及某个主题的时刻,无需手动转录或添加字幕。

完整摘要

本文介绍了 Google 在 Agent Retrieval 中推出的托管式摄取和 SearchDocuments 功能,说明这些功能如何让开发者无需构建自定义摄取流水线,即可搜索文档和视频。托管式摄取会使用 Gemini Embedding 2,自动解析、分段并嵌入来自 Cloud Storage 的文件;SearchDocuments 则通过一次 API 调用返回匹配段落及引用信息,包括文档页码和视频时间戳。系统支持混合搜索(语义搜索 + 全文搜索)、可选的重排序,以及感知音频的视频搜索。测试显示,18 份 Alphabet 投资者 PDF 在约 9 分钟内即可完成索引并支持搜索;针对 19 分钟的视频,每次查询返回结果仅需 0.3-0.5 秒。本文还提供了创建数据存储、导入文件,以及使用筛选条件和重排序进行搜索的详细实现示例。

目录 · 15 节

查看原文
header_en_1600x900

Agent Retrieval 中全新托管式数据摄取和 SearchDocuments 功能指南:无需编写数据摄取管道,即可搜索 PDF、Office 文件和视频。A guide to the new managed ingestion and SearchDocuments features in Agent Retrieval, for searching PDFs, Office files, and videos without writing an ingestion pipeline.

在智能体能够回答有关你的 PDF 的问题之前,必须先有人搭建好这套流水线:解析每个文件,将其切分为多个块,为每个块生成嵌入向量,将这些块写入向量数据库,然后熟悉该数据库的架构,以便查询。视频处理则更复杂:转码文件,将其剪辑成片段,为各帧生成嵌入向量,并记录每个片段来自哪个视频的第几秒。Before an agent can answer a question about your PDFs, someone has to build the pipeline: parse every file, split it into chunks, embed each chunk, write the chunks to a vector database, and then learn that database's schema to query it. For video it gets harder: transcode the files, cut them into clips, embed the frames, and keep track of which second of which video each clip came from.

before_en
Before: every step of the pipeline is custom code you write and maintain, separately for documents and for videos.

其中大部分代码都与你的应用无关。Agent Retrieval(原名 Vector Search 2.0)最近新增的两项功能目前已开放 Public Preview,可替你省去这些工作。托管式数据摄取会为你构建整个流水线:将数据存储指向 Cloud Storage 中的文件,服务便会使用 Gemini Embedding 2 对文件进行解析、分段和嵌入。第二项新功能 SearchDocuments 可通过一次 API 调用搜索数据存储,并返回匹配的段落及其页码或时间戳。在我们的测试中,18 份 Alphabet 投资者 PDF 从公开的 Cloud Storage 存储桶变为可搜索且带页码引用的结果,仅用了约 9 分钟;19 分钟的视频则能同时根据画面和语音内容进行搜索,每次查询耗时 0.3 到 0.5 秒,而且无需编写解析、分块、转录或嵌入代码。Most of that code has nothing to do with your application. Two features recently added to Agent Retrieval (formerly Vector Search 2.0), now in Public Preview, take that work off your hands. Managed ingestion builds the pipeline for you: point a data store at files in Cloud Storage, and the service parses, segments, and embeds them with Gemini Embedding 2. SearchDocuments, the second new feature, then searches the data store with one API call and returns the matching passages with their page numbers or timestamps. In our tests, 18 Alphabet investor PDFs went from a public Cloud Storage bucket to searchable, page cited results in about 9 minutes, and 19 minutes of video became searchable by both visuals and spoken audio in 0.3 to 0.5 seconds per query, with no parsing, chunking, transcription, or embedding code.

本指南介绍如何创建数据存储、导入文档和视频,并通过筛选、重排序和支持音频的视频搜索来检索内容。This guide covers how to create a data store, import documents and videos, and search them with filters, reranking, and audio-aware video search.

托管式摄取和 SearchDocuments 的功能What Managed Ingestion and SearchDocuments Enable

after_en
After: create separate data stores for documents (UNSTRUCTURED) and videos (VIDEO), point each at Cloud Storage, and search each with one SearchDocuments call that returns cited results.

将托管式数据摄取与 SearchDocuments 配合使用,可立即获得以下几项优势:Pairing managed ingestion with SearchDocuments gives you several immediate benefits:

  • 无需摄取管道:直接从云存储导入 PDF、DOCX、PPTX、XLSX 和 TXT 文件,或 MP4、MOV、WebM、MKV 等视频格式。解析、分段、嵌入,以及向量集合及其架构,均由系统为您处理。
    No Ingestion Pipeline: Import PDF, DOCX, PPTX, XLSX, and TXT files, or MP4, MOV, WebM, MKV, and other video formats, straight from Cloud Storage. Parsing, segmentation, embedding, and the vector Collection and its schema are handled for you.
  • 带引用的答案:每个结果都会附带其源文件 URI,以及文档的页码范围或视频的起止时间,让你的智能体能够准确引用答案的出处。
    Answers With Citations: Every result carries its source file URI and a page range for documents, or a start and end time for videos, so your agent can cite exactly where an answer came from.
  • 一次调用即可搜索:SearchDocuments 接受一个数据存储和文本查询。你无需传入集合名称、向量字段、任务类型或输出字段,系统会自动对文档执行混合搜索(语义搜索 + 全文搜索)。
    One Call to Search: SearchDocuments takes a data store and a text query. You don't pass a Collection name, a vector field, a task type, or output fields, and hybrid search (semantic + full-text) runs automatically for documents.
  • 搜索视频中的语音内容:开启音频嵌入后,即使画面显示的是其他内容,查询也能匹配到说话者提及某个主题的时刻。无需转录文本或字幕。
    Search What Was Said in Videos: Turn on audio embedding and a query matches the moment a speaker mentions a topic, even when the picture shows something else. No transcript or captions required.

基准:文档搜索通常需要做什么The Baseline: What Document Search Usually Takes

Agent Retrieval Collections 已经可以让你针对自己的数据对象进行语义搜索、文本搜索和混合搜索。但这些数据对象需要由你自行生成,而且要查询它们,你还得了解它们的存储方式。让数据存储来处理这些工作后,会有哪些变化:Agent Retrieval Collections already give you semantic, text, and hybrid search over your own data objects. But you have to produce those data objects yourself, and to query them you need to know how they are stored. Here is what changes when you let a data store do the work:

table1_managed_ingestion_4x3

底层的 Collection 仍然是一个常规的 Agent Retrieval Collection,因此在需要完全掌控时,你可以随时直接查询它。The backing Collection is still a regular Agent Retrieval Collection, so you can query it directly whenever you need full control.

步骤 1:创建数据存储Step 1: Create a Data Store

数据存储区保存你的文件,以及控制文件解析和分段方式的配置。目前,数据存储区和 SearchDocuments API 通过 v1beta 版 REST 接口提供服务,因此示例使用来自 google-auth 的授权会话:A data store holds your files and the configuration that controls how they are parsed and segmented. The data store and SearchDocuments APIs are currently served over REST at v1beta, so the examples use an authorized session from google-auth:

import google.auth
from google.auth.transport.requests import AuthorizedSession
credentials, _ = google.auth.default(
    scopes=["https://www.googleapis.com/auth/cloud-platform"]
)
session = AuthorizedSession(credentials)
PARENT = "https://vectorsearch.googleapis.com/v1beta/projects/PROJECT_ID/locations/us-east4"
session.post(
    f"{PARENT}/dataStores",
    params={"data_store_id": "investor-reports"},
    json={
        "document_type": "UNSTRUCTURED",
        "processing_config": {
            "unstructured_processing_config": {
                # One segment per page, so results carry page numbers
                "segmentation_config": {"page_segmentation_config": {}},
                "parsing_config": {"digital_parsing_config": {}},
            }
        },
        # Metadata you can filter on and return in search
        "document_metadata_schema": {
            "schema": {
                "type": "object",
                "properties": {
                    "doc_type": {"type": "string"},
                    "year": {"type": "integer"},
                },
            }
        },
    },
)
JSON

有两种可用的分段模式。分块分段(默认模式)会将文本拆分为 100 到 500 个 token 的文本块,并支持布局解析,同时检测表格和标题。页面分段则为每页生成一个片段。对于 PDF 等分页文档,两种模式都会记录页码(startPage 和 endPage),便于引用页面或按页码范围筛选:页面分段会让每个片段对应单独一页,而分块分段会记录每个文本块跨越的页码范围。Two segmentation modes are available. Chunk segmentation (the default) splits text into chunks of 100 to 500 tokens and works with layout parsing, which also detects tables and headings. Page segmentation produces one segment per page. For paginated documents such as PDFs, both modes record page numbers (startPage and endPage) so you can cite pages or filter by page range: page segmentation returns a single page per segment, while chunk segmentation returns the page range each chunk spans.

步骤 2:导入文件Step 2: Import Your Files

使用 URI 模式将导入指向你的文件,例如 gs://my-bucket/reports/*.pdf;如果想自行设置文档 ID 并附加元数据,则可使用 JSONL 清单:Point an import at your files with URI patterns such as gs://my-bucket/reports/*.pdf, or with a JSONL manifest when you want to set your own document IDs and attach metadata:

{"name": "projects/PROJECT_ID/locations/us-east4/dataStores/investor-reports/documents/2023-q4-earnings-release", "content": {"gcsUri": "gs://my-bucket/reports/2023q4-earnings-release.pdf"}, "metadata": {"value": {"doc_type": "earnings_release", "year": 2023}}}
JSON
session.post(
    f"{PARENT}/dataStores/investor-reports:importDocuments",
    json={
        "gcs_source": {
            "input_uris": ["gs://my-bucket/manifests/docs.jsonl"],
            "source_format": "SOURCE_FORMAT_DOCUMENT_JSON",
        },
        "import_output_config": {"error_gcs_prefix": "gs://my-bucket/import-errors/run-1/"},
    },
)
JSON

导入过程以长时间运行的操作执行。重新导入相同的文件会更新现有文档,而不会创建重复项;内容未发生变化的文件则会被跳过。The import runs as a long-running operation. Re-importing the same files updates existing documents instead of duplicating them, and files whose content didn't change are skipped.

步骤 3:一次调用完成搜索Step 3: Search With One Call

SearchDocuments 只需要数据存储和文本查询:SearchDocuments needs only the data store and a text query:

response = session.post(
    f"{PARENT}/dataStores/investor-reports/documents:search",
    json={
        "text_query": {"text": "How much did Google Cloud revenue grow?"},
        "top_k": 5,
    },
).json()
JSON

每个结果都是文档的一个片段,包含文本、页码和源文件:Each result is a segment of a document, with the text, the page, and the source file:

{
  "document": {
    "name": ".../dataStores/investor-reports/documents/2022-annual-report",
    "content": {"gcsUri": "gs://.../2022_alphabet_annual_report.pdf"}
  },
  "unstructuredDetails": {
    "contentSnippet": "... Google Cloud revenues increased $7.1 billion from 2021 to 2022 ...",
    "pageRange": {"startPage": "68", "endPage": "68"}
  }
}
JSON

实际运作方式How It Works in Practice

  1. 混合检索:SearchDocuments 会在底层 Collection 上执行语义检索和全文检索,并融合两份结果列表,因此改述的问题和精确术语都能匹配。
    Hybrid Retrieval: SearchDocuments runs a semantic search and a full-text search over the backing Collection and fuses the two result lists, so both paraphrased questions and exact terms match.
  2. 分段级结果:结果是页面或内容块,而非整个文件。多个结果可能来自同一份文档,每个结果都有各自的页码范围。
    Segment-Level Results: Results are pages or chunks, not whole files. Several results can come from the same document, each with its own page range.
  3. 可选重排序:启用重排序后,SearchDocuments 会检索多于 top_k 个候选项,并使用与 Agent Retrieval 的 VertexRanker 相同的 semantic-ranker-fast@latest 模型,通过 Ranking API 对其重新排序。
    Optional Reranking: With reranking on, SearchDocuments retrieves more candidates than top_k and reorders them with the Ranking API, using the same semantic-ranker-fast@latest model as Agent Retrieval's VertexRanker.

使用筛选器缩小结果范围Narrowing Results With Filters

传入筛选器,仅搜索匹配的片段;使用与结果中读回的相同路径:Pass filter to search only the segments that match, using the same paths you read back in results:

table2_search_filters_4x3

你还可以传入 document_ids,仅在特定文件中搜索;也可以传入 output_fields.metadata,在返回结果时附上每个文档的元数据。在我们的测试中,使用第 1 页筛选条件搜索“What did the CEO say about AI?”,返回了五份不同财报新闻稿标题页上的 CEO 引述。You can also pass document_ids to search only inside specific files, and output_fields.metadata to return each document's metadata with its results. In our test, searching "What did the CEO say about AI?" with the page-1 filter returned the CEO quotes from the headline page of five different earnings releases.

只需一个开关,排名更靠前Better Ranking With One Toggle

"ranking_spec": {"reranking_spec": {"enabled": True}}
JSON

对于“Alphabet 有多少名员工?”,普通混合搜索返回的前五条结果中有两页资产负债表,完全没有提到员工。启用重排序后,所有排名靠前的结果都给出了员工人数:年报中的“Alphabet 有 190,234 名员工”,以及季度补充表格中的“员工人数”一行。For "How many employees does Alphabet have?", plain hybrid search returned two balance sheet pages among the top five, which don't mention employees at all. With reranking on, every top result reported the headcount: the annual report's "Alphabet had 190,234 employees" and the "Number of employees" row of the quarterly supplemental tables.

在视频中搜索Searching Inside Videos

视频的处理方式相同:创建一个 document_type: VIDEO 的数据存储,导入文件,然后调用 SearchDocuments。该服务会对每个视频进行转码,将其拆分为固定时间窗口,并为每个窗口生成嵌入向量:Video works the same way: create a data store with document_type: VIDEO, import the files, and call SearchDocuments. The service transcodes each video, splits it into fixed windows, and embeds each window:

"document_type": "VIDEO",
"processing_config": {
    "video_processing_config": {
        "segmentation_config": {"fixed_window_config": {"segment_duration": "20s"}},
        "embedding_config": {"audio_mode": "ENABLED"},
    }
},
JSON

每个结果都附带其匹配时的时间点:Each result carries the moment it matched:

"videoDetails": {"startOffset": "60s", "endOffset": "80s"}
JSON

我们导入了五段 Google 示例视频(总时长约 19 分钟),并通过场景描述进行搜索。“机器正在分拣黄瓜”返回了机器学习视频中的 03:20–03:40 片段,画面中农场的分拣机正在给黄瓜分级,且没有任何标签或字幕。搜索结果在 0.3 至 0.5 秒内返回。We imported five Google sample videos (about 19 minutes in total) and searched them with scene descriptions. "Cucumbers being sorted by a machine" returned 03:20-03:40 of the machine learning video, where a farm's sorting machine grades cucumbers, without any tags or captions. Searches returned in 0.3 to 0.5 seconds.

抱歉,您的浏览器不支持播放此视频Sorry, your browser doesn't support playback for this video

使用音频模式搜索说过的内容Search What Was Said With Audio Mode

默认情况下(audio_mode: DISABLED),只会嵌入视频帧,因此查询匹配的是画面中显示的内容。启用 ENABLED 后,第一条音轨会与视频帧一同嵌入。我们将相同的视频导入两个数据存储,并向两者询问了主题演讲中的一些陈述;这些陈述是在画面显示演讲者时说出的:By default (audio_mode: DISABLED), only the frames are embedded, so a query matches what is shown. With ENABLED, the first audio track is embedded together with the frames. We imported the same videos into two data stores and asked both about statements from a keynote, spoken while the picture shows the speaker:

table3_video_audio_mode_4x3

只有支持音频的数据存储找到了合适的时机。Only the audio-enabled data store found the right moments.

生产环境注意事项Production Considerations

  • 为查询选择片段时长:较短的窗口(10 到 20 秒)可为特定时刻提供精确的时间戳;较长的窗口(最长 80 秒)可为每个片段提供更多上下文,适用于主题类查询。默认时长为 30 秒。
    Choose the Segment Duration for Your Queries: Short windows (10 to 20 seconds) give precise timestamps for specific moments; long windows (up to 80 seconds) give each segment more context for thematic queries. The default is 30 seconds.
  • 导入时间规划:视频导入会对每个片段进行转码和嵌入。将我们的五个视频导入两个数据存储用了 20 到 45 分钟。每个数据存储一次只运行一个导入任务;不同的数据存储可以并行导入。进行成本规划时请注意,在 Public Preview 期间,托管式摄取本身不另行收费(GA 时将开始计费),而后端 Collection 按标准 Agent Retrieval 定价计费。
    Plan for Import Time: Video imports transcode and embed every segment. Importing our five videos into two data stores took 20 to 45 minutes. Run one import at a time per data store; different data stores import in parallel. For cost planning, managed ingestion itself incurs no separate charge during Public Preview (billing will start at GA), while the backing Collection is billed under standard Agent Retrieval pricing.
  • 清理后端集合:使用 force=true 和 backing_collection_deleted=true 删除数据存储,否则其 Collection 会残留。删除操作在后台运行,在我们的测试中耗时约 20 分钟。
    Clean Up the Backing Collection: Delete a data store with force=true and backing_collection_deleted=true, or its Collection is left behind. Deletion runs in the background and took about 20 minutes in our tests.
  • 重排序适用于文档:VIDEO 数据存储不支持 reranking_spec。
    Reranking Is for Documents: reranking_spec is not supported on VIDEO data stores.

架构概要:选择合适的配置Architecture Summary: Choosing the Right Setup

table4_architecture_summary_1x1

经验法则:如果你的数据是文件,就让数据存储库摄取这些文件,并使用 SearchDocuments 进行搜索。只有在需要数据存储库无法提供的控制能力时,才直接使用 Collection。The rule of thumb: If your data is files, let a data store ingest them and search with SearchDocuments. Drop down to the Collection only when you need control the data store doesn't give you.

结论Conclusion

托管式摄取并不会改变优质检索所需的条件:划分合理的内容、优质的嵌入向量,以及恰当的筛选条件。改变的是由谁来构建这些东西。你无需再维护解析器、分块器、嵌入任务和模式,只需将数据存储指向一个存储桶,就能获得可直接引用的内容片段。Managed ingestion doesn't change what good retrieval needs: well-segmented content, good embeddings, and the right filters. What changes is who builds it. Instead of maintaining parsers, chunkers, embedding jobs, and a schema, you point a data store at a bucket and get back segments that are ready to cite.

对于智能体而言,这意味着通过一次 API 调用,就能使用检索工具获取每个答案的来源,例如 PDF 中的某一页或视频中的某个时刻。For agents, that means a retrieval tool that returns where each answer came from, a page in a PDF or a moment in a video, with one API call.

亲自试试:文档笔记本会导入 18 份 Alphabet 投资者 PDF,并演示筛选和重排序;视频笔记本则会搜索五个示例视频,通过内嵌播放器播放,并比较不同音频模式。Try it yourself: the document notebook ingests 18 Alphabet investor PDFs and walks through filters and reranking, and the video notebook searches five sample videos with an inline player and compares audio modes.

要求与下一步Requirements & Next Steps

  • 区域:在公开预览期间,仅限 us-east4 或 europe-west4
    Regions: During Public Preview, only us-east4 or europe-west4
  • API:Data store 和 SearchDocuments API 处于 v1beta 阶段,通过 REST 调用(计划支持 Python SDK)
    API: Data store and SearchDocuments APIs at v1beta, called over REST (Python SDK support is planned)
  • 权限:向 Agent Retrieval 服务代理授予 roles/vectorsearch.serviceAgent 角色,使其能够读取您的文件。
    Permissions: Grant roles/vectorsearch.serviceAgent to the Agent Retrieval service agent so it can read your files

参考文献References

值得记下

  1. 托管式摄取会为你搭建整条处理流水线:将数据存储指向 Cloud Storage 中的文件,服务就会使用 Gemini Embedding 2 对文件进行解析、分段和嵌入。
  2. SearchDocuments 接收一个数据存储和文本查询。你无需传入 Collection 名称、向量字段、任务类型或输出字段,文档的混合搜索(语义搜索 + 全文搜索)会自动运行。
  3. 在我们的测试中,18 份 Alphabet 投资者 PDF 从位于公开 Cloud Storage 存储桶中,到可以搜索并返回带页码引用的结果,耗时约 9 分钟;19 分钟的视频则可同时通过画面和语音内容进行搜索,每次查询耗时 0.3 到 0.5 秒。