Skip to content

Hypergraph is more powerful. Transform unstructured text into structured knowledge with LLMs. Graphs, hypergraphs, and spatio-temporal extractions — with one command.

refactor(templates): 统一参数命名并优化模板描述

yifanfeng97d63398b5fde2
diff --git a/examples/template_medicine_demo.py b/examples/template_medicine_demo.py
index 5729b53..56631d8 100644
--- a/examples/template_medicine_demo.py
+++ b/examples/template_medicine_demo.py
@@ -84,11 +84,11 @@ TEMPLATE_TO_INPUT = {
# ==========================================
# 医学教科书与专著
-# TEMPLATE = PathologyHypergraph # 多因素病理机制图
+TEMPLATE = PathologyHypergraph # 多因素病理机制图
# TEMPLATE = MedicalConceptNet # 医学概念网络
# TEMPLATE = PharmacologyGraph # 药理机制图
# TEMPLATE = AnatomyHierarchy # 解剖层级树
-TEMPLATE = SymptomDifferential # 鉴别诊断表
+# TEMPLATE = SymptomDifferential # 鉴别诊断表
# 临床诊疗指南
# TEMPLATE = TreatmentRegimenMap # 综合治疗方案图
diff --git a/hyperextract/templates/en/news/opinion_set.py b/hyperextract/templates/en/news/opinion_set.py
index d7d62db..bab2988 100644
--- a/hyperextract/templates/en/news/opinion_set.py
+++ b/hyperextract/templates/en/news/opinion_set.py
@@ -66,4 +66,4 @@ class OpinionSet(AutoSet[OpinionStance]):
def show(self, **kwargs):
def label_func(item: OpinionStance) -> str:
return f"[{item.stance}] {item.entity}: {item.key_argument}"
- super().show(label_extractor=label_func, **kwargs)
+ super().show(item_label_extractor=label_func, **kwargs)
diff --git a/hyperextract/templates/zh/food/culinary_dish.py b/hyperextract/templates/zh/food/culinary_dish.py
index 9a51833..f11e5ee 100644
--- a/hyperextract/templates/zh/food/culinary_dish.py
+++ b/hyperextract/templates/zh/food/culinary_dish.py
@@ -85,8 +85,8 @@ class CulinaryDishSet(AutoSet[CulinaryDish]):
def show(
self,
*,
- top_k_items_for_search: int = 10,
- top_k_items_for_chat: int = 10,
+ top_k_for_search: int = 10,
+ top_k_for_chat: int = 10,
) -> None:
def item_label_extractor(item: CulinaryDish) -> str:
parts = [item.name]
@@ -96,6 +96,6 @@ class CulinaryDishSet(AutoSet[CulinaryDish]):
super().show(
item_label_extractor=item_label_extractor,
- top_k_items_for_search=top_k_items_for_search,
- top_k_items_for_chat=top_k_items_for_chat,
+ top_k_for_search=top_k_for_search,
+ top_k_for_chat=top_k_for_chat,
)
diff --git a/hyperextract/templates/zh/general/concept_hierarchy.py b/hyperextract/templates/zh/general/concept_hierarchy.py
index deda89a..c705653 100644
--- a/hyperextract/templates/zh/general/concept_hierarchy.py
+++ b/hyperextract/templates/zh/general/concept_hierarchy.py
@@ -1,9 +1,9 @@
-"""概念层级图 - 构建上下位关系(Subclass-Of)或组成关系(Part-Of)。
+"""概念层级图 - 构建上下位关系或组成关系。
适用于科学学科、教材知识点等。
"""
-from typing import Any, Optional
+from typing import Any
from pydantic import BaseModel, Field
from langchain_core.language_models import BaseChatModel
from langchain_core.embeddings import Embeddings
@@ -24,7 +24,7 @@ class HierarchyRelation(BaseModel):
source: str = Field(description="父概念/整体概念")
target: str = Field(description="子概念/部分概念")
relationType: str = Field(
- description="关系类型:Subclass-Of(上下位)、Part-Of(组成)、Instance-Of(实例)、Other(其他)"
+ description="关系类型:上下位(Subclass-Of)、组成(Part-Of)、实例(Instance-Of)、其他(Other)"
)
details: str = Field(description="关系详细说明", default="")
@@ -34,7 +34,7 @@ _PROMPT = """## 角色与任务
## 核心概念定义
- **节点 (Node)**:本模板中的"节点"指概念单元,分为核心概念、子概念、属性、实例等类型,用于表示知识体系中的基本概念。
-- **边 (Edge)**:本模板中的"边"指概念之间的层级关系,包括 Subclass-Of(上下位)、Part-Of(组成)、Instance-Of(实例)等二元关系。
+- **边 (Edge)**:本模板中的"边"指概念之间的层级关系,包括上下位(Subclass-Of)、组成(Part-Of)、实例(Instance-Of)等二元关系。
## 提取规则
### 节点提取规则
@@ -45,10 +45,10 @@ _PROMPT = """## 角色与任务
### 关系提取规则
1. 仅从提取的概念中创建边
2. 关系类型包括:
- - Subclass-Of:上下位关系(如"狗"是"动物"的子类)
- - Part-Of:组成关系(如"轮胎"是"汽车"的一部分)
- - Instance-Of:实例关系(如"祖冲之"是"数学家"的实例)
- - Other:其他关系
+ - 上下位(Subclass-Of):如"狗"是"动物"的子类
+ - 组成(Part-Of):如"轮胎"是"汽车"的一部分
+ - 实例(Instance-Of):如"祖冲之"是"数学家"的实例
+ - 其他(Other)
3. 每条边必须连接已提取的节点
### 约束条件
@@ -78,14 +78,14 @@ _EDGE_PROMPT = """## 角色与任务
## 核心概念定义
- **节点 (Node)**:本模板中的"节点"指概念单元,作为层级关系的参与者。
-- **边 (Edge)**:本模板中的"边"指概念之间的层级关系,包括 Subclass-Of(上下位)、Part-Of(组成)、Instance-Of(实例)等二元关系。
+- **边 (Edge)**:本模板中的"边"指概念之间的层级关系,包括上下位(Subclass-Of)、组成(Part-Of)、实例(Instance-Of)等二元关系。
## 提取规则
### 关系类型说明
-- Subclass-Of:上下位关系(子类与父类)
-- Part-Of:组成关系(部分与整体)
-- Instance-Of:实例关系(实例与类)
-- Other:其他关系
+- 上下位(Subclass-Of):子类与父类
+- 组成(Part-Of):部分与整体
+- 实例(Instance-Of):实例与类
+- 其他(Other)
### 约束条件
1. 仅从下方已知概念列表中提取边
@@ -100,7 +100,7 @@ class ConceptHierarchy(AutoGraph[ConceptNode, HierarchyRelation]):
适用文档: 科学学科教材、知识库文档、分类体系说明
功能介绍:
- 构建上下位关系(Subclass-Of)或组成关系(Part-Of)。适用于科学学科、教材知识点等。
+ 构建上下位关系或组成关系。适用于科学学科、教材知识点等。
Example:
>>> template = ConceptHierarchy(llm_client=llm, embedder=embedder)
diff --git a/hyperextract/templates/zh/medicine/adverse_reaction_stats.py b/hyperextract/templates/zh/medicine/adverse_reaction_stats.py
index 964cc43..6af65b3 100644
--- a/hyperextract/templates/zh/medicine/adverse_reaction_stats.py
+++ b/hyperextract/templates/zh/medicine/adverse_reaction_stats.py
@@ -97,6 +97,6 @@ class AdverseReactionStats(AutoList[AdverseReactionItem]):
super().show(
item_label_extractor=item_label_extractor,
- top_k_items_for_search=top_k_for_search,
- top_k_items_for_chat=top_k_for_chat,
+ top_k_for_search=top_k_for_search,
+ top_k_for_chat=top_k_for_chat,
)
\ No newline at end of file
diff --git a/hyperextract/templates/zh/medicine/anatomy_hierarchy.py b/hyperextract/templates/zh/medicine/anatomy_hierarchy.py
index b50a6f1..9b9078e 100644
--- a/hyperextract/templates/zh/medicine/anatomy_hierarchy.py
+++ b/hyperextract/templates/zh/medicine/anatomy_hierarchy.py
@@ -1,4 +1,4 @@
-"""解剖层级树 - 构建解剖位置的包含 (Part-Of) 和毗邻 (Connected-To) 关系。
+"""解剖层级树 - 构建解剖位置的包含和毗邻关系。
适用于医学教科书与专著中关于解剖学的内容。
"""
@@ -24,7 +24,7 @@ class AnatomyRelation(BaseModel):
source: str = Field(description="源解剖结构")
target: str = Field(description="目标解剖结构")
relationType: str = Field(
- description="关系类型:包含(Part-Of)、毗邻(Connected-To)、分支、供应等"
+ description="关系类型:包含、毗邻、分支、供应等"
)
details: str = Field(description="详细描述", default="")
@@ -34,7 +34,7 @@ _PROMPT = """## 角色与任务
## 核心概念定义
- **节点 (Node)**:本模板中的"节点"指解剖实体,包括器官、组织、血管、神经、骨骼等类型,用于表示解剖学中的基本结构。
-- **边 (Edge)**:本模板中的"边"指解剖结构之间的关系,包括包含(Part-Of)、毗邻(Connected-To)、分支、供应等关系类型。
+- **边 (Edge)**:本模板中的"边"指解剖结构之间的关系,包括包含、毗邻、分支、供应等关系类型。
## 提取规则
### 节点提取规则
@@ -44,7 +44,7 @@ _PROMPT = """## 角色与任务
### 关系提取规则
1. 仅从提取的解剖结构中创建关系边
-2. 关系类型包括:包含(Part-Of)、毗邻(Connected-To)、分支、供应等
+2. 关系类型包括:包含、毗邻、分支、供应等
3. 为每个关系添加详细描述
### 约束条件
@@ -74,11 +74,11 @@ _EDGE_PROMPT = """## 角色与任务
## 核心概念定义
- **节点 (Node)**:本模板中的"节点"指解剖实体,作为解剖关系的参与者。
-- **边 (Edge)**:本模板中的"边"指解剖结构之间的关系,包括包含(Part-Of)、毗邻(Connected-To)、分支、供应等关系类型。
+- **边 (Edge)**:本模板中的"边"指解剖结构之间的关系,包括包含、毗邻、分支、供应等关系类型。
## 提取规则
1. 仅从下方已知解剖结构列表中提取边
-2. 关系类型包括:包含(Part-Of)、毗邻(Connected-To)、分支、供应等
+2. 关系类型包括:包含、毗邻、分支、供应等
3. 为每个关系添加详细描述
### 约束条件
@@ -93,7 +93,7 @@ class AnatomyHierarchy(AutoGraph[AnatomyEntity, AnatomyRelation]):
适用文档: 医学教科书、医学专著、解剖学图谱
功能介绍:
- 构建解剖位置的包含 (Part-Of) 和毗邻 (Connected-To) 关系,适用于解剖学图谱、手术导航基础。
+ 构建解剖位置的包含和毗邻关系,适用于解剖学图谱、手术导航基础。
Example:
>>> template = AnatomyHierarchy(llm_client=llm, embedder=embedder)
diff --git a/hyperextract/templates/zh/medicine/clinical_pathway.py b/hyperextract/templates/zh/medicine/clinical_pathway.py
index 8bc6c43..6fc1225 100644
--- a/hyperextract/templates/zh/medicine/clinical_pathway.py
+++ b/hyperextract/templates/zh/medicine/clinical_pathway.py
@@ -1,4 +1,4 @@
-"""临床路径图 - 提取"If 症状A Then 检查B Else 治疗C"的决策树结构。
+"""临床路径图 - 提取"如果症状A则检查B否则治疗C"的决策树结构。
适用于临床诊疗指南中关于决策路径的内容。
"""
@@ -23,12 +23,12 @@ class ClinicalEdge(BaseModel):
source: str = Field(description="源节点")
target: str = Field(description="目标节点")
- condition: str = Field(description="条件描述,如If 症状A Then")
+ condition: str = Field(description="条件描述,如如果症状A则")
details: str = Field(description="详细描述", default="")
_PROMPT = """## 角色与任务
-你是一位专业的临床医生,请从文本中提取"If 症状A Then 检查B Else 治疗C"的决策树结构,构建临床路径图。
+你是一位专业的临床医生,请从文本中提取"如果症状A则检查B否则治疗C"的决策树结构,构建临床路径图。
## 核心概念定义
- **节点 (Node)**:本模板中的"节点"指临床节点,包括症状、检查、治疗、诊断、决策点等类型,用于表示临床路径中的基本要素。
@@ -42,7 +42,7 @@ _PROMPT = """## 角色与任务
### 边提取规则
1. 仅从提取的节点中创建边
-2. 边应包含条件描述,如"If 症状A Then"、"Else"等
+2. 边应包含条件描述,如"如果症状A则"、"否则"等
3. 为每个边添加详细描述,说明具体的决策逻辑
### 约束条件
@@ -76,7 +76,7 @@ _EDGE_PROMPT = """## 角色与任务
## 提取规则
1. 仅从下方已知节点列表中提取边
-2. 边应包含条件描述,如"If 症状A Then"、"Else"等
+2. 边应包含条件描述,如"如果症状A则"、"否则"等
3. 为每个边添加详细描述,说明具体的决策逻辑
### 约束条件
@@ -91,7 +91,7 @@ class ClinicalPathway(AutoGraph[ClinicalNode, ClinicalEdge]):
适用文档: 临床诊疗指南、决策路径手册
功能介绍:
- 提取"If 症状A Then 检查B Else 治疗C"的决策树结构,适用于临床路径管理、诊疗规范化。
+ 提取"如果症状A则检查B否则治疗C"的决策树结构,适用于临床路径管理、诊疗规范化。
Example:
>>> template = ClinicalPathway(llm_client=llm, embedder=embedder)
diff --git a/hyperextract/templates/zh/medicine/contraindication_list.py b/hyperextract/templates/zh/medicine/contraindication_list.py
index cab3d68..c05719a 100644
--- a/hyperextract/templates/zh/medicine/contraindication_list.py
+++ b/hyperextract/templates/zh/medicine/contraindication_list.py
@@ -97,6 +97,6 @@ class ContraindicationList(AutoList[ContraindicationItem]):
super().show(
item_label_extractor=item_label_extractor,
- top_k_items_for_search=top_k_for_search,
- top_k_items_for_chat=top_k_for_chat,
+ top_k_for_search=top_k_for_search,
+ top_k_for_chat=top_k_for_chat,
)
\ No newline at end of file
diff --git a/hyperextract/templates/zh/medicine/level_of_evidence.py b/hyperextract/templates/zh/medicine/level_of_evidence.py
index 04de284..047e85c 100644
--- a/hyperextract/templates/zh/medicine/level_of_evidence.py
+++ b/hyperextract/templates/zh/medicine/level_of_evidence.py
@@ -13,6 +13,8 @@ from hyperextract.types import AutoList
class EvidenceItem(BaseModel):
"""证据条目"""
+ topic: str = Field(description="推荐主题,如血糖控制、血脂管理、阿司匹林使用等")
+ summary: str = Field(description="推荐意见摘要,简短概括该推荐的核心内容,不超过10字")
recommendation: str = Field(description="推荐意见")
evidenceLevel: str = Field(description="证据等级,如A、B、C、1a、1b等")
recommendationLevel: str = Field(description="推荐级别,如强推荐、弱推荐等")
@@ -22,14 +24,13 @@ class EvidenceItem(BaseModel):
_PROMPT = """## 角色与任务
你是一位专业的循证医学专家,请从文本中提取指南中的具体推荐意见及其对应的证据等级,构建证据评级表。
-## 核心概念定义
-- **条目 (Item)**:本模板中的"条目"指证据条目,包含推荐意见、证据等级、推荐级别和详细描述的结构化信息。
-
## 提取规则
1. 提取所有推荐意见及其对应的证据等级
-2. 为每个推荐意见指定推荐级别(如强推荐、弱推荐等)
-3. 为每个推荐意见添加详细描述(如果文本中提供)
-4. 保持推荐意见与原文一致
+2. 为每个推荐意见指定推荐主题(topic),如血糖控制、血脂管理、阿司匹林使用等
+3. 为每个推荐意见提取摘要(summary),简短概括该推荐的核心内容,不超过10字
+4. 为每个推荐意见指定推荐级别(如强推荐、弱推荐等)
+5. 为每个推荐意见添加详细描述(如果文本中提供)
+6. 保持推荐意见与原文一致
### 约束条件
- 只提取文本中明确提及的推荐意见和证据等级
@@ -96,10 +97,10 @@ class LevelOfEvidence(AutoList[EvidenceItem]):
"""
def item_label_extractor(item: EvidenceItem) -> str:
- return f"{item.recommendationLevel} (证据等级: {item.evidenceLevel})"
+ return f"{item.topic} [{item.evidenceLevel}]"
super().show(
item_label_extractor=item_label_extractor,
- top_k_items_for_search=top_k_for_search,
- top_k_items_for_chat=top_k_for_chat,
+ top_k_for_search=top_k_for_search,
+ top_k_for_chat=top_k_for_chat,
)
diff --git a/hyperextract/templates/zh/medicine/microscopic_feature_set.py b/hyperextract/templates/zh/medicine/microscopic_feature_set.py
index 53b8f78..97a34ff 100644
--- a/hyperextract/templates/zh/medicine/microscopic_feature_set.py
+++ b/hyperextract/templates/zh/medicine/microscopic_feature_set.py
@@ -97,11 +97,11 @@ class MicroscopicFeatureSet(AutoSet[MicroscopicFeatureItem]):
top_k_for_chat: 问答使用的条目数量,默认为 3
"""
- def label_extractor(item: MicroscopicFeatureItem) -> str:
+ def item_label_extractor(item: MicroscopicFeatureItem) -> str:
return f"{item.featureName} ({item.featureType}): {item.value}"
super().show(
- label_extractor=label_extractor,
- top_k_items_for_search=top_k_for_search,
- top_k_items_for_chat=top_k_for_chat,
+ item_label_extractor=item_label_extractor,
+ top_k_for_search=top_k_for_search,
+ top_k_for_chat=top_k_for_chat,
)
diff --git a/hyperextract/templates/zh/medicine/pathology_hypergraph.py b/hyperextract/templates/zh/medicine/pathology_hypergraph.py
index 015bb22..2c2b8b7 100644
--- a/hyperextract/templates/zh/medicine/pathology_hypergraph.py
+++ b/hyperextract/templates/zh/medicine/pathology_hypergraph.py
@@ -23,10 +23,17 @@ class PathologyEntity(BaseModel):
class PathologyHyperedge(BaseModel):
"""病理机制超边"""
- name: str = Field(description="超边名称,如致病机制、发病过程等")
- nodes: List[str] = Field(description="参与超边的节点列表")
+ mechanism_name: str = Field(
+ description="机制名称,简短描述性名称,例如 '气道高反应性诱发机制'、'α1-抗胰蛋白酶缺乏致病途径'"
+ )
+ participating_entities: List[str] = Field(
+ description="参与此病理机制的所有实体名称列表"
+ )
relationType: str = Field(description="关系类型:致病、诱发、促进、抑制等")
details: str = Field(description="详细描述,如具体的致病机制")
+ outcome: str = Field(
+ description="该机制导致的结果或效应,例如 '慢性阻塞性肺疾病发生'、'肺气肿形成'"
+ )
_PROMPT = """## 角色与任务
@@ -44,9 +51,11 @@ _PROMPT = """## 角色与任务
### 超边提取规则
1. 仅从提取的实体中创建超边
-2. 超边应表示多个实体之间的复杂关联关系,如"基因+环境+诱因 -> 疾病"
-3. 关系类型包括:致病、诱发、促进、抑制等
-4. 为每个超边添加详细描述,说明具体的致病机制
+2. 超边应表示多个实体之间的复杂关联关系
+3. **mechanism_name 命名要求**:使用简短、清晰的描述性名称,例如 "COPD 环境致病机制"、"气道高反应性诱发机制",不要使用 "A+B+C->D" 这种格式
+4. 关系类型包括:致病、诱发、促进、抑制等
+5. 为每个超边添加详细描述,说明具体的致病机制
+6. outcome 描述该机制导致的结果
### 约束条件
- 每条超边必须连接多个已提取的节点
@@ -79,9 +88,15 @@ _EDGE_PROMPT = """## 角色与任务
## 提取规则
1. 仅从下方已知实体列表中创建超边
-2. 超边应表示多个实体之间的复杂关联关系,如"基因+环境+诱因 -> 疾病"
-3. 关系类型包括:致病、诱发、促进、抑制等
-4. 为每个超边添加详细描述,说明具体的致病机制
+2. 超边应表示多个实体之间的复杂关联关系
+3. **mechanism_name 命名要求**:
+ - 使用简短、清晰、描述性的中文名称
+ - 例如:"COPD 环境致病机制"、"气道高反应性诱发机制"、"α1-抗胰蛋白酶缺乏致病途径"
+ - 绝对不要使用 "基因+环境+诱因->疾病" 或类似的拼接格式
+ - 名称应该像一个医学术语或机制名称
+4. 关系类型包括:致病、诱发、促进、抑制等
+5. 为每个超边添加详细描述,说明具体的致病机制
+6. outcome 描述该机制导致的结果或效应
### 约束条件
1. 不要创建未列出的实体
@@ -129,8 +144,8 @@ class PathologyHypergraph(AutoHypergraph[PathologyEntity, PathologyHyperedge]):
node_schema=PathologyEntity,
edge_schema=PathologyHyperedge,
node_key_extractor=lambda x: x.name,
- edge_key_extractor=lambda x: x.name,
- nodes_in_edge_extractor=lambda x: x.nodes,
+ edge_key_extractor=lambda x: x.mechanism_name,
+ nodes_in_edge_extractor=lambda x: x.participating_entities,
llm_client=llm_client,
embedder=embedder,
extraction_mode=extraction_mode,
@@ -164,7 +179,7 @@ class PathologyHypergraph(AutoHypergraph[PathologyEntity, PathologyHyperedge]):
return f"{node.name} ({node.category})"
def edge_label_extractor(edge: PathologyHyperedge) -> str:
- return f"{edge.name}: {edge.relationType}"
+ return f"[{edge.relationType}] {edge.mechanism_name}"
super().show(
node_label_extractor=node_label_extractor,
diff --git a/hyperextract/templates/zh/medicine/symptom_differential.py b/hyperextract/templates/zh/medicine/symptom_differential.py
index 6459f1c..1a4bc2e 100644
--- a/hyperextract/templates/zh/medicine/symptom_differential.py
+++ b/hyperextract/templates/zh/medicine/symptom_differential.py
@@ -93,11 +93,11 @@ class SymptomDifferential(AutoSet[SymptomDifferentialItem]):
top_k_for_search: 语义检索返回的条目数量,默认为 3
top_k_for_chat: 问答使用的条目数量,默认为 3
"""
- def label_extractor(item: SymptomDifferentialItem) -> str:
- return f"{item.symptom}: {', '.join(item.diseases[:3])}{'...' if len(item.diseases) > 3 else ''}"
+ def item_label_extractor(item: SymptomDifferentialItem) -> str:
+ return f"{item.symptom}"
super().show(
- label_extractor=label_extractor,
- top_k_items_for_search=top_k_for_search,
- top_k_items_for_chat=top_k_for_chat,
+ item_label_extractor=item_label_extractor,
+ top_k_for_search=top_k_for_search,
+ top_k_for_chat=top_k_for_chat,
)
\ No newline at end of file
diff --git a/hyperextract/templates/zh/news/opinion_set.py b/hyperextract/templates/zh/news/opinion_set.py
index b1e1d7c..32d1b88 100644
--- a/hyperextract/templates/zh/news/opinion_set.py
+++ b/hyperextract/templates/zh/news/opinion_set.py
@@ -66,4 +66,4 @@ class OpinionSet(AutoSet[OpinionStance]):
def show(self, **kwargs):
def label_func(item: OpinionStance) -> str:
return f"【{item.stance}】{item.entity}: {item.key_argument}"
- super().show(label_extractor=label_func, **kwargs)
+ super().show(item_label_extractor=label_func, **kwargs)
diff --git a/hyperextract/templates/zh/tcm/processing_method.py b/hyperextract/templates/zh/tcm/processing_method.py
index aad3998..e6e4213 100644
--- a/hyperextract/templates/zh/tcm/processing_method.py
+++ b/hyperextract/templates/zh/tcm/processing_method.py
@@ -107,7 +107,7 @@ class ProcessingMethod(AutoList[ProcessingMethodItem]):
return f"{item.herbName} - {item.methodName}"
super().show(
- label_extractor=label_extractor,
+ item_label_extractor=label_extractor,
top_k_for_search=top_k_for_search,
top_k_for_chat=top_k_for_chat,
)