Hypergraph is more powerful. Transform unstructured text into structured knowledge with LLMs. Graphs, hypergraphs, and spatio-temporal extractions — with one command.
feat(模板): 优化书信知识图谱模板并添加时间处理功能
yifanfeng9786ec53f2b3f5
diff --git a/examples/template_history_demo.py b/examples/template_history_demo.pyindex d762511..08ebddd 100644--- a/examples/template_history_demo.py+++ b/examples/template_history_demo.py@@ -78,8 +78,8 @@ TEMPLATE_TO_INPUT = {# 口述历史# TEMPLATE = PersonalTrajectoryHypergraph-# TEMPLATE = NarrativeRelationGraph-TEMPLATE = MemoryFlashbackList+TEMPLATE = NarrativeRelationGraph+# TEMPLATE = MemoryFlashbackList# 档案馆藏信札# TEMPLATE = EpistolaryKnowledgeGraphdiff --git a/hyperextract/templates/zh/history/epistolary_knowledge_graph.py b/hyperextract/templates/zh/history/epistolary_knowledge_graph.pyindex b0c101c..9b34993 100644--- a/hyperextract/templates/zh/history/epistolary_knowledge_graph.py+++ b/hyperextract/templates/zh/history/epistolary_knowledge_graph.py@@ -3,17 +3,17 @@适用于档案馆藏信札、历史人物书信集。"""+from datetime import datetimefrom typing import Any, Listfrom pydantic import BaseModel, Fieldfrom langchain_core.language_models import BaseChatModelfrom langchain_core.embeddings import Embeddings-from hyperextract.types import AutoGraph+from hyperextract.types import AutoTemporalGraphclass CorrespondentNode(BaseModel):"""书信人物节点"""name: str = Field(description="人物姓名")- role: str = Field(description="角色:写信人、收信人")identity: str = Field(description="身份描述,如北宋文学家、东吴大都督", default="")description: str = Field(description="人物描述", default="")@@ -42,7 +42,6 @@ _NODE_PROMPT = """## 角色与任务2. 实体名称与原文保持一致### 领域特定规则-- 人物角色:写信人、收信人- 人物要包含身份描述### 源文本:@@ -54,6 +53,7 @@ _EDGE_PROMPT = """## 角色与任务## 核心概念定义- **节点 (Node)**:从文档中提取的书信相关人物- **边 (Edge)**:书信内容及其中提及的关系+- **时间**:信件书写或发送的时间## 提取规则### 核心约束@@ -62,10 +62,22 @@ _EDGE_PROMPT = """## 角色与任务### 领域特定规则- 信件主题:信件讨论的主要议题-- 提及人物:信件正文中提到的人物-- 提及事件:信件正文中提到的事件+- 提及人物:必须仔细阅读信件正文,提取正文中明确提到的人物姓名,整理为列表+- 提及事件:必须仔细阅读信件正文,提取正文中明确提到的事件,整理为列表- 内容摘要:信件的主要内容概括+- 重要:如果正文未提及,则留空列表,不要编造+### 时间解析规则+当前观察日期: {observation_time}++1. 相对时间解析(基于观察日期):+ - "去年" → {observation_time} 的前一年+ - "上月" → {observation_time} 的前一个月+ - "本季度" → {observation_time} 所在季度+ - "近期" → {observation_time} 最近 3 个月++2. 精确时间 → 保持原样+3. 时间缺失 → 留空,不要猜测"""_PROMPT = """## 角色与任务@@ -74,6 +86,7 @@ _PROMPT = """## 角色与任务## 核心概念定义- **节点 (Node)**:从文档中提取的书信相关人物- **边 (Edge)**:书信内容及其中提及的关系+- **时间**:信件书写或发送的时间## 提取规则### 核心约束@@ -83,18 +96,30 @@ _PROMPT = """## 角色与任务4. 关系描述应与原文保持一致### 领域特定规则-- 人物角色:写信人、收信人- 人物要包含身份描述- 信件主题:信件讨论的主要议题-- 提及人物:信件正文中提到的人物-- 提及事件:信件正文中提到的事件+- 提及人物:必须仔细阅读信件正文,提取正文中明确提到的人物姓名,整理为列表+- 提及事件:必须仔细阅读信件正文,提取正文中明确提到的事件,整理为列表- 内容摘要:信件的主要内容概括+- 重要:如果正文未提及,则留空列表,不要编造++### 时间解析规则+当前观察日期: {observation_time}++1. 相对时间解析(基于观察日期):+ - "去年" → {observation_time} 的前一年+ - "上月" → {observation_time} 的前一个月+ - "本季度" → {observation_time} 所在季度+ - "近期" → {observation_time} 最近 3 个月++2. 精确时间 → 保持原样+3. 时间缺失 → 留空,不要猜测### 源文本:"""-class EpistolaryKnowledgeGraph(AutoGraph[CorrespondentNode, LetterContentEdge]):+class EpistolaryKnowledgeGraph(AutoTemporalGraph[CorrespondentNode, LetterContentEdge]):"""适用文档: 档案馆藏信札、历史人物书信集@@ -117,6 +142,7 @@ class EpistolaryKnowledgeGraph(AutoGraph[CorrespondentNode, LetterContentEdge]):embedder: Embeddings,*,extraction_mode: str = "two_stage",+ observation_time: str | None = None,chunk_size: int = 512,chunk_overlap: int = 64,max_workers: int = 10,@@ -131,20 +157,27 @@ class EpistolaryKnowledgeGraph(AutoGraph[CorrespondentNode, LetterContentEdge]):embedder: 嵌入模型,用于语义检索extraction_mode: 提取模式,可选 "one_stage"(同时提取节点和边)或 "two_stage"(先提取节点,再提取边),默认为 "two_stage"+ observation_time: 观察时间,用于解析相对时间表达,+ 如未指定则使用当前日期chunk_size: 每个分块的最大字符数,默认为 512(历史文本信息量大,需较小分块)chunk_overlap: 分块之间的重叠字符数,默认为 64max_workers: 最大工作线程数,默认为 10verbose: 是否输出详细日志,默认为 False**kwargs: 其他技术参数,传递给基类"""+ if observation_time is None:+ observation_time = datetime.now().strftime("%Y-%m-%d")+super().__init__(node_schema=CorrespondentNode,edge_schema=LetterContentEdge,node_key_extractor=lambda x: x.name,edge_key_extractor=lambda x: f"{x.source}|{x.subject}|{x.target}",+ time_in_edge_extractor=lambda x: x.time or "",nodes_in_edge_extractor=lambda x: (x.source, x.target),llm_client=llm_client,embedder=embedder,+ observation_time=observation_time,extraction_mode=extraction_mode,max_workers=max_workers,verbose=verbose,@@ -174,10 +207,12 @@ class EpistolaryKnowledgeGraph(AutoGraph[CorrespondentNode, LetterContentEdge]):top_k_edges_for_chat: 问答使用的边数量,默认为 3"""def node_label_extractor(node: CorrespondentNode) -> str:- return f"{node.name}[{node.role}]"+ return node.namedef edge_label_extractor(edge: LetterContentEdge) -> str:- return f"书信: {edge.subject}"+ if edge.time:+ return f"{edge.subject} ({edge.time})"+ return f"{edge.subject}"super().show(node_label_extractor=node_label_extractor,