RAG自动化的架构蓝图:使用Vertex AI搜索进行高级文档

2024年05月23日 由 alex 发表 224 0

生成式人工智能为开发人员和企业带来了众多机遇,可彻底改变业务流程、改变客户体验并释放新的收入来源。然而,要充分发挥这一潜力,构建者和 IT 领导者必须驾驭复杂的局面,在人工智能模型、应用和代理的快速实验和迭代与成本管理、治理和可扩展性等关键因素之间取得平衡。


在最近的 Next'24 Google Cloud 活动中,我们发布了 Vertex AI Agent Builder,这是一个改变游戏规则的解决方案,它将我们强大的 Vertex AI Search 和 Conversation 产品与一系列先进的开发人员工具集成在一起。这一全面的解决方案使开发人员能够创建和部署复杂的人工智能驱动代理,无缝地处理复杂的任务和查询,最终推动各个领域的创新和效率。


了解Vertex AI搜索

Vertex AI Search 是谷歌云内的一个综合平台,旨在帮助企业为其员工和客户设计定制的搜索解决方案。该平台可在各种数据源(包括网站、结构化数据(如 BigQuery 表、JSON 行)和非结构化数据(如 PDF、HTML、文本))中实现类似谷歌搜索的体验。


我们在这篇文章中处理了提取的 PDF 文档。这些文档可能是现成的,如果存在,你可以直接使用。它们也可能是企业机密的专有文档。在这里,我们将探讨如何摄取、处理和理解这些文档中的数据,从而构建一个可以回答复杂查询的系统,例如检索事实信息或从季度报告中的财务表格中提取数字。


首先,Vertex AI Search 是一个由 GCP 全面管理的平台,可将谷歌搜索质量的功能集成到你的企业数据中,提供两大关键优势:


  1. 提升搜索体验: 它将传统的基于关键字的搜索转变为现代的对话式体验,就像谷歌创新的生成式搜索一样。这一改进大大提高了内部和面向客户的应用程序的效率。
  2. 增强的生成式人工智能应用: 它还有助于在生成式人工智能应用中生成答案。通过将生成式人工智能建立在企业数据的基础上,Vertex AI Search 可确保更高的准确性、可靠性和相关性,这对实际业务用例至关重要。它作为一个即用型 RAG 系统,简化了搜索功能的集成。


构建自定义 RAG 管道可能很复杂。Vertex AI Search 通过提供即用型解决方案简化了这一过程。它简化了搜索和发现流程的各个方面,从数据提取和转换到信息检索和汇总,只需简单点击几下即可完成。因此,你可以使用 Vertex AI Search 作为检索引擎,迅速开发出强大的 RAG 驱动型应用程序。


虽然即用型解决方案提供了极大的便利,但 Vertex AI Search 还赋予了开发人员详细的控制权。你可以利用平台的灵活性定制 RAG 管道的每个阶段,使其符合你的独特需求。这种混合方法使你能够在预建组件和定制增强功能之间取得理想的平衡,确保你的应用程序与特定用例完美契合。


Vertex AI Search 通过一套全面的 API 实现了这一目标。这些 API 公开了顶点人工智能搜索 RAG 系统的底层组件,使开发人员能够满足自定义用例或为需要详细控制的客户提供服务。这包括文档 AI 布局解析器 API、排名 API、接地生成 API 和检查接地 API 等。


让我们开始吧!首先,我们将深入了解我们的数据集,这是 RAG 管道的基础。然后,我们将学习如何有效地将这些数据导入 Vertex AI Search,并对其进行组织以实现无缝检索。重点将放在 Vertex AI Search 的索引策略上,以确保我们的人工智能能在需要时访问最相关的信息。我们将探究查询索引文档的技术,尝试各种管道方法。最后,我们将收集结果,并学习如何评估它们的检索准确性和生成答案的质量。通过这次学习,你将掌握利用 RAG 和 Vertex AI Search 的强大功能构建更智能、更明智的 AI 管道的知识。


数据集

我们用于实验的数据集包括三家科技公司的季度报告: Alphabet、亚马逊和微软。从 2021 年第一季度到 2023 年第四季度,这个数据集包含 36 份文件(每家公司 12 份),时间跨度为三年。


为便于实验,我们从这些文档中提取了 100 对问答。每对问答都直接链接到单个文档,建立了单跳问答场景。这些问题和答案经过精心设计,侧重于从表格和复杂段落中提取信息,对 RAG 系统提出了巨大挑战。这 100 对问答将作为基本事实,用于评估我们在此介绍的各种 RAG 管道设计的性能。PDF 金融季报数据集可在此处找到。在名为 ground_truth.csv 的 CSV 文件中可以找到显示问答数据集的基本事实。该文件还包括以下元信息:i) 制图文件;ii) 公司名称;iii) 时间段。这些元信息由 CSV 文件中 documentcolumn 下的一个字段记录。


下面是 Alphabet 2020 年第一季度报告中的一个示例表,总结了主要与营业收入和利润率相关的财务业绩。


5


我们的基本真实 CSV 中的一个示例问题,以及从上表中得出的预期答案。


What was Google's operating income (in billions) at the end of March 2021, and
how did it compare to the same period of the previous year?


Google's operating income was $16.437 billion in Q1 2021. This was an increase
from $7.977 billion in Q1 2020.


要制定这个答案,我们首先需要从查询中推断出具体细节,以检索正确的文档。这包括导航到正确的页面、引用相应的表格并解析列信息。然后,我们将字段映射到列标题,并找到正确的片段。最后,我们将所有收集到的信息整合成一个连贯的答案。


文档输入和索引

要有效使用 Vertex AI Search 理解金融文档和回答问题,我们首先需要准备和摄取数据。这包括在 Vertex AI Search 中创建一个专用数据存储库,并将我们的财务文档从谷歌云存储(GCS)导入到该存储库中。幸运的是,Vertex AI Search 会自动为你处理信息的解析、分块和索引。


接下来,我们将配置一个文档搜索应用程序,利用输入的数据提供强大的搜索和检索功能。通过这些步骤,我们为高效索引和探索金融文档奠定了坚实的基础。这样,我们就能快速获取实验所需的信息,并开发出强大的文档问题解答管道。让我们来详细探讨每个步骤。


1. 创建数据存储:

Vertex AI Search 中的数据存储基本上是存储处理过的文档的容器。要创建数据存储来存储处理过的文档,我们需要为数据存储指定一个唯一的标识符和显示名称,以便在 Vertex AI Search 项目中轻松识别。此时,数据存储不包含任何文档。下一步,我们将向该数据存储推送文档(摄取)。需要注意的是,原始 PDF 文档实际上存储在 GCS 中。


下面的代码片段介绍了如何使用 Vertex AI Search 的 REST API 进行操作。你也可以使用 Vertex AI Python SDK。


url = f"https://discoveryengine.googleapis.com/v1alpha/projects/{config.PROJECT_ID}/locations/global/collections/default_collection/dataStores?dataStoreId={data_store_id}"
headers = {
    'Authorization': f'Bearer {config.ACCESS_TOKEN}',
    'Content-Type': 'application/json',
    'X-Goog-User-Project': config.PROJECT_ID
}
data = {
    'displayName': data_store_display_name,
    'industryVertical': IndustryVertical.GENERIC,
    'solutionTypes': SolutionType.SOLUTION_TYPE_SEARCH,
    'contentConfig': DataStore.ContentConfig.CONTENT_REQUIRED,
    'documentProcessingConfig': {
        'defaultParsingConfig': {
            'layoutParsingConfig': {}
        }
    }  
}
response = requests.post(url, headers=headers, json=data)


2. 从 GCS 导入文档:

数据存储创建完成后,我们将开始从指定的 GCS 数据桶中导入你的财务文档。这个过程包括指定 GCS 文件桶的 URI,我们数据集的所有原始 PDF 文档都存储在该文件桶中。在此之前,我们还需要创建一个清单文件。这是一个 JSON 文件,其中包含了我们要输入 Vertex AI 搜索的文档的所有元数据。该文件中名为 metadata.json 的示例行如下所示。


{
  "id": "1",
  "jsonData": "{\"company\": \"alphabet\", \"time_period\": \"Q1 2021\"}",
  "content": {
    "mimeType": "application/pdf",
    "uri": "gs://vais-rag-patterns/raw_docs/alphabet-q1-2021.pdf"
  }
}


启动摄取所需的代码预览示例如下。该代码使用的是 REST API。


url = f"https://discoveryengine.googleapis.com/v1/projects/{project_id}/locations/global/collections/default_collection/dataStores/{data_store_id}/branches/0/documents:import"
headers = {
    "Authorization": f"Bearer {config.ACCESS_TOKEN}",
    "Content-Type": "application/json"
}
data = {
    "gcsSource": {
        "inputUris": [gcs_input_uri] 
    }    
}
response = requests.post(url, headers=headers, json=data)


3. 创建文档搜索应用程序:

在成功录入我们的文档后,最后一步就是创建一个文档搜索应用程序。该应用程序将与我们的索引数据交互,为搜索、检索和分析我们的财务文档提供必要的工具和功能。


创建该应用程序所需的示例代码如下所示。请注意,我们应启用企业层进行搜索,并使用 LLM 激活高级搜索,以有效执行文档问题解答。此过程使用 REST API,但也可以使用 Python SDK 完成。


url = f"https://discoveryengine.googleapis.com/v1alpha/projects/{config.PROJECT_ID}/locations/global/collections/default_collection/engines?engineId={data_store_id}"
headers = {
    "Authorization": f"Bearer {config.ACCESS_TOKEN}",
    "Content-Type": "application/json",
    "X-Goog-User-Project": config.PROJECT_ID
}
data = {
    "displayName": data_store_display_name,
    "dataStoreIds": [data_store_id],
    "solutionType": SolutionType.SOLUTION_TYPE_SEARCH,
    "searchEngineConfig": {
        "searchTier": SearchTier.SEARCH_TIER_ENTERPRISE,
        "searchAddOns": SearchAddOn.SEARCH_ADD_ON_LLM
    }
}
response = requests.post(url, headers=headers, json=data)


为简化上述整个过程,你可以使用此处提供的脚本,该脚本可处理数据摄取和应用程序设置中涉及的所有必要步骤。按照这种结构化的方法,你就可以利用 Vertex AI Search 的强大功能,将你的财务文档转化为有价值、易于访问的知识库。


RAG 自动化的架构模式

通过在 Vertex AI Search 中对原始 PDF 文档进行摄取和索引,现在可以轻松简化查询已处理文档和生成答案的过程。提供的 Python SDK 示例代码演示了如何通过先前配置的搜索应用程序查询数据存储。


client_options = (
    ClientOptions(api_endpoint=f"{LOCATION}-discoveryengine.googleapis.com")
    if LOCATION != "global"
    else None
)
client = discoveryengine.SearchServiceClient(client_options=client_options)
serving_config = client.serving_config_path(
    project=config.PROJECT_ID,
    location=LOCATION,
    data_store=data_store_id,
    serving_config="default_config",
)
content_search_spec = discoveryengine.SearchRequest.ContentSearchSpec(
    snippet_spec=discoveryengine.SearchRequest.ContentSearchSpec.SnippetSpec(
    return_snippet=False 
    ),
    extractive_content_spec=discoveryengine.SearchRequest.ContentSearchSpec.ExtractiveContentSpec(
        max_extractive_answer_count=3,
        max_extractive_segment_count=3,
    ),
    summary_spec=discoveryengine.SearchRequest.ContentSearchSpec.SummarySpec(
        summary_result_count=5,
        include_citations=True,
        ignore_adversarial_query=False,
        ignore_non_summary_seeking_query=False,
    ),
)
request = discoveryengine.SearchRequest(
    serving_config=serving_config,
    query=search_query,
    filter=filter_str,
    page_size=5,
    content_search_spec=content_search_spec,
    query_expansion_spec=discoveryengine.SearchRequest.QueryExpansionSpec(
        condition=discoveryengine.SearchRequest.QueryExpansionSpec.Condition.AUTO,
    ),
    spell_correction_spec=discoveryengine.SearchRequest.SpellCorrectionSpec(
        mode=discoveryengine.SearchRequest.SpellCorrectionSpec.Mode.AUTO
    ),
)
response = client.search(request)


在此基础上,我们现在可以使用 Vertex AI Search API 以各种方式轻松建立 RAG 管道。接下来,我们将研究四种常见模式,以说明此类管道的灵活性和易实施性。


在 Vertex AI Search 中构建搜索请求时,配置提取有价值信息的规范至关重要。启用片段、段落和答案选项可确保全面检索相关内容。此外,激活 LLM 驱动的摘要功能可生成搜索结果的简明摘要(答案),从而增强用户体验。生成的 JSON 响应将同时包含汇总的答案以及提取的片段和答案。


Vertex AI Search 采用三种不同的方法来分割和提取文本数据:

  • 片段: 从搜索结果文档中简要摘录,提供内容预览,通常包含命中高亮。
  • 提取答案: 直接从原始文档中提取的逐字文本,提供简洁、与上下文相关的答案。
  • 提取片段: 提取的逐字文本更加简洁,适用于答案展示、后处理任务以及大型语言模型的输入。


此外,还可以配置自动拼写纠正和查询扩展设置,从而提高搜索准确性并扩大潜在结果。在我们的使用案例中,我们不考虑片段。


模式 I: 搜索与箱外(OOB)答案生成

模式 I 是一个简单而常见的流水线,可通过Vertex AI Agent Builder 控制台或Discovery Engine API来实现。该流程使用 Vertex AI Search 从搜索索引中检索相关信息,这些索引是我们之前通过数据存储和搜索应用程序设置的。这些索引映射到我们存储在 GCS 上的原始 PDF。该管道还使用由 LLM 驱动的内部系统,根据检索到的搜索结果生成简明的答案。这样就无需明确调用外部 LLM。只需一个 API 请求,用户就能收到详细的答案以及支持文件的引文。这简化了检索和汇总相关信息的过程。


6


上图展示了由 Vertex AI Search 支持的简单 RAG 管道。工作流程从存储在云存储中的原始 PDF 文档集合开始。Vertex AI Search 已经对这些文档进行了摄取和处理,并创建了结构化索引,以促进高效搜索和检索。当用户提交查询时,Vertex AI Search 会利用这些索引快速识别最相关的文档。然后,它从这些文档中提取相关信息,为用户提供答案,并注明信息来源。


模式 I 的显著优势在于其简单性和自足性,无需调用外部 LLM,并将整个过程整合为单个 API 请求。不过,这种简化方法可能会带来灵活性方面的限制,有可能导致在最终摘要中包含来自相关性较低文档的无关信息。鉴于我们的重点是单跳问题解答,即从单一来源获取答案,因此确保仅从检索到的最顶部结果生成答案就变得至关重要。


模式 II: 生成 OOB 答案的过滤搜索

模式 II 通过加入一个预检索步骤来增强搜索功能,该步骤的重点是通过过滤来理解查询。这就解决了用户偏好自然语言查询的问题,无需人工过滤,也不需要随时提供所需的过滤器。


该管道首先使用 Gemini 处理用户的自然语言查询,执行命名实体识别(NER)以提取关键信息,如公司名称和时间段。提取的元数据以 JSON 格式输出,然后用于过滤搜索结果,使其更符合用户的意图。这一过程不仅能减少噪音,还能显著提高搜索指标。


此外,更重要的是,提取的公司名称和时间段信息应符合摄取阶段建立的格式和语法。在本实施方案中,元数据过滤利用了先前在 Vertex AI Search 中分配给文档的公司名称和时间段标签。


因此,这一改进以模式 I 为基础,在检索之前引入了查询理解步骤,最终提高了检索结果的准确性和相关性。


7


工作流程如上图所示,首先是用户使用自然语言发起搜索,表达他们的信息需求,而不受特定过滤器的限制。然后将查询传递给 Gemini,由 Gemini 执行命名实体识别 (NER) 来提取公司名称和时间段等相关元数据。提取的元数据被编排成 JSON 格式,以便于过滤,随后 Vertex AI Search 利用这些元数据过滤文档索引,将结果缩小到与提取的公司和时间段信息一致的文档。最后,过滤后的相关性更强的文档将与注明信息来源的引文一起呈现给用户。


所述管道的代码实现可在此参考。在该实现中,实体识别利用了提示,在公司名称提取方面采用了零镜头方法,在时间段提取方面采用了带有正面和负面示例的几镜头方法。


Given the query below, extract the company name from it.
The company name can be either `Microsoft`, `Alphabet`, or `Amazon`. 
If company name is `LinkedIn`, translate to `Microsoft`.
IMPORTANT: The extracted company name must be a single word ONLY without any 
breaklines or punctuations or extra whitespaces. 
Given a query, extract the specific time period from it. A valid time period 
should be in the form 'Q1 2021' only. 
Examples of invalid formats include: 
'Q2 2020 to Q2 2021'
'Q2 2020 - Q2 2021'
'Q2 2020, Q2 2021'
The extracted time period should represent only one quarter and one year, 
corresponding to the present vs past. 
IMPORTANT: Ignore past references when the query is comparing the present to 
the past. 
Examples
========
Translate 'first quarter of 2020' to 'Q1 2020'.
Translate 'increase in Q2 2021 compared to Q2 2020' to 'Q2 2021'.
Translate 'twelve months ending December 31, 2022,' to 'Q4 2022'.


模式 III :使用提取片段和 Gemini Pro 进行过滤搜索并生成答案

之前,我们了解了在 Vertex AI 搜索中检索相关上下文的三种不同粒度:片段、提取片段和提取答案。在本模式(模式 III )中,我们将利用从搜索结果中提取的片段,并用它们取代开箱即用(OOB)答案生成步骤。当生成的答案需要特定的风格、细微差别、格式、长度或结构时,这一点尤为重要。在这种情况下,我们可以将提取的片段作为上下文,连同查询一起明确传递给 Gemini 等外部 LLM。我们可以通过各种方式灵活设计提示,使答案生成更符合我们的要求。模式 III 就包含了这种方法。


8


上图所示的架构图描述了模式 III 的工作流程。在这里,用户首先提交一个自然语言查询,由 Gemini 进行初步处理,提取相关元数据。然后,Vertex AI Search 会使用这些结构化元数据(与模式 I 和模式 II 类似)来有效搜索文档索引并识别相关文档。从这些经过过滤的文档中,提取出直接回答用户查询的特定片段。在最后阶段,Gemini 会处理这些提取的片段,为用户生成一个综合答案,并结合引用信息来指明源文件。用户最终会收到一个解决其疑问的最终答案,并附有从文档中提取信息的参考文献。


在此工作流程中,用于生成答案的提示可以像下图所示的那样简单,也可以根据具体需要更加复杂和精细。


Based on the following context, provide a clear and concise answer to the 
question below:
Context: {context}
Question: {question}


模式IV:使用提取答案和 Gemini Pro 进行过滤搜索并生成答案

在这一流程迭代中,我们基本保留了模式 III 的结构,但做了一个关键修改:我们不再使用检索文档中的提取片段,而是使用提取答案。这一修改保留了之前的提示结构。为了说明这一变化的影响,我们将检查测试集中的一个问题,并比较从片段和答案中生成的答案。


9


下面是一个示例问题和来自地面实况文件的预期答案。


What was LinkedIn's revenue increase in Q1 2021 according to Microsoft's 
earnings report, and what was the growth rate when adjusted for constant 
currency?


In Q1 2021, LinkedIn's revenue increased by 25% 
year-over-year. When adjusted for constant currency, the growth rate 
was 23%.


首先,让我们检查一下 Vertex AI 搜索针对示例问题返回的提取片段。以下是排名前三的片段。我们可以看到,问题的答案可以从 "业务亮点 "下的片段 1 和作为解析表格内容一部分的片段 2 中得出。要使用 Gemini 后检索生成最终答案,我们只需将这些片段连接起来,并作为单一上下文进行传递。


提取片段 1


Business Highlights
Revenue in Productivity and Business Processes was $13.6 billion and increased
15% (up 12% in constant
currency), with the following business highlights:
• Office Commercial products and cloud services revenue increased 14% 
(up 10% in constant currency) driven by Office 365 Commercial revenue 
growth of 22% (up 19% in constant currency)
• Office Consumer products and cloud services revenue increased 5% 
(up 2% in constant currency) and Microsoft 365 Consumer subscribers 
increased to 50.2 million
• LinkedIn revenue increased 25% (up 23% in constant currency)
• Dynamics products and cloud services revenue increased 26% (up 22% in 
constant currency)
driven by Dynamics 365 revenue growth of 45% (up 40% in constant currency)


提取片段 2


Financial Performance Constant Currency Reconciliation
Three Months Ended March 31,
($ in millions, except per share amounts)
Revenue
Operating
Income
Net
Income
Diluted
Earnings
per Share
2020 As Reported (GAAP)
$35,021
$12,975
$10,752
$1.40
2021 As Reported (GAAP)
$41,706
$17,048
$15,457
$2.03
2021 As Adjusted (non-GAAP)
$41,706
$17,048
$14,837
$1.95
Percentage Change Y/Y (GAAP)
19%
31%
44%
45%
Percentage Change Y/Y (non-GAAP)
19%
31%
38%
39%
Constant Currency Impact
$972
$634
$615
$0.08
Percentage Change Y/Y (non-GAAP) Constant Currency
16%
27%
32%
34%
Segment Revenue Constant Currency Reconciliation
Three Months Ended March 31,
($ in millions)
Productivity and
Business Processes
Intelligent Cloud
More Personal
Computing
2020 As Reported
$11,743
$12,281
$10,997
2021 As Reported
$13,552
$15,118
$13,036
Percentage Change Y/Y
15%
23%
19%
Constant Currency Impact
$366
$367
$239
Percentage Change Y/Y Constant Currency
12%
20%
16%
Selected Product and Service Revenue Constant Currency Reconciliation
Three Months Ended March 31, 2021
Percentage Change
Y/Y (GAAP)
Constant
Currency Impact
Percentage Change
Y/Y Constant
Currency
Office Commercial products and cloud services
14%
(4)%
10%
Office 365 Commercial
22%
(3)%
19%
Office Consumer products and cloud services
5%
(3)%
2%
LinkedIn
25%
(2)%
23%
Dynamics products and cloud services
26%
(4)%
22%
Dynamics 365
45%
(5)%
40%
Server products and cloud services
26%
(3)%
23%
Azure
50%
(4)%
46%
Windows OEM
10%
0%
10%
Windows Commercial products and cloud services
10%
(3)%
7%
Xbox content and services
34%
(2)%
32%
Surface
12%
(5)%
7%
Search advertising excluding traffic acquisition costs
17%
(3)%
14%
About Microsoft
Microsoft (Nasdaq "MSFT" @microsoft) enables digital transformation for the 
era of an intelligent cloud and an intelligent edge. Its mission is to 
empower every person and every organization on the planet to achieve more.


提取片段 3


Revenue in Intelligent Cloud was $15.1 billion and increased 23% 
(up 20% in constant currency), with the following business highlights:
• Server products and cloud services revenue increased 26% (up 23% in 
constant currency) driven by Azure revenue growth of 50% (up 46% in 
constant currency) 
Revenue in More Personal Computing was $13.0 billion and increased 19% 
(up 16% in constant currency), with the following business highlights:
• Windows OEM revenue increased 10%
• Windows Commercial products and cloud services revenue increased 10% 
(up 7% in constant currency)
• Xbox content and services revenue increased 34% (up 32% in constant currency)
• Search advertising revenue excluding traffic acquisition costs increased 17% (up 14% in constantcurrency)
• Surface revenue increased 12% (up 7% in constant currency)
Microsoft returned $10.0 billion to shareholders in the form of share 
repurchases and dividends in the third quarter of fiscal year 2021, an 
increase of 1% compared to the third quarter of fiscal year 2020.
Business Outlook
Microsoft will provide forward-looking guidance in connection with this 
quarterly earnings announcement on its earnings conference call and webcast.
Quarterly Highlights, Product Releases, and Enhancements
Every quarter Microsoft delivers hundreds of products, either as new releases,
services, or enhancements to current products and services. These releases 
are a result of significant research and development investments, made over 
multiple years, designed to help customers be more productive and secure and
to deliver differentiated value across the cloud and the edge.
Here are the major product releases and other highlights for the quarter, 
organized by product categories, to help illustrate how we are accelerating 
innovation across our businesses while expanding our market opportunities.
Responding to COVID-19
At Microsoft, our focus remains on ensuring the safety of our employees, 
striving to protect the health and well-being of the communities in which 
we operate, and providing technology and resources to our customers and 
partners to help them do their best work while remote. Additional information
about Microsoft's COVID-19 response can be found here.
Environmental, Social, and Governance (ESG)
To better execute on Microsoft's mission, we focus our Environmental, 
Social, and Governance (ESG) efforts where we can have the most positive 
impact. To learn more about our latest initiatives and priorities, 
please visit our investor relations ESG website.


以下是使用前三个提取段生成的答案。正如你所看到的,与我们预期的答案相比,答案更加详细和冗长。


LinkedIn's Revenue Increase in Q1 2021:
According to Microsoft's earnings report, LinkedIn's revenue increased 
by 25% in Q1 2021 compared to the same period in 2020. 
When adjusted for constant currency, the growth rate was 23%. 
This means that the increase in revenue was not solely due to fluctuations 
in currency exchange rates. 
Here's a breakdown of the information:
* Reported Revenue Increase: 25%
* Constant Currency Growth Rate: 23% 
This information can be found in the "Financial Performance" and
"Selected Product and Service Revenue Constant Currency Reconciliation" 
sections of the provided context.


现在,让我们来看看 Vertex AI 搜索返回的提取答案(作为搜索响应的一部分)。在之前发送的搜索请求中,我们将搜索规格设置为返回最相关文档的前三个匹配提取答案。下面是返回的提取答案。它们与从匹配度最高的文档--微软 2021 年第一季度--中提取的文本段跨度相似,只是更简洁、更短。


有趣的是,我们可以看到,回答我们的问题所需的信息只包含在最后一个(第三个)提取答案中。为了生成最终答案,我们将所有三个提取答案合并为一个上下文,将其与原始问题一起输入到 Gemini,然后生成答案。


提取答案 1


Microsoft Cloud Fuels Third Quarter Results REDMOND, Wash. - April 27, 2021 
- Microsoft Corp. today announced the following results for the quarter ended
March 31, 2021, as compared to the corresponding period of last fiscal year:
• Revenue was $41.7 billion and increased 19% • Operating income was $17.0 
billion and increased 31% • Net income was $15.5 billion GAAP and $14.8 
billion non-GAAP, and increased 44% and 38%, respectively • Diluted earnings
per share was $2.03 GAAP and $1.95 non-GAAP, and increased 45% and 39%, 
respectively • GAAP results include a $620 million net income tax benefit 
explained in the Non-GAAP Definition section below "Over a year into the 
pandemic, digital adoption curves aren't slowing down. They're 
accelerating, and it's just the beginning," said Satya Nadella, 
chief executive officer of Microsoft. "We are building the cloud for the 
next decade, expanding our addressable market and innovating across every
layer of the tech stack to help our customers be resilient and transform." 
"The Microsoft Cloud, with its end-to-end solutions, continues to provide 
compelling value to our customers generating $17.7 billion in commercial 
cloud revenue, up 33% year over year," said Amy Hood, executive vice 
president and chief financial officer of Microsoft.


提取答案 2


Revenue in Intelligent Cloud was $15.1 billion and increased 23% 
(up 20% in constant currency), with the following business highlights: 
• Server products and cloud services revenue increased 26% (up 23% in 
constant currency) driven by Azure revenue growth of 50% (up 46% in 
constant currency) Revenue in More Personal Computing was $13.0 billion 
and increased 19% (up 16% in constant currency), with the following 
business highlights: • Windows OEM revenue increased 10% • Windows Commercial 
products and cloud services revenue increased 10% (up 7% in constant currency)
• Xbox content and services revenue increased 34% (up 32% in constant currency)
• Search advertising revenue excluding traffic acquisition costs increased 
17% (up 14% in constant currency) • Surface revenue increased 12% (up 7% in 
constant currency) Microsoft returned $10.0 billion to shareholders in the 
form of share repurchases and dividends in the third quarter of fiscal year 
2021, an increase of 1% compared to the third quarter of fiscal year 2020. 
Business Outlook Microsoft will provide forward-looking guidance in connection
with this quarterly earnings announcement on its earnings conference call 
and webcast.


提取答案 3


Financial Performance Constant Currency Reconciliation Three Months Ended 
March 31, ($ in millions, except per share amounts) Revenue Operating Income 
Net Income Diluted Earnings per Share 2020 As Reported (GAAP) $35021 $12975 
$10752 $1.40 2021 As Reported (GAAP) $41706 $17048 $15457 $2.03 2021 As 
Adjusted (non-GAAP) $41706 $17048 $14837 $1.95 Percentage Change Y/Y (GAAP) 
19% 31% 44% 45% Percentage Change Y/Y (non-GAAP) 19% 31% 38% 39% Constant 
Currency Impact $972 $634 $615 $0.08 Percentage Change Y/Y (non-GAAP) 
Constant Currency 16% 27% 32% 34% Segment Revenue Constant Currency 
Reconciliation Three Months Ended March 31, ($ in millions) Productivity 
and Business Processes Intelligent Cloud More Personal Computing 2020 As 
Reported $11743 $12281 $10997 2021 As Reported $13552 $15118 $13036 
Percentage Change Y/Y 15% 23% 19% Constant Currency Impact $366 $367 $239 
Percentage Change Y/Y Constant Currency 12% 20% 16% Selected Product and 
Service Revenue Constant Currency Reconciliation Three Months Ended 
March 31, 2021 Percentage Change Y/Y (GAAP) Constant Currency Impact 
Percentage Change Y/Y Constant Currency Office Commercial products and 
cloud services 14% (4)% 10% Office 365 Commercial 22% (3)% 19% Office 
Consumer products and cloud services 5% (3)% 2% LinkedIn 25% (2)% 23% 
Dynamics products and cloud services 26% (4)% 22% Dynamics 365 45% (5)% 
40% Server products and cloud services 26% (3)% 23% Azure 50% (4)% 46% 
Windows OEM 10% 0% 10% Windows Commercial products and cloud services 10% 
(3)% 7% Xbox content and services 34% (2)% 32% Surface 12% (5)% 7% Search 
advertising excluding traffic acquisition costs 17% (3)% 14% About Microsoft
Microsoft (Nasdaq “MSFT” @microsoft) enables digital transformation for 
the era of an intelligent cloud and an intelligent edge.


我们可以看到,Gemini 生成的最终答案比我们之前使用提取片段生成的答案更短更简洁。


提取答案 1


Based on the provided context, LinkedIn's revenue increase in Q1 2021, 
according to Microsoft's earnings report, was 25%. When adjusted for 
constant currency, the growth rate was 23%.


替代模式

  1. 除了上文讨论的标准工作流程外,还有几种高级技术可以显著提高从 PDF 文档生成答案的能力。其中一种方法是查询扩展,即使用相关术语或同义词扩展初始搜索查询。Vertex AI Search 可以通过将参数设置为 "自动 "轻松启用该功能。另外,还可以设计一个 DIY 预检索步骤,使用 LLM 生成查询变体,然后并行调用 Vertex AI Search。查询扩展是提高问答系统信息检索质量的关键技术。它不仅能通过生成不同的查询变体来提高搜索相关性,还能在确保最高检索文档的代表性方面发挥关键作用,这对生成准确的答案至关重要。
  2. 在文档中增强关键词是提高相关性的另一项强大技术。顶点人工智能搜索开箱即支持这一功能。通过优先处理某些术语,可以提高检索结果的相关性。
  3. 此外,我们还可以通过搜索调整来提高检索性能。这种方法尤其适用于一般语言模型无法很好解决的特定行业或特定公司的查询。Vertex AI Search 完全支持搜索调整。
  4. 还可以通过使用不同的文档解析器为 PDF 文档选择适当的预处理类型来提高检索相关性。Vertex AI Search 主要支持三种类型:布局解析器、OCR 解析器和数字解析器。在我们的使用案例中,我们使用了布局解析器。如果你计划将 Vertex AI Search 用于 RAG,建议对 PDF 文档使用这种方法。另外,我们还可以采用其他复杂的方法,比如使用 Markdown 格式的文档 AI 从文档中提取表格。然后,我们可以将 PDF 与提取的表格一起转换为文本格式,并将其输入 Vertex AI Search,而不是将其作为 PDF 处理。
  5. 最后,对于需要自定义嵌入式信息检索的企业,顶点人工智能提供了强大的矢量搜索功能。Vertex AI 的矢量搜索可扩展至容纳数十亿个矢量,并能在几毫秒内识别近邻。矢量搜索(以前称为匹配引擎)与 Vertex AI 搜索类似,是 Agent Builder 的一部分。将 Agent Builder 视为这两种搜索选项的封装--我们在本文中广泛介绍的都是 Vertex AI Search。另一方面,Vertex AI 向量搜索是一个带有其他支持功能的向量存储,你可以用它来开发完全可定制的 DIY RAG 管道。如果你想完全自定义分块策略、嵌入模型选择、语义相似性检索的评分算法选择等所有内容,这是一个不错的选择。Agent Builder 还包括独立的 API,如检查接地、接地生成和排序 API。这些 API 可用于结合矢量搜索构建自定义 RAG 管道。


评估 RAG 管道


10


接下来,让我们探讨如何评估检索性能和生成答案的质量。首先,我们将尝试各种适合评估检索系统的指标,然后是评估 RAG 管道中答案质量的指标。这一评估过程将帮助我们完善和优化 RAG 系统,并了解哪种方法更有效。


I. 检索评估


a) 精度 @ K

精确度 @ K 是一个指标,用于量化检索结果前 K 个文档中相关结果的比例。在初始结果的质量优先于详尽检索信息的情况下,这一指标尤为重要。网络搜索引擎就是一个典型的例子,用户主要关注搜索结果的第一页。


假设用户在 YouTube 上搜索 "如何制作纸飞机 "的前 5 个教学视频。如果这 5 个视频中有 3 个成功地提供了预期的指导,那么精准度 @5 就会达到 3/5,即 60%。该指标提供了一种量化初始搜索结果相关性的方法。


在我们的使用案例(单跳问题解答)中,我们的目标是为每个查询获取一个相关项目(文档)。因此,计算精确度 @ K=1 是合乎逻辑的。在这种情况下,结果是二进制的:我们要么将所需文档定位在 Vertex AI Search 返回的检索到的项目的顶部,要么不定位。


11


b) Recall @ K

在信息检索领域,Recall @ K 这一指标用于衡量在搜索结果的前 K 个结果中检索到的所有相关文档的比例。它与精确度的不同之处在于,Recall 优先考虑的是系统识别所有潜在相关文档的能力,仅限于考虑的前 K 个文档。在法律或学术研究等领域,这一指标至关重要,因为遗漏关键文档可能会导致严重后果。


举例说明,假设有 10 个关于纸飞机构造的相关视频,用户感兴趣的是前 5 个结果。如果这 10 个视频中有 4 个出现在前 5 个结果中,那么 Recall @ 5 将是 10 个中的 4 个,即 40%。这个百分比说明了搜索系统在前 K 个结果中捕捉所有可能的相关结果的有效性。


考虑到我们使用案例的独特背景,计算 Recall @ K=1 是合乎逻辑的,因为只有一个相关文档。与精确度类似,这将产生一个二进制结果,表示特定项目是否被检索到。


12


在我们的使用案例中,当 k=1 时,不带过滤器的标准搜索的精确度和召回率均为 51%(模式 I)。然而,当我们使用命名实体识别(NER)并应用过滤器时,这些指标都提高到了 90%(模式 II)。


c) 平均互易等级(MRR)

Mean Reciprocal Rank (MRR) 是一种统计量,用于衡量回复列表中第一个正确答案的平均反向排名。在第一个相关文档的位置比其他相关文档的存在更重要的情况下,MRR 尤其有用。这一指标通常用于问题解答系统和其他搜索环境,在这些环境中,用户可能会对他们遇到的第一个正确答案感到满意。


想象一下,你正在使用搜索引擎寻找夏威夷果饼干的完美食谱。如果你点击的第一个食谱正是你所需要的,那么搜索引擎的平均互易等级(MRR)就会达到完美的 1,表明它立即提供了最佳结果。但是,如果理想的食谱不是你查看的第一个,而是你觉得有吸引力的第三个,那么该搜索的平均互惠等级就会下降到 1/3。


总成果报酬率的计算公式为


13


其中,? 是查询总数,rᵢ 是第 i 个查询的第一个相关答案的排名位置。


在我们的使用案例中,使用模式 I(不带筛选器的文档搜索)时,MRR 等于 64%。然而,在模式 II 中,当我们使用过滤器时,这一数字增加到了 91%。


d) DCG(贴现累计收益)

DCG 根据文档在结果列表中的位置来衡量其有用性或 "增益"。这里的假设是,在搜索结果中出现较早的文档比出现较晚的文档与用户更相关。折扣 "部分指的是将每份文档的相关性得分按其在结果列表中的位置成正比地减少一个对数因子。这反映了用户在搜索结果列表中的位置越靠后,查看后面结果的可能性就越小。计算公式如下


14


其中,p 是排序位置,relᵢ 是位于 ? 位置的结果的相关性得分,log₂(?+1) 是 (?+1) 的对数基 2,用作折扣系数。要计算 relᵢ,我们只需使用二进制相关性 1 表示相关文档,0 表示不相关文档。


与 MRR 相比,DCG 提供了一个更全面的搜索质量视角,因为它考虑了多个结果的相关性,而不仅仅是最重要的一个结果。MRR 和 DCG 提供了不同的搜索性能视角,MRR 侧重于顶部结果的准确性,而 DCG 则考虑了整个结果列表的相关性。通过监控这两个指标,可以详细了解检索策略的有效性。


e) NDCG(归一化贴现累积增益)

DCG 是衡量排名列表总相关性的指标,而 NDCG 则是 DCG 的归一化版本,可用于比较不同列表之间的相关性。NDCG 通常比 DCG 更受青睐,因为它为评估排名系统提供了一个更加标准化和可解释的指标。NDCG 的计算公式如下:


15


其中


DCG? 是使用原始公式计算的位置?处的 DCG 值。

IDCG? 是理想 DCG,即如果所有结果都按相关性完美排序,则?位置的 DCG 值可能达到的最大值。IDCG? 的计算公式如下:


16


在此,我们按降序对相关性得分进行排序。


对于我们的用例,我们测量了模式 I 和模式 II 的 NDCG。对于模式 I,平均 NDCG 约为 64%。应用过滤器后,NDCG 提高到 91%左右。


f) 平均精度(AP)

平均精确度衡量的是一个系统(如搜索引擎)对相关条目的排名情况。它既要考虑找到了多少相关条目,也要考虑这些条目排名的高低。比方说,你要求提供关于如何制作纸飞机的前 5 个教学视频,以下是你得到的视频顺序:


  1. 视频 A:完美的说明(相关)
  2. 视频 B:完全无关(不相关)
  3. 视频 C:不错的说明(相关)
  4. 视频 D:另一段无关视频(不相关)
  5. 视频 E:很好的说明(相关)


为了计算 AP,我们将查看找到相关视频的每个点的精确度:


  • 视频 A 之后:1/1 = 100%
  • 视频 C 之后:2/3 = 66.7
  • 视频 E 之后:3/5 = 60


现在,我们将这些精确度值取平均值: (100% + 66.7% + 60%) / 3 = 75.6%


因此,该搜索结果的 AP 为 75.6%。这意味着,平均而言,你在搜索初期就能获得相关结果。Precision @ 5 只关注前 5 个搜索结果的相关性(本例中为 60%),而 AP 则会考虑相关视频的搜索顺序,奖励相关视频的较高位置。这样就能更细致地反映出搜索引擎在为你找到相关结果方面的表现。计算 AP 的公式如下:


17


其中


  • H 是相关文档的位置集。
  • ∣?∣ 是相关文档的数量。
  • ?(?)是?位置的精确度。位置 ? 的相关文档的 P(i) 为 1/i。


g) 平均精度(MAP)

MAP 扩展了平均精度 (AP) 的概念,以评估多次搜索或查询。AP 衡量的是单次搜索对相关条目的排序情况,而 MAP 则是对多次搜索的 AP 分数进行平均,从而得出系统在多次查询中的整体性能指标。这就好比在多次测试中获得平均成绩,而不是只在一次测试中获得平均成绩。


虽然 MAP 因其能够处理每个查询包含多个相关文档的复杂情况而备受青睐,但当每个查询只有一个相关结果时,它就会发生有趣的简化。在处理单个相关文档查询时,MAP 基本上可以归结为以下几点:


  1. 平均精确度变为精确度: 每个查询的平均精确度只是在单个相关文档出现的等级上达到的精确度。
  2. 精确度等于排名倒数: 由于只有一个相关文档,因此精确度是其排名的倒数(例如,如果文档排名为 3,则精确度为 1/3)。这正是 MRR 计算中使用的值。
  3. MAP 反映 MRR:MAP 是这些 AP 值在所有查询中的平均值,最终是相关文档倒数等级的平均值。这正是 MRR 的作用。鉴于这种重叠,在我们的具体设置中,使用 MAP 并不能提供超出 MRR 所能提供的额外洞察力。一般来说,在每个查询涉及多个相关文档(多跳问题解答)的情况下,MAP 的信息量更大,它可以提供一个细致入微的视图,显示所有相关文档在不同等级中的检索效果。


在我们对模式 I 和 II 的实验中,MAP 基本上等同于 MRR。模式 I 的 MAP 为 64%,模式 II 的 MAP 则增至 91%。


评估答案

我们将重点评估 RAG 管道的答案生成组件。鉴于每个问题都有基本真实答案,我们的目标是评估生成答案的质量,确保它们既准确又在语义上与测试集中的预期答案相似。


为此,我们可以采用两种不同的度量方法。第一个指标利用余弦相似度来量化语义相似性,这个指标来自 sklearn.metrics.pairwise。第二个度量指标利用 LLM 作为评判的力量。通过向 LLM 展示生成的答案和人类生成的答案,我们可以评估模型输出与人类期望的一致程度。通过这种方法,我们可以评估生成答案的事实准确性和整体一致性,确保它们与人类撰写的答案无异。


a) 语义相似性

该指标测量生成答案和预期答案的向量表示之间的夹角余弦。它指的是对生成的回答与基本事实(预期答案)之间语义相似性的评估。分数越高,表示生成的答案与基本事实之间的一致性越好。


预期答案和生成答案都使用文本嵌入模型 text-embedding-003 进行编码。你可以在这里找到语义相似性的代码实现。


b) 事实正确性

为了评估之前 RAG 管道中生成答案的准确性,我们利用 Gemini 进行比较。问题、预期答案(基本事实)和生成的答案被传递给 Gemini。提示模板会引导 Gemini 将生成的答案分为 "正确"(与预期答案完全一致)、"部分正确"(包含一些准确信息,但不完整或部分不正确)或 "不正确"(与预期答案不一致)。通过这种分类,可以对答案质量进行细化评估,找出答案生成模型可能需要改进的地方。


Given the question, expected and generated answers as shown below, compare the
answers and classify them into one of the three classes - `correct`, 
`partially correct`, or `incorrect`. 
If the answer is partially correct or incorrect, provide the rationale.
The output should be two things - class and rationale as a Python dictionary. 
For class, it should be one word ONLY (which is the expected class), and for 
rationale, provide the reason succinctly, especially ONLY focusing on numbers
and facts.
DO NOT focus on the semantics between the expected and predicted answers.
IMPORANT: Compare only numbers and facts. 
If the units are different, normalize them before comparing. 
E.g., 1 billion = 1000 million.
Question: {question}
Expected Answer: {expected_ans}xa
Predicted Answer: {predicted_ans}
Compare the predicted answer with the expected answer. 
Determine if the predicted answer is factually correct and satisfies the 
given question.
Provide your response in the following format:
{format_instructions}


下图显示了我们之前试验过的所有四种模式(RAG 管道)的整体答案准确率。为了计算准确率,我们给完全正确的答案打 1.0 分,给部分正确的答案打 0.5 分,给根据 LLM 事实正确性输出错误分类的答案打 0 分。从图中可以看出,利用外部 LLM 通过过滤搜索生成答案的模式 IV 优于所有其他方法,准确率接近 70%。


18


我们还可以细分四种不同方法(流水线)中的类别分布,以便更好地了解改进是如何随着增强而逐步实现的。


19


下面的方框图显示了我们之前创建的四种不同文档问题解答 RAG 管道在不同类别(正确、部分正确、不正确)中的语义相似度得分分布情况。x 轴表示不同的类别,y 轴表示语义相似度得分,从 0(无相似度)到 1(完全相似度)不等。这些方框图显示了每个类别中语义相似度得分的中位数、四分位数和范围。总之,该图简明而翔实地直观展示了各种问题解答方法在语义相似性方面的表现。


20


分布情况显示出不同的模式:


  1. OOB(模式 I): 在所有类别中显示出较大的得分范围,部分正确答案的集中度较高。这表明开箱即用(OOB)方法很难在我们的测试集上持续生成准确的答案。
  2. OOB + 筛选器(模式 II): 与 "开箱即用 "方法相比有了明显的改进,尤其是在减少错误答案方面。分布更倾向于更高的相似性得分,这表明应用过滤器后准确率有所提高。对于错误类别,一个有趣的观察结果是,与 1 相比,模式 II 的语义相似度方差有所减小,集中在 0.5 到 0.6 之间。
  3. 提取片段(模式 III): 与前两种方法相比,正确答案和部分正确答案的比例都有所提高。这表明,从上下文中提取相关片段是一种比单纯依赖 OOB 模型或基本过滤更有效的策略。
  4. 提取式答案(模式 IV): 整体表现最佳,正确答案最集中,错误答案最少。这表明,直接从上下文中提取完整答案可以得到语义最相似、最准确的答案。


到目前为止,我们已经讨论了如何评估检索和答案生成这两个 RAG 管道的主要阶段。在检索方面,我们重点评估了检索文档的相关性。这可以进一步扩展到评估页面或检索上下文的相关性。不过,你需要确保你拥有这方面的基本真实信息。


对于答案质量,我们还可以使用 Ragas 等其他开源替代框架,甚至在适用的情况下利用顶点人工智能的快速评估 API。通过快速评估服务,你可以在多个指标上对你的 LLM 进行点对点和对对对的评估。你可以提供推理时间输入、LLM 响应和其他参数,该服务会返回评估任务的特定指标。


结论

在本文中,我们探讨了如何使用 Vertex AI Search 在金融领域创建企业级 RAG 管道。我们详细介绍了金融数据集的摄取和索引,并利用这些索引使用各种方法执行搜索。我们访问了 Vertex AI Search 提供的不同上下文类型,并创建了四个不同的 RAG 管道进行比较。

文章来源:https://medium.com/google-cloud/architectural-blueprints-for-rag-automation-advanced-document-understanding-using-vertex-ai-search-537ee9376847
欢迎关注ATYUN官方公众号
商务合作及内容投稿请联系邮箱:bd@atyun.com
评论 登录
热门职位
Maluuba
20000~40000/月
Cisco
25000~30000/月 深圳市
PilotAILabs
30000~60000/年 深圳市
写评论取消
回复取消