Hypergraph is more powerful. Transform unstructured text into structured knowledge with LLMs. Graphs, hypergraphs, and spatio-temporal extractions — with one command.
refactor(finance): 移除市场情绪模板并调整模板组织结构
yifanfeng97030f45ea00df
diff --git a/hyperextract/templates/README.md b/hyperextract/templates/README.mdindex 2c5cf32..04e7e05 100644--- a/hyperextract/templates/README.md+++ b/hyperextract/templates/README.md@@ -17,7 +17,6 @@ This library is organized by **Domain**, ensuring that the extraction logic matc| **`biology`** | Biological Science | Genomics, Proteomics, Ecology || **`legal`** | Legal & Compliance | Contracts, Judgments, Regulations || **`literature`** | Literature & Fiction | Scripts, Novels, World Building |-| **`travel`** | Travel & Geography | Itineraries, Guides, Spatial Maps || **`news`** | News & Media | Investigative Reports, Breaking News || **`agriculture`** | Agriculture | Crop Management, Soil Analysis || **`food`** | Food & Culinary | Recipes, Menu Engineering |@@ -53,13 +52,6 @@ The default choice for unstructured text that doesn't fit a specific industry.| **`SocialNetwork`** | `AutoGraph` | **Social Network**. Specifically captures interpersonal relationships and interactions. | Character studies, memoirs || **`PersonalProfile`** | `AutoModel` | **Personal Bio**. Aggregates static personal attributes (birth, education, career tags). | Profiles, CVs, obituary summaries |-* **Non-fiction Books (Popular Science)**: Books covering broad topics with cross-domain concepts and arguments.--| Template Name | Primitive | Description | Typical Use Case |-| :--- | :--- | :--- | :--- |-| **`ArgumentMap`** | `AutoHypergraph` | **Logical Argument Map**. Models complex logic: multiple premises supporting a conclusion. | Philosophical/Scientific books |-| **`ConceptEvolution`** | `AutoTemporalGraph` | **Conceptual History**. Tracks how a specific idea or term evolves throughout a text. | History of ideas, evolving concepts |-* **Regulations & Compliance**: Internal policies, administrative regulations, SOPs, and compliance guidelines.| Template Name | Primitive | Description | Typical Use Case |@@ -84,6 +76,8 @@ Optimized for complex financial relationships, market sentiment, and temporal ev| **`MaterialEventTimeline`** | `AutoTemporalGraph` | **8-K Event Timeline**. Chronologically extracts material events (executive changes, M&A announcements, restatements) from current reports. | Event-driven analysis, regulatory monitoring || **`SegmentPerformanceList`** | `AutoList` | **Segment Breakdown**. Extracts revenue, operating income, and key metrics by business segment or geographic region. | Segment-level valuation, geographic exposure analysis || **`FinancialDataTemporalGraph`** | `AutoTemporalGraph` | **Financial Data Timeline**. Builds a temporal graph of financial metrics across reporting periods, tracking how revenue, margins, segment contributions, and key ratios evolve over time. | Multi-period trend analysis, cross-period comparison, financial data lineage |+| **`RiskAssessmentGraph`** | `AutoGraph` | **Risk Assessment Graph**. Systematically extracts transmission paths from risk factors to financial impacts, including likelihood, severity, and mitigation strategies. | Risk monitoring, cross-filing comparative analysis |+| **`SupplyChainGraph`** | `AutoGraph` | **Supply Chain Graph**. Extracts supply chain entities, transaction relationships, and risk assessments to identify critical suppliers and geopolitical risks. | Supply chain resilience analysis, ESG report analysis |* **Equity Research Reports**: Detailed analysis by analysts containing ratings, target prices, and investment logic.diff --git a/hyperextract/templates/README_ZH.md b/hyperextract/templates/README_ZH.mdindex 46cb6e7..102a49b 100644--- a/hyperextract/templates/README_ZH.md+++ b/hyperextract/templates/README_ZH.md@@ -16,8 +16,7 @@| **`history`** | 历史文化 | 编年史、信札、口述历史 || **`biology`** | 生物科学 | 蛋白组、代谢通路、生态调查 || **`legal`** | 法律合规 | 合同协议、判决书、监管文件 |-| **`literature`** | 文学与剧情 | 剧本、小说、设定集 |-| **`travel`** | 旅游与地理 | 行程单、导览指南 |+| **`literature`** | 文学与影视 | 剧本、小说、设定集 || **`news`** | 新闻传媒 | 调查报道、突发快讯 || **`agriculture`** | 农业 | 巡田报告、土壤分析 || **`food`** | 美食餐饮 | 标准化食谱、菜单分析 |@@ -53,13 +52,6 @@| **`SocialNetwork`** | `AutoGraph` | **社交网络图**。专注于提取人际关系、互动与组织隶属。 | 传记分析、人物设定研究 || **`PersonalProfile`** | `AutoModel` | **个人档案**。聚合人物的静态属性(生卒年、学历、核心身份)。 | 简历、人物简介、讣告摘要 |-* **非虚构类书籍 (大社科/科普)**:跨领域、概念性的书籍,侧重提取通用概念与论点。--| 模板名称 | 底层原语 | 描述 | 典型应用场景 |-| :--- | :--- | :--- | :--- |-| **`ArgumentMap`** | `AutoHypergraph` | **论点逻辑图**。建模复杂的论证逻辑:多个前提支持一个结论。 | 学术著作、分析性散文 |-| **`ConceptEvolution`** | `AutoTemporalGraph` | **概念演变史**。追踪一个特定概念、词汇随文脉演变的轨迹。 | 思想史书籍、长篇论著 |-* **规章制度与合规文档**:公司内部管理制度、行政法规、操作手册、合规指南。| 模板名称 | 底层原语 | 描述 | 典型应用场景 |@@ -84,6 +76,8 @@| **`MaterialEventTimeline`** | `AutoTemporalGraph` | **重大事件时间轴**。按时间顺序提取 8-K 当期报告中的重大事件(高管变动、并购公告、财务重述等)。 | 事件驱动分析、合规监控 || **`SegmentPerformanceList`** | `AutoList` | **分部业绩明细**。按业务分部或地理区域提取营收、经营利润等关键指标。 | 分部估值、区域风险敞口分析 || **`FinancialDataTemporalGraph`** | `AutoTemporalGraph` | **财务数据时序图谱**。构建跨报告期的财务指标时序图谱,追踪营收、利润率、分部贡献及关键比率随时间的演变。 | 多期趋势分析、跨期对比、财务数据溯源 |+| **`RiskAssessmentGraph`** | `AutoGraph` | **风险评估图谱**。系统性提取风险因素到财务影响的传导路径,包含可能性、严重性及缓解措施。 | 风险监控、多报告期对比分析 |+| **`SupplyChainGraph`** | `AutoGraph` | **供应链图谱**。提取供应链主体、交易关系及风险评估,识别关键供应商和地缘政治风险。 | 供应链韧性分析、ESG 报告分析 |* **股票研究报告 (Equity Research)**:分析师撰写的深度报告,包含评级、目标价及投资逻辑相关内容。diff --git a/hyperextract/templates/en/finance/__init__.py b/hyperextract/templates/en/finance/__init__.pyindex fc3169e..0f3b717 100644--- a/hyperextract/templates/en/finance/__init__.py+++ b/hyperextract/templates/en/finance/__init__.py@@ -2,14 +2,9 @@Finance domain templates for extracting structured knowledge from financial documents.Includes templates for SEC filings, equity research, prospectuses, earnings calls,-financial news, risk assessment, market sentiment, and supply chain analysis.+financial news, risk assessment, and supply chain analysis."""-# Existing templates-from .risk_assessment import RiskAssessmentGraph-from .market_sentiment import MarketSentimentGraph-from .supply_chain import SupplyChainGraph-# SEC Filings (10-K / 10-Q / 8-K)from .filing_financial_snapshot import FilingFinancialSnapshotfrom .mda_narrative_graph import MDANarrativeGraph@@ -17,6 +12,8 @@ from .filing_risk_factor_set import FilingRiskFactorSetfrom .material_event_timeline import MaterialEventTimelinefrom .segment_performance_list import SegmentPerformanceListfrom .financial_data_temporal_graph import FinancialDataTemporalGraph+from .risk_assessment import RiskAssessmentGraph+from .supply_chain import SupplyChainGraph# Equity Research Reportsfrom .research_note_summary import ResearchNoteSummary@@ -43,10 +40,6 @@ from .multi_source_sentiment_hypergraph import MultiSourceSentimentHypergraphfrom .market_narrative_timeline import MarketNarrativeTimeline__all__ = [- # Existing templates- "RiskAssessmentGraph",- "MarketSentimentGraph",- "SupplyChainGraph",# SEC Filings"FilingFinancialSnapshot","MDANarrativeGraph",@@ -54,6 +47,8 @@ __all__ = ["MaterialEventTimeline","SegmentPerformanceList","FinancialDataTemporalGraph",+ "RiskAssessmentGraph",+ "SupplyChainGraph",# Equity Research"ResearchNoteSummary","FinancialForecast",diff --git a/hyperextract/templates/en/finance/market_sentiment.py b/hyperextract/templates/en/finance/market_sentiment.pydeleted file mode 100644index a0912d9..0000000--- a/hyperextract/templates/en/finance/market_sentiment.py+++ /dev/null@@ -1,187 +0,0 @@-from typing import Optional, Any-from pydantic import BaseModel, Field-from langchain_core.language_models.chat_models import BaseChatModel-from langchain_core.embeddings import Embeddings-from hyperextract.types import AutoGraph--# ==============================================================================-# 1. Schema Definitions-# ==============================================================================---class MarketDriver(BaseModel):- """- A macroeconomic or company-specific factor that influences market outlook.- """-- name: str = Field(- description="Name of the driver (e.g., 'AI Demand', 'Interest Rate Cut', 'Regulatory Clarity')."- )- driver_type: str = Field(- description="Type: 'Macro', 'Industry', 'Company-Specific', 'Catalyst'."- )- description: Optional[str] = Field(- None, description="Detailed explanation of why this driver matters."- )---class AnalystInfluence(BaseModel):- """- Represents how a market driver influences an asset's outlook.- """-- source: str = Field(description="The market driver name.")- target: str = Field(description="The affected asset/metric ticker.")- sentiment: str = Field(- description="Sentiment direction: 'Positive', 'Negative', 'Neutral'."- )- strength: str = Field(- description="Influence strength: 'Strong', 'Moderate', 'Weak'."- )- analyst_view: str = Field(- description="Analyst recommendation or target adjustment (e.g., 'Upgrade to Buy', 'Raise PT to $150')."- )- time_horizon: Optional[str] = Field(- None, description="Expected timeline (e.g., 'FY24', '6-12 months', 'Long-term')."- )- source_firm: Optional[str] = Field(- None, description="Name of the research firm or analyst source."- )---# ==============================================================================-# 2. Prompts-# ==============================================================================--_PROMPT = (- "You are an equity research analyst. Extract market drivers, target assets, and analyst sentiment "- "from research reports and macroeconomic commentary.\n\n"- "Rules:\n"- "- Identify market drivers (catalysts, policy changes, sector trends).\n"- "- Identify which assets (stocks, indices, bonds) are affected.\n"- "- Extract the sentiment direction and analyst recommendations.\n"- "- Capture time horizons and source attributions."-)--_NODE_PROMPT = (- "You are an equity research analyst. Extract all market drivers and target assets (Nodes) from the text.\n\n"- "Extraction Rules:\n"- "- Identify macroeconomic factors, policy shifts, and catalyst events.\n"- "- Identify tickers and specific financial metrics being discussed.\n"- "- Classify each node by type (Macro, Company-Specific, etc.).\n"- "- DO NOT establish how drivers influence assets at this stage."-)--_EDGE_PROMPT = (- "You are an equity research analyst. Given the list of market drivers and target assets, extract "- "the influence relationships (Edges).\n\n"- "Extraction Rules:\n"- "- Connect each driver to the specific assets it influences.\n"- "- Extract analyst sentiment (upgrade/downgrade language, target price changes).\n"- "- Classify sentiment direction and influence strength.\n"- "- Document the firm/analyst source and expected time horizon.\n"- "- Only create edges between nodes that exist in the provided lists."-)--# ==============================================================================-# 3. Template Class-# ==============================================================================---class MarketSentimentGraph(AutoGraph[MarketDriver, AnalystInfluence]):- """- Applicable to: Equity Research Reports, Macro Strategy Calls, Earnings Call Transcripts,- Macroeconomic Commentary, Analyst Alerts and Updates.-- Template for extracting and mapping market drivers to their expected impact on- asset valuations and analyst recommendations. Enables tracking of investment thesis- evolution and consensus shifts.-- Example Usage:- >>> from langchain_openai import ChatOpenAI, OpenAIEmbeddings- >>> llm = ChatOpenAI(model="gpt-4o")- >>> embedder = OpenAIEmbeddings()- >>> sentiment = MarketSentimentGraph(llm_client=llm, embedder=embedder)- >>> report = "AI demand acceleration is a key driver. We upgrade Tech stocks to Overweight..."- >>> sentiment.feed_text(report)- >>> sentiment.show()- """-- def __init__(- self,- llm_client: BaseChatModel,- embedder: Embeddings,- *,- extraction_mode: str = "one_stage",- chunk_size: int = 2048,- chunk_overlap: int = 256,- max_workers: int = 10,- verbose: bool = False,- **kwargs: Any,- ):- """- Initialize the Market Sentiment Graph template.-- Args:- llm_client (BaseChatModel): The LLM for driver and sentiment extraction.- embedder (Embeddings): Embedding model for deduplication.- extraction_mode (str): "one_stage" or "two_stage".- chunk_size (int): Max characters per chunk.- chunk_overlap (int): Overlap between chunks.- max_workers (int): Parallel processing workers.- verbose (bool): Enable progress logging.- **kwargs: Additional arguments for AutoGraph.- """- super().__init__(- node_schema=MarketDriver,- edge_schema=AnalystInfluence,- node_key_extractor=lambda x: x.name.strip().lower(),- edge_key_extractor=lambda x: (- f"{x.source.strip()}--({x.sentiment})-->{x.target.strip()}"- ),- nodes_in_edge_extractor=lambda x: (x.source.strip(), x.target.strip()),- llm_client=llm_client,- embedder=embedder,- extraction_mode=extraction_mode,- chunk_size=chunk_size,- chunk_overlap=chunk_overlap,- max_workers=max_workers,- verbose=verbose,- prompt=_PROMPT,- prompt_for_node_extraction=_NODE_PROMPT,- prompt_for_edge_extraction=_EDGE_PROMPT,- **kwargs,- )-- def show(- self,- *,- top_k_nodes_for_search: int = 3,- top_k_edges_for_search: int = 3,- top_k_nodes_for_chat: int = 3,- top_k_edges_for_chat: int = 3,- ) -> None:- """- Visualize the market sentiment graph using OntoSight.-- Args:- top_k_nodes_for_search (int): Number of drivers to retrieve. Default 3.- top_k_edges_for_search (int): Number of influences to retrieve. Default 3.- top_k_nodes_for_chat (int): Drivers for chat context. Default 3.- top_k_edges_for_chat (int): Influences for chat context. Default 3.- """-- def node_label_extractor(node: MarketDriver) -> str:- return f"{node.name} ({node.driver_type})"-- def edge_label_extractor(edge: AnalystInfluence) -> str:- return f"[{edge.sentiment}/{edge.strength}] {edge.analyst_view}"-- super().show(- node_label_extractor=node_label_extractor,- edge_label_extractor=edge_label_extractor,- top_k_nodes_for_search=top_k_nodes_for_search,- top_k_edges_for_search=top_k_edges_for_search,- top_k_nodes_for_chat=top_k_nodes_for_chat,- top_k_edges_for_chat=top_k_edges_for_chat,- )diff --git a/hyperextract/templates/zh/finance/__init__.py b/hyperextract/templates/zh/finance/__init__.pyindex a6c7795..68b50b4 100644--- a/hyperextract/templates/zh/finance/__init__.py+++ b/hyperextract/templates/zh/finance/__init__.py@@ -2,14 +2,9 @@金融领域模板,用于从财务文档中提取结构化知识。包含 SEC 文件、股票研究报告、招股说明书、财报电话会议、-金融新闻、风险评估、市场观点和供应链分析等模板。+金融新闻、风险评估和供应链分析等模板。"""-# 已有模板-from .risk_assessment import RiskAssessmentGraph-from .market_sentiment import MarketSentimentGraph-from .supply_chain import SupplyChainGraph-# SEC 文件 (10-K / 10-Q / 8-K)from .filing_financial_snapshot import FilingFinancialSnapshotfrom .mda_narrative_graph import MDANarrativeGraph@@ -17,6 +12,8 @@ from .filing_risk_factor_set import FilingRiskFactorSetfrom .material_event_timeline import MaterialEventTimelinefrom .segment_performance_list import SegmentPerformanceListfrom .financial_data_temporal_graph import FinancialDataTemporalGraph+from .risk_assessment import RiskAssessmentGraph+from .supply_chain import SupplyChainGraph# 股票研究报告from .research_note_summary import ResearchNoteSummary@@ -43,10 +40,6 @@ from .multi_source_sentiment_hypergraph import MultiSourceSentimentHypergraphfrom .market_narrative_timeline import MarketNarrativeTimeline__all__ = [- # 已有模板- "RiskAssessmentGraph",- "MarketSentimentGraph",- "SupplyChainGraph",# SEC 文件"FilingFinancialSnapshot","MDANarrativeGraph",@@ -54,6 +47,8 @@ __all__ = ["MaterialEventTimeline","SegmentPerformanceList","FinancialDataTemporalGraph",+ "RiskAssessmentGraph",+ "SupplyChainGraph",# 股票研究报告"ResearchNoteSummary","FinancialForecast",diff --git a/hyperextract/templates/zh/finance/market_sentiment.py b/hyperextract/templates/zh/finance/market_sentiment.pydeleted file mode 100644index a0759c8..0000000--- a/hyperextract/templates/zh/finance/market_sentiment.py+++ /dev/null@@ -1,174 +0,0 @@-from typing import Optional, Any-from pydantic import BaseModel, Field-from langchain_core.language_models.chat_models import BaseChatModel-from langchain_core.embeddings import Embeddings-from hyperextract.types import AutoGraph--# ==============================================================================-# 1. Schema 定义-# ==============================================================================---class MarketDriver(BaseModel):- """- 影响市场前景的宏观经济或公司特定因素。- """-- name: str = Field(- description='驱动因素名称,例如"AI 需求"、"降息预期"、"监管明确化"等。'- )- driver_type: str = Field(description='类型:"宏观"、"行业"、"公司特定"、"催化剂"。')- description: Optional[str] = Field(- None, description="为什么这个驱动因素重要的详细解释。"- )---class AnalystInfluence(BaseModel):- """- 表示市场驱动因素如何影响资产前景。- """-- source: str = Field(description="市场驱动因素名称。")- target: str = Field(description="受影响资产/指标的股票代码。")- sentiment: str = Field(description='情绪方向:"正面"、"负面"、"中性"。')- strength: str = Field(description='影响力强度:"强"、"中等"、"弱"。')- analyst_view: str = Field(- description='分析师建议或目标调整,例如"升级为买入"、"将目标价上调至 150 美元"等。'- )- time_horizon: Optional[str] = Field(- None, description='预期时间框架,例如"FY24"、"6-12 个月"、"长期"。'- )- source_firm: Optional[str] = Field(None, description="研究机构或分析师来源的名称。")---# ==============================================================================-# 2. 提示词 (Prompts)-# ==============================================================================--_PROMPT = (- "你是股票研究分析师。从研究报告和宏观经济评论中提取市场驱动因素、目标资产和分析师观点。\n\n"- "规则:\n"- "- 识别市场驱动因素(催化剂、政策变化、行业趋势)。\n"- "- 识别哪些资产(股票、指数、债券)受影响。\n"- "- 提取情绪方向和分析师建议。\n"- "- 记录时间框架和来源归属。"-)--_NODE_PROMPT = (- "你是股票研究分析师。从文本中提取所有市场驱动因素和目标资产(节点)。\n\n"- "提取规则:\n"- "- 识别宏观经济因素、政策变化和催化剂事件。\n"- "- 识别被讨论的股票代码和具体财务指标。\n"- "- 按类型分类每个节点(宏观、公司特定等)。\n"- "- 此阶段不建立驱动因素如何影响资产的关系。"-)--_EDGE_PROMPT = (- "你是股票研究分析师。在获得市场驱动因素和目标资产清单的基础上,提取影响关系(边)。\n\n"- "提取规则:\n"- "- 将每个驱动因素连接到它影响的具体资产。\n"- "- 提取分析师观点(升级/降级语言、目标价变化)。\n"- "- 分类情绪方向和影响力强度。\n"- "- 记录机构/分析师来源和预期时间框架。\n"- "- 仅在提供的列表中存在的节点之间创建边。"-)--# ==============================================================================-# 3. 模板类-# ==============================================================================---class MarketSentimentGraph(AutoGraph[MarketDriver, AnalystInfluence]):- """- 适用文档: 股票研究报告、宏观策略观点、财报电话会议记录、宏观经济评论、分析师警报和更新。-- 模板用于提取和映射市场驱动因素到其对资产估值和分析师建议的预期影响。支持跟踪投资主题演变和共识转变。-- 使用示例:- >>> from langchain_openai import ChatOpenAI, OpenAIEmbeddings- >>> llm = ChatOpenAI(model="gpt-4o")- >>> embedder = OpenAIEmbeddings()- >>> sentiment = MarketSentimentGraph(llm_client=llm, embedder=embedder)- >>> report = "AI 需求加速是关键驱动因素。我们将科技股升级为增持..."- >>> sentiment.feed_text(report)- >>> sentiment.show()- """-- def __init__(- self,- llm_client: BaseChatModel,- embedder: Embeddings,- *,- extraction_mode: str = "one_stage",- chunk_size: int = 2048,- chunk_overlap: int = 256,- max_workers: int = 10,- verbose: bool = False,- **kwargs: Any,- ):- """- 初始化市场观点图谱模板。-- Args:- llm_client (BaseChatModel): 用于驱动因素和观点提取的 LLM。- embedder (Embeddings): 用于去重的嵌入模型。- extraction_mode (str): "one_stage" 或 "two_stage"。- chunk_size (int): 每个分块的最大字符数。- chunk_overlap (int): 分块之间的重叠。- max_workers (int): 并行处理工作线程数。- verbose (bool): 是否启用进度日志。- **kwargs: AutoGraph 的其他参数。- """- super().__init__(- node_schema=MarketDriver,- edge_schema=AnalystInfluence,- node_key_extractor=lambda x: x.name.strip().lower(),- edge_key_extractor=lambda x: (- f"{x.source.strip()}--({x.sentiment})-->{x.target.strip()}"- ),- nodes_in_edge_extractor=lambda x: (x.source.strip(), x.target.strip()),- llm_client=llm_client,- embedder=embedder,- extraction_mode=extraction_mode,- chunk_size=chunk_size,- chunk_overlap=chunk_overlap,- max_workers=max_workers,- verbose=verbose,- prompt=_PROMPT,- prompt_for_node_extraction=_NODE_PROMPT,- prompt_for_edge_extraction=_EDGE_PROMPT,- **kwargs,- )-- def show(- self,- *,- top_k_nodes_for_search: int = 3,- top_k_edges_for_search: int = 3,- top_k_nodes_for_chat: int = 3,- top_k_edges_for_chat: int = 3,- ) -> None:- """- 使用 OntoSight 可视化市场观点图。-- Args:- top_k_nodes_for_search (int): 检索的驱动因素数。默认 3。- top_k_edges_for_search (int): 检索的影响关系数。默认 3。- top_k_nodes_for_chat (int): 对话上下文中的驱动因素数。默认 3。- top_k_edges_for_chat (int): 对话上下文中的影响关系数。默认 3。- """-- def node_label_extractor(node: MarketDriver) -> str:- return f"{node.name} ({node.driver_type})"-- def edge_label_extractor(edge: AnalystInfluence) -> str:- return f"[{edge.sentiment}/{edge.strength}] {edge.analyst_view}"-- super().show(- node_label_extractor=node_label_extractor,- edge_label_extractor=edge_label_extractor,- top_k_nodes_for_search=top_k_nodes_for_search,- top_k_edges_for_search=top_k_edges_for_search,- top_k_nodes_for_chat=top_k_nodes_for_chat,- top_k_edges_for_chat=top_k_edges_for_chat,- )