Skip to content

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

feat: Enhance graph templates with additional parameters and improved documentation for better extraction capabilities

yifanfeng97003d549b92fc
diff --git a/examples/auto_graph_demo.py b/examples/auto_graph_demo.py
index 419691c..c8d0127 100644
--- a/examples/auto_graph_demo.py
+++ b/examples/auto_graph_demo.py
@@ -411,7 +411,7 @@ def run_demo():
chat_queries = [
"请总结这个公会冲突的核心原因。",
"暗影刺客和小甜甜的行为对团队造成了什么影响?",
- "从这个事件中,我们学到了什么关于公会管理的经验?"
+ "从这个事件中,我们学到了什么关于公会管理的经验?",
]
for q in chat_queries:
@@ -434,6 +434,11 @@ def run_demo():
except Exception as e:
print(f"⚠️ 保存失败: {e}")
+ graph.show(
+ node_label_extractor=lambda n: f"{n.name} ({n.category})",
+ edge_label_extractor=lambda e: e.action_type,
+ )
+
if __name__ == "__main__":
run_demo()
diff --git a/hyperextract/graphs/base.py b/hyperextract/graphs/base.py
index 6c41e06..a5fe623 100644
--- a/hyperextract/graphs/base.py
+++ b/hyperextract/graphs/base.py
@@ -22,6 +22,7 @@ from langchain_core.language_models.chat_models import BaseChatModel
from langchain_core.prompts import ChatPromptTemplate
from ontomem import OMem
from ontomem.merger import MergeStrategy, create_merger, BaseMerger
+from ontosight import view_graph
from hyperextract.core import BaseAutoType
from hyperextract.utils.logging import logger
@@ -69,7 +70,9 @@ DEFAULT_EDGE_PROMPT = (
class AutoGraphSchema(BaseModel, Generic[NodeSchema, EdgeSchema]):
"""Generic schema container for graph-based knowledge patterns."""
- nodes: List[NodeSchema] = Field(default_factory=list, description="Graph nodes/entities")
+ nodes: List[NodeSchema] = Field(
+ default_factory=list, description="Graph nodes/entities"
+ )
edges: List[EdgeSchema] = Field(
default_factory=list, description="Graph edges/relationships"
)
@@ -93,7 +96,10 @@ class EdgeListSchema(BaseModel, Generic[EdgeSchema]):
)
-class AutoGraph(BaseAutoType[AutoGraphSchema[NodeSchema, EdgeSchema]], Generic[NodeSchema, EdgeSchema]):
+class AutoGraph(
+ BaseAutoType[AutoGraphSchema[NodeSchema, EdgeSchema]],
+ Generic[NodeSchema, EdgeSchema],
+):
"""AutoGraph - extracts knowledge graphs with nodes and edges from text.
This pattern extracts structured knowledge graphs consisting of entities (nodes) and
@@ -388,7 +394,9 @@ class AutoGraph(BaseAutoType[AutoGraphSchema[NodeSchema, EdgeSchema]], Generic[N
self.clear_index()
- def _update_data_state(self, incoming_data: AutoGraphSchema[NodeSchema, EdgeSchema]) -> None:
+ def _update_data_state(
+ self, incoming_data: AutoGraphSchema[NodeSchema, EdgeSchema]
+ ) -> None:
"""Merge incoming graph data into current state.
Args:
@@ -424,7 +432,9 @@ class AutoGraph(BaseAutoType[AutoGraphSchema[NodeSchema, EdgeSchema]], Generic[N
# Prune dangling edges to ensure graph consistency
return self._prune_dangling_edges(raw_graph)
- def _extract_data_by_one_stage(self, text: str) -> AutoGraphSchema[NodeSchema, EdgeSchema]:
+ def _extract_data_by_one_stage(
+ self, text: str
+ ) -> AutoGraphSchema[NodeSchema, EdgeSchema]:
"""Extract nodes and edges simultaneously using single LLM call.
Args:
@@ -454,7 +464,9 @@ class AutoGraph(BaseAutoType[AutoGraphSchema[NodeSchema, EdgeSchema]], Generic[N
# Merge multiple graphs
return self.merge_batch_data(graph_list)
- def _extract_data_by_two_stage(self, text: str) -> AutoGraphSchema[NodeSchema, EdgeSchema]:
+ def _extract_data_by_two_stage(
+ self, text: str
+ ) -> AutoGraphSchema[NodeSchema, EdgeSchema]:
"""Extract nodes first, then edges with node context (batch processing).
Process:
@@ -491,7 +503,9 @@ class AutoGraph(BaseAutoType[AutoGraphSchema[NodeSchema, EdgeSchema]], Generic[N
# 5. Global Merge (passes tuples to merge_batch_data)
return self.merge_batch_data(partial_graphs)
- def _extract_nodes_batch(self, chunks: List[str]) -> List[NodeListSchema[NodeSchema]]:
+ def _extract_nodes_batch(
+ self, chunks: List[str]
+ ) -> List[NodeListSchema[NodeSchema]]:
"""Batch extract nodes from multiple text chunks.
Args:
@@ -542,7 +556,9 @@ class AutoGraph(BaseAutoType[AutoGraphSchema[NodeSchema, EdgeSchema]], Generic[N
return llm_chain.batch(inputs, config={"max_concurrency": self.max_workers})
- def _prune_dangling_edges(self, graph: AutoGraphSchema[NodeSchema, EdgeSchema]) -> AutoGraphSchema[NodeSchema, EdgeSchema]:
+ def _prune_dangling_edges(
+ self, graph: AutoGraphSchema[NodeSchema, EdgeSchema]
+ ) -> AutoGraphSchema[NodeSchema, EdgeSchema]:
"""Prune edges that connect to non-existent nodes (Consistency Check).
Ensures graph consistency by removing any edges where either endpoint
@@ -851,3 +867,49 @@ class AutoGraph(BaseAutoType[AutoGraphSchema[NodeSchema, EdgeSchema]], Generic[N
self._edge_memory.load_index(str(edge_index_path))
except Exception as e:
logger.warning(f"Failed to load edge index: {e}")
+
+ def show(
+ self,
+ node_label_extractor: Callable[[NodeSchema], str],
+ edge_label_extractor: Callable[[EdgeSchema], str],
+ ) -> None:
+ """Visualize the graph using OntoSight.
+
+ Args:
+ node_label_extractor: A function that takes a NodeSchema and returns a string label for visualization.
+ edge_label_extractor: A function that takes an EdgeSchema and returns a string label for visualization.
+ """
+
+ if self._node_memory.has_index() and self._edge_memory.has_index():
+ logger.info(
+ "Visualizing graph with search and chat capabilities (indices detected)."
+ )
+
+ def search_callback(query: str) -> None:
+ return self.search(query, top_k_nodes=3, top_k_edges=3)
+
+ def chat_callback(question: str) -> None:
+ response = self.chat(question, top_k_nodes=3, top_k_edges=3)
+ content = response.content
+ retrieved_nodes = response.additional_kwargs.get("retrieved_nodes", [])
+ retrieved_edges = response.additional_kwargs.get("retrieved_edges", [])
+ return content, (retrieved_nodes, retrieved_edges)
+ else:
+ logger.info(
+ "Visualizing graph without search and chat capabilities (no indices detected)."
+ )
+ search_callback = None
+ chat_callback = None
+
+ view_graph(
+ node_list=self.nodes,
+ edge_list=self.edges,
+ node_schema=self.node_schema,
+ edge_schema=self.edge_schema,
+ node_id_extractor=self.node_key_extractor,
+ node_ids_in_edge_extractor=self.nodes_in_edge_extractor,
+ node_label_extractor=node_label_extractor,
+ edge_label_extractor=edge_label_extractor,
+ on_search=search_callback,
+ on_chat=chat_callback,
+ )
diff --git a/hyperextract/templates/en/general/concept_map.py b/hyperextract/templates/en/general/concept_map.py
index 690d32e..d19fee9 100644
--- a/hyperextract/templates/en/general/concept_map.py
+++ b/hyperextract/templates/en/general/concept_map.py
@@ -1,5 +1,7 @@
-from typing import List, Optional
+from typing import List, Optional, Any
from pydantic import BaseModel, Field
+from langchain_core.language_models.chat_models import BaseChatModel
+from langchain_core.embeddings import Embeddings
from hyperextract.graphs import AutoGraph
# ==============================================================================
@@ -52,16 +54,62 @@ CONCEPT_MAP_EDGE_PROMPT = (
class ConceptMap(AutoGraph[Concept, ConceptRelation]):
"""
- A template for building conceptual graphs and taxonomies.
- Ideal for technical documentation, educational materials, and glossaries.
+ 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")
+ >>> embedder = OpenAIEmbeddings()
+ >>> # Initialize the template
+ >>> cm = ConceptMap(llm_client=llm, embedder=embedder)
+ >>> # Extract taxonomy from text
+ >>> text = "Machine Learning is a subset of Artificial Intelligence that uses data to learn."
+ >>> cm.feed_text(text)
+ >>> print(cm.edges) # Output shows: Machine Learning --(is a)--> Artificial Intelligence
"""
- def __init__(self, **kwargs):
+ def __init__(
+ self,
+ llm_client: BaseChatModel,
+ embedder: Embeddings,
+ *,
+ extraction_mode: str = "one_stage",
+ chunk_size: int = 2048,
+ chunk_overlap: int = 256,
+ max_workers: int = 10,
+ verbose: bool = False,
+ **kwargs: Any
+ ):
+ """
+ Initialize the ConceptMap template.
+
+ Args:
+ llm_client: The language model client used for extraction.
+ embedder: The embedding model used for concept deduplication and indexing.
+ extraction_mode: The strategy for extraction:
+ - "one_stage": Extract nodes and edges simultaneously (faster).
+ - "two_stage": Extract nodes first, then edges (higher accuracy).
+ chunk_size: Maximum number of characters per text chunk.
+ chunk_overlap: Number of characters to overlap between chunks.
+ max_workers: Maximum number of parallel workers for processing.
+ verbose: If True, prints detailed progress logs.
+ **kwargs: Additional arguments passed to the AutoGraph constructor.
+ """
super().__init__(
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()}",
nodes_in_edge_extractor=lambda x: (x.source.strip(), x.target.strip()),
+ llm_client=llm_client,
+ embedder=embedder,
+ extraction_mode=extraction_mode,
+ chunk_size=chunk_size,
+ chunk_overlap=chunk_overlap,
+ max_workers=max_workers,
+ verbose=verbose,
prompt=CONCEPT_MAP_PROMPT,
prompt_for_node_extraction=CONCEPT_MAP_NODE_PROMPT,
prompt_for_edge_extraction=CONCEPT_MAP_EDGE_PROMPT,
diff --git a/hyperextract/templates/en/general/knowledge_graph.py b/hyperextract/templates/en/general/knowledge_graph.py
index 3d3a3bd..6b6880c 100644
--- a/hyperextract/templates/en/general/knowledge_graph.py
+++ b/hyperextract/templates/en/general/knowledge_graph.py
@@ -1,5 +1,7 @@
-from typing import List, Optional
+from typing import List, Optional, Any
from pydantic import BaseModel, Field
+from langchain_core.language_models.chat_models import BaseChatModel
+from langchain_core.embeddings import Embeddings
from hyperextract.graphs import AutoGraph
# ==============================================================================
@@ -55,15 +57,61 @@ KNOWLEDGE_GRAPH_EDGE_PROMPT = (
class KnowledgeGraph(AutoGraph[Entity, Relation]):
"""
A foundational knowledge graph template for factual extraction.
- Best for news, biographies, and descriptive encyclopedic content.
+
+ 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")
+ >>> embedder = OpenAIEmbeddings()
+ >>> # Initialize the template
+ >>> kg = KnowledgeGraph(llm_client=llm, embedder=embedder)
+ >>> # Extract knowledge from text
+ >>> text = "Steve Jobs co-founded Apple in Cupertino."
+ >>> kg.feed_text(text)
+ >>> print(kg.nodes) # Output: [Entity(name='Steve Jobs', ...), Entity(name='Apple', ...)]
"""
- def __init__(self, **kwargs):
+ def __init__(
+ self,
+ llm_client: BaseChatModel,
+ embedder: Embeddings,
+ *,
+ extraction_mode: str = "one_stage",
+ chunk_size: int = 2048,
+ chunk_overlap: int = 256,
+ max_workers: int = 10,
+ verbose: bool = False,
+ **kwargs: Any
+ ):
+ """
+ Initialize the KnowledgeGraph template.
+
+ Args:
+ llm_client: The language model client used for extraction.
+ embedder: The embedding model used for entity deduplication and indexing.
+ extraction_mode: The strategy for extraction:
+ - "one_stage": Extract nodes and edges simultaneously (faster).
+ - "two_stage": Extract nodes first, then edges (higher accuracy).
+ chunk_size: Maximum number of characters per text chunk.
+ chunk_overlap: Number of characters to overlap between chunks.
+ max_workers: Maximum number of parallel workers for processing.
+ verbose: If True, prints detailed progress logs.
+ **kwargs: Additional arguments passed to the AutoGraph constructor.
+ """
super().__init__(
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()}",
nodes_in_edge_extractor=lambda x: (x.source.strip(), x.target.strip()),
+ llm_client=llm_client,
+ embedder=embedder,
+ extraction_mode=extraction_mode,
+ chunk_size=chunk_size,
+ chunk_overlap=chunk_overlap,
+ max_workers=max_workers,
+ verbose=verbose,
prompt=KNOWLEDGE_GRAPH_PROMPT,
prompt_for_node_extraction=KNOWLEDGE_GRAPH_NODE_PROMPT,
prompt_for_edge_extraction=KNOWLEDGE_GRAPH_EDGE_PROMPT,
diff --git a/hyperextract/templates/en/general/logic_graph.py b/hyperextract/templates/en/general/logic_graph.py
index 56362ab..def0289 100644
--- a/hyperextract/templates/en/general/logic_graph.py
+++ b/hyperextract/templates/en/general/logic_graph.py
@@ -1,5 +1,7 @@
-from typing import List, Optional
+from typing import List, Optional, Any
from pydantic import BaseModel, Field
+from langchain_core.language_models.chat_models import BaseChatModel
+from langchain_core.embeddings import Embeddings
from hyperextract.graphs import AutoGraph
# ==============================================================================
@@ -51,15 +53,62 @@ LOGIC_GRAPH_EDGE_PROMPT = (
class LogicGraph(AutoGraph[LogicNode, LogicRelation]):
"""
A template for analyzing reasoning, arguments, and causal chains.
- Ideal for analytical reports, debate transcripts, and scientific reasoning.
+
+ This template is designed to map out the logical structure of analytical reports,
+ scientific papers, or debate transcripts. It captures claims, evidence, and
+ the logical links (Support/Contradict) between them.
+
+ Example:
+ >>> from langchain_openai import ChatOpenAI, OpenAIEmbeddings
+ >>> llm = ChatOpenAI(model="gpt-4o")
+ >>> embedder = OpenAIEmbeddings()
+ >>> # Initialize the template
+ >>> lg = LogicGraph(llm_client=llm, embedder=embedder)
+ >>> # Extract logic from text
+ >>> text = "The climate is warming because carbon levels are rising."
+ >>> lg.feed_text(text)
+ >>> print(lg.edges) # Output shows: Carbon levels rising --(Supports)--> Climate is warming
"""
- def __init__(self, **kwargs):
+ def __init__(
+ self,
+ llm_client: BaseChatModel,
+ embedder: Embeddings,
+ *,
+ extraction_mode: str = "one_stage",
+ chunk_size: int = 2048,
+ chunk_overlap: int = 256,
+ max_workers: int = 10,
+ verbose: bool = False,
+ **kwargs: Any
+ ):
+ """
+ Initialize the LogicGraph template.
+
+ Args:
+ llm_client: The language model client used for extraction.
+ embedder: The embedding model used for logic point deduplication and indexing.
+ extraction_mode: The strategy for extraction:
+ - "one_stage": Extract nodes and edges simultaneously (faster).
+ - "two_stage": Extract nodes first, then edges (higher accuracy).
+ chunk_size: Maximum number of characters per text chunk.
+ chunk_overlap: Number of characters to overlap between chunks.
+ max_workers: Maximum number of parallel workers for processing.
+ verbose: If True, prints detailed progress logs.
+ **kwargs: Additional arguments passed to the AutoGraph constructor.
+ """
super().__init__(
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()}",
nodes_in_edge_extractor=lambda x: (x.source.strip(), x.target.strip()),
+ llm_client=llm_client,
+ embedder=embedder,
+ extraction_mode=extraction_mode,
+ chunk_size=chunk_size,
+ chunk_overlap=chunk_overlap,
+ max_workers=max_workers,
+ verbose=verbose,
prompt=LOGIC_GRAPH_PROMPT,
prompt_for_node_extraction=LOGIC_GRAPH_NODE_PROMPT,
prompt_for_edge_extraction=LOGIC_GRAPH_EDGE_PROMPT,
diff --git a/hyperextract/templates/zh/general/concept_map.py b/hyperextract/templates/zh/general/concept_map.py
index af793dd..80209c7 100644
--- a/hyperextract/templates/zh/general/concept_map.py
+++ b/hyperextract/templates/zh/general/concept_map.py
@@ -1,5 +1,7 @@
-from typing import List, Optional
+from typing import List, Optional, Any
from pydantic import BaseModel, Field
+from langchain_core.language_models.chat_models import BaseChatModel
+from langchain_core.embeddings import Embeddings
from hyperextract.graphs import AutoGraph
# ==============================================================================
@@ -49,16 +51,61 @@ CONCEPT_MAP_EDGE_PROMPT = (
class ConceptMap(AutoGraph[Concept, ConceptRelation]):
"""
- 用于构建概念图谱和分类体系的模板。
- 适用于技术文档、教育材料、术语表解析等场景。
+ 用于构建概念图和分类体系的模板。
+
+ 适用于结构化学习、技术文档和术语表提取。侧重于定义、语义类别和层级关系。
+
+ 示例:
+ >>> from langchain_openai import ChatOpenAI, OpenAIEmbeddings
+ >>> llm = ChatOpenAI(model="gpt-4o")
+ >>> embedder = OpenAIEmbeddings()
+ >>> # 初始化模板
+ >>> cm = ConceptMap(llm_client=llm, embedder=embedder)
+ >>> # 从文本中提取分类体系
+ >>> text = "机器学习是人工智能的一个子集,它利用数据进行学习。"
+ >>> cm.feed_text(text)
+ >>> print(cm.edges) # 输出显示:机器学习 --(是一种)--> 人工智能
"""
- def __init__(self, **kwargs):
+ def __init__(
+ self,
+ llm_client: BaseChatModel,
+ embedder: Embeddings,
+ *,
+ extraction_mode: str = "one_stage",
+ chunk_size: int = 2048,
+ chunk_overlap: int = 256,
+ max_workers: int = 10,
+ verbose: bool = False,
+ **kwargs: Any
+ ):
+ """
+ 初始化 ConceptMap 模板。
+
+ Args:
+ llm_client: 用于提取的语言模型客户端。
+ embedder: 用于概念去重和索引的嵌入模型。
+ extraction_mode: 提取策略:
+ - "one_stage": 同时提取节点和边(速度更快)。
+ - "two_stage": 先提取节点,再提取边(准确度更高)。
+ chunk_size: 每个文本块的最大字符数。
+ chunk_overlap: 文本块之间的重叠字符数。
+ max_workers: 并行处理的最大线程数。
+ verbose: 如果为 True,则打印详细的进度日志。
+ **kwargs: 传给 AutoGraph 构造函数的其他参数。
+ """
super().__init__(
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()}",
nodes_in_edge_extractor=lambda x: (x.source.strip(), x.target.strip()),
+ llm_client=llm_client,
+ embedder=embedder,
+ extraction_mode=extraction_mode,
+ chunk_size=chunk_size,
+ chunk_overlap=chunk_overlap,
+ max_workers=max_workers,
+ verbose=verbose,
prompt=CONCEPT_MAP_PROMPT,
prompt_for_node_extraction=CONCEPT_MAP_NODE_PROMPT,
prompt_for_edge_extraction=CONCEPT_MAP_EDGE_PROMPT,
diff --git a/hyperextract/templates/zh/general/knowledge_graph.py b/hyperextract/templates/zh/general/knowledge_graph.py
index f853d4c..32742b5 100644
--- a/hyperextract/templates/zh/general/knowledge_graph.py
+++ b/hyperextract/templates/zh/general/knowledge_graph.py
@@ -1,5 +1,7 @@
-from typing import List, Optional
+from typing import List, Optional, Any
from pydantic import BaseModel, Field
+from langchain_core.language_models.chat_models import BaseChatModel
+from langchain_core.embeddings import Embeddings
from hyperextract.graphs import AutoGraph
# ==============================================================================
@@ -52,15 +54,61 @@ KNOWLEDGE_GRAPH_EDGE_PROMPT = (
class KnowledgeGraph(AutoGraph[Entity, Relation]):
"""
- 通用事实知识图谱模板,适用于新闻报道、百科条目及传记。
+ 用于事实提取的基础知识图谱模板。
+
+ 该模板经过优化,适用于从新闻文章、传记和百科文本中提取实体(人物、地点、组织)及其事实交互。
+
+ 示例:
+ >>> from langchain_openai import ChatOpenAI, OpenAIEmbeddings
+ >>> llm = ChatOpenAI(model="gpt-4o")
+ >>> embedder = OpenAIEmbeddings()
+ >>> # 初始化模板
+ >>> kg = KnowledgeGraph(llm_client=llm, embedder=embedder)
+ >>> # 从文本中提取知识
+ >>> text = "苏轼出生于眉州眉山,是北宋著名的文学家。"
+ >>> kg.feed_text(text)
+ >>> print(kg.nodes) # 输出: [Entity(name='苏轼', ...), Entity(name='眉州眉山', ...)]
"""
- def __init__(self, **kwargs):
+ def __init__(
+ self,
+ llm_client: BaseChatModel,
+ embedder: Embeddings,
+ *,
+ extraction_mode: str = "one_stage",
+ chunk_size: int = 2048,
+ chunk_overlap: int = 256,
+ max_workers: int = 10,
+ verbose: bool = False,
+ **kwargs: Any
+ ):
+ """
+ 初始化 KnowledgeGraph 模板。
+
+ Args:
+ llm_client: 用于提取的语言模型客户端。
+ embedder: 用于实体去重和索引的嵌入模型。
+ extraction_mode: 提取策略:
+ - "one_stage": 同时提取节点和边(速度更快)。
+ - "two_stage": 先提取节点,再提取边(准确度更高)。
+ chunk_size: 每个文本块的最大字符数。
+ chunk_overlap: 文本块之间的重叠字符数。
+ max_workers: 并行处理的最大线程数。
+ verbose: 如果为 True,则打印详细的进度日志。
+ **kwargs: 传给 AutoGraph 构造函数的其他参数。
+ """
super().__init__(
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()}",
nodes_in_edge_extractor=lambda x: (x.source.strip(), x.target.strip()),
+ llm_client=llm_client,
+ embedder=embedder,
+ extraction_mode=extraction_mode,
+ chunk_size=chunk_size,
+ chunk_overlap=chunk_overlap,
+ max_workers=max_workers,
+ verbose=verbose,
prompt=KNOWLEDGE_GRAPH_PROMPT,
prompt_for_node_extraction=KNOWLEDGE_GRAPH_NODE_PROMPT,
prompt_for_edge_extraction=KNOWLEDGE_GRAPH_EDGE_PROMPT,
diff --git a/hyperextract/templates/zh/general/logic_graph.py b/hyperextract/templates/zh/general/logic_graph.py
index 85718a7..c992d4c 100644
--- a/hyperextract/templates/zh/general/logic_graph.py
+++ b/hyperextract/templates/zh/general/logic_graph.py
@@ -1,5 +1,7 @@
-from typing import List, Optional
+from typing import List, Optional, Any
from pydantic import BaseModel, Field
+from langchain_core.language_models.chat_models import BaseChatModel
+from langchain_core.embeddings import Embeddings
from hyperextract.graphs import AutoGraph
# ==============================================================================
@@ -48,16 +50,61 @@ LOGIC_GRAPH_EDGE_PROMPT = (
class LogicGraph(AutoGraph[LogicNode, LogicRelation]):
"""
- 用于分析论文推理、论证和因果链的模板。
- 适用于分析报告、辩论内容、科研推理及逻辑分析。
+ 用于分析推理、论证和因果链的模板。
+
+ 该模板旨在映射分析报告、科学论文或辩论记录的逻辑结构。它捕捉主张、证据以及它们之间的逻辑链接(支持/反驳)。
+
+ 示例:
+ >>> from langchain_openai import ChatOpenAI, OpenAIEmbeddings
+ >>> llm = ChatOpenAI(model="gpt-4o")
+ >>> embedder = OpenAIEmbeddings()
+ >>> # 初始化模板
+ >>> lg = LogicGraph(llm_client=llm, embedder=embedder)
+ >>> # 从文本中提取逻辑
+ >>> text = "由于碳含量上升,气候正在变暖。"
+ >>> lg.feed_text(text)
+ >>> print(lg.edges) # 输出显示:碳含量上升 --(支持)--> 气候正在变暖
"""
- def __init__(self, **kwargs):
+ def __init__(
+ self,
+ llm_client: BaseChatModel,
+ embedder: Embeddings,
+ *,
+ extraction_mode: str = "one_stage",
+ chunk_size: int = 2048,
+ chunk_overlap: int = 256,
+ max_workers: int = 10,
+ verbose: bool = False,
+ **kwargs: Any
+ ):
+ """
+ 初始化 LogicGraph 模板。
+
+ Args:
+ llm_client: 用于提取的语言模型客户端。
+ embedder: 用于逻辑点去重和索引的嵌入模型。
+ extraction_mode: 提取策略:
+ - "one_stage": 同时提取节点和边(速度更快)。
+ - "two_stage": 先提取节点,再提取边(准确度更高)。
+ chunk_size: 每个文本块的最大字符数。
+ chunk_overlap: 文本块之间的重叠字符数。
+ max_workers: 并行处理的最大线程数。
+ verbose: 如果为 True,则打印详细的进度日志。
+ **kwargs: 传给 AutoGraph 构造函数的其他参数。
+ """
super().__init__(
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()}",
nodes_in_edge_extractor=lambda x: (x.source.strip(), x.target.strip()),
+ llm_client=llm_client,
+ embedder=embedder,
+ extraction_mode=extraction_mode,
+ chunk_size=chunk_size,
+ chunk_overlap=chunk_overlap,
+ max_workers=max_workers,
+ verbose=verbose,
prompt=LOGIC_GRAPH_PROMPT,
prompt_for_node_extraction=LOGIC_GRAPH_NODE_PROMPT,
prompt_for_edge_extraction=LOGIC_GRAPH_EDGE_PROMPT,