在本章中,我们将探讨一个元数据提取用例,这是理解生成式人工智能(GenAI)能力的一个很好的切入点。这个话题在各行各业中都有重要意义,且具有启发性。
为了说明这个用例,让我们考虑一个场景,在这个场景中,我们与一家金融服务公司合作,要求从10-K报告中提取数据。这些报告是由公开交易的公司每年向证券交易委员会(SEC)提交的,提供了公司财务表现、运营情况和重大事件的全面概述。它们是非常庞大的文档,超过100页,并包含大量信息,结构分布在不同的部分以及不同的数据类型(表格、文本等)中。
在本章中,我们的目标是识别需要从这份庞大信息库中提取的特定数据集和关键数据点。这个过程需要一种系统化的方法,以精确定位所需的数据点。
一旦确定了相关数据点,我们将决定将这些提取的数据存储的位置。这可能涉及一个集中式数据库,供组织内授权人员访问,或一个基于云的存储库,便于跨团队和地点的无缝访问和协作。
无论选择哪种存储解决方案,GenAI将在整个过程中发挥关键作用。凭借其理解和处理自然语言的能力,GenAI将在处理复杂的财务报告结构、以惊人的效率和准确性提取关键信息方面发挥不可或缺的作用。
本章将涵盖以下主要主题:
- 用例定义:我们将描述从金融服务公司的10-K报告中提取元数据的示例场景,解释这些报告的结构和重要性。
- 架构:我们将概述使用Google Cloud服务处理10-K报告的基于云的无服务器架构,包括存储、消息传递、处理和数据库组件。
- 入口点:我们将解释如何触发批处理管道,使用Google Cloud Storage和Cloud Functions启动内容提取过程。
- 提示预处理:我们将详细说明为AI模型创建初始提示,利用SEC指南识别要从10-K报告中提取的关键数据点。
- 推理:我们将讨论如何通过Vertex AI提交提示给Gemini Pro 1.5,展示模型如何处理和提取10-K报告中的信息。
- 结果后处理:我们将讨论解析大型语言模型(LLM)的JSON输出,并探讨如何将提取的数据导入数据库,考虑关系型数据库和文档数据库的选项。
- 结果呈现:我们将考虑如何展示提取的数据,包括使用商业智能工具、数据可视化平台和自定义应用程序。
- 代码示例:我们将提供元数据提取过程的实际实现,包括设置、提示创建、推理和结果处理。
用例定义
从10-K报告中提取元数据对金融服务公司和其他利益相关者具有重要价值。这些报告是由证券交易委员会(SEC)要求的,包含大量信息,可以提供关于公司财务健康状况、运营绩效和战略方向的宝贵见解。然而,这些文档的庞大体量、复杂性以及公司在编写报告时缺乏一致性,使得手动提取和分析相关数据点变得具有挑战性。
典型的10-K报告遵循标准化结构,包含多个部分,涵盖公司运营的各个方面。这些部分可能包括业务概况、风险因素、管理层讨论与分析、财务报表,以及关于公司治理的披露等。尽管结构在各公司之间一致,具体的数据点及其展示方式却各不相同,这使得建立统一的提取方法变得困难。
10-K报告包括五个不同的部分:
- 业务:本部分提供公司主要业务的概述,包括其产品和服务、主要市场、竞争格局以及关于其商业模式和运营的其他相关细节。
- 风险因素:在这一部分,公司列出并讨论了它所面临的所有风险。这包括可能影响公司绩效或未来前景的运营、财务、法律、监管以及行业特定的风险。
- 精选财务数据:本部分提供公司过去五年的具体财务信息,通常包括关键财务指标,如收入、净收入、每股收益以及其他相关财务数据。
- 管理层讨论与分析(MD&A) :第四部分提供了公司高层管理团队对公司财务结果的解释和分析,包括详细讨论影响公司绩效的因素、未来战略以及潜在的机会和挑战。
- 财务报表及补充数据:最后一部分提供经审计的财务报表,包括损益表、资产负债表、现金流量表以及附带的注释和披露。这一部分提供了公司在报告期内财务状况和表现的全面详细信息。
这些文档的最大挑战在于,特定的数据点及其呈现方式各不相同,这使得建立统一的数据提取方法变得非常困难。然而,通过利用GenAI的自然语言处理能力,金融服务公司可以高效地浏览这种结构化格式,并从每个部分中提取相关的数据点,针对每个公司报告的独特特征量身定制其提取方法。
从这些文档中提取的结构化元数据为金融服务公司带来了机遇。例如,他们可以深入分析公司的财务表现,并将其与行业同行或历史趋势进行对比。这种分析可以为投资决策、风险评估和战略规划提供依据。
另一个机会是利用提取的元数据开发预测模型,识别手动分析中可能未立即显现的模式。这些模型有助于预测潜在风险,识别新兴趋势,并发现新的投资机会。
进一步思考,提取的元数据可以集成到现有的数据仓库或商业智能平台中,从而实现组织内部各个团队的无缝访问和协作。这种集成可以促进跨职能合作,使投资银行、资产管理和风险管理等不同部门能够利用相同的数据进行各自的分析和决策过程。
除了金融服务公司外,提取的元数据对监管机构、学术研究人员和其他希望研究公司绩效、行业趋势以及金融市场整体健康状况的利益相关者也具有价值。
通过利用GenAI的强大能力从10-K报告中提取元数据,金融服务公司可以解锁大量的见解,简化分析过程,并做出更为明智的决策,从而推动业务增长并降低风险。
架构
尽管本书的范围不是深入探讨大型语言模型(LLM)处理架构的复杂性,但我们将简要讨论一下我们的元数据提取用例的云架构可能是什么样的。在这个示例中,我们将利用Google Cloud的能力,因为它提供了一个名为Vertex AI的原生AI平台,允许我们以企业合规的方式无缝集成领先的模型,包括Google的Gemini和第三方模型,如Anthropic的Claude。
我们为这个用例采用的方法是利用批处理优化架构,这种架构适合高效、可扩展地处理大量数据。这种架构符合云原生原则,是一种无服务器架构,利用了Google Cloud的各种服务。
这个架构将包括一个对象存储(Google Cloud Storage)来存储10-K报告,一个消息队列(Google Cloud Pub/Sub)来协调数据流,一个处理组件(Google Cloud Functions)来执行基于LLM的元数据提取任务,一个LLM模型(如托管在Vertex-AI上的Google Gemini)来执行实际的提取工作,以及一个数据库(Google BigQuery)来存储提取的元数据。
以下是该架构如何运作的更详细的分解:
- 10-K报告存储:10-K报告存储在Google Cloud Storage中,这是一个高扩展性和高耐久性的对象存储。
- 触发器:一个Cloud Function定期触发(例如每天或每周),启动元数据提取过程。
- 消息队列:Cloud Function将从Cloud Storage中读取10-K报告的列表,并向Pub/Sub主题发布消息,有效地创建一个待处理报告的队列。
- 报告处理:另一个订阅了Pub/Sub主题的Cloud Function会根据队列中每条消息触发,对应于给定报告的处理。
- LLM模型调用:第二个Cloud Function调用托管在Vertex-AI上的LLM模型(例如Google Gemini),并将10-K报告的内容作为输入。
- 报告处理:LLM模型处理报告,利用其自然语言理解能力提取相关元数据。
- 存储结果:提取的元数据将以结构化格式(例如BigQuery)存储,以便进一步分析和使用。
这种无服务器架构提供了多个优势,包括自动扩展、成本效益(按需计费定价)以及与其他Google Cloud服务的无缝集成。
以下图示展示了在本示例中将采用的架构,遵循我们在第3章《设计与生成式AI互动的模式》中讨论的GenAI集成框架:
入口点
我们批处理管道的入口点将是一个在Google Cloud Storage(GCS)中创建的对象,这将触发一个Google Cloud Function来启动处理管道。这种设置使我们能够无缝集成到现有的工作流中,其中10-K报告会被上传到指定的GCS存储桶。通过利用Cloud Functions的事件驱动特性,我们的系统可以在新报告上传到存储桶后立即自动启动。
一旦触发,Cloud Function将启动内容提取过程。在这个步骤中,我们决定利用Google的Gemini Pro 1.5的强大能力,这是一款最先进的GenAI多模态模型,支持直接处理PDF文档。Gemini Pro 1.5将分析上传的10-K报告,智能地提取不仅是文本内容,还包括我们感兴趣的相关数据点,如财务数字、公司概述和关键绩效指标。
通过利用Gemini Pro 1.5的先进自然语言处理和文档理解能力,我们可以获得报告内容的全面转录。该转录将作为我们管道中进一步分析和处理步骤的基础。此外,提取的数据点将以我们选择的格式(如JSON、Markup等)进行结构化和组织,这些格式在提示中定义,使我们能够将它们无缝集成到下游系统中,用于生成有洞察力的摘要、可视化和其他有价值的输出。
提示预处理
作为第一步,我们将开始制定一个提示,以提取理解10-K文档所需的关键数据点。为了指导我们的工作,我们将利用SEC自身提供的一份宝贵资源——名为《如何阅读10-K》的文档,该文档可在SEC网站上找到(www.sec.gov/files/reada…)。
这份由SEC提供的文档是一个非常有用的指南,概述了投资者和分析师在深入研究公司10-K报告时应该关注的关键部分和信息。通过仔细研究这份资源,我们可以识别出最相关且最具洞察力的关键数据点,确保我们的GenAI系统提取出真正重要的信息。
为了启动我们的数据提取过程,我们将为我们的GenAI模型创建一个简单而有效的提示。这个初始提示将作为起点,指示模型识别并提取《如何阅读10-K》文档中列出的特定数据点。虽然这个初始提示可能简短,但它将为我们项目进展中的更复杂的提示和微调技术奠定基础。目标是通过迭代优化提示,利用GenAI的强大能力,准确高效地从这些复杂的财务文档中提取最关键的信息。
prompt = """
You are an expert business analyst. Your task is to evaluate documents.
From the context document generate a JSON template with the important data points to be extracted from the document, also remember to add an attribute for the summary of the section.
The output should be a well-formed JSON document.
Context:
reada10k.pdf
Response:
"""
以下是前述提示的示例输出:
{
"10k_template": {
"part_I": {
"item_1_business": {
"description": "Company's main products and services, subsidiaries, and markets",
"recent_events": null,
"competition": null,
"regulations": null,
"labor_issues": null,
"operating_costs": null,
"seasonal_factors": null,
"summary": null
},
"item_1a_risk_factors": {
"risk_factors": [],
"summary": null
},
"item_1b_unresolved_staff_comments": {
"comments": [],
"summary": null
},
"item_2_properties": {
"properties": [],
"summary": null
},
"item_3_legal_proceedings": {
"proceedings": [],
"summary": null
},
"item_4": {
"reserved": true
}
},
"part_II": {
"item_5_market_for_registrants_common_equity": {
"market_information": null,
"number_of_holders": null,
"dividends": null,
"stock_repurchases": null,
"summary": null
},
"item_6_selected_financial_data": {
"financial_data": {},
"summary": null
},
"item_7_managements_discussion_and_analysis": {
"operations_and_financial_results": null,
"liquidity_and_capital_resources": null,
"trends_and_uncertainties": null,
"critical_accounting_judgments": null,
"off_balance_sheet_arrangements": null,
"contractual_obligations": null,
"summary": null
},
"item_7a_quantitative_and_qualitative_disclosures_about_market_risk": {
"market_risk_exposures": null,
"risk_management": null,
"summary": null
},
"item_8_financial_statements_and_supplementary_data": {
"income_statement": null,
"balance_sheets": null,
"statement_of_cash_flows": null,
"statement_of_stockholders_equity": null,
"notes_to_financial_statements": null,
"auditors_report": null,
"summary": null
},
"item_9_changes_in_and_disagreements_with_accountants": {
"changes_in_accountants": null,
"disagreements": null,
"summary": null
},
"item_9a_controls_and_procedures": {
"disclosure_controls_and_procedures": null,
"internal_control_over_financial_reporting": null,
"summary": null
},
"item_9b_other_information": {
"other_information": null,
"summary": null
}
},
"part_III": {
"item_10_directors_executive_officers_and_corporate_governance": {
"directors_and_executive_officers": [],
"code_of_ethics": null,
"board_qualifications": null,
"summary": null
},
"item_11_executive_compensation": {
"compensation_policies_and_programs": null,
"executive_compensation": {},
"summary": null
},
"item_12_security_ownership": {
"ownership_information": {},
"equity_compensation_plans": null,
"summary": null
},
"item_13_certain_relationships_and_related_transactions": {
"relationships_and_transactions": [],
"director_independence": null,
"summary": null
},
"item_14_principal_accountant_fees_and_services": {
"fees_for_services": {},
"summary": null
}
},
"part_IV": {
"item_15_exhibits_financial_statement_schedules": {
"exhibits": [],
"financial_statement_schedules": null,
"summary": null
}
}
}
}
现在我们有了一个关于10-K报告中需要提取的内容的模板,可以创建一个提示来提取这些数据点:
You are an expert business analyst specializing in 10-K documents.
Your task is to extract information from 10-K documents; to achieve this task, follow these steps:
Carefully analyze the document provided as context.
Use the template as a reference to understand which data points to extract.
Never make up information; if you don't remember something, go back to read the document. If the data is not available, add "Not available" as the value.
Return a well-formed JSON document following the template.
Always think step by step.
<template>
{
"10k_template": {
"part_I": {
"item_1_business": {
"description": "Company's main products and services, subsidiaries, and markets",
"recent_events": null,
"competition": null,
"regulations": null,
"labor_issues": null,
"operating_costs": null,
"seasonal_factors": null,
"summary": null
},
"item_1a_risk_factors": {
"risk_factors": [],
"summary": null
},
"item_1b_unresolved_staff_comments": {
"comments": [],
"summary": null
},
"item_2_properties": {
"properties": [],
"summary": null
},
"item_3_legal_proceedings": {
"proceedings": [],
"summary": null
},
"item_4": {
"reserved": true
}
},
"part_II": {
"item_5_market_for_registrants_common_equity": {
"market_information": null,
"number_of_holders": null,
"dividends": null,
"stock_repurchases": null,
"summary": null
},
"item_6_selected_financial_data": {
"financial_data": {},
"summary": null
},
"item_7_managements_discussion_and_analysis": {
"operations_and_financial_results": null,
"liquidity_and_capital_resources": null,
"trends_and_uncertainties": null,
"critical_accounting_judgments": null,
"off_balance_sheet_arrangements": null,
"contractual_obligations": null,
"summary": null
},
"item_7a_quantitative_and_qualitative_disclosures_about_market_risk": {
"market_risk_exposures": null,
"risk_management": null,
"summary": null
},
"item_8_financial_statements_and_supplementary_data": {
"income_statement": null,
"balance_sheets": null,
"statement_of_cash_flows": null,
"statement_of_stockholders_equity": null,
"notes_to_financial_statements": null,
"auditors_report": null,
"summary": null
},
"item_9_changes_in_and_disagreements_with_accountants": {
"changes_in_accountants": null,
"disagreements": null,
"summary": null
},
"item_9a_controls_and_procedures": {
"disclosure_controls_and_procedures": null,
"internal_control_over_financial_reporting": null,
"summary": null
},
"item_9b_other_information": {
"other_information": null,
"summary": null
}
},
"part_III": {
"item_10_directors_executive_officers_and_corporate_governance": {
"directors_and_executive_officers": [],
"code_of_ethics": null,
"board_qualifications": null,
"summary": null
},
"item_11_executive_compensation": {
"compensation_policies_and_programs": null,
"executive_compensation": {},
"summary": null
},
"item_12_security_ownership": {
"ownership_information": {},
"equity_compensation_plans": null,
"summary": null
},
"item_13_certain_relationships_and_related_transactions": {
"relationships_and_transactions": [],
"director_independence": null,
"summary": null
},
"item_14_principal_accountant_fees_and_services": {
"fees_for_services": {},
"summary": null
}
},
"part_IV": {
"item_15_exhibits_financial_statement_schedules": {
"exhibits": [],
"financial_statement_schedules": null,
"summary": null
}
}
}
}
</template>
<document>
</document>
Response:
请注意,模板中有非常具体的指示,明确告知该做什么以及不该做什么。这是为LLM设计的最佳实践,因为这些模型需要如此具体的指令,才能有效地为您提供精确的信息。一个好的类比是将它们视为一年级的学生,提供明确的指导并尽可能多地提供上下文。
推理
在推理阶段,我们将把我们的提示提交给通过Vertex-AI提供的Gemini Pro 1.5模型。
Vertex AI Gemini API是为开发人员和企业量身定制的,旨在将Gemini模型集成到大规模部署中。这一企业级服务提供了一套强大的功能,旨在满足现代高性能应用的需求。通过Vertex AI Gemini API,您可以受益于增强的企业安全措施,确保您的数据和模型受到业界领先的保护。此外,它还提供数据驻留选项,允许您遵守区域数据存储和处理的规定。
{
"10k_template": {
"part_I": {
"item_1_business": {
"description": "Company's main products and services, subsidiaries, and markets",
"recent_events": null,
"competition": null,
"regulations": null,
"labor_issues": null,
"operating_costs": null,
"seasonal_factors": null,
"summary": null
},
"item_1a_risk_factors": {
"risk_factors": [],
"summary": null
},
"item_1b_unresolved_staff_comments": {
"comments": [],
"summary": null
},
"item_2_properties": {
"properties": [],
"summary": null
},
"item_3_legal_proceedings": {
"proceedings": [],
"summary": null
},
"item_4": {
"reserved": true
}
},
"part_II": {
"item_5_market_for_registrants_common_equity": {
"market_information": null,
"number_of_holders": null,
"dividends": null,
"stock_repurchases": null,
"summary": null
},
"item_6_selected_financial_data": {
"financial_data": {},
"summary": null
},
"item_7_managements_discussion_and_analysis": {
"operations_and_financial_results": null,
"liquidity_and_capital_resources": null,
"trends_and_uncertainties": null,
"critical_accounting_judgments": null,
"off_balance_sheet_arrangements": null,
"contractual_obligations": null,
"summary": null
},
"item_7a_quantitative_and_qualitative_disclosures_about_market_risk": {
"market_risk_exposures": null,
"risk_management": null,
"summary": null
},
"item_8_financial_statements_and_supplementary_data": {
"income_statement": null,
"balance_sheets": null,
"statement_of_cash_flows": null,
"statement_of_stockholders_equity": null,
"notes_to_financial_statements": null,
"auditors_report": null,
"summary": null
},
"item_9_changes_in_and_disagreements_with_accountants": {
"changes_in_accountants": null,
"disagreements": null,
"summary": null
},
"item_9a_controls_and_procedures": {
"disclosure_controls_and_procedures": null,
"internal_control_over_financial_reporting": null,
"summary": null
},
"item_9b_other_information": {
"other_information": null,
"summary": null
}
},
"part_III": {
"item_10_directors_executive_officers_and_corporate_governance": {
"directors_and_executive_officers": [],
"code_of_ethics": null,
"board_qualifications": null,
"summary": null
},
"item_11_executive_compensation": {
"compensation_policies_and_programs": null,
"executive_compensation": {},
"summary": null
},
"item_12_security_ownership": {
"ownership_information": {},
"equity_compensation_plans": null,
"summary": null
},
"item_13_certain_relationships_and_related_transactions": {
"relationships_and_transactions": [],
"director_independence": null,
"summary": null
},
"item_14_principal_accountant_fees_and_services": {
"fees_for_services": {},
"summary": null
}
},
"part_IV": {
"item_15_exhibits_financial_statement_schedules": {
"exhibits": [],
"financial_statement_schedules": null,
"summary": null
}
}
}
}
请注意,API中有可调配置,您可以在本书的GitHub仓库中查找代码中的具体调用。
结果后处理
一旦LLM处理完10-K报告,它将以结构化的JSON格式返回结果。这个JSON文档将包含提取的数据点,并以与10-K报告本身结构相一致的层次方式组织。为了有效地利用这些结果,我们需要解析JSON文档并提取相关信息。
我们管道中的下一步是将解析后的数据导入到数据库中,以便高效存储和检索。具体的导入策略将取决于我们选择使用的数据库类型。例如,如果我们选择使用关系型数据库,我们需要将提取的数据点映射到适当的表结构,确保适当的规范化并遵守数据完整性原则。
另一方面,如果我们决定使用文档数据库,导入过程将更加简单,因为这些数据库天生设计用于存储层次结构数据,如JSON文档。在这种情况下,我们可以直接导入解析后的JSON结果,利用数据库有效存储和查询复杂数据结构的能力。
无论选择哪种数据库类型,设计一个确保数据一致性、可扩展性和性能的导入策略都是至关重要的。这可能涉及实现批量导入、索引和分区等策略,以优化数据库的性能并确保高效检索提取的数据点。
除了存储提取的数据点外,我们还可以考虑为10-K报告的各个部分生成嵌入。嵌入是文本的向量表示,能够捕捉语义含义,从而支持高效的相似度搜索和检索。通过为报告部分生成嵌入,我们可以将我们的数据集与向量搜索管道集成,允许用户基于语义相似度执行高级查询。
关于嵌入生成和向量搜索集成的深入探讨,我们将在专门的章节中介绍检索增强生成(RAG)示例。本章将提供有关生成嵌入、构建向量数据库和实现高效向量搜索算法的详细见解,帮助您为GenAI应用程序创建强大的搜索和检索能力。
结果展示
在展示从处理10-K报告中获得的结果时,重要的是要考虑到这些结果被导入到数据库中的事实。这意味着,您需要做出的考虑与开发一个利用数据库中数据的体验类似。
其中一个主要的考虑因素是需要一个能够有效地聚合和分析数据库中存储数据的工具或平台。这可以是一个商业智能(BI)工具、数据可视化平台,甚至是一个为您的特定需求定制的自建应用程序。所选工具应提供强大的查询功能,使您能够从数据库中的不同表或集合中提取和组合数据。
此外,展示层应提供一系列可视化选项,如图表、图形和仪表板,以有效地传达从数据中得出的见解。这些可视化应具备交互性,允许用户从不同的角度探索数据、过滤和排序结果,并深入挖掘特定的兴趣领域。
进一步来说,展示层应以可扩展性和性能为设计重点。随着数据量随时间增长,处理大型数据集并提供响应式用户体验的能力变得至关重要。这可能涉及实现诸如缓存、索引和优化数据库查询等技术,以确保高效的数据检索和渲染。
在本章的GitHub目录中,您将找到完整的代码以及对本章中描述的各层如何协同工作的分析。
总结
在本章中,我们探讨了从财务文件中提取元数据,特别是上市公司提交的10-K报告。我们通过与一家金融服务公司合作,展示了如何利用大型语言模型(LLM)的数据提取能力,从这些庞大的10-K年报中提取关键信息。
我们定义了用例,并利用GenAI的强大能力导航结构化的10-K报告部分,定位并提取最相关的信息,遵循最佳实践文档提供的指导。我们详细介绍了这一过程,从构建有效的提示词(prompt)开始,指导AI模型。这涉及到研究美国证券交易委员会(SEC)提供的资源,了解投资者应该关注的关键部分和数据点。掌握这些知识后,我们可以不断优化提示词,确保提取的准确性和效率。
然后,我们提出了一个基于Google Cloud的云原生无服务器架构来处理这些文档的批处理。这个可扩展的架构可以利用Cloud Storage、Pub/Sub和Cloud Functions等各种服务,允许我们无缝集成AI模型并存储提取的数据。
本章还介绍了后处理步骤,如将提取的数据导入到数据库(关系型或文档型)中,可能为向量相似性搜索生成嵌入(embeddings),以及通过BI工具或自定义应用程序展示结果,并提供交互式可视化。
总而言之,本章为您提供了一个实用的蓝图,展示了如何利用GenAI增强从复杂财务文件中提取和分析关键信息的能力。它演示了如何利用这项技术做出更明智的决策,揭示有价值的洞察,从而优化您的运营效率和战略能力。
在下一章中,我们将研究一个摘要生成的用例。这个例子将展示批处理用例的另一种实现方式。