Hypergraph is more powerful. Transform unstructured text into structured knowledge with LLMs. Graphs, hypergraphs, and spatio-temporal extractions — with one command.
feat: 添加通用知识提取模板并重构模块结构
yifanfeng971d39fd71fe38
diff --git a/hyperextract/templates/en/__init__.py b/hyperextract/templates/en/__init__.pyindex 10dac70..5f14636 100644--- a/hyperextract/templates/en/__init__.py+++ b/hyperextract/templates/en/__init__.py@@ -1,4 +1,35 @@-# from .general import KnowledgeGraph, ConceptMap, LogicGraph+from .general import (+ # Knowledge Graph+ KnowledgeGraph,+ # Entity Registry+ EntityRegistry,+ # Keyword List+ KeywordList,+ # Encyclopedia Item+ EncyclopediaItem,+ # Concept Hierarchy+ ConceptHierarchy,+ # Cross Reference Network+ CrossReferenceNet,+ # Personal Profile+ PersonalProfile,+ # Social Network+ SocialNetwork,+ # Life Event Timeline+ LifeEventTimeline,+ # Regulation Profile+ RegulationProfile,+ # Clause List+ ClauseList,+ # Penalty Registry+ PenaltyRegistry,+ # Operational Procedure+ OperationalProcedure,+ # Penalty Mapping+ PenaltyMapping,+ # Compliance Logic+ ComplianceLogic,+)from .agriculture import CropCycleGraph, LivestockGraph, AgriPestControlfrom .biology import TaxonomyGraph, BiologicalNetwork, MetabolicHypergraphfrom .finance import (@@ -31,18 +62,37 @@ from .medicine import DrugInteractionGraph, ClinicalTreatmentTimeline, Diagnostifrom .news import BreakingEventGraph, TopicTimeline, OpinionSet__all__ = [+ # general+ ## Knowledge Graph"KnowledgeGraph",- "ConceptMap",- "LogicGraph",- "CropCycleGraph",- "LivestockGraph",- "AgriPestControl",- "TaxonomyGraph",- "BiologicalNetwork",- "MetabolicHypergraph",- "MolecularStructureGraph",- "ChemicalReactionHyper",- "LabProtocolTemporal",+ ## Entity Registry+ "EntityRegistry",+ ## Keyword List+ "KeywordList",+ ## Encyclopedia Item+ "EncyclopediaItem",+ ## Concept Hierarchy+ "ConceptHierarchy",+ ## Cross Reference Network+ "CrossReferenceNet",+ ## Personal Profile+ "PersonalProfile",+ ## Social Network+ "SocialNetwork",+ ## Life Event Timeline+ "LifeEventTimeline",+ ## Regulation Profile+ "RegulationProfile",+ ## Clause List+ "ClauseList",+ ## Penalty Registry+ "PenaltyRegistry",+ ## Operational Procedure+ "OperationalProcedure",+ ## Penalty Mapping+ "PenaltyMapping",+ ## Compliance Logic+ "ComplianceLogic",# Finance"RiskAssessmentGraph","MarketSentimentGraph",@@ -81,4 +131,15 @@ __all__ = ["BreakingEventGraph","TopicTimeline","OpinionSet",+ # Agriculture+ "CropCycleGraph",+ "LivestockGraph",+ "AgriPestControl",+ # Biology+ "TaxonomyGraph",+ "BiologicalNetwork",+ "MetabolicHypergraph",+ "MolecularStructureGraph",+ "ChemicalReactionHyper",+ "LabProtocolTemporal",]diff --git a/hyperextract/templates/en/general/__init__.py b/hyperextract/templates/en/general/__init__.pyindex e69de29..d7304a5 100644--- a/hyperextract/templates/en/general/__init__.py+++ b/hyperextract/templates/en/general/__init__.py@@ -0,0 +1,48 @@+from .knowledge_graph import KnowledgeGraph+from .entity_registry import EntityRegistry+from .keyword_list import KeywordList+from .encyclopedia_item import EncyclopediaItem+from .concept_hierarchy import ConceptHierarchy+from .cross_reference_net import CrossReferenceNet+from .personal_profile import PersonalProfile+from .social_network import SocialNetwork+from .life_event_timeline import LifeEventTimeline+from .regulation_profile import RegulationProfile+from .clause_list import ClauseList+from .penalty_registry import PenaltyRegistry+from .operational_procedure import OperationalProcedure+from .penalty_mapping import PenaltyMapping+from .compliance_logic import ComplianceLogic++__all__ = [+ # Knowledge Graph+ "KnowledgeGraph",+ # Entity Registry+ "EntityRegistry",+ # Keyword List+ "KeywordList",+ # Encyclopedia Item+ "EncyclopediaItem",+ # Concept Hierarchy+ "ConceptHierarchy",+ # Cross Reference Network+ "CrossReferenceNet",+ # Personal Profile+ "PersonalProfile",+ # Social Network+ "SocialNetwork",+ # Life Event Timeline+ "LifeEventTimeline",+ # Regulation Profile+ "RegulationProfile",+ # Clause List+ "ClauseList",+ # Penalty Registry+ "PenaltyRegistry",+ # Operational Procedure+ "OperationalProcedure",+ # Penalty Mapping+ "PenaltyMapping",+ # Compliance Logic+ "ComplianceLogic"+]diff --git a/hyperextract/templates/en/general/clause_list.py b/hyperextract/templates/en/general/clause_list.pynew file mode 100644index 0000000..6178318--- /dev/null+++ b/hyperextract/templates/en/general/clause_list.py@@ -0,0 +1,104 @@+"""Clause List - Break down regulations into atomic clauses for quick indexing.++Suitable for clause retrieval, full-text comparison, etc.+"""++from typing import Any+from pydantic import BaseModel, Field+from langchain_core.language_models import BaseChatModel+from langchain_core.embeddings import Embeddings+from hyperextract.types import AutoList+++class Clause(BaseModel):+ """Regulation clause"""+ clauseId: str = Field(description="Clause ID, e.g., Article 1, Section 2.1")+ title: str = Field(description="Clause title/subject", default="")+ content: str = Field(description="Clause content")+ category: str = Field(description="Clause category: GeneralProvisions, RightsAndObligations, Procedures, Penalties, SupplementaryProvisions, Other", default="Other")+++_PROMPT = """You are a professional regulation clause breakdown expert. Please break down the text into atomic clauses.++### Extraction Rules+1. Break down the regulation into independent clauses according to its natural structure+2. Assign an ID to each clause (e.g., Article 1, Section 2.1, etc.)+3. Extract the content of each clause+4. Add an appropriate title or subject to the clause (if available)+5. Assign a category to each clause: GeneralProvisions, RightsAndObligations, Procedures, Penalties, SupplementaryProvisions, Other++### Constraints+- Keep clause content consistent with the original text+- Do not omit any clauses+- Maintain the integrity of clauses++### Source text:+"""+++class ClauseList(AutoList[Clause]):+ """+ Applicable documents: Company internal management systems, administrative regulations, operation manuals, compliance guidelines++ Function introduction:+ Break down regulations into atomic clauses for quick indexing. Suitable for clause retrieval, full-text comparison, etc.++ Example:+ >>> template = ClauseList(llm_client=llm, embedder=embedder)+ >>> template.feed_text("Universe First Slacker Company Employee Attendance Management System...")+ >>> for clause in template:+ ... print(f"{clause.clauseId}: {clause.content}")+ """++ def __init__(+ self,+ llm_client: BaseChatModel,+ embedder: Embeddings,+ *,+ max_workers: int = 10,+ verbose: bool = False,+ **kwargs: Any,+ ):+ """+ Initialize clause list template.++ Args:+ llm_client: LLM client for knowledge extraction+ embedder: Embedding model for semantic search+ 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+ """+ super().__init__(+ item_schema=Clause,+ llm_client=llm_client,+ embedder=embedder,+ prompt=_PROMPT,+ max_workers=max_workers,+ verbose=verbose,+ **kwargs,+ )++ def show(+ self,+ *,+ top_k_for_search: int = 3,+ top_k_for_chat: int = 3,+ ):+ """+ Display clause list.++ Args:+ top_k_for_search: Number of clauses to return for semantic search, default: 3+ top_k_for_chat: Number of clauses to use for chat, default: 3+ """+ def itemLabelExtractor(item: Clause) -> str:+ if item.title:+ return f"{item.clauseId}: {item.title}"+ return f"{item.clauseId}"++ super().show(+ item_label_extractor=itemLabelExtractor,+ top_k_for_search=top_k_for_search,+ top_k_for_chat=top_k_for_chat,+ )diff --git a/hyperextract/templates/en/general/compliance_logic.py b/hyperextract/templates/en/general/compliance_logic.pynew file mode 100644index 0000000..057cb6d--- /dev/null+++ b/hyperextract/templates/en/general/compliance_logic.py@@ -0,0 +1,156 @@+"""Compliance Logic - Model complex logic of "under what conditions, which subjects must/may not perform which actions".++Suitable for compliance auditing, risk identification, etc.+"""++from typing import Any, List+from pydantic import BaseModel, Field+from langchain_core.language_models import BaseChatModel+from langchain_core.embeddings import Embeddings+from hyperextract.types import AutoHypergraph+++class ComplianceElement(BaseModel):+ """Compliance Element - Node"""+ name: str = Field(description="Element name")+ type: str = Field(description="Element type: Subject, Action, Condition, Other")+ description: str = Field(description="Brief description", default="")+++class ComplianceRule(BaseModel):+ """Compliance Rule - Hyperedge"""+ rule_type: str = Field(description="Rule type: Must, MustNot, May, Other")+ action: str = Field(description="Required or prohibited action content")+ condition: str = Field(description="Applicable conditions/prerequisites", default="")+ penalty: str = Field(description="Violation consequences/penalties", default="")+ source_clause: str = Field(description="Source clause of the rule", default="")+ notes: str = Field(description="Additional notes", default="")+ participants: List[str] = Field(description="List of participating element names")+++_PROMPT = """You are a professional compliance analysis expert. Please extract compliance elements and rules from the text, modeling the logic of "under what conditions, which subjects must/may not perform which actions" to build a compliance behavior hypergraph.++### Node Extraction Rules+1. Extract elements involved in rules as nodes+2. Assign type to each element: Subject, Action, Condition, Other+3. Add brief description for each element++### Hyperedge Extraction Rules+1. Only create compliance rule hyperedges from extracted elements+2. Assign rule type: Must, MustNot, May, Other+3. Fill in required or prohibited action content (required)+4. Extract applicable conditions/prerequisites (if available)+5. Extract violation consequences/penalties (if available)+6. Extract source clause (if available)+7. Add additional notes (if available)+8. List all participating element names (required)++### Constraints+- Ensure rule logic accuracy+- Maintain objectivity and accuracy, do not add information not in the text+- Each hyperedge must contain rule_type, action, and participants+- Try to fill other fields, use empty string for missing fields++### Source text:+"""++_NODE_PROMPT = """You are a professional compliance element recognition expert. Please extract all compliance-related elements as nodes from the text.++### Extraction Rules+1. Extract elements involved in rules+2. Assign type to each element: Subject, Action, Condition, Other+3. Add brief description for each element++### Source text:+"""++_EDGE_PROMPT = """You are a professional compliance rule extraction expert. Please extract compliance rule hyperedges from the given node (element) list.++### Constraints+1. Only extract rules (hyperedges) from the known element list below+2. Do not create unlisted elements+3. Each rule must contain rule_type, action, and participants+4. Try to fill other fields, use empty string for missing fields++"""+++class ComplianceLogic(AutoHypergraph[ComplianceElement, ComplianceRule]):+ """+ Applicable documents: Compliance guidelines, administrative regulations, company management systems++ Function introduction:+ Model complex logic of "under what conditions, which subjects must/may not perform which actions". Suitable for compliance auditing, risk identification, etc.++ Example:+ >>> template = ComplianceLogic(llm_client=llm, embedder=embedder)+ >>> template.feed_text("Universe First Slacker Company Compliance Manual...")+ >>> template.show()+ """++ def __init__(+ self,+ llm_client: BaseChatModel,+ embedder: Embeddings,+ *,+ extraction_mode: str = "two_stage",+ max_workers: int = 10,+ verbose: bool = False,+ **kwargs: Any,+ ):+ """+ Initialize compliance logic hypergraph 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+ """+ super().__init__(+ node_schema=ComplianceElement,+ edge_schema=ComplianceRule,+ node_key_extractor=lambda x: x.name,+ edge_key_extractor=lambda x: f"{sorted(x.participants)}_{x.rule_type}_{x.action}",+ nodes_in_edge_extractor=lambda x: tuple(x.participants),+ llm_client=llm_client,+ embedder=embedder,+ extraction_mode=extraction_mode,+ max_workers=max_workers,+ verbose=verbose,+ prompt=_PROMPT,+ prompt_for_node_extraction=_NODE_PROMPT,+ prompt_for_edge_extraction=_EDGE_PROMPT,+ **kwargs,+ )++ def show(+ self,+ *,+ top_k_for_search: int = 3,+ top_k_for_chat: int = 3,+ ):+ """+ Display compliance logic hypergraph.++ Args:+ top_k_for_search: Number of nodes/edges to return for semantic search, default: 3+ top_k_for_chat: Number of nodes/edges to use for chat, default: 3+ """+ def node_label_extractor(node: ComplianceElement) -> str:+ return f"{node.name} ({node.type})"++ def edge_label_extractor(edge: ComplianceRule) -> str:+ return f"[{edge.rule_type}] {edge.action}"++ super().show(+ node_label_extractor=node_label_extractor,+ edge_label_extractor=edge_label_extractor,+ top_k_nodes_for_search=top_k_for_search,+ top_k_edges_for_search=top_k_for_search,+ top_k_nodes_for_chat=top_k_for_chat,+ top_k_edges_for_chat=top_k_for_chat,+ )diff --git a/hyperextract/templates/en/general/concept_hierarchy.py b/hyperextract/templates/en/general/concept_hierarchy.pynew file mode 100644index 0000000..dfefacc--- /dev/null+++ b/hyperextract/templates/en/general/concept_hierarchy.py@@ -0,0 +1,156 @@+"""Concept Hierarchy - Build subclass relationships (Subclass-Of) or composition relationships (Part-Of).++Suitable for scientific disciplines, textbook knowledge points, etc.+"""++from typing import Any, Optional+from pydantic import BaseModel, Field+from langchain_core.language_models import BaseChatModel+from langchain_core.embeddings import Embeddings+from hyperextract.types import AutoGraph+++class ConceptNode(BaseModel):+ """Concept node"""+ name: str = Field(description="Concept name")+ category: str = Field(description="Concept type: CoreConcept, Subconcept, Attribute, Instance, Other")+ description: str = Field(description="Brief concept description", default="")+++class HierarchyRelation(BaseModel):+ """Hierarchy relationship edge"""+ source: str = Field(description="Parent concept/whole concept")+ target: str = Field(description="Subconcept/part concept")+ relationType: str = Field(description="Relationship type: Subclass-Of (taxonomy), Part-Of (composition), Instance-Of (instance), Other")+ details: str = Field(description="Detailed relationship explanation", default="")+++_PROMPT = """You are a professional concept structure analysis expert. Please extract concepts and their hierarchical relationships from the text to build a concept hierarchy graph.++### 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++### 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++### Source text:+"""++_NODE_PROMPT = """You are a professional concept recognition expert. Please extract all concepts as nodes from the text.++### 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++### Source text:+"""++_EDGE_PROMPT = """You are a professional hierarchical relationship extraction expert. Please extract hierarchical relationships between concepts (nodes) from the given concept list.++### 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+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++ Function introduction:+ 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.show()+ """++ def __init__(+ self,+ llm_client: BaseChatModel,+ embedder: Embeddings,+ *,+ extraction_mode: str = "two_stage",+ max_workers: int = 10,+ verbose: bool = False,+ **kwargs: Any,+ ):+ """+ Initialize concept hierarchy 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+ """+ super().__init__(+ node_schema=ConceptNode,+ edge_schema=HierarchyRelation,+ node_key_extractor=lambda x: x.name,+ edge_key_extractor=lambda x: f"{x.source}|{x.relationType}|{x.target}",+ nodes_in_edge_extractor=lambda x: (x.source, x.target),+ llm_client=llm_client,+ embedder=embedder,+ extraction_mode=extraction_mode,+ max_workers=max_workers,+ verbose=verbose,+ prompt=_PROMPT,+ prompt_for_node_extraction=_NODE_PROMPT,+ prompt_for_edge_extraction=_EDGE_PROMPT,+ **kwargs,+ )++ def show(+ self,+ *,+ top_k_for_search: int = 3,+ top_k_for_chat: int = 3,+ ):+ """+ Display concept hierarchy graph.++ Args:+ top_k_for_search: Number of nodes/edges to return for semantic search, default: 3+ top_k_for_chat: Number of nodes/edges to use for chat, default: 3+ """+ def node_label_extractor(node: ConceptNode) -> str:+ return f"{node.name} ({node.category})"++ def edge_label_extractor(edge: HierarchyRelation) -> str:+ return edge.relationType++ super().show(+ node_label_extractor=node_label_extractor,+ edge_label_extractor=edge_label_extractor,+ top_k_nodes_for_search=top_k_for_search,+ top_k_edges_for_search=top_k_for_search,+ top_k_nodes_for_chat=top_k_for_chat,+ top_k_edges_for_chat=top_k_for_chat,+ )diff --git a/hyperextract/templates/en/general/cross_reference_net.py b/hyperextract/templates/en/general/cross_reference_net.pynew file mode 100644index 0000000..8986a3c--- /dev/null+++ b/hyperextract/templates/en/general/cross_reference_net.py@@ -0,0 +1,157 @@+"""Cross Reference Network - Map hyperlinks or mutual reference relationships between concepts.++Suitable for encyclopedia internal links, cross-entry association graphs, etc.+"""++from typing import Any, Optional+from pydantic import BaseModel, Field+from langchain_core.language_models import BaseChatModel+from langchain_core.embeddings import Embeddings+from hyperextract.types import AutoGraph+++class ReferenceNode(BaseModel):+ """Reference node"""+ name: str = Field(description="Concept/entry name")+ type: str = Field(description="Node type: Entry, Section, Concept, Person, Location, Other")+ description: str = Field(description="Brief description", default="")+++class ReferenceRelation(BaseModel):+ """Reference relationship edge"""+ source: str = Field(description="Source entry/concept")+ target: str = Field(description="Referenced entry/concept")+ relationType: str = Field(description="Relationship type: Hyperlink, Reference, SeeAlso, Related, Other")+ context: str = Field(description="Reference context description", default="")+++_PROMPT = """You are a professional knowledge network analysis expert. Please extract concepts/entries and their mutual reference relationships from the text to build a cross-reference network.++### Node Extraction Rules+1. Extract all mentioned concepts or entries+2. Assign a type to each node: Entry, Section, Concept, Person, Location, Other+3. Add a brief description for each node++### Edge Extraction Rules+1. Only create edges from extracted nodes+2. Relationship types include:+ - Hyperlink: Hyperlink relationship+ - Reference: Reference relationship+ - SeeAlso: See also relationship+ - Related: Related relationship+ - Other: Other relationships+3. Record reference context description++### Constraints+- Each edge must connect extracted nodes+- Do not create nodes or relationships not mentioned in the text+- Maintain objectivity and accuracy, do not add information not in the text++### Source text:+"""++_NODE_PROMPT = """You are a professional entry recognition expert. Please extract all concepts or entries as nodes from the text.++### Extraction Rules+1. Extract all mentioned concepts or entries+2. Assign a type to each node: Entry, Section, Concept, Person, Location, Other+3. Add a brief description for each node++### Source text:+"""++_EDGE_PROMPT = """You are a professional reference relationship extraction expert. Please extract mutual reference relationships between nodes from the given node list.++### Relationship Type Explanation+- Hyperlink: Hyperlink relationship+- Reference: Reference relationship+- SeeAlso: See also relationship+- Related: Related relationship+- Other: Other relationships++### Constraints+1. Only extract edges from the known node list below+2. Do not create unlisted nodes++"""+++class CrossReferenceNet(AutoGraph[ReferenceNode, ReferenceRelation]):+ """+ Applicable documents: Wikipedia entries, knowledge base documents, web content with internal links++ Function introduction:+ Map hyperlinks or mutual reference relationships between concepts. Suitable for encyclopedia internal links, cross-entry association graphs, etc.++ Example:+ >>> template = CrossReferenceNet(llm_client=llm, embedder=embedder)+ >>> template.feed_text("Machine Learning is a branch of Artificial Intelligence...")+ >>> template.show()+ """++ def __init__(+ self,+ llm_client: BaseChatModel,+ embedder: Embeddings,+ *,+ extraction_mode: str = "two_stage",+ max_workers: int = 10,+ verbose: bool = False,+ **kwargs: Any,+ ):+ """+ Initialize cross-reference network 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+ """+ super().__init__(+ node_schema=ReferenceNode,+ edge_schema=ReferenceRelation,+ node_key_extractor=lambda x: x.name,+ edge_key_extractor=lambda x: f"{x.source}|{x.relationType}|{x.target}",+ nodes_in_edge_extractor=lambda x: (x.source, x.target),+ llm_client=llm_client,+ embedder=embedder,+ extraction_mode=extraction_mode,+ max_workers=max_workers,+ verbose=verbose,+ prompt=_PROMPT,+ prompt_for_node_extraction=_NODE_PROMPT,+ prompt_for_edge_extraction=_EDGE_PROMPT,+ **kwargs,+ )++ def show(+ self,+ *,+ top_k_for_search: int = 3,+ top_k_for_chat: int = 3,+ ):+ """+ Display cross-reference network.++ Args:+ top_k_for_search: Number of nodes/edges to return for semantic search, default: 3+ top_k_for_chat: Number of nodes/edges to use for chat, default: 3+ """+ def node_label_extractor(node: ReferenceNode) -> str:+ return f"{node.name} ({node.type})"++ def edge_label_extractor(edge: ReferenceRelation) -> str:+ return edge.relationType++ super().show(+ node_label_extractor=node_label_extractor,+ edge_label_extractor=edge_label_extractor,+ top_k_nodes_for_search=top_k_for_search,+ top_k_edges_for_search=top_k_for_search,+ top_k_nodes_for_chat=top_k_for_chat,+ top_k_edges_for_chat=top_k_for_chat,+ )diff --git a/hyperextract/templates/en/general/encyclopedia_item.py b/hyperextract/templates/en/general/encyclopedia_item.pynew file mode 100644index 0000000..ff684ee--- /dev/null+++ b/hyperextract/templates/en/general/encyclopedia_item.py@@ -0,0 +1,106 @@+"""Encyclopedia Item - Extract structured attribute information for a single subject (similar to an Infobox).++Suitable for Wikipedia, professional dictionary entries, etc.+"""++from typing import Any, Optional, List+from pydantic import BaseModel, Field+from langchain_core.language_models import BaseChatModel+from langchain_core.embeddings import Embeddings+from hyperextract.types import AutoModel+++class EncyclopediaInfo(BaseModel):+ """Encyclopedia item infobox"""+ title: str = Field(description="Entry title")+ entityType: str = Field(description="Entity type: Person, Location, Organization, Event, Concept, Item, Other")+ alternativeNames: List[str] = Field(description="Aliases, common names, foreign names", default_factory=list)+ category: List[str] = Field(description="Category tags", default_factory=list)+ summary: str = Field(description="Abstract/introduction", default="")+ attributes: List[str] = Field(description="Key attribute list, each element in format 'AttributeName: AttributeValue'", default_factory=list)+ keyEvents: List[str] = Field(description="Important events/milestones", default_factory=list)+ relatedConcepts: List[str] = Field(description="Related concepts/entries", default_factory=list)+++_PROMPT = """You are a professional encyclopedia editor. Please extract structured attribute information for a single subject from the text to build an encyclopedia item infobox.++### Extraction Rules+1. Identify the core subject of the text (Person, Location, Organization, Event, Concept, etc.)+2. Assign a type to the subject: Person, Location, Organization, Event, Concept, Item, Other+3. Extract all aliases, common names, or foreign names+4. Extract category tags (e.g., "Scientist", "Mathematics", "Ancient China")+5. Write a 100-200 word summary+6. Extract key attributes, each in format 'AttributeName: AttributeValue' (e.g., 'Date of Birth: 429', 'Nationality: China')+7. Extract important events or milestones+8. Extract related concepts or entries++### Constraints+- Only extract information explicitly mentioned in the text, do not add additional content+- Keep attribute names concise and clear+- Maintain objectivity and accuracy, consistent with encyclopedia style++### Source text:+"""+++class EncyclopediaItem(AutoModel[EncyclopediaInfo]):+ """+ Applicable documents: Wikipedia entries, encyclopedia entries, professional dictionary entries++ Function introduction:+ Extract structured attribute information for a single subject (similar to an Infobox). Suitable for Wikipedia, professional dictionary entries, etc.++ Example:+ >>> template = EncyclopediaItem(llm_client=llm, embedder=embedder)+ >>> template.feed_text("Zu Chongzhi (429-500), courtesy name Wenyuan, was from Qiuxian, Fanyang...")+ >>> print(template.data.title)+ >>> print(template.data.summary)+ """++ def __init__(+ self,+ llm_client: BaseChatModel,+ embedder: Embeddings,+ *,+ max_workers: int = 10,+ verbose: bool = False,+ **kwargs: Any,+ ):+ """+ Initialize encyclopedia item template.++ Args:+ llm_client: LLM client for knowledge extraction+ embedder: Embedding model for semantic search+ 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+ """+ super().__init__(+ data_schema=EncyclopediaInfo,+ llm_client=llm_client,+ embedder=embedder,+ prompt=_PROMPT,+ max_workers=max_workers,+ verbose=verbose,+ **kwargs,+ )++ def show(+ self,+ *,+ top_k: int = 3,+ ):+ """+ Display encyclopedia item.++ Args:+ top_k: Number of items to use for chat, default: 3+ """+ def label_extractor(item: EncyclopediaInfo) -> str:+ return f"{item.title} ({item.entityType})"++ super().show(+ label_extractor=label_extractor,+ top_k=top_k,+ )diff --git a/hyperextract/templates/en/general/entity_registry.py b/hyperextract/templates/en/general/entity_registry.pynew file mode 100644index 0000000..3efad83--- /dev/null+++ b/hyperextract/templates/en/general/entity_registry.py@@ -0,0 +1,100 @@+"""Entity Registry - Deduplicate and summarize all unique entities appearing in the text.++Suitable for entity discovery, named entity recognition, etc.+"""++from typing import Any+from pydantic import BaseModel, Field+from langchain_core.language_models import BaseChatModel+from langchain_core.embeddings import Embeddings+from hyperextract.types import AutoSet+++class RegistryEntry(BaseModel):+ """Registry entry"""+ name: str = Field(description="Entity name, consistent with original text")+ category: str = Field(description="Entity type: Person, Organization, Location, Product, Concept, Other")+ description: str = Field(description="Brief description", default="")+++_PROMPT = """You are a professional entity recognition expert. Please extract all unique entities from the text to form an entity registry.++### 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++### Constraints+- Deduplication: Only keep one record for the same entity+- Maintain objectivity and accuracy, do not add information not in the text+- Entity name case should be consistent with the original text++### Source text:+"""+++class EntityRegistry(AutoSet[RegistryEntry]):+ """+ Applicable documents: Arbitrary text, web scraped content, news reports++ Function introduction:+ Deduplicate and summarize all unique entities (e.g., person names, organization names) appearing in the text to form an entity registry. Supports entity discovery and named entity recognition.++ Example:+ >>> template = EntityRegistry(llm_client=llm, embedder=embedder)+ >>> template.feed_text("Galaxy Interstellar announces successful maiden flight of Shenzhou-50...")+ >>> print(list(template))+ """++ def __init__(+ self,+ llm_client: BaseChatModel,+ embedder: Embeddings,+ *,+ max_workers: int = 10,+ verbose: bool = False,+ **kwargs: Any,+ ):+ """+ Initialize entity registry template.++ Args:+ llm_client: LLM client for knowledge extraction+ embedder: Embedding model for semantic search+ 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+ """+ super().__init__(+ item_schema=RegistryEntry,+ key_extractor=lambda x: x.name,+ llm_client=llm_client,+ embedder=embedder,+ prompt=_PROMPT,+ max_workers=max_workers,+ verbose=verbose,+ **kwargs,+ )++ def show(+ self,+ *,+ top_k_for_search: int = 3,+ top_k_for_chat: int = 3,+ ):+ """+ Display entity registry.++ Args:+ top_k_for_search: Number of items to return for semantic search, default: 3+ top_k_for_chat: Number of items to use for chat, default: 3+ """+ def itemLabelExtractor(item: RegistryEntry) -> str:+ return f"{item.name} ({item.category})"++ super().show(+ item_label_extractor=itemLabelExtractor,+ top_k_for_search=top_k_for_search,+ top_k_for_chat=top_k_for_chat,+ )diff --git a/hyperextract/templates/en/general/keyword_list.py b/hyperextract/templates/en/general/keyword_list.pynew file mode 100644index 0000000..71f712f--- /dev/null+++ b/hyperextract/templates/en/general/keyword_list.py@@ -0,0 +1,98 @@+"""Keyword List - Extract keywords from text with category and description information.++Suitable for content tag generation, topic analysis, etc.+"""++from typing import Any+from pydantic import BaseModel, Field+from langchain_core.language_models import BaseChatModel+from langchain_core.embeddings import Embeddings+from hyperextract.types import AutoList+++class Keyword(BaseModel):+ """Keyword entry"""+ term: str = Field(description="Keyword")+ category: str = Field(description="Category: CoreConcept, Term, PersonName, OrganizationName, Other")+ description: str = Field(description="Brief description or context explanation of the keyword", default="")+++_PROMPT = """You are a professional keyword extraction expert. Please extract all keywords from the text with category and description information.++### Extraction Rules+1. Extract all keywords and core phrases from the text+2. Assign a category to each keyword: CoreConcept, Term, PersonName, OrganizationName, Other+3. Add a brief description for each keyword, explaining its meaning or context in the text++### Constraints+- Ensure keyword accuracy, do not add information not in the text+- Keep keyword case consistent with the original text++### Source text:+"""+++class KeywordList(AutoList[Keyword]):+ """+ Applicable documents: Arbitrary text, blog articles, academic papers++ Function introduction:+ Extract keywords from text with category and description information. Suitable for content tag generation and topic analysis.++ Example:+ >>> template = KeywordList(llm_client=llm, embedder=embedder)+ >>> template.feed_text("Galaxy Interstellar announces successful maiden flight of Shenzhou-50...")+ >>> for keyword in template:+ ... print(f"{keyword.term} ({keyword.category}): {keyword.description}")+ """++ def __init__(+ self,+ llm_client: BaseChatModel,+ embedder: Embeddings,+ *,+ max_workers: int = 10,+ verbose: bool = False,+ **kwargs: Any,+ ):+ """+ Initialize keyword list template.++ Args:+ llm_client: LLM client for knowledge extraction+ embedder: Embedding model for semantic search+ 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+ """+ super().__init__(+ item_schema=Keyword,+ llm_client=llm_client,+ embedder=embedder,+ prompt=_PROMPT,+ max_workers=max_workers,+ verbose=verbose,+ **kwargs,+ )++ def show(+ self,+ *,+ top_k_for_search: int = 3,+ top_k_for_chat: int = 3,+ ):+ """+ Display keyword list.++ Args:+ top_k_for_search: Number of keywords to return for semantic search, default: 3+ top_k_for_chat: Number of keywords to use for chat, default: 3+ """+ def itemLabelExtractor(item: Keyword) -> str:+ return f"{item.term} ({item.category})"++ super().show(+ item_label_extractor=itemLabelExtractor,+ top_k_for_search=top_k_for_search,+ top_k_for_chat=top_k_for_chat,+ )diff --git a/hyperextract/templates/en/general/knowledge_graph.py b/hyperextract/templates/en/general/knowledge_graph.pynew file mode 100644index 0000000..b4ea04d--- /dev/null+++ b/hyperextract/templates/en/general/knowledge_graph.py@@ -0,0 +1,147 @@+"""General Knowledge Graph - Extract entities and relationships from any text.++Suitable for unstructured documents like arbitrary text, web scraped content, etc., to extract general entities and their relationships.+"""++from typing import Any, Optional, Tuple+from pydantic import BaseModel, Field+from langchain_core.language_models import BaseChatModel+from langchain_core.embeddings import Embeddings+from hyperextract.types import AutoGraph+++class GeneralEntity(BaseModel):+ """General entity node"""+ name: str = Field(description="Entity name, e.g., person name, organization name, product name")+ category: str = Field(description="Entity type: Person, Organization, Location, Product, Concept, Other")+ description: str = Field(description="Brief description", default="")+++class GeneralRelation(BaseModel):+ """General relationship edge"""+ source: str = Field(description="Source entity")+ target: str = Field(description="Target entity")+ relationType: str = Field(description="Relationship type: BelongsTo, LocatedIn, CollaboratesWith, CompetesWith, InventedBy, CreatedBy, RelatedTo, etc.")+ details: str = Field(description="Detailed description", default="")+++_PROMPT = """You are a professional knowledge graph extraction expert. Please extract all entities (nodes) and their relationships (edges) from the text.++### 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++### 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++### Source text:+"""++_NODE_PROMPT = """You are a professional entity recognition expert. Please extract all key entities as nodes from the text.++### 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++### Source text:+"""++_EDGE_PROMPT = """You are a professional relationship extraction expert. Please extract relationships between entities from the given entity list.++### Constraints+1. Only extract edges from the known entity list below+2. Do not create unlisted entities+3. Relationship types include: BelongsTo, LocatedIn, CollaboratesWith, CompetesWith, InventedBy, CreatedBy, RelatedTo, etc.++"""+++class KnowledgeGraph(AutoGraph[GeneralEntity, GeneralRelation]):+ """+ Applicable documents: Arbitrary text, web scraped content, blog articles, news reports++ Function introduction:+ Extract general entities and their relationships from any text to build a knowledge graph. Supports multiple entity types including Person, Organization, Location, Product, Concept, etc.++ Example:+ >>> template = KnowledgeGraph(llm_client=llm, embedder=embedder)+ >>> template.feed_text("Galaxy Interstellar announces successful maiden flight of Shenzhou-50...")+ >>> template.show()+ """++ def __init__(+ self,+ llm_client: BaseChatModel,+ embedder: Embeddings,+ *,+ extraction_mode: str = "two_stage",+ max_workers: int = 10,+ verbose: bool = False,+ **kwargs: Any,+ ):+ """+ Initialize 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+ """+ super().__init__(+ node_schema=GeneralEntity,+ edge_schema=GeneralRelation,+ node_key_extractor=lambda x: x.name,+ edge_key_extractor=lambda x: f"{x.source}|{x.relationType}|{x.target}",+ nodes_in_edge_extractor=lambda x: (x.source, x.target),+ llm_client=llm_client,+ embedder=embedder,+ extraction_mode=extraction_mode,+ max_workers=max_workers,+ verbose=verbose,+ prompt=_PROMPT,+ prompt_for_node_extraction=_NODE_PROMPT,+ prompt_for_edge_extraction=_EDGE_PROMPT,+ **kwargs,+ )++ def show(+ self,+ *,+ top_k_for_search: int = 3,+ top_k_for_chat: int = 3,+ ):+ """+ Display knowledge graph.++ Args:+ top_k_for_search: Number of nodes/edges to return for semantic search, default: 3+ top_k_for_chat: Number of nodes/edges to use for chat, default: 3+ """+ def node_label_extractor(node: GeneralEntity) -> str:+ return f"{node.name} ({node.category})"++ def edge_label_extractor(edge: GeneralRelation) -> str:+ return edge.relationType++ super().show(+ node_label_extractor=node_label_extractor,+ edge_label_extractor=edge_label_extractor,+ top_k_nodes_for_search=top_k_for_search,+ top_k_edges_for_search=top_k_for_search,+ top_k_nodes_for_chat=top_k_for_chat,+ top_k_edges_for_chat=top_k_for_chat,+ )diff --git a/hyperextract/templates/en/general/life_event_timeline.py b/hyperextract/templates/en/general/life_event_timeline.pynew file mode 100644index 0000000..04729dc--- /dev/null+++ b/hyperextract/templates/en/general/life_event_timeline.py@@ -0,0 +1,230 @@+"""Life Event Timeline - Extract timestamped events and arrange them chronologically.++Suitable for biographies, chronologies, memoirs, etc.+"""++from typing import Any, Optional+from pydantic import BaseModel, Field+from langchain_core.language_models import BaseChatModel+from langchain_core.embeddings import Embeddings+from hyperextract.types import AutoTemporalGraph+++class LifeEntity(BaseModel):+ """Life entity node"""+ name: str = Field(description="Entity name")+ type: str = Field(description="Entity type: Person, Location, Organization, Item, Concept, Other")+ description: str = Field(description="Brief description", default="")+++class LifeEvent(BaseModel):+ """Life event edge (with optional timestamp)"""+ source: str = Field(description="Event subject/related entity")+ target: str = Field(description="Event object/related entity")+ eventType: str = Field(description="Event type: Birth, Death, Education, Work, Achievement, Interaction, Travel, Other")+ eventDate: Optional[str] = Field(description="Event date, format: YYYY-MM-DD or YYYY, optional", default=None)+ details: str = Field(description="Detailed event description", default="")+++_PROMPT = """You are a professional biography chronology expert. Please extract entities such as people, locations, organizations, etc., and life events from the text to build a life event timeline.++### Node Extraction Rules+1. Extract all related entities: Person, Location, Organization, Item, Concept, etc.+2. Assign a type to each entity: Person, Location, Organization, Item, Concept, Other+3. Add a brief description for each entity++### Event Extraction Rules+1. Only create event edges from extracted entities+2. Event types include:+ - Birth: Birth events+ - Death: Death events+ - Education: Learning, education events+ - Work: Work, career events+ - Achievement: Achievement, honor events+ - Interaction: Interpersonal interaction, social activity events+ - Travel: Travel, relocation events+ - Other: Other events+3. Event date is optional, only extract when explicitly mentioned in the text+4. Date format: YYYY-MM-DD (e.g., 429-01-01) or year only (e.g., 429)+5. Each edge must connect extracted nodes++### English Time Calculation Rules+Current observation date: {observation_time}++1. **Relative Time Parsing**: Must parse relative time expressions based on observation date+ - "last year" → Calculate as {observation_time} minus one year+ - "this year" → The year of {observation_time}+ - "next year" → {observation_time} plus one year+ - "yesterday" → {observation_time} minus one day+ - "today" → {observation_time}+ - "tomorrow" → {observation_time} plus one day+ - "last month" → First day of the month before {observation_time}+ - "this month" → First day of the month of {observation_time}+ - "next month" → First day of the month after {observation_time}+ - "ten years ago" → {observation_time} minus ten years+ - "five years later" → {observation_time} plus five years+ - "beginning of year" → January 1st of the current year+ - "end of year" → December 31st of the current year+ - "beginning of century" → The 1st year of the century (e.g., 2000)+ - "end of century" → The last year of the century (e.g., 2099)++2. **Explicit Dates**: Keep explicit date formats from the original text unchanged+ - Year only (e.g., 429, 2023) → 429, 2023+ - Year-month (e.g., March 429) → 429-03+ - Year-month-day (e.g., March 14, 429) → 429-03-14++3. **Missing Time**: If there is no time information in the text, leave eventDate blank (None), do not fabricate dates++### Constraints+- Do not create entities or events not mentioned in the text+- Maintain objectivity and accuracy, do not add information not in the text++### Source text:+"""++_NODE_PROMPT = """You are a professional entity recognition expert. Please extract all related entities as nodes from the text.++### Extraction Rules+1. Extract all related entities: Person, Location, Organization, Item, Concept, etc.+2. Assign a type to each entity: Person, Location, Organization, Item, Concept, Other+3. Add a brief description for each entity++### Source text:+"""++_EDGE_PROMPT = """You are a professional life event extraction expert. Please extract life events from the given entity list.++### Event Type Explanation+- Birth: Birth events+- Death: Death events+- Education: Learning, education events+- Work: Work, career events+- Achievement: Achievement, honor events+- Interaction: Interpersonal interaction, social activity events+- Travel: Travel, relocation events+- Other: Other events++### English Time Calculation Rules+Current observation date: {observation_time}++1. **Relative Time Parsing**: Must parse relative time expressions based on observation date+ - "last year" → Calculate as {observation_time} minus one year+ - "this year" → The year of {observation_time}+ - "next year" → {observation_time} plus one year+ - "yesterday" → {observation_time} minus one day+ - "today" → {observation_time}+ - "tomorrow" → {observation_time} plus one day+ - "last month" → First day of the month before {observation_time}+ - "this month" → First day of the month of {observation_time}+ - "next month" → First day of the month after {observation_time}+ - "ten years ago" → {observation_time} minus ten years+ - "five years later" → {observation_time} plus five years+ - "beginning of year" → January 1st of the current year+ - "end of year" → December 31st of the current year+ - "beginning of century" → The 1st year of the century (e.g., 2000)+ - "end of century" → The last year of the century (e.g., 2099)++2. **Explicit Dates**: Keep explicit date formats from the original text unchanged+ - Year only (e.g., 429, 2023) → 429, 2023+ - Year-month (e.g., March 429) → 429-03+ - Year-month-day (e.g., March 14, 429) → 429-03-14++3. **Missing Time**: If there is no time information in the text, leave eventDate blank (None), do not fabricate dates++### Date Format Requirements+- All date information should be converted to "YYYY-MM-DD" format (e.g., 429-01-01) or year only (e.g., 429)+- Time information is optional, only extract when explicitly mentioned in the text++### Constraints+1. Only extract events from the known entity list below+2. Do not create unlisted entities+3. Time information is optional, only extract when explicitly mentioned in the text++"""+++class LifeEventTimeline(AutoTemporalGraph[LifeEntity, LifeEvent]):+ """+ Applicable documents: Biographies, chronologies, memoirs, autobiographies++ Function introduction:+ Extract timestamped events and arrange them chronologically. Suitable for biographies, chronologies, memoirs, etc.++ Example:+ >>> template = LifeEventTimeline(llm_client=llm, embedder=embedder)+ >>> template.feed_text("Zu Chongzhi (429-500), courtesy name Wenyuan, was from Qiuxian, Fanyang...")+ >>> template.show()+ """++ def __init__(+ self,+ llm_client: BaseChatModel,+ embedder: Embeddings,+ *,+ observation_time: str | None = None,+ extraction_mode: str = "two_stage",+ max_workers: int = 10,+ verbose: bool = False,+ **kwargs: Any,+ ):+ """+ Initialize life event timeline template.++ Args:+ llm_client: LLM client for knowledge extraction+ embedder: Embedding model for semantic search+ observation_time: Observation date, used for parsing relative time expressions, default: today+ 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+ """+ super().__init__(+ node_schema=LifeEntity,+ edge_schema=LifeEvent,+ node_key_extractor=lambda x: x.name,+ edge_key_extractor=lambda x: f"{x.source}|{x.eventType}|{x.target}",+ time_in_edge_extractor=lambda x: x.eventDate,+ nodes_in_edge_extractor=lambda x: (x.source, x.target),+ llm_client=llm_client,+ embedder=embedder,+ observation_time=observation_time,+ extraction_mode=extraction_mode,+ max_workers=max_workers,+ verbose=verbose,+ prompt=_PROMPT,+ prompt_for_node_extraction=_NODE_PROMPT,+ prompt_for_edge_extraction=_EDGE_PROMPT,+ **kwargs,+ )++ def show(+ self,+ *,+ top_k_for_search: int = 3,+ top_k_for_chat: int = 3,+ ):+ """+ Display life event timeline.++ Args:+ top_k_for_search: Number of nodes/edges to return for semantic search, default: 3+ top_k_for_chat: Number of nodes/edges to use for chat, default: 3+ """+ def node_label_extractor(node: LifeEntity) -> str:+ return f"{node.name} ({node.type})"++ def edge_label_extractor(edge: LifeEvent) -> str:+ if edge.eventDate:+ return f"{edge.eventType} ({edge.eventDate})"+ return edge.eventType++ super().show(+ node_label_extractor=node_label_extractor,+ edge_label_extractor=edge_label_extractor,+ top_k_nodes_for_search=top_k_for_search,+ top_k_edges_for_search=top_k_for_search,+ top_k_nodes_for_chat=top_k_for_chat,+ top_k_edges_for_chat=top_k_for_chat,+ )diff --git a/hyperextract/templates/en/general/operational_procedure.py b/hyperextract/templates/en/general/operational_procedure.pynew file mode 100644index 0000000..d88f004--- /dev/null+++ b/hyperextract/templates/en/general/operational_procedure.py@@ -0,0 +1,156 @@+"""Operational Procedure - Extract the specific sequential steps for declaration, approval, or operations specified in regulations.++Suitable for operation guidance, SOP process visualization, etc.+"""++from typing import Any+from pydantic import BaseModel, Field+from langchain_core.language_models import BaseChatModel+from langchain_core.embeddings import Embeddings+from hyperextract.types import AutoGraph+++class ProcedureStep(BaseModel):+ """Procedure step node"""+ stepId: str = Field(description="Step ID, e.g., Step 1, Step 2")+ name: str = Field(description="Step name")+ description: str = Field(description="Detailed step description")+ role: str = Field(description="Executor role", default="")+ inputRequired: str = Field(description="Required inputs/materials", default="")+ outputResult: str = Field(description="Output/result", default="")+++class ProcedureTransition(BaseModel):+ """Procedure transition edge"""+ source: str = Field(description="Source step ID")+ target: str = Field(description="Target step ID")+ condition: str = Field(description="Transition condition", default="")+ details: str = Field(description="Detailed explanation", default="")+++_PROMPT = """You are a professional process analysis expert. Please extract the specific sequential steps for declaration, approval, or operations specified in regulations from the text to build an operational procedure flowchart.++### Node Extraction Rules+1. Extract all process steps+2. Assign an ID to each step (e.g., Step 1, Step 2)+3. Add a name and detailed description to each step+4. Record the executor role (if available)+5. Record required inputs or materials (if available)+6. Record output or result (if available)++### Edge Extraction Rules+1. Only create transition edges from extracted steps+2. Record the sequential relationship between steps+3. Record transition conditions (if available)+4. Record detailed explanations (if available)++### Constraints+- Ensure the sequential order of process steps is accurate+- Maintain objectivity and accuracy, do not add information not in the text++### Source text:+"""++_NODE_PROMPT = """You are a professional process step recognition expert. Please extract all process steps as nodes from the text.++### Extraction Rules+1. Extract all process steps+2. Assign an ID to each step (e.g., Step 1, Step 2)+3. Add a name and detailed description to each step+4. Record the executor role (if available)+5. Record required inputs or materials (if available)+6. Record output or result (if available)++### Source text:+"""++_EDGE_PROMPT = """You are a professional process transition recognition expert. Please extract the sequential relationship between steps from the given step list.++### Constraints+1. Only extract edges from the known step list below+2. Do not create unlisted steps++"""+++class OperationalProcedure(AutoGraph[ProcedureStep, ProcedureTransition]):+ """+ Applicable documents: Operation manuals, SOPs, compliance guidelines, approval processes++ Function introduction:+ Extract the specific sequential steps for declaration, approval, or operations specified in regulations. Suitable for operation guidance, SOP process visualization, etc.++ Example:+ >>> template = OperationalProcedure(llm_client=llm, embedder=embedder)+ >>> template.feed_text("Universe First Slacker Company Leave Approval Process...")+ >>> template.show()+ """++ def __init__(+ self,+ llm_client: BaseChatModel,+ embedder: Embeddings,+ *,+ extraction_mode: str = "two_stage",+ max_workers: int = 10,+ verbose: bool = False,+ **kwargs: Any,+ ):+ """+ Initialize operational procedure 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+ """+ super().__init__(+ node_schema=ProcedureStep,+ edge_schema=ProcedureTransition,+ node_key_extractor=lambda x: x.stepId,+ edge_key_extractor=lambda x: f"{x.source}|{x.target}",+ nodes_in_edge_extractor=lambda x: (x.source, x.target),+ llm_client=llm_client,+ embedder=embedder,+ extraction_mode=extraction_mode,+ max_workers=max_workers,+ verbose=verbose,+ prompt=_PROMPT,+ prompt_for_node_extraction=_NODE_PROMPT,+ prompt_for_edge_extraction=_EDGE_PROMPT,+ **kwargs,+ )++ def show(+ self,+ *,+ top_k_for_search: int = 3,+ top_k_for_chat: int = 3,+ ):+ """+ Display operational procedure flowchart.++ Args:+ top_k_for_search: Number of nodes/edges to return for semantic search, default: 3+ top_k_for_chat: Number of nodes/edges to use for chat, default: 3+ """+ def node_label_extractor(node: ProcedureStep) -> str:+ return f"{node.stepId}: {node.name}"++ def edge_label_extractor(edge: ProcedureTransition) -> str:+ if edge.condition:+ return edge.condition+ return "→"++ super().show(+ node_label_extractor=node_label_extractor,+ edge_label_extractor=edge_label_extractor,+ top_k_nodes_for_search=top_k_for_search,+ top_k_edges_for_search=top_k_for_search,+ top_k_nodes_for_chat=top_k_for_chat,+ top_k_edges_for_chat=top_k_for_chat,+ )diff --git a/hyperextract/templates/en/general/penalty_mapping.py b/hyperextract/templates/en/general/penalty_mapping.pynew file mode 100644index 0000000..a641b86--- /dev/null+++ b/hyperextract/templates/en/general/penalty_mapping.py@@ -0,0 +1,145 @@+"""Penalty Mapping - Describe the evolution path from "violation behavior" to "handling procedure" to "final result".++Suitable for risk tracing, penalty logic analysis, etc.+"""++from typing import Any+from pydantic import BaseModel, Field+from langchain_core.language_models import BaseChatModel+from langchain_core.embeddings import Embeddings+from hyperextract.types import AutoGraph+++class PenaltyNode(BaseModel):+ """Penalty causal node"""+ name: str = Field(description="Node name")+ type: str = Field(description="Node type: Violation, HandlingProcedure, PenaltyResult, Other")+ description: str = Field(description="Detailed description")+++class PenaltyPath(BaseModel):+ """Penalty causal path edge"""+ source: str = Field(description="Source node name")+ target: str = Field(description="Target node name")+ relation: str = Field(description="Relationship type: Causes, Triggers, Executes, Produces, Other")+ details: str = Field(description="Detailed explanation", default="")+++_PROMPT = """You are a professional penalty logic analysis expert. Please extract the evolution path from "violation behavior" to "handling procedure" to "final result" from the text to build a penalty mapping.++### Node Extraction Rules+1. Extract all key nodes: Violation, HandlingProcedure, PenaltyResult, etc.+2. Assign a type to each node: Violation, HandlingProcedure, PenaltyResult, Other+3. Add a detailed description to each node++### Edge Extraction Rules+1. Only create causal path edges from extracted nodes+2. Record the causal relationship between nodes+3. Relationship types include: Causes, Triggers, Executes, Produces, Other+4. Add detailed explanations (if available)++### Constraints+- Ensure the logical order of causal relationships is correct+- Maintain objectivity and accuracy, do not add information not in the text++### Source text:+"""++_NODE_PROMPT = """You are a professional penalty node recognition expert. Please extract all key nodes from the text.++### Extraction Rules+1. Extract all key nodes: Violation, HandlingProcedure, PenaltyResult, etc.+2. Assign a type to each node: Violation, HandlingProcedure, PenaltyResult, Other+3. Add a detailed description to each node++### Source text:+"""++_EDGE_PROMPT = """You are a professional penalty causal relationship recognition expert. Please extract the causal relationship between nodes from the given node list.++### Constraints+1. Only extract edges from the known node list below+2. Do not create unlisted nodes++"""+++class PenaltyMapping(AutoGraph[PenaltyNode, PenaltyPath]):+ """+ Applicable documents: Company internal management systems, administrative regulations, compliance guidelines, penalty cases++ Function introduction:+ Describe the evolution path from "violation behavior" to "handling procedure" to "final result". Suitable for risk tracing, penalty logic analysis, etc.++ Example:+ >>> template = PenaltyMapping(llm_client=llm, embedder=embedder)+ >>> template.feed_text("Universe First Slacker Company Employee Penalty Regulations...")+ >>> template.show()+ """++ def __init__(+ self,+ llm_client: BaseChatModel,+ embedder: Embeddings,+ *,+ extraction_mode: str = "two_stage",+ max_workers: int = 10,+ verbose: bool = False,+ **kwargs: Any,+ ):+ """+ Initialize penalty mapping 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+ """+ super().__init__(+ node_schema=PenaltyNode,+ edge_schema=PenaltyPath,+ node_key_extractor=lambda x: x.name,+ edge_key_extractor=lambda x: f"{x.source}|{x.relation}|{x.target}",+ nodes_in_edge_extractor=lambda x: (x.source, x.target),+ llm_client=llm_client,+ embedder=embedder,+ extraction_mode=extraction_mode,+ max_workers=max_workers,+ verbose=verbose,+ prompt=_PROMPT,+ prompt_for_node_extraction=_NODE_PROMPT,+ prompt_for_edge_extraction=_EDGE_PROMPT,+ **kwargs,+ )++ def show(+ self,+ *,+ top_k_for_search: int = 3,+ top_k_for_chat: int = 3,+ ):+ """+ Display penalty mapping.++ Args:+ top_k_for_search: Number of nodes/edges to return for semantic search, default: 3+ top_k_for_chat: Number of nodes/edges to use for chat, default: 3+ """+ def node_label_extractor(node: PenaltyNode) -> str:+ return f"{node.name} ({node.type})"++ def edge_label_extractor(edge: PenaltyPath) -> str:+ return edge.relation++ super().show(+ node_label_extractor=node_label_extractor,+ edge_label_extractor=edge_label_extractor,+ top_k_nodes_for_search=top_k_for_search,+ top_k_edges_for_search=top_k_for_search,+ top_k_nodes_for_chat=top_k_for_chat,+ top_k_edges_for_chat=top_k_for_chat,+ )diff --git a/hyperextract/templates/en/general/penalty_registry.py b/hyperextract/templates/en/general/penalty_registry.pynew file mode 100644index 0000000..1fb73f4--- /dev/null+++ b/hyperextract/templates/en/general/penalty_registry.py@@ -0,0 +1,103 @@+"""Penalty Registry - Deduplicate and summarize specific violations and their corresponding consequences to form a clear penalty ledger.++Suitable for internal risk control, compliance checks, etc.+"""++from typing import Any+from pydantic import BaseModel, Field+from langchain_core.language_models import BaseChatModel+from langchain_core.embeddings import Embeddings+from hyperextract.types import AutoSet+++class PenaltyEntry(BaseModel):+ """Violation penalty entry"""+ violation: str = Field(description="Violation description")+ severity: str = Field(description="Severity: Minor, Moderate, Serious, VerySerious")+ penalty: str = Field(description="Penalty measure")+ applicableClause: str = Field(description="Applicable clause", default="")+ notes: str = Field(description="Notes", default="")+++_PROMPT = """You are a professional compliance analysis expert. Please extract all violations and their corresponding penalty measures from the text to form a violation penalty registry.++### Extraction Rules+1. Extract all violations and their corresponding penalty measures+2. Assign a severity level to each violation: Minor, Moderate, Serious, VerySerious+3. Maintain the accuracy of violations and penalty measures+4. Record applicable clauses (if available)+5. Add notes (if available)++### Constraints+- Deduplication: Only keep one record for the same violation and penalty measure combination+- Maintain objectivity and accuracy, do not add information not in the text++### Source text:+"""+++class PenaltyRegistry(AutoSet[PenaltyEntry]):+ """+ Applicable documents: Company internal management systems, administrative regulations, compliance guidelines++ Function introduction:+ Deduplicate and summarize specific violations and their corresponding consequences to form a clear penalty ledger. Suitable for internal risk control, compliance checks, etc.++ Example:+ >>> template = PenaltyRegistry(llm_client=llm, embedder=embedder)+ >>> template.feed_text("Universe First Slacker Company Employee Attendance Management System...")+ >>> for entry in template:+ ... print(f"{entry.violation} -> {entry.penalty}")+ """++ def __init__(+ self,+ llm_client: BaseChatModel,+ embedder: Embeddings,+ *,+ max_workers: int = 10,+ verbose: bool = False,+ **kwargs: Any,+ ):+ """+ Initialize penalty registry template.++ Args:+ llm_client: LLM client for knowledge extraction+ embedder: Embedding model for semantic search+ 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+ """+ super().__init__(+ item_schema=PenaltyEntry,+ key_extractor=lambda x: f"{x.violation}_{x.penalty}",+ llm_client=llm_client,+ embedder=embedder,+ prompt=_PROMPT,+ max_workers=max_workers,+ verbose=verbose,+ **kwargs,+ )++ def show(+ self,+ *,+ top_k_for_search: int = 3,+ top_k_for_chat: int = 3,+ ):+ """+ Display penalty registry.++ Args:+ top_k_for_search: Number of entries to return for semantic search, default: 3+ top_k_for_chat: Number of entries to use for chat, default: 3+ """+ def itemLabelExtractor(item: PenaltyEntry) -> str:+ return f"{item.violation} ({item.severity})"++ super().show(+ item_label_extractor=itemLabelExtractor,+ top_k_for_search=top_k_for_search,+ top_k_for_chat=top_k_for_chat,+ )diff --git a/hyperextract/templates/en/general/personal_profile.py b/hyperextract/templates/en/general/personal_profile.pynew file mode 100644index 0000000..417bcd0--- /dev/null+++ b/hyperextract/templates/en/general/personal_profile.py@@ -0,0 +1,114 @@+"""Personal Profile - Aggregate static attributes of a person (birth/death dates, education, core identity).++Suitable for resumes, person introductions, obituary summaries, etc.+"""++from typing import Any, Optional, List+from pydantic import BaseModel, Field+from langchain_core.language_models import BaseChatModel+from langchain_core.embeddings import Embeddings+from hyperextract.types import AutoModel+++class PersonalInfo(BaseModel):+ """Personal profile information"""+ name: str = Field(description="Name")+ gender: Optional[str] = Field(description="Gender", default=None)+ birthDate: Optional[str] = Field(description="Date of birth, format: YYYY-MM-DD or YYYY", default=None)+ deathDate: Optional[str] = Field(description="Date of death, format: YYYY-MM-DD or YYYY", default=None)+ nationality: Optional[str] = Field(description="Nationality", default=None)+ birthPlace: Optional[str] = Field(description="Place of birth", default=None)+ education: List[str] = Field(description="Education background", default_factory=list)+ occupations: List[str] = Field(description="Occupations/identities", default_factory=list)+ coreIdentity: List[str] = Field(description="Core identity/tags", default_factory=list)+ majorAchievements: List[str] = Field(description="Major achievements", default_factory=list)+ affiliations: List[str] = Field(description="Affiliated organizations/institutions", default_factory=list)+ summary: str = Field(description="Personal introduction", default="")+++_PROMPT = """You are a professional personal profile editor. Please extract static attribute information of this person from the text to build a personal profile.++### Extraction Rules+1. Extract basic information: name, gender, date of birth, date of death, nationality, place of birth, etc.+2. Extract education background+3. Extract occupation or identity information+4. Extract core identity tags+5. Extract major achievements+6. Extract affiliated organizations or institutions+7. Write a 100-200 word personal introduction++### Date Format Requirements+All date information should be converted to "YYYY-MM-DD" format (e.g., 429-01-01) or year only (e.g., 429).++### Constraints+- Only extract information explicitly mentioned in the text, do not add additional content+- Maintain objectivity and accuracy+- Leave blank if information is missing, do not fabricate++### Source text:+"""+++class PersonalProfile(AutoModel[PersonalInfo]):+ """+ Applicable documents: Resumes, person introductions, obituary summaries, Wikipedia person entries++ Function introduction:+ Aggregate static attributes of a person (birth/death dates, education, core identity). Suitable for resumes, person introductions, obituary summaries, etc.++ Example:+ >>> template = PersonalProfile(llm_client=llm, embedder=embedder)+ >>> template.feed_text("Zu Chongzhi (429-500), courtesy name Wenyuan, was from Qiuxian, Fanyang...")+ >>> print(template.data.name)+ >>> print(template.data.birthDate)+ """++ def __init__(+ self,+ llm_client: BaseChatModel,+ embedder: Embeddings,+ *,+ max_workers: int = 10,+ verbose: bool = False,+ **kwargs: Any,+ ):+ """+ Initialize personal profile template.++ Args:+ llm_client: LLM client for knowledge extraction+ embedder: Embedding model for semantic search+ 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+ """+ super().__init__(+ data_schema=PersonalInfo,+ llm_client=llm_client,+ embedder=embedder,+ prompt=_PROMPT,+ max_workers=max_workers,+ verbose=verbose,+ **kwargs,+ )++ def show(+ self,+ *,+ top_k: int = 3,+ ):+ """+ Display personal profile.++ Args:+ top_k: Number of items to use for chat, default: 3+ """+ def label_extractor(item: PersonalInfo) -> str:+ if item.birthDate and item.deathDate:+ return f"{item.name} ({item.birthDate}-{item.deathDate})"+ return item.name++ super().show(+ label_extractor=label_extractor,+ top_k=top_k,+ )diff --git a/hyperextract/templates/en/general/regulation_profile.py b/hyperextract/templates/en/general/regulation_profile.pynew file mode 100644index 0000000..487d89c--- /dev/null+++ b/hyperextract/templates/en/general/regulation_profile.py@@ -0,0 +1,112 @@+"""Regulation Profile - Extract the name, version, scope, effective date, and core purpose of a regulation.++Suitable for regulation overview, version management, etc.+"""++from typing import Any, Optional, List+from pydantic import BaseModel, Field+from langchain_core.language_models import BaseChatModel+from langchain_core.embeddings import Embeddings+from hyperextract.types import AutoModel+++class RegulationInfo(BaseModel):+ """Regulation metadata information"""+ title: str = Field(description="Regulation title")+ version: Optional[str] = Field(description="Version number", default=None)+ issuer: Optional[str] = Field(description="Issuing authority", default=None)+ issueDate: Optional[str] = Field(description="Issue date", default=None)+ effectiveDate: Optional[str] = Field(description="Effective date", default=None)+ expiryDate: Optional[str] = Field(description="Expiry date", default=None)+ scope: List[str] = Field(description="Applicable scope", default_factory=list)+ targetAudience: List[str] = Field(description="Target audience", default_factory=list)+ corePurpose: str = Field(description="Core purpose/objective", default="")+ keywords: List[str] = Field(description="Keywords/tags", default_factory=list)+ relatedRegulations: List[str] = Field(description="Related regulations", default_factory=list)+++_PROMPT = """You are a professional regulation analysis expert. Please extract metadata information of this regulation from the text to build a regulation profile.++### Extraction Rules+1. Extract the full name of the regulation+2. Extract version number (if available)+3. Extract issuing authority+4. Extract issue date, effective date, expiry date (if available)+5. Extract applicable scope+6. Extract target audience+7. Extract core purpose or objective (100-200 words)+8. Extract keywords or tags+9. Extract related other regulations++### Constraints+- Only extract information explicitly mentioned in the text, do not add additional content+- Maintain objectivity and accuracy+- Leave blank if information is missing, do not fabricate++### Source text:+"""+++class RegulationProfile(AutoModel[RegulationInfo]):+ """+ Applicable documents: Company internal management systems, administrative regulations, operation manuals, compliance guidelines++ Function introduction:+ Extract the name, version, scope, effective date, and core purpose of a regulation. Suitable for regulation overview, version management, etc.++ Example:+ >>> template = RegulationProfile(llm_client=llm, embedder=embedder)+ >>> template.feed_text("Universe First Slacker Company Employee Attendance Management System...")+ >>> print(template.data.title)+ >>> print(template.data.effectiveDate)+ """++ def __init__(+ self,+ llm_client: BaseChatModel,+ embedder: Embeddings,+ *,+ max_workers: int = 10,+ verbose: bool = False,+ **kwargs: Any,+ ):+ """+ Initialize regulation profile template.++ Args:+ llm_client: LLM client for knowledge extraction+ embedder: Embedding model for semantic search+ 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+ """+ super().__init__(+ data_schema=RegulationInfo,+ llm_client=llm_client,+ embedder=embedder,+ prompt=_PROMPT,+ max_workers=max_workers,+ verbose=verbose,+ **kwargs,+ )++ def show(+ self,+ *,+ top_k: int = 3,+ ):+ """+ Display regulation profile.++ Args:+ top_k: Number of items to use for chat, default: 3+ """+ def label_extractor(item: RegulationInfo) -> str:+ if item.version:+ return f"{item.title} (v{item.version})"+ return item.title++ super().show(+ label_extractor=label_extractor,+ top_k=top_k,+ )diff --git a/hyperextract/templates/en/general/social_network.py b/hyperextract/templates/en/general/social_network.pynew file mode 100644index 0000000..47eeed2--- /dev/null+++ b/hyperextract/templates/en/general/social_network.py@@ -0,0 +1,169 @@+"""Social Network Graph - Focus on extracting interpersonal relationships, interactions, and organizational affiliations.++Suitable for biography analysis, character setting research, etc.+"""++from typing import Any+from pydantic import BaseModel, Field+from langchain_core.language_models import BaseChatModel+from langchain_core.embeddings import Embeddings+from hyperextract.types import AutoGraph+++class PersonNode(BaseModel):+ """Person/organization node"""+ name: str = Field(description="Name")+ type: str = Field(description="Node type: Person, Organization, Institution, Other")+ description: str = Field(description="Brief description", default="")+ role: str = Field(description="Role/identity", default="")+++class SocialRelation(BaseModel):+ """Social relationship edge"""+ source: str = Field(description="Source node")+ target: str = Field(description="Target node")+ relationType: str = Field(description="Relationship type: Family, Friend, Colleague, TeacherStudent, SupervisorSubordinate, Collaboration, Competition, Affiliation, Other")+ details: str = Field(description="Detailed relationship explanation", default="")+++_PROMPT = """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.++### 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++### 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++### Source text:+"""++_NODE_PROMPT = """You are a professional person and organization recognition expert. Please extract all people and organizations as nodes from the text.++### 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++### Source text:+"""++_EDGE_PROMPT = """You are a professional social relationship extraction expert. Please extract mutual relationships between nodes from the given node list.++### 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+2. Do not create unlisted nodes++"""+++class SocialNetwork(AutoGraph[PersonNode, SocialRelation]):+ """+ Applicable documents: Biographies, memoirs, novels, character setting documents++ Function introduction:+ Focus on extracting interpersonal relationships, interactions, and organizational affiliations. Suitable for biography analysis, character setting research, etc.++ Example:+ >>> template = SocialNetwork(llm_client=llm, embedder=embedder)+ >>> template.feed_text("Zu Chongzhi studied under He Chengtian and debated with Dai Faxing...")+ >>> template.show()+ """++ def __init__(+ self,+ llm_client: BaseChatModel,+ embedder: Embeddings,+ *,+ extraction_mode: str = "two_stage",+ max_workers: int = 10,+ verbose: bool = False,+ **kwargs: Any,+ ):+ """+ Initialize 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+ """+ super().__init__(+ node_schema=PersonNode,+ edge_schema=SocialRelation,+ node_key_extractor=lambda x: x.name,+ edge_key_extractor=lambda x: f"{x.source}|{x.relationType}|{x.target}",+ nodes_in_edge_extractor=lambda x: (x.source, x.target),+ llm_client=llm_client,+ embedder=embedder,+ extraction_mode=extraction_mode,+ max_workers=max_workers,+ verbose=verbose,+ prompt=_PROMPT,+ prompt_for_node_extraction=_NODE_PROMPT,+ prompt_for_edge_extraction=_EDGE_PROMPT,+ **kwargs,+ )++ def show(+ self,+ *,+ top_k_for_search: int = 3,+ top_k_for_chat: int = 3,+ ):+ """+ Display social network graph.++ Args:+ top_k_for_search: Number of nodes/edges to return for semantic search, default: 3+ top_k_for_chat: Number of nodes/edges to use for chat, default: 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:+ return edge.relationType++ super().show(+ node_label_extractor=node_label_extractor,+ edge_label_extractor=edge_label_extractor,+ top_k_nodes_for_search=top_k_for_search,+ top_k_edges_for_search=top_k_for_search,+ top_k_nodes_for_chat=top_k_for_chat,+ top_k_edges_for_chat=top_k_for_chat,+ )diff --git a/hyperextract/templates/zh/__init__.py b/hyperextract/templates/zh/__init__.pyindex fa57f54..c303507 100644--- a/hyperextract/templates/zh/__init__.py+++ b/hyperextract/templates/zh/__init__.py@@ -1,57 +1,34 @@from .general import (# 知识图谱KnowledgeGraph,- GeneralEntity,- GeneralRelation,# 实体集合EntityRegistry,- RegistryEntry,# 关键词列表KeywordList,- Keyword,# 百科条目EncyclopediaItem,- EncyclopediaInfo,# 概念层级图ConceptHierarchy,- ConceptNode,- HierarchyRelation,# 引用网络CrossReferenceNet,- ReferenceNode,- ReferenceRelation,# 个人档案PersonalProfile,- PersonalInfo,# 社会网络SocialNetwork,- PersonNode,- SocialRelation,# 生平时序图LifeEventTimeline,- LifeEntity,- LifeEvent,# 规章元数据快照RegulationProfile,- RegulationInfo,# 规章条文清单ClauseList,- Clause,# 违规处罚对照表PenaltyRegistry,- PenaltyEntry,# 执行程序流程图OperationalProcedure,- ProcedureStep,- ProcedureTransition,# 违规处罚因果链PenaltyMapping,- PenaltyNode,- PenaltyPath,# 合规行为超图ComplianceLogic,- ComplianceEntity,- ComplianceRule,)from .agriculture import CropCycleGraph, LivestockGraph, AgriPestControlfrom .biology import TaxonomyGraph, BiologicalNetwork, MetabolicHypergraph@@ -99,57 +76,34 @@ __all__ = [# general## 知识图谱"KnowledgeGraph",- "GeneralEntity",- "GeneralRelation",## 实体集合"EntityRegistry",- "RegistryEntry",## 关键词列表"KeywordList",- "Keyword",## 百科条目"EncyclopediaItem",- "EncyclopediaInfo",## 概念层级图"ConceptHierarchy",- "ConceptNode",- "HierarchyRelation",## 引用网络"CrossReferenceNet",- "ReferenceNode",- "ReferenceRelation",## 个人档案"PersonalProfile",- "PersonalInfo",## 社会网络"SocialNetwork",- "PersonNode",- "SocialRelation",## 生平时序图"LifeEventTimeline",- "LifeEntity",- "LifeEvent",## 规章元数据快照"RegulationProfile",- "RegulationInfo",## 规章条文清单"ClauseList",- "Clause",## 违规处罚对照表"PenaltyRegistry",- "PenaltyEntry",## 执行程序流程图"OperationalProcedure",- "ProcedureStep",- "ProcedureTransition",## 违规处罚因果链"PenaltyMapping",- "PenaltyNode",- "PenaltyPath",## 合规行为超图"ComplianceLogic",- "ComplianceEntity",- "ComplianceRule",# Finance"RiskAssessmentGraph","MarketSentimentGraph",@@ -196,9 +150,11 @@ __all__ = ["CultivationSystemMap","SectRelationGraph","ArtifactRegistry",+ # Agriculture"CropCycleGraph","LivestockGraph","AgriPestControl",+ # Biology"TaxonomyGraph","BiologicalNetwork","MetabolicHypergraph",diff --git a/hyperextract/templates/zh/general/__init__.py b/hyperextract/templates/zh/general/__init__.pyindex f6a38eb..d3a0ed0 100644--- a/hyperextract/templates/zh/general/__init__.py+++ b/hyperextract/templates/zh/general/__init__.py@@ -1,71 +1,48 @@-from .knowledge_graph import KnowledgeGraph, GeneralEntity, GeneralRelation-from .entity_registry import EntityRegistry, RegistryEntry-from .keyword_list import KeywordList, Keyword-from .encyclopedia_item import EncyclopediaItem, EncyclopediaInfo-from .concept_hierarchy import ConceptHierarchy, ConceptNode, HierarchyRelation-from .cross_reference_net import CrossReferenceNet, ReferenceNode, ReferenceRelation-from .personal_profile import PersonalProfile, PersonalInfo-from .social_network import SocialNetwork, PersonNode, SocialRelation-from .life_event_timeline import LifeEventTimeline, LifeEntity, LifeEvent-from .regulation_profile import RegulationProfile, RegulationInfo-from .clause_list import ClauseList, Clause-from .penalty_registry import PenaltyRegistry, PenaltyEntry-from .operational_procedure import OperationalProcedure, ProcedureStep, ProcedureTransition-from .penalty_mapping import PenaltyMapping, PenaltyNode, PenaltyPath-from .compliance_logic import ComplianceLogic, ComplianceEntity, ComplianceRule+from .knowledge_graph import KnowledgeGraph+from .entity_registry import EntityRegistry+from .keyword_list import KeywordList+from .encyclopedia_item import EncyclopediaItem+from .concept_hierarchy import ConceptHierarchy+from .cross_reference_net import CrossReferenceNet+from .personal_profile import PersonalProfile+from .social_network import SocialNetwork+from .life_event_timeline import LifeEventTimeline+from .regulation_profile import RegulationProfile+from .clause_list import ClauseList+from .penalty_registry import PenaltyRegistry+from .operational_procedure import OperationalProcedure+from .penalty_mapping import PenaltyMapping+from .compliance_logic import ComplianceLogic__all__ = [# 知识图谱"KnowledgeGraph",- "GeneralEntity",- "GeneralRelation",# 实体集合"EntityRegistry",- "RegistryEntry",# 关键词列表"KeywordList",- "Keyword",# 百科条目"EncyclopediaItem",- "EncyclopediaInfo",# 概念层级图"ConceptHierarchy",- "ConceptNode",- "HierarchyRelation",# 引用网络"CrossReferenceNet",- "ReferenceNode",- "ReferenceRelation",# 个人档案"PersonalProfile",- "PersonalInfo",# 社会网络"SocialNetwork",- "PersonNode",- "SocialRelation",# 生平时序图"LifeEventTimeline",- "LifeEntity",- "LifeEvent",# 规章元数据快照"RegulationProfile",- "RegulationInfo",# 规章条文清单"ClauseList",- "Clause",# 违规处罚对照表"PenaltyRegistry",- "PenaltyEntry",# 执行程序流程图"OperationalProcedure",- "ProcedureStep",- "ProcedureTransition",# 违规处罚因果链"PenaltyMapping",- "PenaltyNode",- "PenaltyPath",# 合规行为超图- "ComplianceLogic",- "ComplianceEntity",- "ComplianceRule"+ "ComplianceLogic"]diff --git a/hyperextract/templates/zh/general/compliance_logic.py b/hyperextract/templates/zh/general/compliance_logic.pyindex 51cf98d..571ae9f 100644--- a/hyperextract/templates/zh/general/compliance_logic.py+++ b/hyperextract/templates/zh/general/compliance_logic.py@@ -1,78 +1,86 @@-"""合规行为超图 - 建模“在何种条件下,何种主体,必须/禁止执行何种操作”的复杂逻辑。+"""合规行为超图 - 建模"在何种条件下,何种主体,必须/禁止执行何种操作"的复杂逻辑。适用于合规审计、风险识别等。"""-from typing import Any, List, Tuple+from typing import Any, Listfrom pydantic import BaseModel, Fieldfrom langchain_core.language_models import BaseChatModelfrom langchain_core.embeddings import Embeddingsfrom hyperextract.types import AutoHypergraph-class ComplianceEntity(BaseModel):- """合规实体节点"""- name: str = Field(description="实体名称")- type: str = Field(description="实体类型:主体、操作、条件、约束、其他")+class ComplianceElement(BaseModel):+ """合规要素 - 节点"""+ name: str = Field(description="要素名称")+ type: str = Field(description="要素类型:主体、操作、条件、其他")description: str = Field(description="简要描述", default="")class ComplianceRule(BaseModel):- """合规规则超边(连接多个实体)"""- ruleType: str = Field(description="规则类型:必须、禁止、允许、其他")- participants: List[str] = Field(description="参与实体名称列表")- condition: str = Field(description="适用条件", default="")- action: str = Field(description="操作内容")- details: str = Field(description="详细说明", default="")+ """合规规则 - 超边"""+ rule_type: str = Field(description="规则类型:必须、禁止、允许、其他")+ action: str = Field(description="要求执行或禁止的操作内容")+ condition: str = Field(description="适用条件/前置要求", default="")+ penalty: str = Field(description="违规后果/处罚措施", default="")+ source_clause: str = Field(description="规则来源条款", default="")+ notes: str = Field(description="补充说明", default="")+ participants: List[str] = Field(description="参与要素名称列表")-_PROMPT = """你是一位专业的合规分析专家。请从文本中提取合规实体和规则,建模“在何种条件下,何种主体,必须/禁止执行何种操作”的复杂逻辑,构建合规行为超图。+_PROMPT = """你是一位专业的合规分析专家。请从文本中提取合规要素和规则,建模"在何种条件下,何种主体,必须/禁止执行何种操作"的复杂逻辑,构建合规行为超图。### 节点提取规则-1. 提取所有合规相关实体-2. 为每个实体指定类型:主体、操作、条件、约束、其他-3. 为每个实体添加简要描述+1. 提取规则中涉及的要素作为节点+2. 为每个要素指定类型:主体、操作、条件、其他+3. 为每个要素添加简要描述### 超边提取规则-1. 仅从提取的实体中创建合规规则超边+1. 仅从提取的要素中创建合规规则(超边)2. 指定规则类型:必须、禁止、允许、其他-3. 列出所有参与实体(主体、操作、条件等)-4. 描述适用条件(如有)-5. 描述操作内容-6. 添加详细说明(如有)+3. 填写要求执行或禁止的操作内容(必须)+4. 提取适用条件/前置要求(如有)+5. 提取违规后果/处罚措施(如有)+6. 提取规则来源条款(如有)+7. 添加补充说明(如有)+8. 列出所有参与要素名称(必须)### 约束条件- 确保规则逻辑准确- 保持客观准确,不添加文本中没有的信息+- 每条超边必须包含 rule_type、action 和 participants+- 其他字段尽量填充,缺失的使用空字符串### 源文本:"""-_NODE_PROMPT = """你是一位专业的合规实体识别专家。请从文本中提取所有合规相关实体作为节点。+_NODE_PROMPT = """你是一位专业的合规要素识别专家。请从文本中提取所有合规相关要素作为节点。### 提取规则-1. 提取所有合规相关实体-2. 为每个实体指定类型:主体、操作、条件、约束、其他-3. 为每个实体添加简要描述+1. 提取规则中涉及的要素+2. 为每个要素指定类型:主体、操作、条件、其他+3. 为每个要素添加简要描述### 源文本:"""-_EDGE_PROMPT = """你是一位专业的合规规则提取专家。请从给定实体列表中提取合规规则超边。+_EDGE_PROMPT = """你是一位专业的合规规则提取专家。请从给定节点(要素)列表中提取合规规则超边。### 约束条件-1. 仅从下方已知实体列表中提取超边-2. 不要创建未列出的实体+1. 仅从下方已知要素列表中提取规则(超边)+2. 不要创建未列出的要素+3. 每条规则必须包含 rule_type、action 和 participants+4. 其他字段尽量填充,缺失的使用空字符串"""-class ComplianceLogic(AutoHypergraph[ComplianceEntity, ComplianceRule]):+class ComplianceLogic(AutoHypergraph[ComplianceElement, ComplianceRule]):"""适用文档: 合规指南、行政法规、公司管理制度功能介绍:- 建模“在何种条件下,何种主体,必须/禁止执行何种操作”的复杂逻辑。适用于合规审计、风险识别等。+ 建模"在何种条件下,何种主体,必须/禁止执行何种操作"的复杂逻辑。适用于合规审计、风险识别等。Example:>>> template = ComplianceLogic(llm_client=llm, embedder=embedder)@@ -92,7 +100,7 @@ class ComplianceLogic(AutoHypergraph[ComplianceEntity, ComplianceRule]):):"""初始化合规行为超图模板。-+Args:llm_client: LLM 客户端,用于知识提取embedder: 嵌入模型,用于语义检索@@ -103,10 +111,10 @@ class ComplianceLogic(AutoHypergraph[ComplianceEntity, ComplianceRule]):**kwargs: 其他技术参数,传递给基类"""super().__init__(- node_schema=ComplianceEntity,+ node_schema=ComplianceElement,edge_schema=ComplianceRule,node_key_extractor=lambda x: x.name,- edge_key_extractor=lambda x: f"{x.ruleType}_{sorted(x.participants)}",+ edge_key_extractor=lambda x: f"{sorted(x.participants)}_{x.rule_type}_{x.action}",nodes_in_edge_extractor=lambda x: tuple(x.participants),llm_client=llm_client,embedder=embedder,@@ -127,17 +135,17 @@ class ComplianceLogic(AutoHypergraph[ComplianceEntity, ComplianceRule]):):"""展示合规行为超图。-+Args:top_k_for_search: 语义检索返回的节点/边数量,默认为 3top_k_for_chat: 问答使用的节点/边数量,默认为 3"""- def node_label_extractor(node: ComplianceEntity) -> str:+ def node_label_extractor(node: ComplianceElement) -> str:return f"{node.name} ({node.type})"-+def edge_label_extractor(edge: ComplianceRule) -> str:- return edge.ruleType-+ return f"[{edge.rule_type}] {edge.action}"+super().show(node_label_extractor=node_label_extractor,edge_label_extractor=edge_label_extractor,