Hypergraph is more powerful. Transform unstructured text into structured knowledge with LLMs. Graphs, hypergraphs, and spatio-temporal extractions — with one command.
feat: 优化概念图、知识图谱和逻辑图的模型定义,增强字段描述和格式化
yifanfeng97f2824d2f8914
diff --git a/hyperextract/templates/en/general/concept_map.py b/hyperextract/templates/en/general/concept_map.pyindex 78f2dac..bbdeae4 100644--- a/hyperextract/templates/en/general/concept_map.py+++ b/hyperextract/templates/en/general/concept_map.py@@ -8,21 +8,32 @@ from hyperextract.types import AutoGraph# 1. Schema Definitions# ==============================================================================+class Concept(BaseModel):"""A conceptual node representing a term, idea, or theory."""+term: str = Field(description="The technical term or concept name.")- definition: str = Field(description="A formal definition or explanation of the concept.")- examples: Optional[List[str]] = Field(default_factory=list, description="Examples that illustrate this concept.")- attributes: Optional[List[str]] = Field(default_factory=list, description="Key properties or attributes of the concept.")+ definition: str = Field(+ description="A formal definition or explanation of the concept."+ )+ examples: Optional[List[str]] = Field(+ default_factory=list, description="Examples that illustrate this concept."+ )+ attributes: Optional[List[str]] = Field(+ default_factory=list, description="Key properties or attributes of the concept."+ )+class ConceptRelation(BaseModel):"""A semantic relationship between two concepts."""+source: str = Field(description="The source concept.")target: str = Field(description="The target concept.")relation_type: str = Field(description="Type of relationship (e.g., is-a, part-of, related-to, used-for, instance-of).")+# ==============================================================================# 2. Prompts# ==============================================================================@@ -52,13 +63,14 @@ CONCEPT_MAP_EDGE_PROMPT = (# 3. Template Class# ==============================================================================+class ConceptMap(AutoGraph[Concept, ConceptRelation]):"""A template for building concept maps and taxonomies.-+Ideal for structured learning, technical documentation, and glossary extraction.It focuses on definitions, semantic categories, and hierarchical relationships.-+Example:>>> from langchain_openai import ChatOpenAI, OpenAIEmbeddings>>> llm = ChatOpenAI(model="gpt-4o")@@ -70,6 +82,7 @@ class ConceptMap(AutoGraph[Concept, ConceptRelation]):>>> cm.feed_text(text)>>> print(cm.edges) # Output shows: Machine Learning --(is a)--> Artificial Intelligence"""+def __init__(self,llm_client: BaseChatModel,@@ -80,7 +93,7 @@ class ConceptMap(AutoGraph[Concept, ConceptRelation]):chunk_overlap: int = 256,max_workers: int = 10,verbose: bool = False,- **kwargs: Any+ **kwargs: Any,):"""Initialize the ConceptMap template.@@ -101,7 +114,9 @@ class ConceptMap(AutoGraph[Concept, ConceptRelation]):node_schema=Concept,edge_schema=ConceptRelation,node_key_extractor=lambda x: x.term.strip(),- edge_key_extractor=lambda x: f"{x.source.strip()}--({x.relation_type.lower()})-->{x.target.strip()}",+ edge_key_extractor=lambda x: (+ f"{x.source.strip()}--({x.relation_type.lower()})-->{x.target.strip()}"+ ),nodes_in_edge_extractor=lambda x: (x.source.strip(), x.target.strip()),llm_client=llm_client,embedder=embedder,@@ -113,8 +128,9 @@ class ConceptMap(AutoGraph[Concept, ConceptRelation]):prompt=CONCEPT_MAP_PROMPT,prompt_for_node_extraction=CONCEPT_MAP_NODE_PROMPT,prompt_for_edge_extraction=CONCEPT_MAP_EDGE_PROMPT,- **kwargs+ **kwargs,)+def show(self,*,@@ -125,19 +141,20 @@ class ConceptMap(AutoGraph[Concept, ConceptRelation]):) -> None:"""Visualize the graph using OntoSight.-+Args:top_k_nodes_for_search (int): Number of nodes to retrieve for search context. Default 3.top_k_edges_for_search (int): Number of edges to retrieve for search context. Default 3.top_k_nodes_for_chat (int): Number of nodes to retrieve for chat context. Default 3.top_k_edges_for_chat (int): Number of edges to retrieve for chat context. Default 3."""+def node_label_extractor(node: Concept) -> str:- return f"{ node.term }"-+ return f"{node.term}"+def edge_label_extractor(edge: ConceptRelation) -> str:- return f"{ edge.source }"-+ return f"{edge.relation_type}"+super().show(node_label_extractor=node_label_extractor,edge_label_extractor=edge_label_extractor,diff --git a/hyperextract/templates/en/general/knowledge_graph.py b/hyperextract/templates/en/general/knowledge_graph.pyindex fb59066..0cbb637 100644--- a/hyperextract/templates/en/general/knowledge_graph.py+++ b/hyperextract/templates/en/general/knowledge_graph.py@@ -8,8 +8,10 @@ from hyperextract.types import AutoGraph# 1. Schema Definitions# ==============================================================================+class Entity(BaseModel):"""A general entity representing a person, organization, location, or object."""+name: str = Field(description="The primary name of the entity.")category: str = Field(description="Category of the entity (e.g., Person, Organization, Location, Event, Object)."@@ -18,12 +20,19 @@ class Entity(BaseModel):description="A concise summary of the entity and its role in the text.")+class Relation(BaseModel):"""A factual relationship between two entities."""+source: str = Field(description="The name of the source entity.")target: str = Field(description="The name of the target entity.")- relation: str = Field(description="The type of relationship (e.g., Works for, Located in, Born in, Acquired).")- details: Optional[str] = Field(description="Additional context or details about this relationship.")+ relation: str = Field(+ description="The type of relationship (e.g., Works for, Located in, Born in, Acquired)."+ )+ details: Optional[str] = Field(+ description="Additional context or details about this relationship."+ )+# ==============================================================================# 2. Prompts@@ -54,13 +63,14 @@ KNOWLEDGE_GRAPH_EDGE_PROMPT = (# 3. Template Class# ==============================================================================+class KnowledgeGraph(AutoGraph[Entity, Relation]):"""A foundational knowledge graph template for factual extraction.-- This template is optimized for extracting entities (People, Places, Organizations)++ This template is optimized for extracting entities (People, Places, Organizations)and their factual interactions from news articles, biographies, and encyclopedic texts.-+Example:>>> from langchain_openai import ChatOpenAI, OpenAIEmbeddings>>> llm = ChatOpenAI(model="gpt-4o")@@ -72,6 +82,7 @@ class KnowledgeGraph(AutoGraph[Entity, Relation]):>>> kg.feed_text(text)>>> print(kg.nodes) # Output: [Entity(name='Steve Jobs', ...), Entity(name='Apple', ...)]"""+def __init__(self,llm_client: BaseChatModel,@@ -82,7 +93,7 @@ class KnowledgeGraph(AutoGraph[Entity, Relation]):chunk_overlap: int = 256,max_workers: int = 10,verbose: bool = False,- **kwargs: Any+ **kwargs: Any,):"""Initialize the KnowledgeGraph template.@@ -103,7 +114,9 @@ class KnowledgeGraph(AutoGraph[Entity, Relation]):node_schema=Entity,edge_schema=Relation,node_key_extractor=lambda x: x.name.strip(),- edge_key_extractor=lambda x: f"{x.source.strip()}--[{x.relation.lower()}]-->{x.target.strip()}",+ edge_key_extractor=lambda x: (+ f"{x.source.strip()}--[{x.relation.lower()}]-->{x.target.strip()}"+ ),nodes_in_edge_extractor=lambda x: (x.source.strip(), x.target.strip()),llm_client=llm_client,embedder=embedder,@@ -115,8 +128,9 @@ class KnowledgeGraph(AutoGraph[Entity, Relation]):prompt=KNOWLEDGE_GRAPH_PROMPT,prompt_for_node_extraction=KNOWLEDGE_GRAPH_NODE_PROMPT,prompt_for_edge_extraction=KNOWLEDGE_GRAPH_EDGE_PROMPT,- **kwargs+ **kwargs,)+def show(self,*,@@ -127,20 +141,21 @@ class KnowledgeGraph(AutoGraph[Entity, Relation]):) -> None:"""Visualize the graph using OntoSight.-+Args:top_k_nodes_for_search (int): Number of nodes to retrieve for search context. Default 3.top_k_edges_for_search (int): Number of edges to retrieve for search context. Default 3.top_k_nodes_for_chat (int): Number of nodes to retrieve for chat context. Default 3.top_k_edges_for_chat (int): Number of edges to retrieve for chat context. Default 3."""+def node_label_extractor(node: Entity) -> str:- info = f" ({ node.category })" if getattr(node, "category", None) else ""- return f"{ node.name }{info}"-+ info = f" ({node.category})" if getattr(node, "category", None) else ""+ return f"{node.name}{info}"+def edge_label_extractor(edge: Relation) -> str:- return f"{ edge.source }"-+ return f"{edge.relation}"+super().show(node_label_extractor=node_label_extractor,edge_label_extractor=edge_label_extractor,diff --git a/hyperextract/templates/en/general/logic_graph.py b/hyperextract/templates/en/general/logic_graph.pyindex d864794..737c3cf 100644--- a/hyperextract/templates/en/general/logic_graph.py+++ b/hyperextract/templates/en/general/logic_graph.py@@ -1,4 +1,4 @@-from typing import List, Optional, Any+from typing import Optional, Anyfrom pydantic import BaseModel, Fieldfrom langchain_core.language_models.chat_models import BaseChatModelfrom langchain_core.embeddings import Embeddings@@ -8,22 +8,29 @@ from hyperextract.types import AutoGraph# 1. Schema Definitions# ==============================================================================+class LogicNode(BaseModel):"""A node representing a claim, evidence, or premise in an argument."""+statement: str = Field(description="The central claim, fact, or observation.")node_type: str = Field(description="Type of node: Claim (main point), Evidence (supporting data), Premise (underlying assumption).")- source_attribution: Optional[str] = Field(description="Where or who this logic point originates from.")+ source_attribution: Optional[str] = Field(+ description="Where or who this logic point originates from."+ )+class LogicRelation(BaseModel):"""The logical connection between two statements."""+source: str = Field(description="The source statement.")target: str = Field(description="The target statement.")inference: str = Field(description="The logical link: Supports, Contradicts, Proves, Leads to, Explains.")+# ==============================================================================# 2. Prompts# ==============================================================================@@ -50,14 +57,15 @@ LOGIC_GRAPH_EDGE_PROMPT = (# 3. Template Class# ==============================================================================+class LogicGraph(AutoGraph[LogicNode, LogicRelation]):"""A template for analyzing reasoning, arguments, and causal chains.-- This template is designed to map out the logical structure of analytical reports,- scientific papers, or debate transcripts. It captures claims, evidence, and++ This template is designed to map out the logical structure of analytical reports,+ scientific papers, or debate transcripts. It captures claims, evidence, andthe logical links (Support/Contradict) between them.-+Example:>>> from langchain_openai import ChatOpenAI, OpenAIEmbeddings>>> llm = ChatOpenAI(model="gpt-4o")@@ -69,6 +77,7 @@ class LogicGraph(AutoGraph[LogicNode, LogicRelation]):>>> lg.feed_text(text)>>> print(lg.edges) # Output shows: Carbon levels rising --(Supports)--> Climate is warming"""+def __init__(self,llm_client: BaseChatModel,@@ -79,7 +88,7 @@ class LogicGraph(AutoGraph[LogicNode, LogicRelation]):chunk_overlap: int = 256,max_workers: int = 10,verbose: bool = False,- **kwargs: Any+ **kwargs: Any,):"""Initialize the LogicGraph template.@@ -100,7 +109,9 @@ class LogicGraph(AutoGraph[LogicNode, LogicRelation]):node_schema=LogicNode,edge_schema=LogicRelation,node_key_extractor=lambda x: x.statement.strip(),- edge_key_extractor=lambda x: f"{x.source.strip()}--({x.inference.lower()})-->{x.target.strip()}",+ edge_key_extractor=lambda x: (+ f"{x.source.strip()}--({x.inference.lower()})-->{x.target.strip()}"+ ),nodes_in_edge_extractor=lambda x: (x.source.strip(), x.target.strip()),llm_client=llm_client,embedder=embedder,@@ -112,8 +123,9 @@ class LogicGraph(AutoGraph[LogicNode, LogicRelation]):prompt=LOGIC_GRAPH_PROMPT,prompt_for_node_extraction=LOGIC_GRAPH_NODE_PROMPT,prompt_for_edge_extraction=LOGIC_GRAPH_EDGE_PROMPT,- **kwargs+ **kwargs,)+def show(self,*,@@ -124,19 +136,20 @@ class LogicGraph(AutoGraph[LogicNode, LogicRelation]):) -> None:"""Visualize the graph using OntoSight.-+Args:top_k_nodes_for_search (int): Number of nodes to retrieve for search context. Default 3.top_k_edges_for_search (int): Number of edges to retrieve for search context. Default 3.top_k_nodes_for_chat (int): Number of nodes to retrieve for chat context. Default 3.top_k_edges_for_chat (int): Number of edges to retrieve for chat context. Default 3."""+def node_label_extractor(node: LogicNode) -> str:- return f"{ node.statement }"-+ return f"{node.statement}"+def edge_label_extractor(edge: LogicRelation) -> str:- return f"{ edge.source }"-+ return f"{edge.inference}"+super().show(node_label_extractor=node_label_extractor,edge_label_extractor=edge_label_extractor,