Skip to content

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

feat: 重构模板系统并优化代码结构

yifanfeng976519816a46a4
diff --git a/examples/template_medicine_demo.py b/examples/template_medicine_demo.py
index 56631d8..a03f877 100644
--- a/examples/template_medicine_demo.py
+++ b/examples/template_medicine_demo.py
@@ -84,7 +84,7 @@ TEMPLATE_TO_INPUT = {
# ==========================================
# 医学教科书与专著
-TEMPLATE = PathologyHypergraph # 多因素病理机制图
+# TEMPLATE = PathologyHypergraph # 多因素病理机制图
# TEMPLATE = MedicalConceptNet # 医学概念网络
# TEMPLATE = PharmacologyGraph # 药理机制图
# TEMPLATE = AnatomyHierarchy # 解剖层级树
@@ -101,7 +101,7 @@ TEMPLATE = PathologyHypergraph # 多因素病理机制图
# TEMPLATE = DischargeInstruction # 出院医嘱摘要
# 病理报告
-# TEMPLATE = TumorStagingItem # TNM 分期表
+TEMPLATE = TumorStagingItem # TNM 分期表
# TEMPLATE = MicroscopicFeatureSet # 微观特征集
# 药品说明书
diff --git a/examples/template_tcm_demo.py b/examples/template_tcm_demo.py
index e0338ba..f5e19e5 100644
--- a/examples/template_tcm_demo.py
+++ b/examples/template_tcm_demo.py
@@ -49,8 +49,8 @@ TEST_SAMPLES_ROOT = Path(__file__).parent.parent / "tests" / "fixtures" / "test_
# 模板到输入文件的映射关系(根据 README 中的典型应用场景)
TEMPLATE_TO_INPUT = {
- # 本草典籍 -> 本草纲目
- HerbPropertyModel: TEST_SAMPLES_ROOT / "herbal_compendium_sample.md",
+ # 本草典籍 -> 多味药 / 单味药
+ HerbPropertyModel: TEST_SAMPLES_ROOT / "herb_property_single.md",
ProcessingMethod: TEST_SAMPLES_ROOT / "herbal_compendium_sample.md",
CompatibilityNet: TEST_SAMPLES_ROOT / "herbal_compendium_sample.md",
# 方剂规范 -> 伤寒论
@@ -61,7 +61,7 @@ TEMPLATE_TO_INPUT = {
AcupointLocationMap: TEST_SAMPLES_ROOT / "meridian_treatise_sample.md",
# 名医医案
SyndromeReasoningGraph: TEST_SAMPLES_ROOT / "medical_case_record_sample.md",
- PrescriptionModification: TEST_SAMPLES_ROOT / "medical_case_record_sample.md",
+ PrescriptionModification: TEST_SAMPLES_ROOT / "prescription_modification_sample.md",
PulseTongueRecord: TEST_SAMPLES_ROOT / "medical_case_record_sample.md",
}
@@ -75,11 +75,11 @@ TEMPLATE_TO_INPUT = {
# TEMPLATE = CompatibilityNet # 七情配伍网络
# 方剂规范
-# TEMPLATE = FormulaComposition # 君臣佐使结构图
+TEMPLATE = FormulaComposition # 君臣佐使结构图
# TEMPLATE = FunctionIndicationMap # 主治功效映射
# 经络腧穴专著
-TEMPLATE = MeridianFlowGraph # 经络流注图
+# TEMPLATE = MeridianFlowGraph # 经络流注图
# TEMPLATE = AcupointLocationMap # 腧穴空间定位
# 名医医案
diff --git a/hyperextract/templates/en/INSTRUCTION.md b/hyperextract/templates/en/INSTRUCTION.md
index 45cfd43..e0cee29 100644
--- a/hyperextract/templates/en/INSTRUCTION.md
+++ b/hyperextract/templates/en/INSTRUCTION.md
@@ -192,7 +192,23 @@ You are a professional xxx expert, please extract xxx from the text...
- **Concept 2**: xxx
## Extraction Rules
-...
+### Core Constraints
+#### _NODE_PROMPT / _PROMPT (Node Extraction)
+1. Each node must correspond to a single independent entity; do not merge multiple entities into one node
+2. Entity names should remain consistent with the source text
+
+#### _EDGE_PROMPT (Edge Extraction)
+1. Only extract edges from the known entity list; do not create unlisted entities
+2. Relationship descriptions should remain consistent with the source text
+
+### Domain-Specific Rules (Optional)
+Different domains have specialized terminology or specific expressions. You may add targeted rules as needed. For example:
+- Traditional Chinese Medicine: Ancient dosage units (两, 钱, 分, 匕, 枚, etc.) can be kept in original form
+- Finance: Professional terminology should remain in original form
+- Medical: Professional abbreviations should remain in original form
+
+### Spatio-Temporal Resolution Rules (required for spatio-temporal graph types only)
+- Time/location resolution rules...
```
**Why concept definitions are needed?**
@@ -228,23 +244,23 @@ You are a professional xxx expert, please extract xxx from the text...
#### AutoGraph
```
## Core Concept Definitions
-- **Node**: Entities in the graph
-- **Edge**: Binary relationships between nodes
+- **Node**: Entities extracted from the document
+- **Edge**: Relationships between nodes
```
#### AutoHypergraph
```
## Core Concept Definitions
-- **Node**: Basic elements that make up hyperedges (edges), serving as participants in hyperedges
-- **Edge**: Connects multiple nodes and expresses complex relationships among multiple entities
+- **Node**: Entities extracted from the document
+- **Edge**: Connects multiple nodes, expressing complex relationships among multiple entities
```
#### AutoTemporalGraph / AutoSpatialGraph / AutoSpatioTemporalGraph
In addition to node and edge definitions, time/location definitions must also be added:
```
## Core Concept Definitions
-- **Node**: Entities in the graph
-- **Edge**: Binary relationships between nodes
+- **Node**: Entities extracted from the document
+- **Edge**: Relationships between nodes
- **Time**: xxx (define according to actual needs)
- **Location**: xxx (define according to actual needs)
```
@@ -345,77 +361,70 @@ class FinancialRelation(BaseModel):
details: str = Field(description="Detailed description", default="")
# ==============================================================================
-# 2. Predefined Prompts (All in English)
+# 2. Predefined Prompts (Simplified Version)
# ==============================================================================
_NODE_PROMPT = """## Role and Task
-You are a professional financial analyst, extract all key entities as nodes from the text.
+You are a professional [domain] expert, extract all entities as nodes from the text.
## Core Concept Definitions
-- **Node**: Entities in the graph
-- **Edge**: Binary relationships between nodes
+- **Node**: Entities extracted from the document
+- **Edge**: Relationships between nodes
## Extraction Rules
-1. Extract all entities such as companies, products, departments
-2. Assign type for each entity: company, product, department
-3. Keep entity names consistent with the source text
-4. **NEVER extract time or location as independent nodes**
+### Core Constraints
+1. Each node must correspond to a single independent entity; do not merge multiple entities into one node
+2. Entity names should remain consistent with the source text
### Source Text:
"""
_EDGE_PROMPT = """## Role and Task
-You are a professional financial analyst, extract relationships between given entities.
+You are a professional [domain] expert, extract relationships between given entities.
## Core Concept Definitions
-- **Node**: Entities in the graph
-- **Edge**: Binary relationships between nodes
-- **Time**: Records time information when relationships occur
+- **Node**: Entities extracted from the document
+- **Edge**: Relationships between nodes
## Extraction Rules
-### Time Format Requirements
-All time information must be unified to "YYYY-MM-DD" format (e.g., 2023-06-15).
+### Core Constraints
+1. Only extract edges from the known entity list; do not create unlisted entities
+2. Relationship descriptions should remain consistent with the source text
### Time Resolution Rules
Current Observation Date: {observation_time}
1. Relative time resolution (based on observation date):
- - "last year" → year before {observation_time}, formatted as YYYY-01-01
- - "last month" → month before {observation_time}, formatted as YYYY-MM-01
- - "June 2023" → 2023-06-15
+ - "last year" → year before {observation_time}
+ - "last month" → month before {observation_time}
-2. Exact time → convert to YYYY-MM-DD format
+2. Exact time → keep as-is
3. Missing time → leave empty
-
-### Constraints
-1. Only extract edges from the known entity list below
-2. Do NOT create entities not listed
-3. Each edge must contain source, target, relationType
-
"""
_PROMPT = """## Role and Task
-You are a professional financial analyst, extract entities and their relationships from text.
+You are a professional [domain] expert, extract entities and their relationships from text.
## Core Concept Definitions
-- **Node**: Entities in the graph
-- **Edge**: Binary relationships between nodes
-- **Time**: Records time information when relationships occur
+- **Node**: Entities extracted from the document
+- **Edge**: Relationships between nodes
## Extraction Rules
-### Node Extraction Rules
-1. Extract all entities such as companies, products, departments
-2. **NEVER extract time or location as independent nodes**
+### Core Constraints
+1. Each node must correspond to a single independent entity; do not merge multiple entities into one node
+2. Entity names should remain consistent with the source text
+3. Only extract edges from the known entity list; do not create unlisted entities
+4. Relationship descriptions should remain consistent with the source text
-### Relationship Extraction Rules
+### Time Resolution Rules
Current Observation Date: {observation_time}
-### Time Format Requirements
-All time information must be unified to "YYYY-MM-DD" format (e.g., 2023-06-15).
+1. Relative time resolution (based on observation date):
+ - "last year" → year before {observation_time}
+ - "last month" → month before {observation_time}
-1. Resolve relative time and convert to standard format
-2. Convert exact time to YYYY-MM-DD format
-3. Leave empty when time is missing
+2. Exact time → keep as-is
+3. Missing time → leave empty
### Source Text:
"""
diff --git a/hyperextract/templates/en/general/concept_hierarchy.py b/hyperextract/templates/en/general/concept_hierarchy.py
index 6d2d671..ea9ad9c 100644
--- a/hyperextract/templates/en/general/concept_hierarchy.py
+++ b/hyperextract/templates/en/general/concept_hierarchy.py
@@ -32,81 +32,60 @@ class HierarchyRelation(BaseModel):
_PROMPT = """## Role and Task
-You are a professional concept structure analysis expert. Please extract concepts and their hierarchical relationships from the text to build a concept hierarchy graph.
+You are a professional concept structure analysis expert. Please extract concepts and their hierarchical relationships from the text.
## Core Concept Definitions
-- **Node**: In this template, "Node" refers to a concept unit, categorized into types such as CoreConcept, Subconcept, Attribute, and Instance, used to represent basic concepts in a knowledge system.
-- **Edge**: In this template, "Edge" refers to a hierarchical relationship between concepts, including binary relationships such as Subclass-Of (taxonomy), Part-Of (composition), and Instance-Of (instance).
+- **Node**: concept unit
+- **Edge**: hierarchical relationship between concepts
## Extraction Rules
### Node Extraction Rules
-1. Extract all concepts: CoreConcept, Subconcept, Attribute, Instance, etc.
-2. Assign a type to each concept: CoreConcept, Subconcept, Attribute, Instance, Other
-3. Add a brief description for each concept
+1. Extract all concepts
+2. Assign a type to each concept
### Edge Extraction Rules
1. Only create edges from extracted concepts
-2. Relationship types include:
- - Subclass-Of: Taxonomy relationship (e.g., "Dog" is a subclass of "Animal")
- - Part-Of: Composition relationship (e.g., "Tire" is part of "Car")
- - Instance-Of: Instance relationship (e.g., "Zu Chongzhi" is an instance of "Mathematician")
- - Other: Other relationships
-3. Each edge must connect extracted nodes
-
-### Constraints
-- Maintain clear hierarchical relationships, avoid circular dependencies
-- Do not create concepts or relationships not mentioned in the text
-- Maintain objectivity and accuracy, do not add information not in the text
+2. Each edge must connect extracted nodes
### Source text:
"""
_NODE_PROMPT = """## Role and Task
-You are a professional concept recognition expert. Please extract all concepts as nodes from the text.
+Please extract concepts as nodes from the text.
## Core Concept Definitions
-- **Node**: In this template, "Node" refers to a concept unit, categorized into types such as CoreConcept, Subconcept, Attribute, and Instance, used to represent basic concepts in a knowledge system.
+- **Node**: concept unit
## Extraction Rules
-1. Extract all concepts: CoreConcept, Subconcept, Attribute, Instance, etc.
-2. Assign a type to each concept: CoreConcept, Subconcept, Attribute, Instance, Other
-3. Add a brief description for each concept
+1. Extract all concepts
+2. Assign a type to each concept
### Source text:
"""
_EDGE_PROMPT = """## Role and Task
-You are a professional hierarchical relationship extraction expert. Please extract hierarchical relationships between concepts (nodes) from the given concept list.
+Please extract hierarchical relationships between concepts from the known concept list.
## Core Concept Definitions
-- **Node**: In this template, "Node" refers to a concept unit, as participants in hierarchical relationships.
-- **Edge**: In this template, "Edge" refers to a hierarchical relationship between concepts, including binary relationships such as Subclass-Of (taxonomy), Part-Of (composition), and Instance-Of (instance).
+- **Edge**: hierarchical relationship between concepts
-## Extraction Rules
-### Relationship Type Explanation
-- Subclass-Of: Taxonomy relationship (subclass and superclass)
-- Part-Of: Composition relationship (part and whole)
-- Instance-Of: Instance relationship (instance and class)
-- Other: Other relationships
-
-### Constraints
-1. Only extract edges from the known concept list below
+## Constraints
+1. Only extract edges from the known concept list
2. Do not create unlisted concepts
-3. Maintain clear hierarchical relationships, avoid circular dependencies
-
"""
class ConceptHierarchy(AutoGraph[ConceptNode, HierarchyRelation]):
"""
- Applicable documents: Science textbooks, knowledge base documents, classification system descriptions
+ Applicable Documents: Scientific disciplines, textbook knowledge points, ontology documents
- Function introduction:
- Build subclass relationships (Subclass-Of) or composition relationships (Part-Of). Suitable for scientific disciplines, textbook knowledge points, etc.
+ Function Introduction:
+ Extract concepts and build subclass relationships (Subclass-Of) or composition relationships (Part-Of),
+ suitable for scientific disciplines, textbook knowledge points, etc.
Example:
>>> template = ConceptHierarchy(llm_client=llm, embedder=embedder)
- >>> template.feed_text("Machine learning is a branch of artificial intelligence...")
+ >>> template.feed_text("Animals are divided into mammals, birds, reptiles, amphibians and fish...")
>>> template.show()
"""
@@ -121,16 +100,16 @@ class ConceptHierarchy(AutoGraph[ConceptNode, HierarchyRelation]):
**kwargs: Any,
):
"""
- Initialize concept hierarchy graph template.
+ Initialize the concept hierarchy template.
Args:
llm_client: LLM client for knowledge extraction
- embedder: Embedding model for semantic search
- extraction_mode: Extraction mode, either "one_stage" (extract nodes and edges simultaneously)
- or "two_stage" (extract nodes first, then edges), default: "two_stage"
- max_workers: Maximum number of worker threads, default: 10
- verbose: Whether to output detailed logs, default: False
- **kwargs: Other technical parameters, passed to base class
+ embedder: Embedding model for semantic retrieval
+ extraction_mode: Extraction mode, optional "one_stage" (extract nodes and edges simultaneously)
+ or "two_stage" (extract nodes first, then edges), default is "two_stage"
+ max_workers: Maximum worker threads, default is 10
+ verbose: Whether to output detailed logs, default is False
+ **kwargs: Other technical parameters, passed to the base class
"""
super().__init__(
node_schema=ConceptNode,
@@ -158,13 +137,13 @@ class ConceptHierarchy(AutoGraph[ConceptNode, HierarchyRelation]):
top_k_edges_for_chat: int = 3,
):
"""
- Display concept hierarchy graph.
+ Display the concept hierarchy.
Args:
- top_k_nodes_for_search: Number of nodes to return for semantic search, default: 3
- top_k_edges_for_search: Number of edges to return for semantic search, default: 3
- top_k_nodes_for_chat: Number of nodes to use for chat, default: 3
- top_k_edges_for_chat: Number of edges to use for chat, default: 3
+ top_k_nodes_for_search: Number of nodes returned by semantic search, default is 3
+ top_k_edges_for_search: Number of edges returned by semantic search, default is 3
+ top_k_nodes_for_chat: Number of nodes used for Q&A, default is 3
+ top_k_edges_for_chat: Number of edges used for Q&A, default is 3
"""
def node_label_extractor(node: ConceptNode) -> str:
diff --git a/hyperextract/templates/en/general/knowledge_graph.py b/hyperextract/templates/en/general/knowledge_graph.py
index 65553fc..ddde61a 100644
--- a/hyperextract/templates/en/general/knowledge_graph.py
+++ b/hyperextract/templates/en/general/knowledge_graph.py
@@ -34,59 +34,46 @@ class GeneralRelation(BaseModel):
_PROMPT = """## Role and Task
-You are a professional knowledge graph extraction expert. Please extract all entities (nodes) and their relationships (edges) from the text.
+You are a professional knowledge graph extraction expert. Please extract entities and their relationships from the text.
## Core Concept Definitions
-- **Node**: In this template, "Node" refers to a general entity, including types such as Person, Organization, Location, Product, Concept, etc., used to represent basic entities in a knowledge graph.
-- **Edge**: In this template, "Edge" refers to a binary relationship between entities, including relationship types such as BelongsTo, LocatedIn, CollaboratesWith, CompetesWith, InventedBy, CreatedBy, and RelatedTo.
+- **Node**: general entity
+- **Edge**: binary relationship between entities
## Extraction Rules
### Node Extraction Rules
-1. Extract all entities: Person, Organization, Location, Product, Concept, etc.
-2. Assign a type to each entity: Person, Organization, Location, Product, Concept, Other
-3. Keep entity names consistent with the original text
-4. Add a brief description for each entity
+1. Extract all entities
+2. Assign a type to each entity
### Edge Extraction Rules
1. Only create edges from extracted entities
-2. Relationship types include: BelongsTo, LocatedIn, CollaboratesWith, CompetesWith, InventedBy, CreatedBy, RelatedTo, etc.
-
-### Constraints
-- Each edge must connect extracted nodes
-- Do not create entities or relationships not mentioned in the text
-- Maintain objectivity and accuracy, do not add information not in the text
+2. Each edge must connect extracted nodes
### Source text:
"""
_NODE_PROMPT = """## Role and Task
-You are a professional entity recognition expert. Please extract all key entities as nodes from the text.
+Please extract key entities as nodes from the text.
## Core Concept Definitions
-- **Node**: In this template, "Node" refers to a general entity, including types such as Person, Organization, Location, Product, Concept, etc., used to represent basic entities in a knowledge graph.
+- **Node**: general entity
## Extraction Rules
-1. Extract all entities: Person, Organization, Location, Product, Concept, etc.
-2. Assign a type to each entity: Person, Organization, Location, Product, Concept, Other
-3. Keep entity names consistent with the original text
-4. Add a brief description for each entity
+1. Extract all entities
+2. Assign a type to each entity
### Source text:
"""
_EDGE_PROMPT = """## Role and Task
-You are a professional relationship extraction expert. Please extract relationships between entities from the given entity list.
+Please extract relationships between entities from the known entity list.
## Core Concept Definitions
-- **Node**: In this template, "Node" refers to a general entity, as participants in relationships.
-- **Edge**: In this template, "Edge" refers to a binary relationship between entities, including relationship types such as BelongsTo, LocatedIn, CollaboratesWith, CompetesWith, InventedBy, CreatedBy, and RelatedTo.
+- **Edge**: binary relationship between entities
-## Extraction Rules
-### Constraints
-1. Only extract edges from the known entity list below
+## Constraints
+1. Only extract edges from the known entity list
2. Do not create unlisted entities
-3. Relationship types include: BelongsTo, LocatedIn, CollaboratesWith, CompetesWith, InventedBy, CreatedBy, RelatedTo, etc.
-
"""
@@ -114,16 +101,16 @@ class KnowledgeGraph(AutoGraph[GeneralEntity, GeneralRelation]):
**kwargs: Any,
):
"""
- Initialize general knowledge graph template.
+ Initialize the general knowledge graph template.
Args:
llm_client: LLM client for knowledge extraction
- embedder: Embedding model for semantic search
- extraction_mode: Extraction mode, either "one_stage" (extract nodes and edges simultaneously)
- or "two_stage" (extract nodes first, then edges), default: "two_stage"
- max_workers: Maximum number of worker threads, default: 10
- verbose: Whether to output detailed logs, default: False
- **kwargs: Other technical parameters, passed to base class
+ embedder: Embedding model for semantic retrieval
+ extraction_mode: Extraction mode, optional "one_stage" (extract nodes and edges simultaneously)
+ or "two_stage" (extract nodes first, then edges), default is "two_stage"
+ max_workers: Maximum worker threads, default is 10
+ verbose: Whether to output detailed logs, default is False
+ **kwargs: Other technical parameters, passed to the base class
"""
super().__init__(
node_schema=GeneralEntity,
@@ -151,13 +138,13 @@ class KnowledgeGraph(AutoGraph[GeneralEntity, GeneralRelation]):
top_k_edges_for_chat: int = 3,
):
"""
- Display knowledge graph.
+ Display the knowledge graph.
Args:
- top_k_nodes_for_search: Number of nodes to return for semantic search, default: 3
- top_k_edges_for_search: Number of edges to return for semantic search, default: 3
- top_k_nodes_for_chat: Number of nodes to use for chat, default: 3
- top_k_edges_for_chat: Number of edges to use for chat, default: 3
+ top_k_nodes_for_search: Number of nodes returned by semantic search, default is 3
+ top_k_edges_for_search: Number of edges returned by semantic search, default is 3
+ top_k_nodes_for_chat: Number of nodes used for Q&A, default is 3
+ top_k_edges_for_chat: Number of edges used for Q&A, default is 3
"""
def node_label_extractor(node: GeneralEntity) -> str:
diff --git a/hyperextract/templates/en/general/social_network.py b/hyperextract/templates/en/general/social_network.py
index a6d5ec3..38cf4e3 100644
--- a/hyperextract/templates/en/general/social_network.py
+++ b/hyperextract/templates/en/general/social_network.py
@@ -31,91 +31,60 @@ class SocialRelation(BaseModel):
_PROMPT = """## Role and Task
-You are a professional social relationship analysis expert. Please extract people, organizations, and their mutual relationships from the text to build a social network graph.
+You are a professional social relationship analysis expert. Please extract people, organizations, and their mutual relationships from the text.
## Core Concept Definitions
-- **Node**: In this template, "Node" refers to a person or organization unit, including types such as Person, Organization, Institution, etc., used to represent entities in a social network.
-- **Edge**: In this template, "Edge" refers to a social relationship between people/organizations, including binary relationships such as Family, Friend, Colleague, TeacherStudent, SupervisorSubordinate, Collaboration, Competition, and Affiliation.
+- **Node**: person or organization unit
+- **Edge**: social relationship between people/organizations
## Extraction Rules
### Node Extraction Rules
-1. Extract all people, organizations, institutions, etc.
-2. Assign a type to each node: Person, Organization, Institution, Other
-3. Add a brief description for each node
-4. Annotate role or identity for each node
+1. Extract all people, organizations, institutions
+2. Assign a type to each node
### Edge Extraction Rules
1. Only create edges from extracted nodes
-2. Relationship types include:
- - Family: Family relationships (parents, children, spouse, siblings, etc.)
- - Friend: Friend relationships
- - Colleague: Work partner relationships
- - TeacherStudent: Educational relationships
- - SupervisorSubordinate: Management relationships
- - Collaboration: Collaborative relationships
- - Competition: Competitive relationships
- - Affiliation: Organizational affiliation relationships
- - Other: Other relationships
-3. Each edge must connect extracted nodes
-
-### Constraints
-- Do not create nodes or relationships not mentioned in the text
-- Maintain objectivity and accuracy, do not add information not in the text
+2. Each edge must connect extracted nodes
### Source text:
"""
_NODE_PROMPT = """## Role and Task
-You are a professional person and organization recognition expert. Please extract all people and organizations as nodes from the text.
+Please extract people and organizations as nodes from the text.
## Core Concept Definitions
-- **Node**: In this template, "Node" refers to a person or organization unit, including types such as Person, Organization, Institution, etc., used to represent entities in a social network.
+- **Node**: person or organization unit
## Extraction Rules
-1. Extract all people, organizations, institutions, etc.
-2. Assign a type to each node: Person, Organization, Institution, Other
-3. Add a brief description for each node
-4. Annotate role or identity for each node
+1. Extract all people, organizations, institutions
+2. Assign a type to each node
### Source text:
"""
_EDGE_PROMPT = """## Role and Task
-You are a professional social relationship extraction expert. Please extract mutual relationships between nodes from the given node list.
+Please extract social relationships between nodes from the known node list.
## Core Concept Definitions
-- **Node**: In this template, "Node" refers to a person or organization unit, as participants in social relationships.
-- **Edge**: In this template, "Edge" refers to a social relationship between people/organizations, including binary relationships such as Family, Friend, Colleague, TeacherStudent, SupervisorSubordinate, Collaboration, Competition, and Affiliation.
+- **Edge**: social relationship between people/organizations
-## Extraction Rules
-### Relationship Type Explanation
-- Family: Family relationships (parents, children, spouse, siblings, etc.)
-- Friend: Friend relationships
-- Colleague: Work partner relationships
-- TeacherStudent: Educational relationships
-- SupervisorSubordinate: Management relationships
-- Collaboration: Collaborative relationships
-- Competition: Competitive relationships
-- Affiliation: Organizational affiliation relationships
-- Other: Other relationships
-
-### Constraints
-1. Only extract edges from the known node list below
+## Constraints
+1. Only extract edges from the known node list
2. Do not create unlisted nodes
-
"""
class SocialNetwork(AutoGraph[PersonNode, SocialRelation]):
"""
- Applicable documents: Biographies, memoirs, novels, character setting documents
+ Applicable Documents: Biographies, news articles, literary works, organizational documents
- Function introduction:
- Focus on extracting interpersonal relationships, interactions, and organizational affiliations. Suitable for biography analysis, character setting research, etc.
+ Function Introduction:
+ Extract people, organizations, and their mutual relationships to build a social network graph.
+ Suitable for biography analysis, character setting research, organizational structure analysis, etc.
Example:
>>> template = SocialNetwork(llm_client=llm, embedder=embedder)
- >>> template.feed_text("Zu Chongzhi studied under He Chengtian and debated with Dai Faxing...")
+ >>> template.feed_text("John works at ABC Company as a manager...")
>>> template.show()
"""
@@ -130,16 +99,16 @@ class SocialNetwork(AutoGraph[PersonNode, SocialRelation]):
**kwargs: Any,
):
"""
- Initialize social network graph template.
+ Initialize the social network graph template.
Args:
llm_client: LLM client for knowledge extraction
- embedder: Embedding model for semantic search
- extraction_mode: Extraction mode, either "one_stage" (extract nodes and edges simultaneously)
- or "two_stage" (extract nodes first, then edges), default: "two_stage"
- max_workers: Maximum number of worker threads, default: 10
- verbose: Whether to output detailed logs, default: False
- **kwargs: Other technical parameters, passed to base class
+ embedder: Embedding model for semantic retrieval
+ extraction_mode: Extraction mode, optional "one_stage" (extract nodes and edges simultaneously)
+ or "two_stage" (extract nodes first, then edges), default is "two_stage"
+ max_workers: Maximum worker threads, default is 10
+ verbose: Whether to output detailed logs, default is False
+ **kwargs: Other technical parameters, passed to the base class
"""
super().__init__(
node_schema=PersonNode,
@@ -167,18 +136,16 @@ class SocialNetwork(AutoGraph[PersonNode, SocialRelation]):
top_k_edges_for_chat: int = 3,
):
"""
- Display social network graph.
+ Display the social network graph.
Args:
- top_k_nodes_for_search: Number of nodes to return for semantic search, default: 3
- top_k_edges_for_search: Number of edges to return for semantic search, default: 3
- top_k_nodes_for_chat: Number of nodes to use for chat, default: 3
- top_k_edges_for_chat: Number of edges to use for chat, default: 3
+ top_k_nodes_for_search: Number of nodes returned by semantic search, default is 3
+ top_k_edges_for_search: Number of edges returned by semantic search, default is 3
+ top_k_nodes_for_chat: Number of nodes used for Q&A, default is 3
+ top_k_edges_for_chat: Number of edges used for Q&A, default is 3
"""
def node_label_extractor(node: PersonNode) -> str:
- if node.role:
- return f"{node.name} ({node.role})"
return f"{node.name} ({node.type})"
def edge_label_extractor(edge: SocialRelation) -> str:
diff --git a/hyperextract/templates/zh/INSTRUCTION.md b/hyperextract/templates/zh/INSTRUCTION.md
index ac58388..91f4206 100644
--- a/hyperextract/templates/zh/INSTRUCTION.md
+++ b/hyperextract/templates/zh/INSTRUCTION.md
@@ -190,7 +190,23 @@ class FinancialRelation(BaseModel):
- **概念2**:xxx
## 提取规则
-...
+### 核心约束
+#### _NODE_PROMPT / _PROMPT(节点提取)
+1. 每个节点只能对应一个独立的实体,禁止将多个实体合并为一个节点
+2. 实体名称与原文保持一致
+
+#### _EDGE_PROMPT(边提取)
+1. 仅从已知实体列表中提取边,不要创建未列出的实体
+2. 关系描述应与原文保持一致
+
+### 领域特定规则(可选)
+不同领域存在专业术语或特定表述方式,可根据实际需求在对应Prompt中添加针对性规则。例如:
+- 中医文献:古代剂量单位(两、钱、分、匕、枚等)可保留原文
+- 金融领域:专业术语保持原文表述
+- 医学领域:专业缩写保持原文
+
+### 时空解析规则(仅时空图类型需要)
+- 时间/位置解析规则...
```
**为什么需要概念定义?**
@@ -226,14 +242,14 @@ class FinancialRelation(BaseModel):
#### AutoGraph
```
## 核心概念定义
-- **节点 (Node)**:图谱中的实体
-- **边 (Edge)**:节点之间的二元关系
+- **节点 (Node)**:从文档中提取的实体
+- **边 (Edge)**:节点之间的关系
```
#### AutoHypergraph
```
## 核心概念定义
-- **节点 (Node)**:构成超边(边)的基本要素,作为超边的参与者
+- **节点 (Node)**:从文档中提取的实体
- **边 (Edge)**:连接多个节点,表达多个实体间的复杂关联关系
```
@@ -241,8 +257,8 @@ class FinancialRelation(BaseModel):
除了节点和边的定义,还需要添加时间/位置的定义:
```
## 核心概念定义
-- **节点 (Node)**:图谱中的实体
-- **边 (Edge)**:节点之间的二元关系
+- **节点 (Node)**:从文档中提取的实体
+- **边 (Edge)**:节点之间的关系
- **时间**:xxx(根据实际需求定义)
- **位置**:xxx(根据实际需求定义)
```
@@ -343,77 +359,70 @@ class FinancialRelation(BaseModel):
details: str = Field(description="详细描述", default="")
# ==============================================================================
-# 2. 预定义 Prompt(全中文)
+# 2. 预定义 Prompt(精简版)
# ==============================================================================
_NODE_PROMPT = """## 角色与任务
-你是一位专业的财报分析专家,请从文本中提取所有关键实体作为节点。
+你是一位专业的[领域]专家,请从文本中提取所有实体作为节点。
## 核心概念定义
-- **节点 (Node)**:图谱中的实体
-- **边 (Edge)**:节点之间的二元关系
+- **节点 (Node)**:从文档中提取的实体
+- **边 (Edge)**:节点之间的关系
## 提取规则
-1. 提取所有公司、产品、部门等实体
-2. 为每个实体指定类型:公司、产品、部门
-3. 保持实体名称与原文一致
-4. **禁止将时间或地点作为独立节点提取**
+### 核心约束
+1. 每个节点只能对应一个独立的实体,禁止将多个实体合并为一个节点
+2. 实体名称与原文保持一致
### 源文本:
"""
_EDGE_PROMPT = """## 角色与任务
-你是一位专业的财报分析专家,请从给定实体列表中提取实体之间的关系。
+你是一位专业的[领域]专家,请从给定实体列表中提取实体之间的关系。
## 核心概念定义
-- **节点 (Node)**:图谱中的实体
-- **边 (Edge)**:节点之间的二元关系
-- **时间**:记录关系发生的时间信息
+- **节点 (Node)**:从文档中提取的实体
+- **边 (Edge)**:节点之间的关系
## 提取规则
-### 时间格式要求
-所有时间信息必须统一转换为「年-月-日」格式(如 2023-06-15)。
+### 核心约束
+1. 仅从已知实体列表中提取边,不要创建未列出的实体
+2. 关系描述应与原文保持一致
### 时间解析规则
当前观察日期: {observation_time}
1. 相对时间解析(基于观察日期):
- - "去年" → {observation_time} 的前一年,格式化为 YYYY-01-01
- - "上月" → {observation_time} 的前一个月,格式化为 YYYY-MM-01
- - "2023年6月" → 2023-06-15
+ - "去年" → {observation_time} 的前一年
+ - "上月" → {observation_time} 的前一个月
-2. 精确时间 → 转换为年-月-日格式
+2. 精确时间 → 保持原样
3. 时间缺失 → 留空
-
-### 约束条件
-1. 仅从下方已知实体列表中提取边
-2. 不要创建未列出的实体
-3. 每条边必须包含 source、target、relationType
-
"""
_PROMPT = """## 角色与任务
-你是一位专业的财报分析专家,请从文本中提取实体和它们之间的关系。
+你是一位专业的[领域]专家,请从文本中提取实体和它们之间的关系。
## 核心概念定义
-- **节点 (Node)**:图谱中的实体
-- **边 (Edge)**:节点之间的二元关系
-- **时间**:记录关系发生的时间信息
+- **节点 (Node)**:从文档中提取的实体
+- **边 (Edge)**:节点之间的关系
## 提取规则
-### 节点提取规则
-1. 提取所有公司、产品、部门等实体
-2. **禁止将时间或地点作为独立节点提取**
+### 核心约束
+1. 每个节点只能对应一个独立的实体,禁止将多个实体合并
+2. 实体名称与原文保持一致
+3. 仅从已知实体列表中提取边,不要创建未列出的实体
+4. 关系描述应与原文保持一致
-### 关系提取规则
+### 时间解析规则
当前观察日期: {observation_time}
-### 时间格式要求
-所有时间信息必须统一转换为「年-月-日」格式(如 2023-06-15)。
+1. 相对时间解析(基于观察日期):
+ - "去年" → {observation_time} 的前一年
+ - "上月" → {observation_time} 的前一个月
-1. 解析相对时间并转换为标准格式
-2. 精确时间转换为年-月-日格式
-3. 时间缺失时留空
+2. 精确时间 → 保持原样
+3. 时间缺失 → 留空
### 源文本:
"""
diff --git a/hyperextract/templates/zh/general/compliance_logic.py b/hyperextract/templates/zh/general/compliance_logic.py
index dd3e650..329e981 100644
--- a/hyperextract/templates/zh/general/compliance_logic.py
+++ b/hyperextract/templates/zh/general/compliance_logic.py
@@ -32,79 +32,52 @@ class ComplianceRule(BaseModel):
_PROMPT = """
-你是一位专业的合规评估员,擅长从文档中提取合规要素并分析规则逻辑。
+## 角色与任务
+你是一位专业的合规评估员,请从文本中提取合规要素和规则。
-## 任务
-请从文本中提取合规要素和规则,构建合规行为超图。
-
-## 概念定义
-- **节点(Node)**:构成规则的基本要素,分为四类:
- - **主体**:执行动作的实体(个人、部门、组织、角色等)
- - **操作**:具体的动作或行为(提交、审批、执行、禁止等)
- - **条件**:规则适用的前提或场景(时间、地点、状态、数量等)
- - **其他**:不属于上述三类的要素(凭证、文件、状态、结果等)
-- **超边(Hyperedge/Edge)**:即"规则",连接多个节点,表达"在何种条件下,何种主体,必须/禁止执行何种操作"的逻辑。
+## 核心概念定义
+- **节点 (Node)**:构成规则的基本要素
+- **边 (Edge)**:即"规则",连接多个节点,表达"在何种条件下,何种主体,必须/禁止执行何种操作"的逻辑
## 提取规则
### 节点提取
1. 提取规则中涉及的要素作为节点
-2. 为每个要素指定类型:主体、操作、条件、其他
-3. 为每个要素添加简要描述
+2. 为每个要素指定类型
### 超边提取
1. 仅从已提取节点创建规则
2. 规则类型:必须、禁止、允许、其他
-3. 必须包含 ruleType、participants、action
-4. participants 严格使用已提取节点的 name
-5. 其他字段尽量填充,缺失用空字符串
### 源文本:
"""
_NODE_PROMPT = """
-你是一位专业的合规要素识别专家,擅长从文档中精准识别合规相关的核心要素。
+## 角色与任务
+请从文本中提取合规相关要素作为节点。
-## 任务
-请从文本中提取所有合规相关要素作为节点。
-
-## 概念定义
-- **节点(Node)**:构成规则的基本要素,分为四类:
- - **主体**:执行动作的实体(个人、部门、组织、角色等)
- - **操作**:具体的动作或行为(提交、审批、执行、禁止等)
- - **条件**:规则适用的前提或场景(时间、地点、状态、数量等)
- - **其他**:不属于上述三类的要素(凭证、文件、状态、结果等)
+## 核心概念定义
+- **节点 (Node)**:构成规则的基本要素
## 提取规则
-1. 提取具体、原子化的要素,不要抽象的"流程"或"对照表"
-2. 要素名称直接使用文本中出现的名词或动词
-3. 为每个要素指定正确的类型
-4. 为每个要素添加简要描述(1-2句话)
+1. 提取规则中涉及的要素
+2. 为每个要素指定类型
+3. 为每个要素添加简要描述
### 源文本:
"""
_EDGE_PROMPT = """
-你是一位专业的合规规则分析师,擅长从给定要素中构建清晰的合规规则关系。
+## 角色与任务
+请从已知节点列表中提取合规规则超边。
-## 任务
-请从给定节点列表中提取合规规则超边。
-
-## 概念定义
-- **节点(Node)**:构成规则的基本要素,作为超边(Hyperedge/Edge)的参与者
-- **超边(Hyperedge/Edge)**:即"规则",连接多个节点,表达"在何种条件下,何种主体,必须/禁止执行何种操作"的逻辑。
+## 核心概念定义
+- **边 (Edge)**:即"规则",连接多个节点
## 提取规则
-1. **participants 必须严格使用下方中的名称,不要创建任何新的要素名称**
+1. participants 必须严格使用已知节点列表中的名称
2. 规则必须包含 ruleType、participants、action
-3. participants 应包含:涉及的主体、执行的操作、相关的其他要素
-
-### 规则类型
-- **必须**:强制要求执行的行为
-- **禁止**:不允许执行的行为
-- **允许**:可以执行的行为
-- **其他**:不属于上述三类的规则
-
+3. participants 应包含涉及的主体和操作
"""
diff --git a/hyperextract/templates/zh/general/concept_hierarchy.py b/hyperextract/templates/zh/general/concept_hierarchy.py
index c705653..da7de76 100644
--- a/hyperextract/templates/zh/general/concept_hierarchy.py
+++ b/hyperextract/templates/zh/general/concept_hierarchy.py
@@ -30,67 +30,46 @@ class HierarchyRelation(BaseModel):
_PROMPT = """## 角色与任务
-你是一位专业的概念结构分析专家,请从文本中提取概念及其层级关系,构建概念层级图。
+你是一位专业的概念结构分析专家,请从文本中提取概念及其层级关系。
## 核心概念定义
-- **节点 (Node)**:本模板中的"节点"指概念单元,分为核心概念、子概念、属性、实例等类型,用于表示知识体系中的基本概念。
-- **边 (Edge)**:本模板中的"边"指概念之间的层级关系,包括上下位(Subclass-Of)、组成(Part-Of)、实例(Instance-Of)等二元关系。
+- **节点 (Node)**:概念单元
+- **边 (Edge)**:概念之间的层级关系
## 提取规则
### 节点提取规则
-1. 提取所有概念:核心概念、子概念、属性、实例等
-2. 为每个概念指定类型:核心概念、子概念、属性、实例、其他
-3. 为每个概念添加简要描述
+1. 提取所有概念
+2. 为每个概念指定类型
### 关系提取规则
1. 仅从提取的概念中创建边
-2. 关系类型包括:
- - 上下位(Subclass-Of):如"狗"是"动物"的子类
- - 组成(Part-Of):如"轮胎"是"汽车"的一部分
- - 实例(Instance-Of):如"祖冲之"是"数学家"的实例
- - 其他(Other)
-3. 每条边必须连接已提取的节点
-
-### 约束条件
-- 保持层级关系清晰,避免循环依赖
-- 不要创建未在文本中提及的概念或关系
-- 保持客观准确,不添加文本中没有的信息
+2. 每条边必须连接已提取的节点
### 源文本:
"""
_NODE_PROMPT = """## 角色与任务
-你是一位专业的概念识别专家,请从文本中提取所有概念作为节点。
+请从文本中提取概念作为节点。
## 核心概念定义
-- **节点 (Node)**:本模板中的"节点"指概念单元,分为核心概念、子概念、属性、实例等类型,用于表示知识体系中的基本概念。
+- **节点 (Node)**:概念单元
## 提取规则
-1. 提取所有概念:核心概念、子概念、属性、实例等
-2. 为每个概念指定类型:核心概念、子概念、属性、实例、其他
-3. 为每个概念添加简要描述
+1. 提取所有概念
+2. 为每个概念指定类型
### 源文本:
"""
_EDGE_PROMPT = """## 角色与任务
-你是一位专业的层级关系提取专家,请从给定概念列表中提取概念(节点)之间的层级关系。
+请从已知概念列表中提取概念之间的层级关系。
## 核心概念定义
-- **节点 (Node)**:本模板中的"节点"指概念单元,作为层级关系的参与者。
-- **边 (Edge)**:本模板中的"边"指概念之间的层级关系,包括上下位(Subclass-Of)、组成(Part-Of)、实例(Instance-Of)等二元关系。
+- **边 (Edge)**:概念之间的层级关系
-## 提取规则
-### 关系类型说明
-- 上下位(Subclass-Of):子类与父类
-- 组成(Part-Of):部分与整体
-- 实例(Instance-Of):实例与类
-- 其他(Other)
-
-### 约束条件
-1. 仅从下方已知概念列表中提取边
+## 约束条件
+1. 仅从已知概念列表中提取边
2. 不要创建未列出的概念
-3. 保持层级关系清晰,避免循环依赖
"""
diff --git a/hyperextract/templates/zh/general/cross_reference_net.py b/hyperextract/templates/zh/general/cross_reference_net.py
index 1c0a5e5..02e456d 100644
--- a/hyperextract/templates/zh/general/cross_reference_net.py
+++ b/hyperextract/templates/zh/general/cross_reference_net.py
@@ -30,69 +30,46 @@ class ReferenceRelation(BaseModel):
_PROMPT = """## 角色与任务
-你是一位专业的知识网络分析专家,请从文本中提取概念/条目及其相互引用关系,构建交叉引用网络。
+你是一位专业的知识网络分析专家,请从文本中提取概念/条目及其相互引用关系。
## 核心概念定义
-- **节点 (Node)**:本模板中的"节点"指概念或条目单元,包括条目、章节、概念、人物、地点等类型,用于表示知识网络中的实体。
-- **边 (Edge)**:本模板中的"边"指概念/条目之间的引用关系,包括 Hyperlink(超链接)、Reference(引用)、SeeAlso(参见)、Related(相关)等二元关系。
+- **节点 (Node)**:概念或条目单元
+- **边 (Edge)**:概念/条目之间的引用关系
## 提取规则
### 节点提取规则
1. 提取所有被提及的概念或条目
-2. 为每个节点指定类型:条目、章节、概念、人物、地点、其他
-3. 为每个节点添加简要描述
+2. 为每个节点指定类型
### 关系提取规则
1. 仅从提取的节点中创建边
-2. 关系类型包括:
- - Hyperlink:超链接关系
- - Reference:引用关系
- - SeeAlso:参见关系
- - Related:相关关系
- - Other:其他关系
-3. 记录引用的上下文描述
-
-### 约束条件
-- 每条边必须连接已提取的节点
-- 不要创建未在文本中提及的节点或关系
-- 保持客观准确,不添加文本中没有的信息
+2. 每条边必须连接已提取的节点
### 源文本:
"""
_NODE_PROMPT = """## 角色与任务
-你是一位专业的条目识别专家,请从文本中提取所有概念或条目作为节点。
+请从文本中提取概念或条目作为节点。
## 核心概念定义
-- **节点 (Node)**:本模板中的"节点"指概念或条目单元,包括条目、章节、概念、人物、地点等类型,用于表示知识网络中的实体。
+- **节点 (Node)**:概念或条目单元
## 提取规则
1. 提取所有被提及的概念或条目
-2. 为每个节点指定类型:条目、章节、概念、人物、地点、其他
-3. 为每个节点添加简要描述
+2. 为每个节点指定类型
### 源文本:
"""
_EDGE_PROMPT = """## 角色与任务
-你是一位专业的引用关系提取专家,请从给定节点列表中提取节点之间的相互引用关系。
+请从已知节点列表中提取节点之间的引用关系。
## 核心概念定义
-- **节点 (Node)**:本模板中的"节点"指概念或条目单元,作为引用关系的参与者。
-- **边 (Edge)**:本模板中的"边"指概念/条目之间的引用关系,包括 Hyperlink(超链接)、Reference(引用)、SeeAlso(参见)、Related(相关)等二元关系。
+- **边 (Edge)**:概念/条目之间的引用关系
-## 提取规则
-### 关系类型说明
-- Hyperlink:超链接关系
-- Reference:引用关系
-- SeeAlso:参见关系
-- Related:相关关系
-- Other:其他关系
-
-### 约束条件
-1. 仅从下方已知节点列表中提取边
+## 约束条件
+1. 仅从已知节点列表中提取边
2. 不要创建未列出的节点
-
"""
diff --git a/hyperextract/templates/zh/general/knowledge_graph.py b/hyperextract/templates/zh/general/knowledge_graph.py
index af7bdd1..f300fe2 100644
--- a/hyperextract/templates/zh/general/knowledge_graph.py
+++ b/hyperextract/templates/zh/general/knowledge_graph.py
@@ -30,58 +30,46 @@ class GeneralRelation(BaseModel):
_PROMPT = """## 角色与任务
-你是一位专业的知识图谱提取专家,请从文本中提取所有实体(节点)和它们之间的关系(边)。
+你是一位专业的知识图谱提取专家,请从文本中提取实体及其关系。
## 核心概念定义
-- **节点 (Node)**:本模板中的"节点"指通用实体,包括人物、机构、地点、产品、概念等类型,用于表示知识图谱中的基本实体。
-- **边 (Edge)**:本模板中的"边"指实体之间的二元关系,包括属于、位于、合作、竞争、发明、创建、相关等关系类型。
+- **节点 (Node)**:通用实体
+- **边 (Edge)**:实体之间的二元关系
## 提取规则
### 节点提取规则
-1. 提取所有实体:人物、机构、地点、产品、概念等
-2. 为每个实体指定类型:人物、机构、地点、产品、概念、其他
-3. 保持实体名称与原文一致
-4. 为每个实体添加简要描述
+1. 提取所有实体
+2. 为每个实体指定类型
### 关系提取规则
1. 仅从提取的实体中创建边
-2. 关系类型包括:属于、位于、合作、竞争、发明、创建、相关等
-
-### 约束条件
-- 每条边必须连接已提取的节点
-- 不要创建未在文本中提及的实体或关系
-- 保持客观准确,不添加文本中没有的信息
+2. 每条边必须连接已提取的节点
### 源文本:
"""
_NODE_PROMPT = """## 角色与任务
-你是一位专业的实体识别专家,请从文本中提取所有关键实体作为节点。
+请从文本中提取关键实体作为节点。
## 核心概念定义
-- **节点 (Node)**:本模板中的"节点"指通用实体,包括人物、机构、地点、产品、概念等类型,用于表示知识图谱中的基本实体。
+- **节点 (Node)**:通用实体
## 提取规则
-1. 提取所有实体:人物、机构、地点、产品、概念等
-2. 为每个实体指定类型:人物、机构、地点、产品、概念、其他
-3. 保持实体名称与原文一致
-4. 为每个实体添加简要描述
+1. 提取所有实体
+2. 为每个实体指定类型
### 源文本:
"""
_EDGE_PROMPT = """## 角色与任务
-你是一位专业的关系提取专家,请从给定实体列表中提取实体之间的关系。
+请从已知实体列表中提取实体之间的关系。
## 核心概念定义
-- **节点 (Node)**:本模板中的"节点"指通用实体,作为关系的参与者。
-- **边 (Edge)**:本模板中的"边"指实体之间的二元关系,包括属于、位于、合作、竞争、发明、创建、相关等关系类型。
+- **边 (Edge)**:实体之间的二元关系
-## 提取规则
-### 约束条件
-1. 仅从下方已知实体列表中提取边
+## 约束条件
+1. 仅从已知实体列表中提取边
2. 不要创建未列出的实体
-3. 关系类型包括:属于、位于、合作、竞争、发明、创建、相关等
"""
diff --git a/hyperextract/templates/zh/general/life_event_timeline.py b/hyperextract/templates/zh/general/life_event_timeline.py
index 78bba00..2f794e2 100644
--- a/hyperextract/templates/zh/general/life_event_timeline.py
+++ b/hyperextract/templates/zh/general/life_event_timeline.py
@@ -28,124 +28,84 @@ class LifeEvent(BaseModel):
details: str = Field(description="事件详细描述", default="")
-_PROMPT = """你是一位专业的传记年表专家。请从文本中提取人物、地点、组织等实体以及生活事件,构建生平时序图。
+_PROMPT = """## 角色与任务
+你是一位专业的传记年表专家,请从文本中提取人物、地点、组织等实体以及生活事件。
+## 核心概念定义
+- **节点 (Node)**:人物、地点、组织等实体
+- **边 (Edge)**:生活事件
+- **时间**:事件发生的时间
+
+## 提取规则
### 节点提取规则
-1. 提取所有相关实体:人物、地点、组织、物品、概念等
-2. 为每个实体指定类型:人物、地点、组织、物品、概念、其他
-3. 为每个实体添加简要描述
+1. 提取所有相关实体
+2. 为每个实体指定类型
### 事件提取规则
1. 仅从提取的实体中创建事件边
-2. 事件类型包括:
- - 出生:出生事件
- - 逝世:逝世事件
- - 教育:学习、受教育事件
- - 工作:工作、职业事件
- - 成就:取得成就、获得荣誉事件
- - 交往:人际交往、社会活动事件
- - 旅行:旅行、迁居事件
- - 其他:其他事件
-3. 事件日期为可选提取项,仅在文本中明确提及时才提取
-4. 日期格式:年-月-日(如 429-01-01)或年份(如 429)
-5. 每条边必须连接已提取的节点
-
-### 中文时间计算规则
+2. 每条边必须连接已提取的节点
+3. 时间信息为可选,仅在文本中明确提及时才提取
+
+### 时间解析规则
当前观察日期:{observation_time}
-1. **相对时间解析**:必须根据观察日期解析相对时间表达
- - "去年" → 计算为 {observation_time} 前一年
+1. 相对时间解析(基于观察日期):
+ - "去年" → {observation_time} 前一年
- "今年" → {observation_time} 所属年份
- "明年" → {observation_time} 后一年
- - "昨天" → {observation_time} 前一天
- - "今天" → {observation_time}
- - "明天" → {observation_time} 后一天
- - "上个月" → {observation_time} 前一个月的第一天
- - "这个月" → {observation_time} 所属月份的第一天
- - "下个月" → {observation_time} 后一个月的第一天
- - "十年前" → {observation_time} 前十年
- - "五年后" → {observation_time} 后五年
- - "年初" → 当年的 1月1日
- - "年末" → 当年的 12月31日
- - "世纪初" → 该世纪的第 1 年(如 2000 年)
- - "世纪末" → 该世纪的最后 1 年(如 2099 年)
- - "朝代" → 保留朝代名称(如"唐朝"、"宋代"),无需转换
-
-2. **明确日期**:保持原文中的明确日期格式不变
- - 年份(如 429 年、2023 年)→ 429、2023
- - 年-月(如 429年3月)→ 429-03
- - 年-月-日(如 429年3月14日)→ 429-03-14
- - 中文数字日期(如"民国三十八年")→ 转换为公元纪年
-
-3. **缺失时间**:如果文本中没有时间信息,将 eventDate 留空(None),不要编造日期
+ - "昨天/今天/明天" → {observation_time} 前/当/后一天
+ - "十年前/五年后" → {observation_time} 前/后相应年数
+ - "年初/年末" → 当年 1月1日/12月31日
+ - "朝代" → 保留朝代名称
-### 约束条件
-- 不要创建未在文本中提及的实体或事件
-- 保持客观准确,不添加文本中没有的信息
+2. 明确日期:保持原格式
+ - 年份(如 429年、2023年)→ 429、2023
+ - 年-月-日 → 保持原格式
+
+3. 缺失时间:留空
### 源文本:
"""
-_NODE_PROMPT = """你是一位专业的实体识别专家。请从文本中提取所有相关实体作为节点。
+_NODE_PROMPT = """## 角色与任务
+请从文本中提取人物、地点、组织等实体作为节点。
+
+## 核心概念定义
+- **节点 (Node)**:人物、地点、组织等实体
-### 提取规则
-1. 提取所有相关实体:人物、地点、组织、物品、概念等
-2. 为每个实体指定类型:人物、地点、组织、物品、概念、其他
-3. 为每个实体添加简要描述
+## 提取规则
+1. 提取所有相关实体
+2. 为每个实体指定类型
### 源文本:
"""
-_EDGE_PROMPT = """你是一位专业的生活事件提取专家。请从给定实体列表中提取生活事件。
+_EDGE_PROMPT = """## 角色与任务
+请从已知实体列表中提取生活事件。
-### 事件类型说明
-- 出生:出生事件
-- 逝世:逝世事件
-- 教育:学习、受教育事件
-- 工作:工作、职业事件
-- 成就:取得成就、获得荣誉事件
-- 交往:人际交往、社会活动事件
-- 旅行:旅行、迁居事件
-- 其他:其他事件
+## 核心概念定义
+- **边 (Edge)**:生活事件
+- **时间**:事件发生的时间
-### 中文时间计算规则
+### 时间解析规则
当前观察日期:{observation_time}
-1. **相对时间解析**:必须根据观察日期解析相对时间表达
- - "去年" → 计算为 {observation_time} 前一年
+1. 相对时间解析(基于观察日期):
+ - "去年" → {observation_time} 前一年
- "今年" → {observation_time} 所属年份
- "明年" → {observation_time} 后一年
- - "昨天" → {observation_time} 前一天
- - "今天" → {observation_time}
- - "明天" → {observation_time} 后一天
- - "上个月" → {observation_time} 前一个月的第一天
- - "这个月" → {observation_time} 所属月份的第一天
- - "下个月" → {observation_time} 后一个月的第一天
- - "十年前" → {observation_time} 前十年
- - "五年后" → {observation_time} 后五年
- - "年初" → 当年的 1月1日
- - "年末" → 当年的 12月31日
- - "世纪初" → 该世纪的第 1 年(如 2000 年)
- - "世纪末" → 该世纪的最后 1 年(如 2099 年)
- - "朝代" → 保留朝代名称(如"唐朝"、"宋代"),无需转换
-
-2. **明确日期**:保持原文中的明确日期格式不变
- - 年份(如 429 年、2023 年)→ 429、2023
- - 年-月(如 429年3月)→ 429-03
- - 年-月-日(如 429年3月14日)→ 429-03-14
- - 中文数字日期(如"民国三十八年")→ 转换为公元纪年
-
-3. **缺失时间**:如果文本中没有时间信息,将 eventDate 留空(None),不要编造日期
-
-### 日期格式要求
-- 所有日期信息统一转换为「年-月-日」格式(如 429-01-01)或年份(如 429)
-- 时间信息为可选提取项,仅在文本中明确提及时才提取
+ - "昨天/今天/明天" → {observation_time} 前/当/后一天
+ - "十年前/五年后" → {observation_time} 前/后相应年数
+ - "年初/年末" → 当年 1月1日/12月31日
+ - "朝代" → 保留朝代名称
+
+2. 明确日期:保持原格式
+3. 缺失时间:留空
### 约束条件
-1. 仅从下方已知实体列表中提取事件
+1. 仅从已知实体列表中提取事件
2. 不要创建未列出的实体
3. 时间信息为可选,仅在文本中明确提及时才提取
-
"""
diff --git a/hyperextract/templates/zh/general/operational_procedure.py b/hyperextract/templates/zh/general/operational_procedure.py
index fe5bdd4..082745d 100644
--- a/hyperextract/templates/zh/general/operational_procedure.py
+++ b/hyperextract/templates/zh/general/operational_procedure.py
@@ -31,63 +31,48 @@ class ProcedureTransition(BaseModel):
_PROMPT = """## 角色与任务
-你是一位专业的流程分析专家,请从文本中提取规章中规定的申报、审批或操作的具体先后步骤,构建执行程序流程图。
+你是一位专业的流程分析专家,请从文本中提取执行程序的步骤和转换关系。
## 核心概念定义
-- **节点 (Node)**:本模板中的"节点"指流程步骤单元,包含步骤编号、名称、描述、执行角色、输入、输出等字段的操作单元。
-- **边 (Edge)**:本模板中的"边"指流程步骤之间的转换关系,记录步骤之间的先后顺序、转换条件等二元关系。
+- **节点 (Node)**:流程步骤单元
+- **边 (Edge)**:流程步骤之间的转换关系
## 提取规则
### 节点提取规则
1. 提取所有流程步骤
-2. 为每个步骤分配编号(如步骤1、步骤2)
-3. 为每个步骤添加名称和详细描述
-4. 记录执行角色(如有)
-5. 记录所需输入或材料(如有)
-6. 记录输出或结果(如有)
+2. 为每个步骤分配编号
+3. 记录步骤名称和描述
### 边提取规则
1. 仅从提取的步骤中创建转换边
2. 记录步骤之间的先后关系
-3. 记录转换条件(如有)
-4. 记录详细说明(如有)
-
-### 约束条件
-- 确保流程步骤的先后顺序准确
-- 保持客观准确,不添加文本中没有的信息
### 源文本:
"""
_NODE_PROMPT = """## 角色与任务
-你是一位专业的流程步骤识别专家,请从文本中提取所有流程步骤作为节点。
+请从文本中提取流程步骤作为节点。
## 核心概念定义
-- **节点 (Node)**:本模板中的"节点"指流程步骤单元,包含步骤编号、名称、描述、执行角色、输入、输出等字段的操作单元。
+- **节点 (Node)**:流程步骤单元
## 提取规则
1. 提取所有流程步骤
-2. 为每个步骤分配编号(如步骤1、步骤2)
-3. 为每个步骤添加名称和详细描述
-4. 记录执行角色(如有)
-5. 记录所需输入或材料(如有)
-6. 记录输出或结果(如有)
+2. 为每个步骤分配编号
+3. 记录步骤名称和描述
### 源文本:
"""
_EDGE_PROMPT = """## 角色与任务
-你是一位专业的流程转换识别专家,请从给定步骤列表中提取步骤之间的先后关系。
+请从已知步骤列表中提取步骤之间的转换关系。
## 核心概念定义
-- **节点 (Node)**:本模板中的"节点"指流程步骤单元,作为流程转换的参与者。
-- **边 (Edge)**:本模板中的"边"指流程步骤之间的转换关系,记录步骤之间的先后顺序、转换条件等二元关系。
+- **边 (Edge)**:流程步骤之间的转换关系
-## 提取规则
-### 约束条件
-1. 仅从下方已知步骤列表中提取边
+## 约束条件
+1. 仅从已知步骤列表中提取边
2. 不要创建未列出的步骤
-
"""
diff --git a/hyperextract/templates/zh/general/penalty_mapping.py b/hyperextract/templates/zh/general/penalty_mapping.py
index 8e7092b..eccf949 100644
--- a/hyperextract/templates/zh/general/penalty_mapping.py
+++ b/hyperextract/templates/zh/general/penalty_mapping.py
@@ -28,60 +28,51 @@ class PenaltyPath(BaseModel):
_PROMPT = """## 角色与任务
-你是一位专业的处罚逻辑分析专家,请从文本中提取从“违规行为”到“处理程序”再到“最终结果”的演变路径,构建违规处罚因果链。
+你是一位专业的处罚逻辑分析专家,请从文本中提取违规处罚的因果链。
## 核心概念定义
-- **节点 (Node)**:本模板中的"节点"指处罚因果链中的关键单元,包括违规行为、处理程序、处罚结果等类型,用于表示处罚流程中的关键环节。
-- **边 (Edge)**:本模板中的"边"指节点之间的因果关系,包括导致、触发、执行、产生等二元关系,用于表示从违规到处罚的演变路径。
+- **节点 (Node)**:处罚因果链中的关键单元
+- **边 (Edge)**:节点之间的因果关系
## 提取规则
### 节点提取规则
-1. 提取所有关键节点:违规行为、处理程序、处罚结果等
-2. 为每个节点指定类型:违规行为、处理程序、处罚结果、其他
-3. 为每个节点添加详细描述
+1. 提取所有关键节点
+2. 为每个节点指定类型
### 边提取规则
1. 仅从提取的节点中创建因果路径边
2. 记录节点之间的因果关系
-3. 关系类型包括:导致、触发、执行、产生、其他
-4. 添加详细说明(如有)
-
-### 约束条件
-- 确保因果关系的逻辑顺序正确
-- 保持客观准确,不添加文本中没有的信息
### 源文本:
"""
_NODE_PROMPT = """## 角色与任务
-你是一位专业的处罚节点识别专家,请从文本中提取所有关键节点。
+请从文本中提取处罚因果链的关键节点。
## 核心概念定义
-- **节点 (Node)**:本模板中的"节点"指处罚因果链中的关键单元,包括违规行为、处理程序、处罚结果等类型,用于表示处罚流程中的关键环节。
+- **节点 (Node)**:处罚因果链中的关键单元
## 提取规则
-1. 提取所有关键节点:违规行为、处理程序、处罚结果等
-2. 为每个节点指定类型:违规行为、处理程序、处罚结果、其他
-3. 为每个节点添加详细描述
+1. 提取所有关键节点
+2. 为每个节点指定类型
### 源文本:
"""
_EDGE_PROMPT = """## 角色与任务
-你是一位专业的处罚因果关系识别专家,请从给定节点列表中提取节点之间的因果关系。
+请从已知节点列表中提取节点之间的因果关系。
## 核心概念定义
-- **节点 (Node)**:本模板中的"节点"指处罚因果链中的关键单元,作为因果关系的参与者。
-- **边 (Edge)**:本模板中的"边"指节点之间的因果关系,包括导致、触发、执行、产生等二元关系,用于表示从违规到处罚的演变路径。
+- **边 (Edge)**:节点之间的因果关系
-## 提取规则
-### 约束条件
-1. 仅从下方已知节点列表中提取边
+## 约束条件
+1. 仅从已知节点列表中提取边
2. 不要创建未列出的节点
"""
+
class PenaltyMapping(AutoGraph[PenaltyNode, PenaltyPath]):
"""
适用文档: 公司内部管理制度、行政法规、合规指南、处罚案例
diff --git a/hyperextract/templates/zh/general/social_network.py b/hyperextract/templates/zh/general/social_network.py
index 0bd2ea8..64ca88e 100644
--- a/hyperextract/templates/zh/general/social_network.py
+++ b/hyperextract/templates/zh/general/social_network.py
@@ -31,76 +31,46 @@ class SocialRelation(BaseModel):
_PROMPT = """## 角色与任务
-你是一位专业的社会关系分析专家,请从文本中提取人物、组织及其相互关系,构建社交网络图。
+你是一位专业的社会关系分析专家,请从文本中提取人物、组织及其相互关系。
## 核心概念定义
-- **节点 (Node)**:本模板中的"节点"指人物或组织单元,包括人物、组织、机构等类型,用于表示社交网络中的实体。
-- **边 (Edge)**:本模板中的"边"指人物/组织之间的社会关系,包括亲属、朋友、同事、师生、上下级、合作、竞争、隶属等二元关系。
+- **节点 (Node)**:人物或组织单元
+- **边 (Edge)**:人物/组织之间的社会关系
## 提取规则
### 节点提取规则
1. 提取所有人物、组织、机构等
-2. 为每个节点指定类型:人物、组织、机构、其他
-3. 为每个节点添加简要描述
-4. 为每个节点标注角色或身份
+2. 为每个节点指定类型
+3. 为每个节点标注角色或身份
### 关系提取规则
1. 仅从提取的节点中创建边
-2. 关系类型包括:
- - 亲属:家庭关系(父母、子女、配偶、兄弟姐妹等)
- - 朋友:朋友关系
- - 同事:工作伙伴关系
- - 师生:教育关系
- - 上下级:管理关系
- - 合作:协作关系
- - 竞争:竞争关系
- - 隶属:组织隶属关系
- - 其他:其他关系
-3. 每条边必须连接已提取的节点
-
-### 约束条件
-- 不要创建未在文本中提及的节点或关系
-- 保持客观准确,不添加文本中没有的信息
+2. 每条边必须连接已提取的节点
### 源文本:
"""
_NODE_PROMPT = """## 角色与任务
-你是一位专业的人物和组织识别专家,请从文本中提取所有人物、组织作为节点。
+请从文本中提取人物、组织作为节点。
## 核心概念定义
-- **节点 (Node)**:本模板中的"节点"指人物或组织单元,包括人物、组织、机构等类型,用于表示社交网络中的实体。
+- **节点 (Node)**:人物或组织单元
## 提取规则
1. 提取所有人物、组织、机构等
-2. 为每个节点指定类型:人物、组织、机构、其他
-3. 为每个节点添加简要描述
-4. 为每个节点标注角色或身份
+2. 为每个节点指定类型和角色
### 源文本:
"""
_EDGE_PROMPT = """## 角色与任务
-你是一位专业的社会关系提取专家,请从给定节点列表中提取节点之间的相互关系。
+请从已知节点列表中提取节点之间的社会关系。
## 核心概念定义
-- **节点 (Node)**:本模板中的"节点"指人物或组织单元,作为社会关系的参与者。
-- **边 (Edge)**:本模板中的"边"指人物/组织之间的社会关系,包括亲属、朋友、同事、师生、上下级、合作、竞争、隶属等二元关系。
+- **边 (Edge)**:人物/组织之间的社会关系
-## 提取规则
-### 关系类型说明
-- 亲属:家庭关系(父母、子女、配偶、兄弟姐妹等)
-- 朋友:朋友关系
-- 同事:工作伙伴关系
-- 师生:教育关系
-- 上下级:管理关系
-- 合作:协作关系
-- 竞争:竞争关系
-- 隶属:组织隶属关系
-- 其他:其他关系
-
-### 约束条件
-1. 仅从下方已知节点列表中提取边
+## 约束条件
+1. 仅从已知节点列表中提取边
2. 不要创建未列出的节点
"""
diff --git a/hyperextract/templates/zh/medicine/discharge_instruction.py b/hyperextract/templates/zh/medicine/discharge_instruction.py
index ab10d58..10c8045 100644
--- a/hyperextract/templates/zh/medicine/discharge_instruction.py
+++ b/hyperextract/templates/zh/medicine/discharge_instruction.py
@@ -10,16 +10,33 @@ from langchain_core.embeddings import Embeddings
from hyperextract.types import AutoModel
+class MedicationItem(BaseModel):
+ """出院带药项"""
+ name: str = Field(description="药物名称")
+ dosage: str = Field(description="剂量")
+ usage: str = Field(description="用法")
+ frequency: str = Field(description="频次")
+
+
+class FollowUpItem(BaseModel):
+ """复诊计划项"""
+ time: str = Field(description="时间")
+ department: str = Field(description="科室")
+ examItems: List[str] = Field(description="检查项目", default_factory=list)
+
+
class DischargeInstructionInfo(BaseModel):
"""出院医嘱信息"""
- medications: List[dict] = Field(
- description="出院带药列表,每个药物包含名称、剂量、用法、频次等信息"
+ medications: List[MedicationItem] = Field(
+ description="出院带药列表,每个药物包含名称、剂量、用法、频次等信息",
+ default_factory=list
)
- followUp: List[dict] = Field(
- description="复诊计划列表,每个复诊包含时间、科室、检查项目等信息"
+ followUp: List[FollowUpItem] = Field(
+ description="复诊计划列表,每个复诊包含时间、科室、检查项目等信息",
+ default_factory=list
)
- rehabilitation: List[str] = Field(description="康复建议列表")
+ rehabilitation: List[str] = Field(description="康复建议列表", default_factory=list)
diet: List[str] = Field(description="饮食建议列表", default_factory=list)
activity: List[str] = Field(description="活动建议列表", default_factory=list)
notes: List[str] = Field(description="其他注意事项列表", default_factory=list)
diff --git a/hyperextract/templates/zh/medicine/pathology_hypergraph.py b/hyperextract/templates/zh/medicine/pathology_hypergraph.py
index 2c2b8b7..ec2971c 100644
--- a/hyperextract/templates/zh/medicine/pathology_hypergraph.py
+++ b/hyperextract/templates/zh/medicine/pathology_hypergraph.py
@@ -45,20 +45,13 @@ _PROMPT = """## 角色与任务
## 提取规则
### 节点提取规则
-1. 提取所有相关实体:基因、环境因素、诱因、疾病、病理过程、症状等
-2. 为每个实体指定类型:基因、环境因素、诱因、疾病、病理过程、症状等
-3. 为每个实体添加简要描述
+1. 提取所有相关实体,确保每个实体的原子性,即不能将多个相关实体合并为一个节点。
### 超边提取规则
1. 仅从提取的实体中创建超边
2. 超边应表示多个实体之间的复杂关联关系
-3. **mechanism_name 命名要求**:使用简短、清晰的描述性名称,例如 "COPD 环境致病机制"、"气道高反应性诱发机制",不要使用 "A+B+C->D" 这种格式
-4. 关系类型包括:致病、诱发、促进、抑制等
-5. 为每个超边添加详细描述,说明具体的致病机制
-6. outcome 描述该机制导致的结果
### 约束条件
-- 每条超边必须连接多个已提取的节点
- 不要创建未在文本中提及的实体或关系
- 保持客观准确,不添加文本中没有的信息
@@ -72,9 +65,7 @@ _NODE_PROMPT = """## 角色与任务
- **节点 (Node)**:本模板中的"节点"指病理实体,包括基因、环境因素、诱因、疾病、病理过程、症状等类型,用于表示病理机制中的基本要素。
## 提取规则
-1. 提取所有相关实体:基因、环境因素、诱因、疾病、病理过程、症状等
-2. 为每个实体指定类型:基因、环境因素、诱因、疾病、病理过程、症状等
-3. 为每个实体添加简要描述
+1. 提取所有相关实体,确保每个实体的原子性,即不能将多个相关实体合并为一个节点。
### 源文本:
"""
@@ -89,14 +80,6 @@ _EDGE_PROMPT = """## 角色与任务
## 提取规则
1. 仅从下方已知实体列表中创建超边
2. 超边应表示多个实体之间的复杂关联关系
-3. **mechanism_name 命名要求**:
- - 使用简短、清晰、描述性的中文名称
- - 例如:"COPD 环境致病机制"、"气道高反应性诱发机制"、"α1-抗胰蛋白酶缺乏致病途径"
- - 绝对不要使用 "基因+环境+诱因->疾病" 或类似的拼接格式
- - 名称应该像一个医学术语或机制名称
-4. 关系类型包括:致病、诱发、促进、抑制等
-5. 为每个超边添加详细描述,说明具体的致病机制
-6. outcome 描述该机制导致的结果或效应
### 约束条件
1. 不要创建未列出的实体
diff --git a/hyperextract/templates/zh/tcm/acupoint_location_map.py b/hyperextract/templates/zh/tcm/acupoint_location_map.py
index ea36b18..5db72c2 100644
--- a/hyperextract/templates/zh/tcm/acupoint_location_map.py
+++ b/hyperextract/templates/zh/tcm/acupoint_location_map.py
@@ -32,66 +32,43 @@ _PROMPT = """## 角色与任务
你是一位专业的针灸学专家,请从文本中提取穴位的相对位置关系。
## 核心概念定义
-- **节点 (Node)**:本模板中的"节点"指穴位实体或解剖标志,包括名称、所属经脉、位置描述、主治功能等信息。
-- **边 (Edge)**:本模板中的"边"指穴位之间或穴位与解剖标志之间的位置关系,包括源实体、目标穴位、相对位置描述等信息。
+- **节点 (Node)**:穴位实体或解剖标志
+- **边 (Edge)**:穴位之间的位置关系
## 提取规则
### 节点提取规则
-1. 提取所有穴位实体:如合谷、足三里、三阴交等
-2. 提取所有解剖标志:如脐、腕横纹、肘横纹等
-3. 为每个穴位指定所属经脉
-4. 提取每个穴位的位置描述和主治功能
+1. 提取穴位实体
+2. 提取解剖标志
### 关系提取规则
-1. 提取穴位之间或穴位与解剖标志之间的位置关系
-2. 为每种关系指定类型:位于
-3. 提取相对位置描述:如脐旁2寸、腕横纹上3寸等
-
-### 约束条件
-- 每条边必须连接已提取的节点
-- 不要创建未在文本中提及的实体或关系
-- 保持客观准确,符合针灸学专业术语规范
-
-### 术语标准化指导
-- **穴位名称统一**:确保使用标准穴位名称
-- **位置描述保留**:文言中位置描述(如"寸"、"分"、"旁"、"上"、"下"等)保持原文
-- **解剖标志识别**:准确识别文言中提到的解剖标志
+1. 提取穴位之间的位置关系
+2. 提取相对位置描述
### 源文本:
"""
_NODE_PROMPT = """## 角色与任务
-你是一位专业的针灸学专家,请从文本中提取所有穴位实体和解剖标志作为节点。
+请从文本中提取穴位实体和解剖标志作为节点。
## 核心概念定义
-- **节点 (Node)**:本模板中的"节点"指穴位实体或解剖标志,包括名称、所属经脉、位置描述、主治功能等信息。
+- **节点 (Node)**:穴位实体或解剖标志
## 提取规则
-1. 提取所有穴位实体:如合谷、足三里、三阴交等
-2. 提取所有解剖标志:如脐、腕横纹、肘横纹等
-3. 为每个穴位指定所属经脉
-4. 提取每个穴位的位置描述和主治功能
+1. 提取穴位实体
+2. 提取解剖标志
### 源文本:
"""
_EDGE_PROMPT = """## 角色与任务
-你是一位专业的针灸学专家,请从给定实体列表中提取穴位的相对位置关系。
+请从已知穴位列表中提取穴位的相对位置关系。
## 核心概念定义
-- **节点 (Node)**:本模板中的"节点"指穴位实体或解剖标志,作为位置关系的参与者。
-- **边 (Edge)**:本模板中的"边"指穴位之间或穴位与解剖标志之间的位置关系,包括源实体、目标穴位、相对位置描述等信息。
-
-## 提取规则
-1. 提取穴位之间或穴位与解剖标志之间的位置关系
-2. 为每种关系指定类型:位于
-3. 提取相对位置描述:如脐旁2寸、腕横纹上3寸等
-
-### 约束条件
-1. 仅从下方已知实体列表中提取边
-2. 不要创建未列出的实体
-3. 每条边必须包含 source、target、relationType、relativePosition
+- **边 (Edge)**:穴位之间的位置关系
+## 约束条件
+1. 仅从已知穴位列表中提取边
+2. 不要创建未列出的穴位
"""
@@ -166,10 +143,10 @@ class AcupointLocationMap(AutoGraph[AcupointNode, LocationRelation]):
"""
def node_label_extractor(node: AcupointNode) -> str:
- return f"{node.name} ({node.meridian})"
+ return node.name
def edge_label_extractor(edge: LocationRelation) -> str:
- return edge.relativePosition
+ return f"{edge.relationType}: {edge.relativePosition}"
super().show(
node_label_extractor=node_label_extractor,
diff --git a/hyperextract/templates/zh/tcm/compatibility_net.py b/hyperextract/templates/zh/tcm/compatibility_net.py
index e50c1b6..08009c2 100644
--- a/hyperextract/templates/zh/tcm/compatibility_net.py
+++ b/hyperextract/templates/zh/tcm/compatibility_net.py
@@ -32,59 +32,41 @@ _PROMPT = """## 角色与任务
你是一位专业的中药配伍专家,请从文本中提取药物间的配伍关系。
## 核心概念定义
-- **节点 (Node)**:本模板中的"节点"指中药药物实体,用于表示知识图谱中的基本药物。
-- **边 (Edge)**:本模板中的"边"指药物之间的配伍关系,包括相须、相使、相畏、相杀、相恶、相反、十八反、十九畏等关系类型。
+- **节点 (Node)**:中药药物实体
+- **边 (Edge)**:药物之间的配伍关系
## 提取规则
### 节点提取规则
1. 提取所有药物实体
-2. 为每个药物添加简要描述
### 关系提取规则
1. 提取所有药物之间的配伍关系
-2. 为每种配伍关系指定类型:相须、相使、相畏、相杀、相恶、相反、十八反、十九畏
-3. 提取每种配伍关系的效果描述
-
-### 约束条件
-- 每条边必须连接已提取的节点
-- 不要创建未在文本中提及的实体或关系
-- 保持客观准确,符合中药配伍专业术语规范
-
-### 术语标准化指导
-- **药物名称标准化**:同一药物的不同名称需统一使用规范正名
-- **配伍关系统一**:确保关系类型使用标准术语:相须、相使、相畏、相杀、相恶、相反、十八反、十九畏
-- **禁忌表述识别**:准确识别"反"、"畏"、"恶"等配伍禁忌表述
-- **双向关系处理**:配伍关系通常是双向的,但按原文表述顺序提取即可
+2. 为每种配伍关系指定类型
### 源文本:
"""
_NODE_PROMPT = """## 角色与任务
-你是一位专业的中药识别专家,请从文本中提取所有药物实体作为节点。
+请从文本中提取药物实体作为节点。
## 核心概念定义
-- **节点 (Node)**:本模板中的"节点"指中药药物实体,用于表示知识图谱中的基本药物。
+- **节点 (Node)**:中药药物实体
## 提取规则
1. 提取所有药物实体
-2. 为每个药物添加简要描述
### 源文本:
"""
_EDGE_PROMPT = """## 角色与任务
-你是一位专业的中药配伍专家,请从给定药物列表中提取药物之间的配伍关系。
+请从已知药物列表中提取药物之间的配伍关系。
## 核心概念定义
-- **节点 (Node)**:本模板中的"节点"指中药药物实体,作为配伍关系的参与者。
-- **边 (Edge)**:本模板中的"边"指药物之间的配伍关系,包括相须、相使、相畏、相杀、相恶、相反、十八反、十九畏等关系类型。
+- **边 (Edge)**:药物之间的配伍关系
-## 提取规则
-1. 仅从下方已知药物列表中提取边
+## 约束条件
+1. 仅从已知药物列表中提取边
2. 不要创建未列出的药物
-3. 为每种配伍关系指定类型:相须、相使、相畏、相杀、相恶、相反、十八反、十九畏
-4. 提取每种配伍关系的效果描述
-
"""
@@ -97,7 +79,7 @@ class CompatibilityNet(AutoGraph[HerbEntity, CompatibilityRelation]):
Example:
>>> template = CompatibilityNet(llm_client=llm, embedder=embedder)
- >>> template.feed_text("甘草反大戟、芫花、甘遂、海藻...")
+ >>> template.feed_text("甘草反大戟、芫花、甘遂,海藻...")
>>> template.show()
"""
diff --git a/hyperextract/templates/zh/tcm/formula_composition.py b/hyperextract/templates/zh/tcm/formula_composition.py
index 857d313..6d84c71 100644
--- a/hyperextract/templates/zh/tcm/formula_composition.py
+++ b/hyperextract/templates/zh/tcm/formula_composition.py
@@ -1,6 +1,10 @@
"""君臣佐使结构图 - 将方剂建模为 {君药, 臣药, 佐药, 使药} 的层级化超边结构。
适用于方剂结构解析、组方规律研究。
+
+设计原则:
+- 节点(HerbNode):存储药物的固有属性(性味归经、功效),不随方剂变化
+- 边(FormulaHyperedge):存储方剂信息,以及药物在该方剂中的具体信息(剂量、角色)
"""
from typing import Any, List
@@ -11,92 +15,106 @@ from hyperextract.types import AutoHypergraph
class HerbNode(BaseModel):
- """药物节点"""
+ """药物节点 - 存储药物的固有属性(不随方剂变化)"""
+
+ name: str = Field(description="药物名称")
+ nature: str = Field(description="性味,如:辛温、苦寒、甘平", default="")
+ meridian: str = Field(description="归经,如:归肺经、归脾经、归心经", default="")
+ effect: str = Field(description="功效,如:发汗解表、温通经脉、补气健脾", default="")
+
+
+class HerbInFormula(BaseModel):
+ """药物在方剂中的信息 - 包含剂量和炮制方法"""
name: str = Field(description="药物名称")
- role: str = Field(description="药物角色:君药、臣药、佐药、使药")
- dosage: str = Field(description="药物剂量", default="")
- effect: str = Field(description="药物功效", default="")
+ dosage: str = Field(description="剂量,如:三两、五钱、10g", default="")
+ processing: str = Field(description="炮制方法,如:炙、炒、煅、酒制", default="")
class FormulaHyperedge(BaseModel):
- """方剂超边"""
+ """方剂超边 - 存储方剂信息和药物在方剂中的具体角色"""
formulaName: str = Field(description="方剂名称")
- junHerbs: List[str] = Field(description="君药列表")
- chenHerbs: List[str] = Field(description="臣药列表")
- zuoHerbs: List[str] = Field(description="佐药列表")
- shiHerbs: List[str] = Field(description="使药列表")
- functions: List[str] = Field(description="方剂功能", default_factory=list)
- indications: List[str] = Field(description="主治证候", default_factory=list)
- decoctionMethod: str = Field(description="煎服法", default="")
+ source: str = Field(description="方剂来源,如:伤寒论、金匮要略、方剂学教材", default="")
+ junHerbs: List[HerbInFormula] = Field(description="君药列表", default_factory=list)
+ chenHerbs: List[HerbInFormula] = Field(description="臣药列表", default_factory=list)
+ zuoHerbs: List[HerbInFormula] = Field(description="佐药列表", default_factory=list)
+ shiHerbs: List[HerbInFormula] = Field(description="使药列表", default_factory=list)
+ functions: List[str] = Field(description="方剂功能,如:解肌发表、调和营卫", default_factory=list)
+ indications: List[str] = Field(description="主治证候,如:外感风寒表虚证", default_factory=list)
+ decoctionMethod: str = Field(description="煎服法,如:水煎服,日三服", default="")
_PROMPT = """## 角色与任务
你是一位专业的方剂学专家,请从文本中提取方剂的君臣佐使结构。
## 核心概念定义
-- **节点 (Node)**:本模板中的"节点"指方剂中的药物实体,包括药物名称、角色、剂量、功效等信息。
-- **边 (Edge)**:本模板中的"边"指方剂超边,包含方剂名称、君药、臣药、佐药、使药列表、方剂功能、主治证候、煎服法等信息。
+- **节点 (Node)**:药物实体,存储药物的固有属性(性味归经、功效)
+- **边 (Edge)**:方剂超边,存储方剂信息及药物在该方剂中的具体信息
## 提取规则
+
### 节点提取规则
1. 提取方剂中的所有药物实体
-2. 为每个药物指定角色:君药、臣药、佐药、使药
-3. 提取每个药物的剂量和功效
+2. 提取每个药物的固有属性:
+ - 性味:如辛温、苦寒、甘平
+ - 归经:如归肺经、归脾经
+ - 功效:如发汗解表、温通经脉
+3. 注意:性味归经和功效是药物本身的属性,不随方剂变化
### 边提取规则
-1. 提取方剂名称
-2. 提取君药、臣药、佐药、使药列表
+1. 提取方剂名称和来源
+2. 提取君药、臣药、佐药、使药列表,每个药物包含:
+ - 名称
+ - 剂量(如三两、五钱)
+ - 炮制方法(如炙、炒、煅)
3. 提取方剂功能和主治证候
4. 提取煎服法
-### 术语标准化指导
-- **药物名称标准化**:同一药物的不同名称需统一使用规范正名(如"国老"→"甘草","仙灵脾"→"淫羊藿")
-- **角色术语统一**:君、臣、佐、使的不同表述需统一为"君药"、"臣药"、"佐药"、"使药"
-- **剂量单位保留**:古代剂量单位(两、钱、分、匕、枚等)可保留原文,无需转换
-- **同义词处理**:同一概念的不同表述(如"主治"与"主疗","功效"与"功用")应统一提取
-
### 约束条件
-- 每条边必须包含方剂名称和至少一种角色的药物列表
-- 不要创建未在文本中提及的实体或关系
-- 保持客观准确,符合方剂学专业术语规范
+- 每条边必须包含方剂名称
+- 药物角色(君臣佐使)通过所属列表区分
### 源文本:
"""
_NODE_PROMPT = """## 角色与任务
-你是一位专业的药物识别专家,请从文本中提取方剂中的所有药物实体作为节点。
+请从文本中提取方剂中的药物实体作为节点。
## 核心概念定义
-- **节点 (Node)**:本模板中的"节点"指方剂中的药物实体,包括药物名称、角色、剂量、功效等信息。
+- **节点 (Node)**:药物实体,存储药物的固有属性
## 提取规则
1. 提取方剂中的所有药物实体
-2. 为每个药物指定角色:君药、臣药、佐药、使药
-3. 提取每个药物的剂量和功效
+2. 提取每个药物的固有属性:
+ - 性味:如辛温、苦寒、甘平
+ - 归经:如归肺经、归脾经
+ - 功效:如发汗解表、温通经脉
+3. 注意:只提取药物本身的属性,不要提取剂量和角色
### 源文本:
"""
_EDGE_PROMPT = """## 角色与任务
-你是一位专业的方剂学专家,请从给定药物列表中提取方剂的君臣佐使结构。
+请从已知药物列表中提取方剂的君臣佐使结构。
## 核心概念定义
-- **节点 (Node)**:本模板中的"节点"指方剂中的药物实体,作为方剂的组成部分。
-- **边 (Edge)**:本模板中的"边"指方剂超边,包含方剂名称、君药、臣药、佐药、使药列表、方剂功能、主治证候、煎服法等信息。
+- **边 (Edge)**:方剂超边,存储方剂信息及药物在该方剂中的具体信息
## 提取规则
-1. 提取方剂名称
-2. 从已知药物列表中提取君药、臣药、佐药、使药列表
-3. 提取方剂功能和主治证候
-4. 提取煎服法
-
-### 约束条件
-1. 仅从下方已知药物列表中提取药物
-2. 不要创建未列出的药物
-3. 每条边必须包含方剂名称和至少一种角色的药物列表
-
+1. 提取方剂名称和来源
+2. 提取君药、臣药、佐药、使药列表
+3. 每个药物需包含:
+ - 名称(必须从已知药物列表中选择)
+ - 剂量
+ - 炮制方法(如有)
+4. 提取方剂功能和主治证候
+5. 提取煎服法
+
+## 约束条件
+1. 药物名称必须从已知药物列表中选择
+2. 每条边必须包含方剂名称
+3. 药物角色通过所属列表(君/臣/佐/使)区分
"""
@@ -107,6 +125,10 @@ class FormulaComposition(AutoHypergraph[HerbNode, FormulaHyperedge]):
功能介绍:
将方剂建模为 {君药, 臣药, 佐药, 使药} 的层级化超边结构,适用于方剂结构解析、组方规律研究。
+ 设计说明:
+ - 节点(HerbNode):存储药物的固有属性(性味归经、功效),不随方剂变化
+ - 边(FormulaHyperedge):存储方剂信息,以及药物在该方剂中的具体信息(剂量、角色)
+
Example:
>>> template = FormulaComposition(llm_client=llm, embedder=embedder)
>>> template.feed_text("桂枝汤:桂枝三两,芍药三两,甘草二两,生姜三两,大枣十二枚...")
@@ -135,19 +157,25 @@ class FormulaComposition(AutoHypergraph[HerbNode, FormulaHyperedge]):
verbose: 是否输出详细日志,默认为 False
**kwargs: 其他技术参数,传递给基类
"""
+
+ def nodes_in_edge_extractor(edge: FormulaHyperedge) -> set:
+ herbs = []
+ for herb_list in [
+ edge.junHerbs,
+ edge.chenHerbs,
+ edge.zuoHerbs,
+ edge.shiHerbs,
+ ]:
+ for herb in herb_list:
+ herbs.append(herb.name)
+ return set(herbs)
+
super().__init__(
node_schema=HerbNode,
edge_schema=FormulaHyperedge,
node_key_extractor=lambda x: x.name,
edge_key_extractor=lambda x: x.formulaName,
- nodes_in_edge_extractor=lambda x: set(
- [
- *x.junHerbs,
- *x.chenHerbs,
- *x.zuoHerbs,
- *x.shiHerbs,
- ]
- ),
+ nodes_in_edge_extractor=nodes_in_edge_extractor,
llm_client=llm_client,
embedder=embedder,
extraction_mode=extraction_mode,
@@ -178,7 +206,7 @@ class FormulaComposition(AutoHypergraph[HerbNode, FormulaHyperedge]):
"""
def node_label_extractor(node: HerbNode) -> str:
- return f"{node.name} ({node.role})"
+ return f"{node.name}"
def edge_label_extractor(edge: FormulaHyperedge) -> str:
return edge.formulaName
diff --git a/hyperextract/templates/zh/tcm/function_indication_map.py b/hyperextract/templates/zh/tcm/function_indication_map.py
index e5fe75e..20df459 100644
--- a/hyperextract/templates/zh/tcm/function_indication_map.py
+++ b/hyperextract/templates/zh/tcm/function_indication_map.py
@@ -31,69 +31,45 @@ _PROMPT = """## 角色与任务
你是一位专业的方剂学专家,请从文本中提取方剂、功能和主治证候之间的关联关系。
## 核心概念定义
-- **节点 (Node)**:本模板中的"节点"指通用实体,包括方剂、功能、主治证候等类型,用于表示知识图谱中的基本元素。
-- **边 (Edge)**:本模板中的"边"指实体之间的二元关系,包括具有功能、主治等关系类型。
+- **节点 (Node)**:方剂、功能、主治证候
+- **边 (Edge)**:实体之间的二元关系
## 提取规则
### 节点提取规则
-1. 提取所有实体:方剂、功能、主治证候
-2. 为每个实体指定类型:方剂、功能、主治证候
-3. 保持实体名称与原文一致
-4. 为每个实体添加简要描述
-5. **关键原子化要求**:如果文本中出现逗号、顿号、及、与、和等分隔的多个实体,必须将每个实体单独提取为一个独立节点,不要合并在一起。例如:
- - 原文"清热解毒、凉血止血"→拆分为"清热解毒"和"凉血止血"两个节点
- - 原文"头痛、发热、咳嗽"→拆分为"头痛"、"发热"、"咳嗽"三个节点
- - 原文"太阳病及阳明病"→拆分为"太阳病"和"阳明病"两个节点
+1. 提取所有方剂、功能、主治证候
+2. 为每个实体指定类型
+3. 原子化提取
### 关系提取规则
-1. 提取方剂与功能之间的关系(具有功能)
-2. 提取功能与主治证候之间的关系(主治)
-3. 为每种关系添加详细描述
-
-### 约束条件
-- 每条边必须连接已提取的节点
-- 不要创建未在文本中提及的实体或关系
-- 保持客观准确,符合方剂学专业术语规范
+1. 提取方剂与功能之间的关系
+2. 提取功能与主治证候之间的关系
### 源文本:
"""
_NODE_PROMPT = """## 角色与任务
-你是一位专业的实体识别专家,请从文本中提取所有方剂、功能和主治证候实体作为节点。
+请从文本中提取方剂、功能和主治证候实体作为节点。
## 核心概念定义
-- **节点 (Node)**:本模板中的"节点"指通用实体,包括方剂、功能、主治证候等类型,用于表示知识图谱中的基本元素。
+- **节点 (Node)**:方剂、功能、主治证候
## 提取规则
-1. 提取所有实体:方剂、功能、主治证候
-2. 为每个实体指定类型:方剂、功能、主治证候
-3. 保持实体名称与原文一致
-4. 为每个实体添加简要描述
-5. **关键原子化要求**:如果文本中出现逗号、顿号、及、与、和等分隔的多个实体,必须将每个实体单独提取为一个独立节点,不要合并在一起。例如:
- - 原文"清热解毒、凉血止血"→拆分为"清热解毒"和"凉血止血"两个节点
- - 原文"头痛、发热、咳嗽"→拆分为"头痛"、"发热"、"咳嗽"三个节点
- - 原文"太阳病及阳明病"→拆分为"太阳病"和"阳明病"两个节点
+1. 提取所有方剂、功能、主治证候
+2. 为每个实体指定类型
+3. 原子化提取
### 源文本:
"""
_EDGE_PROMPT = """## 角色与任务
-你是一位专业的关系提取专家,请从给定实体列表中提取方剂、功能和主治证候之间的关联关系。
+请从已知实体列表中提取方剂、功能和主治证候之间的关联关系。
## 核心概念定义
-- **节点 (Node)**:本模板中的"节点"指通用实体,作为关系的参与者。
-- **边 (Edge)**:本模板中的"边"指实体之间的二元关系,包括具有功能、主治等关系类型。
-
-## 提取规则
-1. 提取方剂与功能之间的关系(具有功能)
-2. 提取功能与主治证候之间的关系(主治)
-3. 为每种关系添加详细描述
+- **边 (Edge)**:实体之间的二元关系
-### 约束条件
-1. 仅从下方已知实体列表中提取边
+## 约束条件
+1. 仅从已知实体列表中提取边
2. 不要创建未列出的实体
-3. 每条边必须包含 source、target、relationType
-
"""
diff --git a/hyperextract/templates/zh/tcm/meridian_flow_graph.py b/hyperextract/templates/zh/tcm/meridian_flow_graph.py
index 5c50147..cb7f7c9 100644
--- a/hyperextract/templates/zh/tcm/meridian_flow_graph.py
+++ b/hyperextract/templates/zh/tcm/meridian_flow_graph.py
@@ -33,68 +33,49 @@ _PROMPT = """## 角色与任务
你是一位专业的经络学专家,请从文本中提取十二经脉的循行路线及经气流注次序。
## 核心概念定义
-- **节点 (Node)**:本模板中的"节点"指经脉实体,包括经脉名称、类别、关联脏腑、循行路线等信息。
-- **边 (Edge)**:本模板中的"边"指经脉之间的流注关系,包括源经脉、目标经脉、流注次序、连接部位等信息。
+- **节点 (Node)**:经脉实体
+- **边 (Edge)**:经脉之间的流注关系
## 提取规则
### 节点提取规则
-1. 提取所有经脉实体:手太阴肺经、手阳明大肠经、足阳明胃经、足太阴脾经、手少阴心经、手太阳小肠经、足太阳膀胱经、足少阴肾经、手厥阴心包经、手少阳三焦经、足少阳胆经、足厥阴肝经
-2. 为每个经脉指定类别:阴经、阳经
-3. 提取每个经脉的关联脏腑
-4. 提取每个经脉的循行路线
+1. 提取所有经脉实体
+2. 为每个经脉指定类别
+3. 提取关联脏腑和循行路线
### 关系提取规则
1. 提取经脉之间的流注关系
-2. 为每种关系指定类型:流注
-3. 提取流注次序
-4. 提取连接部位
-
-### 约束条件
-- 每条边必须连接已提取的节点
-- 不要创建未在文本中提及的实体或关系
-- 保持客观准确,符合经络学专业术语规范
-
-### 术语标准化指导
-- **经脉名称统一**:确保使用标准经脉名称(如"手太阴肺经"而非简称)
-- **流注次序确认**:十二经脉流注有固定次序,可参考标准次序辅助确认
-- **循行路线保留**:文言中循行路线描述保持原文,无需改写
+2. 提取流注次序和连接部位
### 源文本:
"""
_NODE_PROMPT = """## 角色与任务
-你是一位专业的经络学专家,请从文本中提取所有经脉实体作为节点。
+请从文本中提取经脉实体作为节点。
## 核心概念定义
-- **节点 (Node)**:本模板中的"节点"指经脉实体,包括经脉名称、类别、关联脏腑、循行路线等信息。
+- **节点 (Node)**:经脉实体
## 提取规则
-1. 提取所有经脉实体:手太阴肺经、手阳明大肠经、足阳明胃经、足太阴脾经、手少阴心经、手太阳小肠经、足太阳膀胱经、足少阴肾经、手厥阴心包经、手少阳三焦经、足少阳胆经、足厥阴肝经
-2. 为每个经脉指定类别:阴经、阳经
-3. 提取每个经脉的关联脏腑
-4. 提取每个经脉的循行路线
+1. 提取所有经脉实体
+2. 为每个经脉指定类别
+3. 提取关联脏腑和循行路线
### 源文本:
"""
_EDGE_PROMPT = """## 角色与任务
-你是一位专业的经络学专家,请从给定经脉列表中提取经脉之间的流注关系。
+请从已知经脉列表中提取经脉之间的流注关系。
## 核心概念定义
-- **节点 (Node)**:本模板中的"节点"指经脉实体,作为流注关系的参与者。
-- **边 (Edge)**:本模板中的"边"指经脉之间的流注关系,包括源经脉、目标经脉、流注次序、连接部位等信息。
+- **边 (Edge)**:经脉之间的流注关系
## 提取规则
1. 提取经脉之间的流注关系
-2. 为每种关系指定类型:流注
-3. 提取流注次序
-4. 提取连接部位
+2. 提取流注次序和连接部位
-### 约束条件
-1. 仅从下方已知经脉列表中提取边
+## 约束条件
+1. 仅从已知经脉列表中提取边
2. 不要创建未列出的经脉
-3. 每条边必须包含 source、target、relationType、flowOrder
-
"""
diff --git a/hyperextract/templates/zh/tcm/prescription_modification.py b/hyperextract/templates/zh/tcm/prescription_modification.py
index 55ce08d..f7849b9 100644
--- a/hyperextract/templates/zh/tcm/prescription_modification.py
+++ b/hyperextract/templates/zh/tcm/prescription_modification.py
@@ -1,112 +1,108 @@
-"""处方加减逻辑 - 提取基于特定症状对基础方剂进行的药物加减(如"随证加减")。
+"""处方加减逻辑 - 提取基于特定证型或症状对基础方剂进行的药物加减(如"随证加减")。
-适用于临证用药规律分析。
+适用于临证用药规律分析、临床处方加减逻辑提取。
+
+设计原则:
+- 节点(PrescriptionNode):存储方剂、药物、证型、症状等实体
+- 超边(ModificationHyperedge):存储完整的加减关系,包含基础方、加减条件、加减药物
"""
-from typing import Any
+from typing import Any, List
from langchain_core.language_models import BaseChatModel
from langchain_core.embeddings import Embeddings
from pydantic import BaseModel, Field
-from hyperextract.types import AutoGraph
-
-class PrescriptionEntity(BaseModel):
- """处方实体节点"""
- name: str = Field(description="实体名称,如方剂名、药物名、症状名")
- category: str = Field(description="实体类型:方剂、药物、症状")
- description: str = Field(description="简要描述", default="")
-
-class ModificationRelation(BaseModel):
- """处方加减关系边"""
- source: str = Field(description="源实体,如症状或证型")
- target: str = Field(description="目标实体,如药物")
+from hyperextract.types import AutoHypergraph
+
+
+class PrescriptionNode(BaseModel):
+ """处方加减实体节点"""
+
+ name: str = Field(description="实体名称")
+ category: str = Field(description="实体类型:基础方、药物、证型、症状")
+
+
+class ModificationHyperedge(BaseModel):
+ """处方加减超边 - 存储完整的加减关系"""
+
+ baseFormula: str = Field(description="基础方剂名称")
+ condition: str = Field(description="加减条件(证型或症状)")
modificationType: str = Field(description="加减类型:加、减、替换")
+ herbs: List[str] = Field(description="加减药物列表")
reason: str = Field(description="加减原因", default="")
- dosage: str = Field(description="药物剂量", default="")
- baseFormula: str = Field(description="基础方剂名称", default="")
+ dosage: str = Field(description="调整后的剂量", default="")
+
_NODE_PROMPT = """## 角色与任务
-你是一位专业的中医方剂专家,请从文本中提取所有关键实体作为节点。
+你是一位专业的中医方剂专家,请从文本中提取处方加减相关的实体作为节点。
## 核心概念定义
-- **节点 (Node)**:图谱中的实体
-- **边 (Edge)**:节点之间的二元关系
+- **节点 (Node)**:从文档中提取的实体
## 提取规则
-1. 提取所有方剂、药物、症状等实体
-2. 为每个实体指定类型:方剂、药物、症状
-3. 保持实体名称与原文一致
-4. 方剂应包含完整的方剂名称
-5. 药物应包含具体的中药名称
-6. 症状应包含具体的临床表现
+### 核心约束
+1. 每个节点只能对应一个独立的实体,禁止将多个实体合并为一个节点
+2. 实体名称与原文保持一致
+
+### 领域特定规则
+- 基础方:方剂名称,如桂枝汤、麻黄汤、小柴胡汤
+- 药物:中药名称,如葛根、厚朴、杏仁、附子
+- 证型/症状:临床表现,如项背强几几、喘、腹满痛
### 源文本:
"""
_EDGE_PROMPT = """## 角色与任务
-你是一位专业的中医方剂专家,请从给定实体列表中提取处方加减逻辑。
+你是一位专业的中医方剂专家,请从已知实体列表中提取处方加减逻辑作为超边。
## 核心概念定义
-- **节点 (Node)**:图谱中的实体
-- **边 (Edge)**:节点之间的二元关系
+- **节点 (Node)**:从文档中提取的实体
+- **边 (Edge)**:连接多个节点,表达多个实体间的复杂关联关系
## 提取规则
-1. 仅从下方已知实体列表中提取边
-2. 不要创建未列出的实体
-3. 每条边必须包含 source、target、modificationType
-4. source 应为症状或证型
-5. target 应为药物
-6. modificationType 应为:加、减、替换
-7. 提取加减的原因和剂量信息
-8. 提取基础方剂名称
+### 核心约束
+1. 仅从已知实体列表中提取边,不要创建未列出的实体
+2. 关系描述应与原文保持一致
+
+### 领域特定规则
+- 加减类型:加、减、替换(通过 modificationType 字段区分)
+### 已知实体列表:
"""
_PROMPT = """## 角色与任务
-你是一位专业的中医方剂专家,请从文本中提取实体和处方加减逻辑。
+你是一位专业的中医方剂专家,请从文本中提取处方加减的实体和超边结构。
## 核心概念定义
-- **节点 (Node)**:图谱中的实体,如方剂、药物、症状等
-- **边 (Edge)**:节点之间的二元关系,如症状与药物的加减关系
+- **节点 (Node)**:从文档中提取的实体
+- **边 (Edge)**:连接多个节点,表达多个实体间的复杂关联关系
## 提取规则
-### 节点提取规则
-1. 提取所有方剂、药物、症状等实体
-2. 为每个实体指定类型:方剂、药物、症状
-3. 保持实体名称与原文一致
-
-### 关系提取规则
-1. 提取基于特定症状对基础方剂进行的药物加减
-2. 每条边必须包含 source、target、modificationType
-3. source 应为症状或证型
-4. target 应为药物
-5. modificationType 应为:加、减、替换
-6. 提取加减的原因和剂量信息
-7. 提取基础方剂名称
-
-### 术语标准化指导
-- **药物名称标准化**:同一药物的不同名称需统一使用规范正名
-- **加减术语统一**:"加"、"添"、"增"统一为"加";"减"、"去"、"除"统一为"减";"易"、"换"统一为"替换"
-- **症状表述规范化**:文言症状表述需转换为规范中医术语
-- **方剂名称保留**:基础方剂名称保持原文表述
+### 核心约束
+1. 每个节点只能对应一个独立的实体,禁止将多个实体合并为一个节点
+2. 实体名称与原文保持一致
+3. 仅从已知实体列表中提取边,不要创建未列出的实体
+
+### 领域特定规则
+- 基础方:方剂名称
+- 药物:中药名称
+- 证型/症状:临床表现
+- 加减类型:加、减、替换
### 源文本:
"""
-# ==============================================================================
-# 3. 模板类
-# ==============================================================================
-class PrescriptionModification(AutoGraph[PrescriptionEntity, ModificationRelation]):
+class PrescriptionModification(AutoHypergraph[PrescriptionNode, ModificationHyperedge]):
"""
- 适用文档: 中医方剂学著作、临床医案
-
+ 适用文档: 中医方剂学著作、临床医案、伤寒论、金匮要略
+
功能介绍:
- 从中医文本中提取基于特定症状对基础方剂进行的药物加减逻辑,
+ 从中医文本中提取基于特定证型或症状对基础方剂进行的药物加减逻辑,
如"随证加减"等临证用药规律。
-
+
Example:
>>> template = PrescriptionModification(llm_client=llm, embedder=embedder)
- >>> template.feed_text("...")
+ >>> template.feed_text("若兼喘者,加厚朴二两、杏仁五十枚。")
>>> template.show()
"""
@@ -122,7 +118,7 @@ class PrescriptionModification(AutoGraph[PrescriptionEntity, ModificationRelatio
):
"""
初始化处方加减逻辑模板。
-
+
Args:
llm_client: LLM 客户端,用于知识提取
embedder: 嵌入模型,用于语义检索
@@ -132,12 +128,22 @@ class PrescriptionModification(AutoGraph[PrescriptionEntity, ModificationRelatio
verbose: 是否输出详细日志,默认为 False
**kwargs: 其他技术参数,传递给基类
"""
+
+ def nodes_in_edge_extractor(edge: ModificationHyperedge) -> set:
+ nodes = set()
+ if edge.baseFormula:
+ nodes.add(edge.baseFormula)
+ for herb in edge.herbs:
+ if herb:
+ nodes.add(herb)
+ return nodes
+
super().__init__(
- node_schema=PrescriptionEntity,
- edge_schema=ModificationRelation,
+ node_schema=PrescriptionNode,
+ edge_schema=ModificationHyperedge,
node_key_extractor=lambda x: x.name,
- edge_key_extractor=lambda x: f"{x.source}|{x.modificationType}|{x.target}",
- nodes_in_edge_extractor=lambda x: (x.source, x.target),
+ edge_key_extractor=lambda x: f"{x.baseFormula}|{x.condition}",
+ nodes_in_edge_extractor=nodes_in_edge_extractor,
llm_client=llm_client,
embedder=embedder,
extraction_mode=extraction_mode,
@@ -159,19 +165,20 @@ class PrescriptionModification(AutoGraph[PrescriptionEntity, ModificationRelatio
):
"""
展示处方加减逻辑图。
-
+
Args:
top_k_nodes_for_search: 语义检索返回的节点数量,默认为 3
top_k_edges_for_search: 语义检索返回的边数量,默认为 3
top_k_nodes_for_chat: 问答使用的节点数量,默认为 3
top_k_edges_for_chat: 问答使用的边数量,默认为 3
"""
- def node_label_extractor(node: PrescriptionEntity) -> str:
+
+ def node_label_extractor(node: PrescriptionNode) -> str:
return f"{node.name} ({node.category})"
-
- def edge_label_extractor(edge: ModificationRelation) -> str:
- return f"{edge.modificationType}: {edge.target}"
-
+
+ def edge_label_extractor(edge: ModificationHyperedge) -> str:
+ return f"{edge.baseFormula} | {edge.condition}"
+
super().show(
node_label_extractor=node_label_extractor,
edge_label_extractor=edge_label_extractor,
diff --git a/hyperextract/templates/zh/tcm/processing_method.py b/hyperextract/templates/zh/tcm/processing_method.py
index e6e4213..e188c9b 100644
--- a/hyperextract/templates/zh/tcm/processing_method.py
+++ b/hyperextract/templates/zh/tcm/processing_method.py
@@ -22,26 +22,13 @@ class ProcessingMethodItem(BaseModel):
_PROMPT = """## 角色与任务
你是一位专业的中药炮制专家,请从文本中提取药物的不同炮制方法及其对药效的影响。
-## 核心概念定义
-- **条目 (Item)**:本模板中的"条目"指单一炮制方法,包含药物名称、炮制方法名称、具体工艺流程、对药效的影响、适用病症或方剂等字段的结构化信息。
-
## 提取规则
1. 识别文本中提到的所有药物及其炮制方法
-2. 提取每种炮制方法的名称(如蜜炙、酒炒、醋制、盐制等)
+2. 提取每种炮制方法的名称
3. 提取每种炮制方法的具体工艺流程
4. 提取炮制对药效的影响
5. 提取该炮制方法的适用病症或方剂
-### 约束条件
-- 只提取文本中明确提及的信息,不添加额外内容
-- 保持客观准确,符合中药炮制专业术语规范
-
-### 文言文特殊处理指导
-- **炮制术语理解**:文言炮制术语需准确理解(如"炙"、"炒"、"煅"、"蒸"等)
-- **工艺流程解析**:文言炮制工艺流程常采用简洁表述,需完整理解其步骤
-- **药效变化描述**:文言中对药效变化的描述需准确提取(如"增强"、"缓和"、"改变"等)
-- **古代计量单位**:保留古代剂量单位和工艺描述,无需转换
-
### 源文本:
"""
diff --git a/hyperextract/templates/zh/tcm/pulse_tongue_record.py b/hyperextract/templates/zh/tcm/pulse_tongue_record.py
index 1fccae0..3e2f504 100644
--- a/hyperextract/templates/zh/tcm/pulse_tongue_record.py
+++ b/hyperextract/templates/zh/tcm/pulse_tongue_record.py
@@ -101,7 +101,7 @@ class PulseTongueRecord(AutoList[PulseTongueItem]):
top_k_for_chat: 问答使用的条目数量,默认为 3
"""
def item_label_extractor(item: PulseTongueItem) -> str:
- return f"{item.type} - {item.subType}: {item.feature}"
+ return f"{item.type} - {item.subType}"
super().show(
item_label_extractor=item_label_extractor,
diff --git a/hyperextract/templates/zh/tcm/syndrome_reasoning_graph.py b/hyperextract/templates/zh/tcm/syndrome_reasoning_graph.py
index ed04e55..21cdb1e 100644
--- a/hyperextract/templates/zh/tcm/syndrome_reasoning_graph.py
+++ b/hyperextract/templates/zh/tcm/syndrome_reasoning_graph.py
@@ -22,7 +22,7 @@ class SyndromeReasoningEdge(BaseModel):
"""辨证论治逻辑边"""
nodes: List[str] = Field(
- description="参与推理的节点列表,按推理顺序排列:[症状1, 症状2, ..., 证型, 治则, 药物1, 药物2, ...]"
+ description="参与推理的节点列表:[症状1, 症状2, ..., 证型, 治则, 药物1, 药物2, ...]"
)
reasoningType: str = Field(description="推理类型:辨证论治、随证加减")
reasoningProcess: str = Field(description="推理过程描述", default="")
@@ -30,74 +30,69 @@ class SyndromeReasoningEdge(BaseModel):
_NODE_PROMPT = """## 角色与任务
-你是一位专业的中医辨证论治专家,请从文本中提取所有关键实体作为节点。
+你是一位专业的中医辨证论治专家,请从文本中提取中医实体作为节点。
## 核心概念定义
-- **节点 (Node)**:图谱中的实体
-- **边 (Edge)**:连接多个节点的超边,表达复杂的推理关系
+- **节点 (Node)**:从文档中提取的实体
+- **边 (Edge)**:连接多个节点,表达多个实体间的复杂关联关系
## 提取规则
-1. 提取所有症状、证型、治则、药物等实体
-2. 为每个实体指定类型:症状、证型、治则、药物
-3. 保持实体名称与原文一致
-4. 症状应包含望闻问切所获得的各种临床表现
-5. 证型应包含中医诊断的证候类型
-6. 治则应包含治疗原则和方法
-7. 药物应包含处方中的各种中药
+### 核心约束
+1. 每个节点只能对应一个独立的实体,禁止将多个实体合并为一个节点
+2. 实体名称与原文保持一致
+
+### 领域特定规则
+- 症状:临床表现,如发热、汗出、恶风
+- 证型:疾病分类,如太阳中风、太阳伤寒
+- 治则:治疗原则,如解肌发表、调和营卫
+- 药物:中药名称,如桂枝、麻黄、杏仁
### 源文本:
"""
_EDGE_PROMPT = """## 角色与任务
-你是一位专业的中医辨证论治专家,请从给定实体列表中提取辨证论治的推理逻辑。
+你是一位专业的中医辨证论治专家,请从已知实体列表中提取辨证论治的推理逻辑作为超边。
## 核心概念定义
-- **节点 (Node)**:图谱中的实体
-- **边 (Edge)**:连接多个节点的超边,表达复杂的推理关系
+- **节点 (Node)**:从文档中提取的实体
+- **边 (Edge)**:连接多个节点,表达多个实体间的复杂关联关系
## 提取规则
-1. 仅从下方已知实体列表中提取边
-2. 不要创建未列出的实体
-3. 每条边必须包含 nodes、reasoningType、reasoningProcess
-4. nodes 列表必须按推理顺序排列:[症状1, 症状2, ..., 证型, 治则, 药物1, 药物2, ...]
-5. reasoningType 应为"辨证论治"或"随证加减"
-6. reasoningProcess 应详细描述从症状到证型、证型到治则、治则到处方的推理过程
+### 核心约束
+1. 仅从已知实体列表中提取边,不要创建未列出的实体
+2. 关系描述应与原文保持一致
+
+### 领域特定规则
+- 推理类型:辨证论治、随证加减
+- 推理链条:症状群 → 证型 → 治则 → 处方
+### 已知实体列表:
"""
_PROMPT = """## 角色与任务
-你是一位专业的中医辨证论治专家,请从文本中提取中医实体和它们之间的辨证论治推理逻辑。
+你是一位专业的中医辨证论治专家,请从文本中提取中医实体和辨证论治推理逻辑作为超边。
## 核心概念定义
-- **节点 (Node)**:图谱中的实体,如症状、证型、治则、药物等
-- **边 (Edge)**:连接多个节点的超边,表达复杂的推理关系
+- **节点 (Node)**:从文档中提取的实体
+- **边 (Edge)**:连接多个节点,表达多个实体间的复杂关联关系
## 提取规则
-### 节点提取规则
-1. 提取所有症状、证型、治则、药物等实体
-2. 为每个实体指定类型:症状、证型、治则、药物
-3. 保持实体名称与原文一致
-
-### 边提取规则
-1. 提取从症状到证型、证型到治则、治则到处方的完整推理链条
-2. 每条边必须包含 nodes、reasoningType、reasoningProcess
-3. nodes 列表必须按推理顺序排列:[症状1, 症状2, ..., 证型, 治则, 药物1, 药物2, ...]
-4. reasoningType 应为"辨证论治"或"随证加减"
-5. reasoningProcess 应详细描述推理过程
-
-### 文言文特殊处理指导
-- **症状表述解析**:文言症状常采用简洁表述,需完整理解其含义(如"恶寒发热"指怕冷和发热两个症状)
-- **证型术语理解**:文言中证型表述需准确识别(如"太阳中风证"、"阳明腑实证")
-- **省略句式处理**:文言常省略主语或连接词,需根据上下文补充理解
-- **医案结构识别**:医案通常有"症状→辨证→治法→方药"的结构,需按此顺序提取
+### 核心约束
+1. 每个节点只能对应一个独立的实体,禁止将多个实体合并为一个节点
+2. 实体名称与原文保持一致
+3. 仅从已知实体列表中提取边,不要创建未列出的实体
+
+### 领域特定规则
+- 症状:临床表现
+- 证型:疾病分类
+- 治则:治疗原则
+- 药物:中药名称
+- 推理类型:辨证论治、随证加减
+- 推理链条:症状群 → 证型 → 治则 → 处方
### 源文本:
"""
-# ==============================================================================
-# 3. 模板类
-# ==============================================================================
-
class SyndromeReasoningGraph(AutoHypergraph[TCMEntity, SyndromeReasoningEdge]):
"""
diff --git a/hyperextract/types/model.py b/hyperextract/types/model.py
index 81109b9..b284479 100644
--- a/hyperextract/types/model.py
+++ b/hyperextract/types/model.py
@@ -136,11 +136,13 @@ class AutoModel(BaseAutoType[T]):
return self._data
def empty(self) -> bool:
- """Checks if the model is empty (all fields are None).
+ """Checks if the model is empty (no data stored).
Returns:
True if no data is stored, False otherwise.
"""
+ if self._data is None:
+ return True
for field_name in self._data_schema.model_fields:
value = getattr(self._data, field_name)
# Check if the value is not considered empty
@@ -158,10 +160,10 @@ class AutoModel(BaseAutoType[T]):
def _init_data_state(self) -> None:
"""
- INIT/RESET: Initialize or reset with empty schema instance.
+ INIT/RESET: Initialize or reset to empty state (None).
Called during __init__ and when clear() is called.
"""
- self._data = self._data_schema()
+ self._data = None
def _init_index_state(self) -> None:
"""Initialize vector index to empty state."""
@@ -210,7 +212,7 @@ class AutoModel(BaseAutoType[T]):
A new merged knowledge object with intelligently combined fields.
"""
if not data_list:
- return self._data_schema()
+ return None
if len(data_list) == 1:
return data_list[0]
diff --git a/tests/fixtures/test_samples/en/tcm/herb_property_single.md b/tests/fixtures/test_samples/en/tcm/herb_property_single.md
new file mode 100644
index 0000000..64a2382
--- /dev/null
+++ b/tests/fixtures/test_samples/en/tcm/herb_property_single.md
@@ -0,0 +1,18 @@
+# Herb Property Sample - Single Herb
+
+## Ginseng
+
+### Alternative Names
+Ginseng, also known as Yellow Ginseng, Blood Ginseng, Human Bearing, Ghost Cover, Divine Herb, Earth Essence, Sea Cream, and Wrinkled Face Rejuvenation.
+
+### Collection and Identification
+*Bielu* states: Ginseng grows in the valleys of Shangdang and Liaodong. The roots are collected in the first ten days of February, April, and August, scraped with a bamboo knife, and dried in the sun, avoiding wind exposure.
+
+### Properties and Flavors
+Sweet, slightly cold, non-toxic.
+
+### Indications
+Tonifies the five viscera, calms the spirit, stabilizes the soul, stops palpitations, eliminates pathogenic factors, improves eyesight, opens the heart, and enhances wisdom. Long-term use promotes lightness of body and prolongs life.
+
+### Meridians
+Enters the Spleen Meridian, Lung Meridian, and Heart Meridian.
diff --git a/tests/fixtures/test_samples/en/tcm/prescription_modification_sample.md b/tests/fixtures/test_samples/en/tcm/prescription_modification_sample.md
new file mode 100644
index 0000000..43fe3c9
--- /dev/null
+++ b/tests/fixtures/test_samples/en/tcm/prescription_modification_sample.md
@@ -0,0 +1,59 @@
+# Prescription Modification Sample
+
+## Modified Gui Zhi Tang (Cinnamon Decoction)
+
+### Original Formula
+Gui Zhi Tang: Gui Zhi (Cinnamon Twig) 3 liang (peeled), Shao Yao (White Peony) 3 liang, Gan Cao (Licorice) 2 liang (honey-fried), Sheng Jiang (Fresh Ginger) 3 liang (sliced), Da Zao (Jujube) 12 pieces (broken).
+
+### Modification 1: For Neck and Back Stiffness
+If (the condition) involves neck and back stiffness, **add Ge Gen (Kudzu Root) 4 liang**.
+
+### Modification 2: For Asthma
+If (the condition) involves asthma, **add Hou Po (Magnolia Bark) 2 liang, Xing Ren (Apricot Kernel) 50 pieces**.
+
+### Modification 3: For Abdominal Fullness and Pain
+If (the condition) involves abdominal fullness and pain, **remove Shao Yao (White Peony), add Fu Zi (Aconite) 1 piece**.
+
+### Modification 4: For Yang Deficiency with Sweating
+If (the condition) involves yang deficiency with profuse sweating, **remove Shao Yao (White Peony), add Fu Zi (Aconite) 1 piece**.
+
+---
+
+## Modified Ma Huang Tang (Ephedra Decoction)
+
+### Original Formula
+Ma Huang Tang: Ma Huang (Ephedra) 3 liang (removed nodes), Gui Zhi (Cinnamon Twig) 2 liang (peeled), Gan Cao (Licorice) 1 liang (honey-fried), Xing Ren (Apricot Kernel) 70 pieces (peeled tip).
+
+### Modification 1: For Internal Heat
+If (the condition) involves internal heat, **remove Gui Zhi (Cinnamon Twig), add Shi Gao (Gypsum) half jin**.
+
+### Modification 2: For Dampness
+If (the condition) involves dampness, **add Cang Zhu (Atractylodes) 2 liang**.
+
+---
+
+## Modified Xiao Chai Hu Tang (Minor Bupleurum Decoction)
+
+### Original Formula
+Xiao Chai Hu Tang: Chai Hu (Bupleurum) half jin, Huang Qin (Scutellaria) 3 liang, Ren Shen (Ginseng) 3 liang, Gan Cao (Licorice) 3 liang (honey-fried), Sheng Jiang (Fresh Ginger) 3 liang (sliced), Da Zao (Jujube) 12 pieces (broken), Ban Xia (Pinellia) half Sheng (washed).
+
+### Modification 1: For Chest Distress without Vomiting
+If (the condition) involves chest distress without vomiting, **remove Ban Xia (Pinellia) and Ren Shen (Ginseng), add Gua Lou Shi (Trichosanthes Fruit) 1 piece**.
+
+### Modification 2: For Thirst
+If (the condition) involves thirst, **remove Ban Xia (Pinellia), add Ren Shen (Ginseng) to make 4.5 liang total, add Tian Hua Fen (Trichosanthes Root) 4 liang**.
+
+### Modification 3: For Abdominal Pain
+If (the condition) involves abdominal pain, **remove Huang Qin (Scutellaria), add Shao Yao (White Peony) 3 liang**.
+
+### Modification 4: For Subcostal Fullness and Hardness
+If (the condition) involves subcostal fullness and hardness, **remove Da Zao (Jujube), add Mu Li (Oyster Shell) 4 liang**.
+
+### Modification 5: For Palpitation
+If (the condition) involves palpitation, **remove Huang Qin (Scutellaria), add Fu Ling (Poria) 4 liang**.
+
+### Modification 6: For No Thirst with Mild Fever
+If (the condition) involves no thirst with mild fever, **remove Ren Shen (Ginseng), add Gui Zhi (Cinnamon Twig) 3 liang, cover with blanket to induce mild sweating**.
+
+### Modification 7: For Cough
+If (the condition) involves cough, **remove Ren Shen (Ginseng), Da Zao (Jujube), Sheng Jiang (Fresh Ginger), add Wu Wei Zi (Schisandra) half Sheng, Gan Jiang (Dried Ginger) 2 liang**.
diff --git a/tests/fixtures/test_samples/zh/tcm/herb_property_single.md b/tests/fixtures/test_samples/zh/tcm/herb_property_single.md
new file mode 100644
index 0000000..ab4b93b
--- /dev/null
+++ b/tests/fixtures/test_samples/zh/tcm/herb_property_single.md
@@ -0,0 +1,18 @@
+# 药物性味归经样本 - 单味药
+
+## 人参
+
+### 释名
+人参,亦名黄参、血参、人衔、鬼盖、神草、土精、地精、海腴、皱面还丹。
+
+### 集解
+《别录》曰:人参生上党山谷及辽东,二月、四月、八月上旬采根,竹刀刮暴干,无令见风。
+
+### 气味
+甘,微寒,无毒。
+
+### 主治
+补五脏,安精神,定魂魄,止惊悸,除邪气,明目,开心益智。久服轻身延年。
+
+### 归经
+归脾经、肺经、心经
diff --git a/tests/fixtures/test_samples/zh/tcm/prescription_modification_sample.md b/tests/fixtures/test_samples/zh/tcm/prescription_modification_sample.md
new file mode 100644
index 0000000..a62a99a
--- /dev/null
+++ b/tests/fixtures/test_samples/zh/tcm/prescription_modification_sample.md
@@ -0,0 +1,59 @@
+# 处方加减逻辑样本
+
+## 桂枝汤加减
+
+### 原方
+桂枝汤:桂枝三两(去皮)、芍药三两、甘草二两(炙)、生姜三两(切)、大枣十二枚(擘)。
+
+### 加减一:兼项背强几几
+若(病)兼项背强几几者,**加葛根四两**。
+
+### 加减二:兼喘
+若(病)兼喘者,**加厚朴二两、杏仁五十枚**。
+
+### 加减三:兼腹满痛
+若(病)兼腹满痛者,**去芍药,加附子一枚**。
+
+### 加减四:兼阳虚漏汗
+若(病)兼阳虚漏汗者,**去芍药,加附子一枚**。
+
+---
+
+## 麻黄汤加减
+
+### 原方
+麻黄汤:麻黄三两(去节)、桂枝二两(去皮)、甘草一两(炙)、杏仁七十个(去皮尖)。
+
+### 加减一:兼里有热
+若(病)兼里有热者,**去桂枝,加石膏半斤**。
+
+### 加减二:兼湿
+若(病)兼湿者,**加苍术二两**。
+
+---
+
+## 小柴胡汤加减
+
+### 原方
+小柴胡汤:柴胡半斤、黄芩三两、人参三两、甘草三两(炙)、生姜三两(切)、大枣十二枚(擘)、半夏半升(洗)。
+
+### 加减一:兼胸中烦而不呕
+若(病)兼胸中烦而不呕者,**去半夏、人参,加瓜蒌实一枚**。
+
+### 加减二:兼渴
+若(病)兼渴,**去半夏,加人参合前成四两半,瓜蒌根四两**。
+
+### 加减三:兼腹中痛
+若(病)兼腹中痛者,**去黄芩,加芍药三两**。
+
+### 加减四:兼胁下痞硬
+若(病)兼胁下痞硬者,**去大枣,加牡蛎四两**。
+
+### 加减五:兼心下悸
+若(病)兼心下悸,**去黄芩,加茯苓四两**。
+
+### 加减六:兼不渴
+若(病)兼不渴,外有微热者,**去人参,加桂枝三两,温覆微汗愈**。
+
+### 加减七:兼咳
+若(病)兼咳者,**去人参、大枣、生姜,加五味子半升、干姜二两**。