Skip to content

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

Add templates for construction project management and cybersecurity analysis

yifanfeng9727c33fad0837
diff --git a/examples/auto_list_demo.py b/examples/auto_list_demo.py
index b8f74e1..19cbdd4 100644
--- a/examples/auto_list_demo.py
+++ b/examples/auto_list_demo.py
@@ -20,6 +20,7 @@ import dotenv
dotenv.load_dotenv()
+
# 定义单个新闻事件的 Schema
class NewsEvent(BaseModel):
"""新闻事件结构"""
@@ -214,7 +215,7 @@ def main():
chat_questions = [
"这些新闻中,哪些事件对科技行业的影响最大?",
- "请总结一下2024年科技领域的主要趋势。"
+ "请总结一下2024年科技领域的主要趋势。",
]
for q in chat_questions:
@@ -229,6 +230,12 @@ def main():
print("示例运行完成!")
print("=" * 80)
+ a_event_list.show(
+ item_label_extractor=lambda item: item.title,
+ top_k_for_search=3,
+ top_k_for_chat=3,
+ )
+
if __name__ == "__main__":
main()
diff --git a/examples/auto_set_demo.py b/examples/auto_set_demo.py
index 881307c..fa41a35 100644
--- a/examples/auto_set_demo.py
+++ b/examples/auto_set_demo.py
@@ -280,6 +280,12 @@ def main():
print("=" * 80)
+ monster_codex.show(
+ item_label_extractor=lambda x: x.name,
+ top_k_for_search=5,
+ top_k_for_chat=5
+ )
+
if __name__ == "__main__":
main()
diff --git a/hyperextract/templates/en/agriculture/crop_cycle.py b/hyperextract/templates/en/agriculture/crop_cycle.py
index c51a51d..f8c5310 100644
--- a/hyperextract/templates/en/agriculture/crop_cycle.py
+++ b/hyperextract/templates/en/agriculture/crop_cycle.py
@@ -118,3 +118,36 @@ class CropCycleGraph(AutoGraph[AgriEntity, AgriRelation]):
prompt_for_edge_extraction=CROP_CYCLE_EDGE_PROMPT,
**kwargs
)
+ def show(
+ self,
+ *,
+ top_k_nodes_for_search: int = 3,
+ top_k_edges_for_search: int = 3,
+ top_k_nodes_for_chat: int = 3,
+ top_k_edges_for_chat: int = 3,
+ ) -> 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: AgriEntity) -> str:
+ info = f" ({ node.category })" if getattr(node, "category", None) else ""
+ return f"{ node.name }{info}"
+
+ def edge_label_extractor(edge: AgriRelation) -> str:
+ info = f" ({ edge.specification })" if getattr(edge, "specification", None) else ""
+ return f"{ edge.source }{info}"
+
+ super().show(
+ node_label_extractor=node_label_extractor,
+ edge_label_extractor=edge_label_extractor,
+ top_k_nodes_for_search=top_k_nodes_for_search,
+ top_k_edges_for_search=top_k_edges_for_search,
+ top_k_nodes_for_chat=top_k_nodes_for_chat,
+ top_k_edges_for_chat=top_k_edges_for_chat,
+ )
diff --git a/hyperextract/templates/en/agriculture/livestock.py b/hyperextract/templates/en/agriculture/livestock.py
index 996f14e..399d99b 100644
--- a/hyperextract/templates/en/agriculture/livestock.py
+++ b/hyperextract/templates/en/agriculture/livestock.py
@@ -109,3 +109,35 @@ class LivestockGraph(AutoGraph[AnimalNode, BreedingRelation]):
prompt_for_edge_extraction=LIVESTOCK_EDGE_PROMPT,
**kwargs
)
+ def show(
+ self,
+ *,
+ top_k_nodes_for_search: int = 3,
+ top_k_edges_for_search: int = 3,
+ top_k_nodes_for_chat: int = 3,
+ top_k_edges_for_chat: int = 3,
+ ) -> 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: AnimalNode) -> str:
+ info = f" ({ node.category })" if getattr(node, "category", None) else ""
+ return f"{ node.name }{info}"
+
+ def edge_label_extractor(edge: BreedingRelation) -> str:
+ return f"{ edge.source }"
+
+ super().show(
+ node_label_extractor=node_label_extractor,
+ edge_label_extractor=edge_label_extractor,
+ top_k_nodes_for_search=top_k_nodes_for_search,
+ top_k_edges_for_search=top_k_edges_for_search,
+ top_k_nodes_for_chat=top_k_nodes_for_chat,
+ top_k_edges_for_chat=top_k_edges_for_chat,
+ )
diff --git a/hyperextract/templates/en/agriculture/supply_chain.py b/hyperextract/templates/en/agriculture/supply_chain.py
index c615bef..2d4f2eb 100644
--- a/hyperextract/templates/en/agriculture/supply_chain.py
+++ b/hyperextract/templates/en/agriculture/supply_chain.py
@@ -109,3 +109,36 @@ class AgriSupplyChain(AutoGraph[SupplyEntity, SupplyFlow]):
prompt_for_edge_extraction=AGRI_SUPPLY_CHAIN_EDGE_PROMPT,
**kwargs
)
+ def show(
+ self,
+ *,
+ top_k_nodes_for_search: int = 3,
+ top_k_edges_for_search: int = 3,
+ top_k_nodes_for_chat: int = 3,
+ top_k_edges_for_chat: int = 3,
+ ) -> 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: SupplyEntity) -> str:
+ info = f" ({ node.category })" if getattr(node, "category", None) else ""
+ return f"{ node.name }{info}"
+
+ def edge_label_extractor(edge: SupplyFlow) -> str:
+ info = f" ({ edge.specification })" if getattr(edge, "specification", None) else ""
+ return f"{ edge.source }{info}"
+
+ super().show(
+ node_label_extractor=node_label_extractor,
+ edge_label_extractor=edge_label_extractor,
+ top_k_nodes_for_search=top_k_nodes_for_search,
+ top_k_edges_for_search=top_k_edges_for_search,
+ top_k_nodes_for_chat=top_k_nodes_for_chat,
+ top_k_edges_for_chat=top_k_edges_for_chat,
+ )
diff --git a/hyperextract/templates/en/art/exhibition.py b/hyperextract/templates/en/art/exhibition.py
index 8a1f522..65ffe83 100644
--- a/hyperextract/templates/en/art/exhibition.py
+++ b/hyperextract/templates/en/art/exhibition.py
@@ -107,3 +107,35 @@ class ExhibitionGraph(AutoGraph[ExhibitionEntity, ExhibitionRelation]):
prompt_for_edge_extraction=EXHIBITION_EDGE_PROMPT,
**kwargs
)
+ def show(
+ self,
+ *,
+ top_k_nodes_for_search: int = 3,
+ top_k_edges_for_search: int = 3,
+ top_k_nodes_for_chat: int = 3,
+ top_k_edges_for_chat: int = 3,
+ ) -> 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: ExhibitionEntity) -> str:
+ info = f" ({ node.type })" if getattr(node, "type", None) else ""
+ return f"{ node.name }{info}"
+
+ def edge_label_extractor(edge: ExhibitionRelation) -> str:
+ return f"{ edge.source }"
+
+ super().show(
+ node_label_extractor=node_label_extractor,
+ edge_label_extractor=edge_label_extractor,
+ top_k_nodes_for_search=top_k_nodes_for_search,
+ top_k_edges_for_search=top_k_edges_for_search,
+ top_k_nodes_for_chat=top_k_nodes_for_chat,
+ top_k_edges_for_chat=top_k_edges_for_chat,
+ )
diff --git a/hyperextract/templates/en/art/movement.py b/hyperextract/templates/en/art/movement.py
index e479a90..063997a 100644
--- a/hyperextract/templates/en/art/movement.py
+++ b/hyperextract/templates/en/art/movement.py
@@ -108,3 +108,35 @@ class ArtMovementGraph(AutoGraph[MovementEntity, ArtInfluenceEdge]):
prompt_for_edge_extraction=MOVEMENT_EDGE_PROMPT,
**kwargs
)
+ def show(
+ self,
+ *,
+ top_k_nodes_for_search: int = 3,
+ top_k_edges_for_search: int = 3,
+ top_k_nodes_for_chat: int = 3,
+ top_k_edges_for_chat: int = 3,
+ ) -> 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: MovementEntity) -> str:
+ info = f" ({ node.category })" if getattr(node, "category", None) else ""
+ return f"{ node.name }{info}"
+
+ def edge_label_extractor(edge: ArtInfluenceEdge) -> str:
+ return f"{ edge.source }"
+
+ super().show(
+ node_label_extractor=node_label_extractor,
+ edge_label_extractor=edge_label_extractor,
+ top_k_nodes_for_search=top_k_nodes_for_search,
+ top_k_edges_for_search=top_k_edges_for_search,
+ top_k_nodes_for_chat=top_k_nodes_for_chat,
+ top_k_edges_for_chat=top_k_edges_for_chat,
+ )
diff --git a/hyperextract/templates/en/art/provenance.py b/hyperextract/templates/en/art/provenance.py
index d7271fa..8c65bf4 100644
--- a/hyperextract/templates/en/art/provenance.py
+++ b/hyperextract/templates/en/art/provenance.py
@@ -109,3 +109,36 @@ class ProvenanceGraph(AutoGraph[ArtEntity, ProvenanceEdge]):
prompt_for_edge_extraction=PROVENANCE_EDGE_PROMPT,
**kwargs
)
+ def show(
+ self,
+ *,
+ top_k_nodes_for_search: int = 3,
+ top_k_edges_for_search: int = 3,
+ top_k_nodes_for_chat: int = 3,
+ top_k_edges_for_chat: int = 3,
+ ) -> 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: ArtEntity) -> str:
+ info = f" ({ node.category })" if getattr(node, "category", None) else ""
+ return f"{ node.name }{info}"
+
+ def edge_label_extractor(edge: ProvenanceEdge) -> str:
+ info = f" ({ edge.specification })" if getattr(edge, "specification", None) else ""
+ return f"{ edge.source }{info}"
+
+ super().show(
+ node_label_extractor=node_label_extractor,
+ edge_label_extractor=edge_label_extractor,
+ top_k_nodes_for_search=top_k_nodes_for_search,
+ top_k_edges_for_search=top_k_edges_for_search,
+ top_k_nodes_for_chat=top_k_nodes_for_chat,
+ top_k_edges_for_chat=top_k_edges_for_chat,
+ )
diff --git a/hyperextract/templates/en/automotive/car_spec.py b/hyperextract/templates/en/automotive/car_spec.py
index e9fd603..70a20c6 100644
--- a/hyperextract/templates/en/automotive/car_spec.py
+++ b/hyperextract/templates/en/automotive/car_spec.py
@@ -107,3 +107,35 @@ class CarSpecGraph(AutoGraph[CarComponent, CarSystemRelation]):
prompt_for_edge_extraction=CAR_SPEC_EDGE_PROMPT,
**kwargs
)
+ def show(
+ self,
+ *,
+ top_k_nodes_for_search: int = 3,
+ top_k_edges_for_search: int = 3,
+ top_k_nodes_for_chat: int = 3,
+ top_k_edges_for_chat: int = 3,
+ ) -> 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: CarComponent) -> str:
+ info = f" ({ node.category })" if getattr(node, "category", None) else ""
+ return f"{ node.name }{info}"
+
+ def edge_label_extractor(edge: CarSystemRelation) -> str:
+ return f"{ edge.relation_type }"
+
+ super().show(
+ node_label_extractor=node_label_extractor,
+ edge_label_extractor=edge_label_extractor,
+ top_k_nodes_for_search=top_k_nodes_for_search,
+ top_k_edges_for_search=top_k_edges_for_search,
+ top_k_nodes_for_chat=top_k_nodes_for_chat,
+ top_k_edges_for_chat=top_k_edges_for_chat,
+ )
diff --git a/hyperextract/templates/en/automotive/maintenance.py b/hyperextract/templates/en/automotive/maintenance.py
index 4a63d24..603c387 100644
--- a/hyperextract/templates/en/automotive/maintenance.py
+++ b/hyperextract/templates/en/automotive/maintenance.py
@@ -103,3 +103,35 @@ class CarMaintenanceGraph(AutoGraph[MaintenanceItem, MaintenanceSequence]):
prompt_for_edge_extraction=MAINTENANCE_EDGE_PROMPT,
**kwargs
)
+ def show(
+ self,
+ *,
+ top_k_nodes_for_search: int = 3,
+ top_k_edges_for_search: int = 3,
+ top_k_nodes_for_chat: int = 3,
+ top_k_edges_for_chat: int = 3,
+ ) -> 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: MaintenanceItem) -> str:
+ info = f" ({ node.category })" if getattr(node, "category", None) else ""
+ return f"{ node.item_name }{info}"
+
+ def edge_label_extractor(edge: MaintenanceSequence) -> str:
+ return f"{ edge.action }"
+
+ super().show(
+ node_label_extractor=node_label_extractor,
+ edge_label_extractor=edge_label_extractor,
+ top_k_nodes_for_search=top_k_nodes_for_search,
+ top_k_edges_for_search=top_k_edges_for_search,
+ top_k_nodes_for_chat=top_k_nodes_for_chat,
+ top_k_edges_for_chat=top_k_edges_for_chat,
+ )
diff --git a/hyperextract/templates/en/automotive/manufacturing.py b/hyperextract/templates/en/automotive/manufacturing.py
index 6f332a7..5304cdc 100644
--- a/hyperextract/templates/en/automotive/manufacturing.py
+++ b/hyperextract/templates/en/automotive/manufacturing.py
@@ -107,3 +107,35 @@ class AutomotiveMfgGraph(AutoGraph[ManufacturingEntity, SupplyChainRelation]):
prompt_for_edge_extraction=MFG_EDGE_PROMPT,
**kwargs
)
+ def show(
+ self,
+ *,
+ top_k_nodes_for_search: int = 3,
+ top_k_edges_for_search: int = 3,
+ top_k_nodes_for_chat: int = 3,
+ top_k_edges_for_chat: int = 3,
+ ) -> 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: ManufacturingEntity) -> str:
+ info = f" ({ node.category })" if getattr(node, "category", None) else ""
+ return f"{ node.name }{info}"
+
+ def edge_label_extractor(edge: SupplyChainRelation) -> str:
+ return f"{ edge.relation_type }"
+
+ super().show(
+ node_label_extractor=node_label_extractor,
+ edge_label_extractor=edge_label_extractor,
+ top_k_nodes_for_search=top_k_nodes_for_search,
+ top_k_edges_for_search=top_k_edges_for_search,
+ top_k_nodes_for_chat=top_k_nodes_for_chat,
+ top_k_edges_for_chat=top_k_edges_for_chat,
+ )
diff --git a/hyperextract/templates/en/biology/metabolic.py b/hyperextract/templates/en/biology/metabolic.py
index 9a4a75f..dff01c2 100644
--- a/hyperextract/templates/en/biology/metabolic.py
+++ b/hyperextract/templates/en/biology/metabolic.py
@@ -8,26 +8,43 @@ from hyperextract.types import AutoHypergraph
# 1. Schema Definitions
# ==============================================================================
+
class MetaboliteNode(BaseModel):
"""
Any physical substance (substrate or product) involved in a metabolic reaction.
"""
- name: str = Field(description="Chemical or common name of the molecule (e.g., 'Pyruvate').")
+
+ name: str = Field(
+ description="Chemical or common name of the molecule (e.g., 'Pyruvate')."
+ )
formula: Optional[str] = Field(None, description="Chemical formula/notation.")
- charge: Optional[int] = Field(None, description="Net electrical charge of the metabolite.")
- compartment: Optional[str] = Field(description="Cellular location: 'Cytosol', 'Mitochondria', 'Extracellular'.")
+ charge: Optional[int] = Field(
+ None, description="Net electrical charge of the metabolite."
+ )
+ compartment: Optional[str] = Field(
+ description="Cellular location: 'Cytosol', 'Mitochondria', 'Extracellular'."
+ )
+
class MetabolicReactionHyperedge(BaseModel):
"""
A multi-reagent biochemical reaction where multiple substrates yield multiple products.
"""
- reaction_id: str = Field(description="Unique identifier or EC number (e.g., 'EC 1.1.1.1').")
+
+ reaction_id: str = Field(
+ description="Unique identifier or EC number (e.g., 'EC 1.1.1.1')."
+ )
substrates: List[str] = Field(description="List of substrate metabolite names.")
products: List[str] = Field(description="List of product metabolite names.")
- enzymes: List[str] = Field(default_factory=list, description="List of catalysts (enzymes) involved.")
- stoichiometry: Optional[str] = Field(None, description="Balancing coefficients if mentioned in text.")
+ enzymes: List[str] = Field(
+ default_factory=list, description="List of catalysts (enzymes) involved."
+ )
+ stoichiometry: Optional[str] = Field(
+ None, description="Balancing coefficients if mentioned in text."
+ )
reversibility: bool = Field(True, description="Whether the reaction is reversible.")
+
# ==============================================================================
# 2. Prompts
# ==============================================================================
@@ -62,10 +79,12 @@ METABOLIC_EDGE_PROMPT = (
# 3. Template Class
# ==============================================================================
+
class MetabolicHypergraph(AutoHypergraph[MetaboliteNode, MetabolicReactionHyperedge]):
"""
Complex hypergraph template for metabolic pathways involving multi-substrate/multi-product reactions.
"""
+
def __init__(
self,
llm_client: BaseChatModel,
@@ -76,14 +95,16 @@ class MetabolicHypergraph(AutoHypergraph[MetaboliteNode, MetabolicReactionHypere
chunk_overlap: int = 256,
max_workers: int = 10,
verbose: bool = False,
- **kwargs: Any
+ **kwargs: Any,
):
super().__init__(
node_schema=MetaboliteNode,
edge_schema=MetabolicReactionHyperedge,
node_key_extractor=lambda x: x.name.strip(),
edge_key_extractor=lambda x: x.reaction_id.strip(),
- nodes_in_edge_extractor=lambda x: tuple(set(x.substrates + x.products + x.enzymes)),
+ nodes_in_edge_extractor=lambda x: tuple(
+ set(x.substrates + x.products + x.enzymes)
+ ),
llm_client=llm_client,
embedder=embedder,
extraction_mode=extraction_mode,
@@ -94,5 +115,38 @@ class MetabolicHypergraph(AutoHypergraph[MetaboliteNode, MetabolicReactionHypere
prompt=METABOLIC_CONSOLIDATED_PROMPT,
prompt_for_node_extraction=METABOLIC_NODE_PROMPT,
prompt_for_edge_extraction=METABOLIC_EDGE_PROMPT,
- **kwargs
+ **kwargs,
+ )
+
+ def show(
+ self,
+ *,
+ top_k_nodes_for_search: int = 3,
+ top_k_edges_for_search: int = 3,
+ top_k_nodes_for_chat: int = 3,
+ top_k_edges_for_chat: int = 3,
+ ) -> 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: MetaboliteNode) -> str:
+ return f"{node.name}"
+
+ def edge_label_extractor(edge: MetabolicReactionHyperedge) -> str:
+ return f"{edge.reaction_id}"
+
+ super().show(
+ node_label_extractor=node_label_extractor,
+ edge_label_extractor=edge_label_extractor,
+ top_k_nodes_for_search=top_k_nodes_for_search,
+ top_k_edges_for_search=top_k_edges_for_search,
+ top_k_nodes_for_chat=top_k_nodes_for_chat,
+ top_k_edges_for_chat=top_k_edges_for_chat,
)
diff --git a/hyperextract/templates/en/biology/network.py b/hyperextract/templates/en/biology/network.py
index a052f84..50699c4 100644
--- a/hyperextract/templates/en/biology/network.py
+++ b/hyperextract/templates/en/biology/network.py
@@ -96,3 +96,35 @@ class BiologicalNetwork(AutoGraph[BioEntityNode, BioInteractionEdge]):
prompt_for_edge_extraction=NETWORK_EDGE_PROMPT,
**kwargs
)
+ def show(
+ self,
+ *,
+ top_k_nodes_for_search: int = 3,
+ top_k_edges_for_search: int = 3,
+ top_k_nodes_for_chat: int = 3,
+ top_k_edges_for_chat: int = 3,
+ ) -> 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: BioEntityNode) -> str:
+ info = f" ({ node.category })" if getattr(node, "category", None) else ""
+ return f"{ node.name }{info}"
+
+ def edge_label_extractor(edge: BioInteractionEdge) -> str:
+ return f"{ edge.interaction_type }"
+
+ super().show(
+ node_label_extractor=node_label_extractor,
+ edge_label_extractor=edge_label_extractor,
+ top_k_nodes_for_search=top_k_nodes_for_search,
+ top_k_edges_for_search=top_k_edges_for_search,
+ top_k_nodes_for_chat=top_k_nodes_for_chat,
+ top_k_edges_for_chat=top_k_edges_for_chat,
+ )
diff --git a/hyperextract/templates/en/biology/taxonomy.py b/hyperextract/templates/en/biology/taxonomy.py
index cd15fda..94ae1ea 100644
--- a/hyperextract/templates/en/biology/taxonomy.py
+++ b/hyperextract/templates/en/biology/taxonomy.py
@@ -8,23 +8,43 @@ from hyperextract.types import AutoGraph
# 1. Schema Definitions
# ==============================================================================
+
class TaxonNode(BaseModel):
"""
Represents a biological taxon in the Tree of Life.
"""
- name: str = Field(description="Scientific name (Latin) or standardized common name of the organism/group.")
- rank: str = Field(description="Taxonomic rank: 'Domain', 'Kingdom', 'Phylum', 'Class', 'Order', 'Family', 'Genus', 'Species'.")
- common_name: Optional[str] = Field(None, description="General name used in non-scientific contexts.")
- description: Optional[str] = Field(None, description="Brief biological characteristics, habitat, or distinguishing traits.")
+
+ name: str = Field(
+ description="Scientific name (Latin) or standardized common name of the organism/group."
+ )
+ rank: str = Field(
+ description="Taxonomic rank: 'Domain', 'Kingdom', 'Phylum', 'Class', 'Order', 'Family', 'Genus', 'Species'."
+ )
+ common_name: Optional[str] = Field(
+ None, description="General name used in non-scientific contexts."
+ )
+ description: Optional[str] = Field(
+ None,
+ description="Brief biological characteristics, habitat, or distinguishing traits.",
+ )
+
class TaxonomyRelation(BaseModel):
"""
A direct hierarchical relationship between two taxons.
"""
+
source: str = Field(description="The parent taxon (e.g., 'Mammalia').")
target: str = Field(description="The child taxon (e.g., 'Primates').")
- type: str = Field("parent_of", description="Relationship type: 'parent_of', 'subspecies_of', 'classified_under'.")
- evidence: Optional[str] = Field(None, description="Reference to phenotypic or genomic data justifying the classification.")
+ type: str = Field(
+ "parent_of",
+ description="Relationship type: 'parent_of', 'subspecies_of', 'classified_under'.",
+ )
+ evidence: Optional[str] = Field(
+ None,
+ description="Reference to phenotypic or genomic data justifying the classification.",
+ )
+
# ==============================================================================
# 2. Prompts
@@ -60,10 +80,12 @@ TAXONOMY_EDGE_PROMPT = (
# 3. Template Class
# ==============================================================================
+
class TaxonomyGraph(AutoGraph[TaxonNode, TaxonomyRelation]):
"""
High-fidelity template for biological classification and phylogenetic hierarchies.
"""
+
def __init__(
self,
llm_client: BaseChatModel,
@@ -74,13 +96,15 @@ class TaxonomyGraph(AutoGraph[TaxonNode, TaxonomyRelation]):
chunk_overlap: int = 256,
max_workers: int = 10,
verbose: bool = False,
- **kwargs: Any
+ **kwargs: Any,
):
super().__init__(
node_schema=TaxonNode,
edge_schema=TaxonomyRelation,
node_key_extractor=lambda x: x.name.strip(),
- edge_key_extractor=lambda x: f"{x.source.strip()}--({x.type})-->{x.target.strip()}",
+ edge_key_extractor=lambda x: (
+ f"{x.source.strip()}--({x.type})-->{x.target.strip()}"
+ ),
nodes_in_edge_extractor=lambda x: (x.source.strip(), x.target.strip()),
llm_client=llm_client,
embedder=embedder,
@@ -92,5 +116,38 @@ class TaxonomyGraph(AutoGraph[TaxonNode, TaxonomyRelation]):
prompt=TAXONOMY_CONSOLIDATED_PROMPT,
prompt_for_node_extraction=TAXONOMY_NODE_PROMPT,
prompt_for_edge_extraction=TAXONOMY_EDGE_PROMPT,
- **kwargs
+ **kwargs,
+ )
+
+ def show(
+ self,
+ *,
+ top_k_nodes_for_search: int = 3,
+ top_k_edges_for_search: int = 3,
+ top_k_nodes_for_chat: int = 3,
+ top_k_edges_for_chat: int = 3,
+ ) -> 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: TaxonNode) -> str:
+ return f"{node.name}"
+
+ def edge_label_extractor(edge: TaxonomyRelation) -> str:
+ return f"{edge.type}"
+
+ super().show(
+ node_label_extractor=node_label_extractor,
+ edge_label_extractor=edge_label_extractor,
+ top_k_nodes_for_search=top_k_nodes_for_search,
+ top_k_edges_for_search=top_k_edges_for_search,
+ top_k_nodes_for_chat=top_k_nodes_for_chat,
+ top_k_edges_for_chat=top_k_edges_for_chat,
)
diff --git a/hyperextract/templates/en/business/ecosystem.py b/hyperextract/templates/en/business/ecosystem.py
index 150a350..789d3ca 100644
--- a/hyperextract/templates/en/business/ecosystem.py
+++ b/hyperextract/templates/en/business/ecosystem.py
@@ -115,3 +115,34 @@ class MarketEcosystemHyper(AutoHypergraph[IndustryParticipant, CollectiveAllianc
prompt_for_edge_extraction=ECOSYSTEM_EDGE_PROMPT,
**kwargs,
)
+ def show(
+ self,
+ *,
+ top_k_nodes_for_search: int = 3,
+ top_k_edges_for_search: int = 3,
+ top_k_nodes_for_chat: int = 3,
+ top_k_edges_for_chat: int = 3,
+ ) -> 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: IndustryParticipant) -> str:
+ return f"{ node.name }"
+
+ def edge_label_extractor(edge: CollectiveAlliance) -> str:
+ return f"{ edge.alliance_id }"
+
+ super().show(
+ node_label_extractor=node_label_extractor,
+ edge_label_extractor=edge_label_extractor,
+ top_k_nodes_for_search=top_k_nodes_for_search,
+ top_k_edges_for_search=top_k_edges_for_search,
+ top_k_nodes_for_chat=top_k_nodes_for_chat,
+ top_k_edges_for_chat=top_k_edges_for_chat,
+ )
diff --git a/hyperextract/templates/en/business/equity.py b/hyperextract/templates/en/business/equity.py
index 930c014..84109b0 100644
--- a/hyperextract/templates/en/business/equity.py
+++ b/hyperextract/templates/en/business/equity.py
@@ -122,3 +122,35 @@ class EquityStructureGraph(AutoGraph[StakeholderNode, OwnershipRelation]):
prompt_for_edge_extraction=EQUITY_EDGE_PROMPT,
**kwargs,
)
+ def show(
+ self,
+ *,
+ top_k_nodes_for_search: int = 3,
+ top_k_edges_for_search: int = 3,
+ top_k_nodes_for_chat: int = 3,
+ top_k_edges_for_chat: int = 3,
+ ) -> 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: StakeholderNode) -> str:
+ info = f" ({ node.type })" if getattr(node, "type", None) else ""
+ return f"{ node.name }{info}"
+
+ def edge_label_extractor(edge: OwnershipRelation) -> str:
+ return f"{edge.control_type}"
+
+ super().show(
+ node_label_extractor=node_label_extractor,
+ edge_label_extractor=edge_label_extractor,
+ top_k_nodes_for_search=top_k_nodes_for_search,
+ top_k_edges_for_search=top_k_edges_for_search,
+ top_k_nodes_for_chat=top_k_nodes_for_chat,
+ top_k_edges_for_chat=top_k_edges_for_chat,
+ )
diff --git a/hyperextract/templates/en/business/strategy.py b/hyperextract/templates/en/business/strategy.py
index c4988b2..8171d6a 100644
--- a/hyperextract/templates/en/business/strategy.py
+++ b/hyperextract/templates/en/business/strategy.py
@@ -120,3 +120,34 @@ class StrategicChainGraph(AutoTemporalGraph[StrategicNode, StrategicPivot]):
prompt_for_edge_extraction=STRATEGY_EDGE_PROMPT,
**kwargs,
)
+ def show(
+ self,
+ *,
+ top_k_nodes_for_search: int = 3,
+ top_k_edges_for_search: int = 3,
+ top_k_nodes_for_chat: int = 3,
+ top_k_edges_for_chat: int = 3,
+ ) -> 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: StrategicNode) -> str:
+ return f"{node.name}"
+
+ def edge_label_extractor(edge: StrategicPivot) -> str:
+ return f"{edge.action}"
+
+ super().show(
+ node_label_extractor=node_label_extractor,
+ edge_label_extractor=edge_label_extractor,
+ top_k_nodes_for_search=top_k_nodes_for_search,
+ top_k_edges_for_search=top_k_edges_for_search,
+ top_k_nodes_for_chat=top_k_nodes_for_chat,
+ top_k_edges_for_chat=top_k_edges_for_chat,
+ )
diff --git a/hyperextract/templates/en/chemistry/molecular.py b/hyperextract/templates/en/chemistry/molecular.py
index 370e3a1..37e8457 100644
--- a/hyperextract/templates/en/chemistry/molecular.py
+++ b/hyperextract/templates/en/chemistry/molecular.py
@@ -117,3 +117,36 @@ class MolecularStructureGraph(AutoSpatialGraph[AtomNode, ChemicalBond]):
prompt_for_edge_extraction=MOLECULAR_EDGE_PROMPT,
**kwargs,
)
+
+ def show(
+ self,
+ *,
+ top_k_nodes_for_search: int = 3,
+ top_k_edges_for_search: int = 3,
+ top_k_nodes_for_chat: int = 3,
+ top_k_edges_for_chat: int = 3,
+ ) -> 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: AtomNode) -> str:
+ return f"{node.name}"
+
+ def edge_label_extractor(edge: ChemicalBond) -> str:
+ return f"{edge.bond_type}"
+
+ super().show(
+ node_label_extractor=node_label_extractor,
+ edge_label_extractor=edge_label_extractor,
+ top_k_nodes_for_search=top_k_nodes_for_search,
+ top_k_edges_for_search=top_k_edges_for_search,
+ top_k_nodes_for_chat=top_k_nodes_for_chat,
+ top_k_edges_for_chat=top_k_edges_for_chat,
+ )
diff --git a/hyperextract/templates/en/chemistry/protocol.py b/hyperextract/templates/en/chemistry/protocol.py
index 5b657fc..eb11ec5 100644
--- a/hyperextract/templates/en/chemistry/protocol.py
+++ b/hyperextract/templates/en/chemistry/protocol.py
@@ -122,3 +122,36 @@ class LabProtocolTemporal(AutoTemporalGraph[LabOperation, ProtocolTransition]):
prompt_for_edge_extraction=PROTOCOL_EDGE_PROMPT,
**kwargs,
)
+
+ def show(
+ self,
+ *,
+ top_k_nodes_for_search: int = 3,
+ top_k_edges_for_search: int = 3,
+ top_k_nodes_for_chat: int = 3,
+ top_k_edges_for_chat: int = 3,
+ ) -> 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: LabOperation) -> str:
+ return f"{node.op_id}"
+
+ def edge_label_extractor(edge: ProtocolTransition) -> str:
+ return f"{edge.transition_id}"
+
+ super().show(
+ node_label_extractor=node_label_extractor,
+ edge_label_extractor=edge_label_extractor,
+ top_k_nodes_for_search=top_k_nodes_for_search,
+ top_k_edges_for_search=top_k_edges_for_search,
+ top_k_nodes_for_chat=top_k_nodes_for_chat,
+ top_k_edges_for_chat=top_k_edges_for_chat,
+ )
diff --git a/hyperextract/templates/en/chemistry/reaction.py b/hyperextract/templates/en/chemistry/reaction.py
index 2da5829..254f8b6 100644
--- a/hyperextract/templates/en/chemistry/reaction.py
+++ b/hyperextract/templates/en/chemistry/reaction.py
@@ -124,3 +124,37 @@ class ChemicalReactionHyper(AutoHypergraph[ChemicalEntity, ReactionEvent]):
prompt_for_edge_extraction=REACTION_EDGE_PROMPT,
**kwargs,
)
+
+ def show(
+ self,
+ *,
+ top_k_nodes_for_search: int = 3,
+ top_k_edges_for_search: int = 3,
+ top_k_nodes_for_chat: int = 3,
+ top_k_edges_for_chat: int = 3,
+ ) -> 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: ChemicalEntity) -> str:
+ info = f" ({node.phase})" if getattr(node, "phase", None) else ""
+ return f"{node.name}{info}"
+
+ def edge_label_extractor(edge: ReactionEvent) -> str:
+ return f"{edge.reaction_id}"
+
+ super().show(
+ node_label_extractor=node_label_extractor,
+ edge_label_extractor=edge_label_extractor,
+ top_k_nodes_for_search=top_k_nodes_for_search,
+ top_k_edges_for_search=top_k_edges_for_search,
+ top_k_nodes_for_chat=top_k_nodes_for_chat,
+ top_k_edges_for_chat=top_k_edges_for_chat,
+ )
diff --git a/hyperextract/templates/en/construction/project_timeline.py b/hyperextract/templates/en/construction/project_timeline.py
new file mode 100644
index 0000000..247086a
--- /dev/null
+++ b/hyperextract/templates/en/construction/project_timeline.py
@@ -0,0 +1,185 @@
+from typing import Optional, Any
+from pydantic import BaseModel, Field
+from langchain_core.language_models.chat_models import BaseChatModel
+from langchain_core.embeddings import Embeddings
+from hyperextract.types import AutoTemporalGraph
+
+# ==============================================================================
+# 1. Schema Definitions
+# ==============================================================================
+
+
+class MilestoneTaskNode(BaseModel):
+ """
+ A specific task, milestone, or phase in a construction project schedule.
+ """
+
+ task_name: str = Field(
+ description="Operational name of the task (e.g., 'Foundation Pouring')."
+ )
+ phase: str = Field(
+ description="Project phase: 'Pre-con', 'Substructure', 'Superstructure', 'Fit-out', 'Commissioning'."
+ )
+ duration_estimate: Optional[str] = Field(
+ None, description="Estimated time required (e.g., '14 days', '2 months')."
+ )
+ responsible_party: Optional[str] = Field(
+ None, description="Subcontractor or team lead in charge."
+ )
+
+
+class TemporalDependency(BaseModel):
+ """
+ A time-based link indicating task sequencing.
+ """
+
+ source: str = Field(description="The predecessor task.")
+ target: str = Field(description="The successor task.")
+ timestamp: str = Field(
+ description="Temporal marker or sequence ID (e.g., 'Phase 1', 'Step 2', or a specific date/time)."
+ )
+ dependency_type: str = Field(
+ "Finish-to-Start",
+ description="Type: 'Finish-to-Start (FS)', 'Start-to-Start (SS)', 'Finish-to-Finish (FF)', 'Start-to-Finish (SF)'.",
+ )
+ lag_time: Optional[str] = Field(
+ None,
+ description="Wait time between tasks (e.g., '3 days for concrete curing').",
+ )
+
+
+# ==============================================================================
+# 2. Prompts
+# ==============================================================================
+
+CONSTRUCTION_TEMPORAL_CONSOLIDATED_PROMPT = (
+ "You are a Senior Project Planner and Scheduler (P6/MS Project expert). Extract the construction project timeline.\n\n"
+ "Rules:\n"
+ "- Identify granular tasks and major milestones as Nodes.\n"
+ "- Establish clear temporal dependencies (Edges) using industry-standard logic (FS, SS, etc.).\n"
+ "- Capture durations and curing/lag times where mentioned.\n"
+ "- Focus on the critical path and sequencing logic."
+)
+
+CONSTRUCTION_TEMPORAL_NODE_PROMPT = (
+ "Identify all tasks, scheduled activities, and project milestones.\n\n"
+ "Rules:\n"
+ "- Extract action-oriented task names.\n"
+ "- Group tasks into project phases and identify responsible parties.\n"
+ "- DO NOT identify sequences or dependencies yet."
+)
+
+CONSTRUCTION_TEMPORAL_EDGE_PROMPT = (
+ "Link construction tasks based on their logical sequence in time.\n\n"
+ "Rules:\n"
+ "- Define the predecessor and successor for each link.\n"
+ "- Classify the link type (FS is default if not specified).\n"
+ "- Identify any required lag times (e.g., waiting for inspection or drying).\n"
+ "- Only link tasks from the provided list."
+)
+
+# ==============================================================================
+# 3. Template Class
+# ==============================================================================
+
+
+class ProjectSchedulingTimeline(
+ AutoTemporalGraph[MilestoneTaskNode, TemporalDependency]
+):
+ """
+ Template for extracting project schedules and Critical Path Method (CPM) networks.
+
+ Ideal for schedule risk analysis, delay tracking, and progress monitoring.
+
+ Example:
+ >>> from langchain_openai import ChatOpenAI, OpenAIEmbeddings
+ >>> timeline = ProjectSchedulingTimeline(llm_client=llm, embedder=embedder)
+ >>> timeline.feed_text("Excavation must finish before the foundation slab can be poured.")
+ >>> print(timeline.nodes) # Access tasks and milestones
+ >>> print(timeline.edges) # Access temporal dependencies
+ """
+
+ 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 Project Scheduling Timeline template.
+
+ Args:
+ llm_client (BaseChatModel): The language model client used for schedule extraction.
+ embedder (Embeddings): The embedding model used for task and dependency deduplication.
+ extraction_mode (str, optional): 'one_stage' for joint extraction,
+ 'two_stage' for separate passes. Defaults to "one_stage".
+ chunk_size (int, optional): Maximum characters per text chunk. Defaults to 2048.
+ chunk_overlap (int, optional): Overlap between chunks. Defaults to 256.
+ max_workers (int, optional): Parallel processing workers. Defaults to 10.
+ verbose (bool, optional): If True, enables progress logging. Defaults to False.
+ **kwargs (Any): Additional parameters for the AutoTemporalGraph base class.
+ """
+ super().__init__(
+ node_schema=MilestoneTaskNode,
+ edge_schema=TemporalDependency,
+ node_key_extractor=lambda x: x.task_name.strip().lower(),
+ edge_key_extractor=lambda x: (
+ f"{x.source.strip().lower()}--[{x.dependency_type}]-->{x.target.strip().lower()}"
+ ),
+ nodes_in_edge_extractor=lambda x: (
+ x.source.strip().lower(),
+ x.target.strip().lower(),
+ ),
+ time_in_edge_extractor=lambda x: x.timestamp.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=CONSTRUCTION_TEMPORAL_CONSOLIDATED_PROMPT,
+ prompt_for_node_extraction=CONSTRUCTION_TEMPORAL_NODE_PROMPT,
+ prompt_for_edge_extraction=CONSTRUCTION_TEMPORAL_EDGE_PROMPT,
+ **kwargs,
+ )
+
+ def show(
+ self,
+ *,
+ top_k_nodes_for_search: int = 3,
+ top_k_edges_for_search: int = 3,
+ top_k_nodes_for_chat: int = 3,
+ top_k_edges_for_chat: int = 3,
+ ) -> 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: MilestoneTaskNode) -> str:
+ info = f" ({node.phase})" if getattr(node, "phase", None) else ""
+ return f"{node.task_name}{info}"
+
+ def edge_label_extractor(edge: TemporalDependency) -> str:
+ return f"{edge.dependency_type}"
+
+ super().show(
+ node_label_extractor=node_label_extractor,
+ edge_label_extractor=edge_label_extractor,
+ top_k_nodes_for_search=top_k_nodes_for_search,
+ top_k_edges_for_search=top_k_edges_for_search,
+ top_k_nodes_for_chat=top_k_nodes_for_chat,
+ top_k_edges_for_chat=top_k_edges_for_chat,
+ )
diff --git a/hyperextract/templates/en/construction/resource_registry.py b/hyperextract/templates/en/construction/resource_registry.py
new file mode 100644
index 0000000..8d23f05
--- /dev/null
+++ b/hyperextract/templates/en/construction/resource_registry.py
@@ -0,0 +1,108 @@
+from typing import Optional, Any
+from pydantic import BaseModel, Field
+from langchain_core.language_models.chat_models import BaseChatModel
+from langchain_core.embeddings import Embeddings
+from hyperextract.types import AutoSet
+
+# ==============================================================================
+# 1. Schema Definitions
+# ==============================================================================
+
+class ConstructionResource(BaseModel):
+ """
+ A specific asset, material, or labor category assigned to a construction project.
+ """
+ resource_name: str = Field(description="Name of the resource (e.g., 'Tower Crane TC-01', 'Fly Ash Concrete').")
+ category: str = Field(description="Category: 'Heavy Machinery', 'Bulk Material', 'Skilled Labor', 'Fixed Equipment', 'Consumables'.")
+ quantity: Optional[str] = Field(None, description="Current quantity or capacity on site.")
+ supplier_or_team: Optional[str] = Field(None, description="The entity providing or managing this resource.")
+ status: str = Field("Active", description="Current operational status: 'Active', 'Idle', 'Maintenance', 'Depleted'.")
+
+# ==============================================================================
+# 2. Prompts
+# ==============================================================================
+
+CONSTRUCTION_RESOURCE_PROMPT = (
+ "You are a Construction Logistics and Procurement Manager. Extract all project resources and assets.\n\n"
+ "Rules:\n"
+ "- Identify machinery, materials, and labor types.\n"
+ "- Capture specific quantities and supplier names if mentioned.\n"
+ "- Deduplicate similar items (e.g., 'Excavator A' and 'The primary excavator' should be merged if referring to the same unit)."
+)
+
+# ==============================================================================
+# 3. Template Class
+# ==============================================================================
+
+class ResourceRegistrySet(AutoSet[ConstructionResource]):
+ """
+ Template for managing a consolidated site asset and material registry.
+
+ Uses AutoSet for automatic deduplication and merging of multi-chunk resource mentions.
+
+ Example:
+ >>> from langchain_openai import ChatOpenAI, OpenAIEmbeddings
+ >>> registry = ResourceRegistrySet(llm_client=llm, embedder=embedder)
+ >>> registry.feed_text("We have two 50-ton cranes from Liebherr on site.")
+ >>> registry.feed_text("The Liebherr cranes are currently being serviced.")
+ >>> print(registry.items) # Mentions will be merged into a single Tower Crane resource.
+ """
+
+ def __init__(
+ self,
+ llm_client: BaseChatModel,
+ embedder: Embeddings,
+ *,
+ chunk_size: int = 2048,
+ chunk_overlap: int = 256,
+ max_workers: int = 10,
+ verbose: bool = False,
+ **kwargs: Any,
+ ):
+ """
+ Initialize the Resource Registry Set template.
+
+ Args:
+ llm_client (BaseChatModel): The language model client used for resource extraction.
+ embedder (Embeddings): The embedding model used for resource deduplication.
+ chunk_size (int, optional): Maximum characters per text chunk. Defaults to 2048.
+ chunk_overlap (int, optional): Overlap between chunks. Defaults to 256.
+ max_workers (int, optional): Parallel processing workers. Defaults to 10.
+ verbose (bool, optional): If True, enables progress logging. Defaults to False.
+ **kwargs (Any): Additional parameters for the AutoSet base class.
+ """
+ super().__init__(
+ item_schema=ConstructionResource,
+ key_extractor=lambda x: x.resource_name.strip().lower(),
+ llm_client=llm_client,
+ embedder=embedder,
+ chunk_size=chunk_size,
+ chunk_overlap=chunk_overlap,
+ max_workers=max_workers,
+ verbose=verbose,
+ prompt=CONSTRUCTION_RESOURCE_PROMPT,
+ **kwargs,
+ )
+
+ def show(
+ self,
+ *,
+ top_k_for_search: int = 3,
+ top_k_for_chat: int = 3,
+ ) -> None:
+ """
+ Visualize the collection using OntoSight.
+
+ Args:
+ top_k_for_search (int): Number of items to retrieve for search context. Default 3.
+ top_k_for_chat (int): Number of items to retrieve for chat context. Default 3.
+ """
+ def item_label_extractor(item: ConstructionResource) -> str:
+ info = f" ({ item.category })" if getattr(item, "category", None) else ""
+ return f"{ item.resource_name }{info}"
+
+ super().show(
+ item_label_extractor=item_label_extractor,
+ top_k_for_search=top_k_for_search,
+ top_k_for_chat=top_k_for_chat,
+ )
diff --git a/hyperextract/templates/en/construction/site_topology.py b/hyperextract/templates/en/construction/site_topology.py
new file mode 100644
index 0000000..b17b294
--- /dev/null
+++ b/hyperextract/templates/en/construction/site_topology.py
@@ -0,0 +1,180 @@
+from typing import Optional, Any
+from pydantic import BaseModel, Field
+from langchain_core.language_models.chat_models import BaseChatModel
+from langchain_core.embeddings import Embeddings
+from hyperextract.types import AutoSpatialGraph
+
+# ==============================================================================
+# 1. Schema Definitions
+# ==============================================================================
+
+
+class PhysicalElementNode(BaseModel):
+ """
+ A specific physical component or zone within a construction site.
+ """
+
+ name: str = Field(
+ description="Unique identifier or name of the element (e.g., 'Column-A1', 'Zone-B')."
+ )
+ element_type: str = Field(
+ description="Type: 'Structural', 'MEP', 'Architectural', 'Temporary Structure', 'Safety Zone'."
+ )
+ location_coordinates: Optional[str] = Field(
+ None,
+ description="Spatial coordinates or relative grid location (e.g., 'X:10, Y:20, Z:5').",
+ )
+ material_properties: Optional[str] = Field(
+ None, description="Key material info (e.g., 'C30 Concrete', 'Steel S355')."
+ )
+
+
+class SpatialRelation(BaseModel):
+ """
+ A spatial or topological connection between two physical elements.
+ """
+
+ source: str = Field(description="The reference element.")
+ target: str = Field(description="The related element.")
+ location_marker: str = Field(
+ description="The physical location or zone where this relationship resides (e.g., 'Level 3', 'North Wing', 'Grid A-1')."
+ )
+ relation_type: str = Field(
+ description="Type: 'Supporting', 'Adjacent', 'Enclosed By', 'Intersects', 'Aligned With'."
+ )
+ offset_distance: Optional[float] = Field(
+ None, description="Physical distance between elements in meters."
+ )
+
+
+# ==============================================================================
+# 2. Prompts
+# ==============================================================================
+
+CONSTRUCTION_SPATIAL_CONSOLIDATED_PROMPT = (
+ "You are a Senior BIM (Building Information Modeling) Engineer and Site Surveyor. Extract the spatial topology of the construction project.\n\n"
+ "Rules:\n"
+ "- Identify fixed physical elements and their properties as Nodes.\n"
+ "- Map the structural and spatial relationships (Edges) between these elements.\n"
+ "- Ensure coordinates and distances are captured if mentioned.\n"
+ "- Focus on how objects are physically connected or positioned relative to each other."
+)
+
+CONSTRUCTION_SPATIAL_NODE_PROMPT = (
+ "Identify all physical building components and site zones.\n\n"
+ "Rules:\n"
+ "- Extract distinct structural, mechanical, or architectural elements.\n"
+ "- Capture any mentioned locations, levels, or material information.\n"
+ "- DO NOT identify connections at this stage."
+)
+
+CONSTRUCTION_SPATIAL_EDGE_PROMPT = (
+ "Map the spatial and structural relationships between the identified building elements.\n\n"
+ "Rules:\n"
+ "- Determine how Element A relates to Element B (e.g., A supports B, A is adjacent to B).\n"
+ "- Extract physical offsets or distances if provided.\n"
+ "- Only link elements from the provided list."
+)
+
+# ==============================================================================
+# 3. Template Class
+# ==============================================================================
+
+
+class SiteTopologyGraph(AutoSpatialGraph[PhysicalElementNode, SpatialRelation]):
+ """
+ Template for building a spatial digital twin of a construction site.
+
+ Useful for structural analysis, BIM-to-Graph conversion, and site logistics planning.
+
+ Example:
+ >>> from langchain_openai import ChatOpenAI, OpenAIEmbeddings
+ >>> graph = SiteTopologyGraph(llm_client=llm, embedder=embedder)
+ >>> graph.feed_text("The foundation slab supports the four main corner columns.")
+ >>> print(graph.nodes) # Access structural elements
+ >>> print(graph.edges) # Access spatial relationships
+ """
+
+ 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 Site Topology Graph template.
+
+ Args:
+ llm_client (BaseChatModel): The language model client used for topology extraction.
+ embedder (Embeddings): The embedding model used for element deduplication.
+ extraction_mode (str, optional): 'one_stage' for joint extraction,
+ 'two_stage' for separate passes. Defaults to "one_stage".
+ chunk_size (int, optional): Maximum characters per text chunk. Defaults to 2048.
+ chunk_overlap (int, optional): Overlap between chunks. Defaults to 256.
+ max_workers (int, optional): Parallel processing workers. Defaults to 10.
+ verbose (bool, optional): If True, enables progress logging. Defaults to False.
+ **kwargs (Any): Additional parameters for the AutoSpatialGraph base class.
+ """
+ super().__init__(
+ node_schema=PhysicalElementNode,
+ edge_schema=SpatialRelation,
+ node_key_extractor=lambda x: x.name.strip().lower(),
+ edge_key_extractor=lambda x: (
+ f"{x.source.strip().lower()}--{x.relation_type.lower()}--{x.target.strip().lower()}"
+ ),
+ nodes_in_edge_extractor=lambda x: (
+ x.source.strip().lower(),
+ x.target.strip().lower(),
+ ),
+ location_in_edge_extractor=lambda x: x.location_marker.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=CONSTRUCTION_SPATIAL_CONSOLIDATED_PROMPT,
+ prompt_for_node_extraction=CONSTRUCTION_SPATIAL_NODE_PROMPT,
+ prompt_for_edge_extraction=CONSTRUCTION_SPATIAL_EDGE_PROMPT,
+ **kwargs,
+ )
+
+ def show(
+ self,
+ *,
+ top_k_nodes_for_search: int = 3,
+ top_k_edges_for_search: int = 3,
+ top_k_nodes_for_chat: int = 3,
+ top_k_edges_for_chat: int = 3,
+ ) -> 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: PhysicalElementNode) -> str:
+ return f"{node.name}"
+
+ def edge_label_extractor(edge: SpatialRelation) -> str:
+ return f"{edge.relation_type}"
+
+ super().show(
+ node_label_extractor=node_label_extractor,
+ edge_label_extractor=edge_label_extractor,
+ top_k_nodes_for_search=top_k_nodes_for_search,
+ top_k_edges_for_search=top_k_edges_for_search,
+ top_k_nodes_for_chat=top_k_nodes_for_chat,
+ top_k_edges_for_chat=top_k_edges_for_chat,
+ )
diff --git a/hyperextract/templates/en/cybersecurity/intrusion_kill_chain.py b/hyperextract/templates/en/cybersecurity/intrusion_kill_chain.py
new file mode 100644
index 0000000..d69cf63
--- /dev/null
+++ b/hyperextract/templates/en/cybersecurity/intrusion_kill_chain.py
@@ -0,0 +1,179 @@
+from typing import Optional, List, Any
+from pydantic import BaseModel, Field
+from langchain_core.language_models.chat_models import BaseChatModel
+from langchain_core.embeddings import Embeddings
+from hyperextract.types import AutoGraph
+
+# ==============================================================================
+# 1. Schema Definitions
+# ==============================================================================
+
+
+class AttackStageNode(BaseModel):
+ """
+ A specific phase or action in a cyber attack (aligned with MITRE ATT&CK or Lockheed Kill Chain).
+ """
+
+ stage_name: str = Field(
+ description="Name of the attack stage (e.g., 'Initial Access', 'Lateral Movement')."
+ )
+ tactics_used: List[str] = Field(
+ default_factory=list,
+ description="Specific techniques or tactics employed (e.g., 'Phishing', 'Pass-the-Hash').",
+ )
+ adversary_tools: List[str] = Field(
+ default_factory=list,
+ description="Malware or tools used in this stage (e.g., 'Cobalt Strike', 'Mimikatz').",
+ )
+ timestamp: Optional[str] = Field(
+ None, description="Time or sequence marker for the action."
+ )
+
+
+class KillChainLink(BaseModel):
+ """
+ A directional link representing the progression of an attack from one stage to another.
+ """
+
+ source: str = Field(description="The preceding attack stage.")
+ target: str = Field(description="The succeeding attack stage.")
+ causality: str = Field(
+ "Progression",
+ description="Type: 'Progression' (standard), 'Triggered' (automatic), 'Pivoted' (infrastructure shift).",
+ )
+ observables: List[str] = Field(
+ default_factory=list,
+ description="IOCs (Indicators of Compromise) linking the stages.",
+ )
+
+
+# ==============================================================================
+# 2. Prompts
+# ==============================================================================
+
+CYBER_KILL_CHAIN_CONSOLIDATED_PROMPT = (
+ "You are a Forensic Investigator and Incident Responder. Reconstruct the attack timeline and kill chain from incident reports.\n\n"
+ "Rules:\n"
+ "- Identify distinct attack stages as Nodes.\n"
+ "- Map the chronological and logical progression (Edges) of the intrusion.\n"
+ "- Capture specific tactics, tools (TTPs), and timestamps for each stage.\n"
+ "- Focus on the 'how' and 'when' of the adversary's movement."
+)
+
+CYBER_KILL_CHAIN_NODE_PROMPT = (
+ "Identify all suspicious activities and distinct phases of a cyber intrusion.\n\n"
+ "Rules:\n"
+ "- Extract action-oriented stages (e.g., 'Delivery of Payload', 'C2 Establishment').\n"
+ "- Identify the tools and techniques associated with each discovery.\n"
+ "- DO NOT link the stages yet."
+)
+
+CYBER_KILL_CHAIN_EDGE_PROMPT = (
+ "Establish the sequence and progression between identified attack stages.\n\n"
+ "Rules:\n"
+ "- Define which stage led to or enabled the next.\n"
+ "- Identify Indicators of Compromise (IPs, Hashes) that connect the stages.\n"
+ "- Only link stages from the provided list."
+)
+
+# ==============================================================================
+# 3. Template Class
+# ==============================================================================
+
+
+class IntrusionKillChainGraph(AutoGraph[AttackStageNode, KillChainLink]):
+ """
+ Template for reconstructing cyber attack lifecycles.
+
+ Useful for incident response (IR), forensic reporting, and threat hunting.
+
+ Example:
+ >>> from langchain_openai import ChatOpenAI, OpenAIEmbeddings
+ >>> kill_chain = IntrusionKillChainGraph(llm_client=llm, embedder=embedder)
+ >>> kill_chain.feed_text("The attacker gained initial access via a phishing email.")
+ >>> print(kill_chain.nodes) # Access attack stages
+ >>> print(kill_chain.edges) # Access progression links
+ """
+
+ 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 Intrusion Kill Chain Graph template.
+
+ Args:
+ llm_client (BaseChatModel): The language model client used for kill chain extraction.
+ embedder (Embeddings): The embedding model used for stage deduplication.
+ extraction_mode (str, optional): 'one_stage' for joint extraction,
+ 'two_stage' for separate passes. Defaults to "one_stage".
+ chunk_size (int, optional): Maximum characters per text chunk. Defaults to 2048.
+ chunk_overlap (int, optional): Overlap between chunks. Defaults to 256.
+ max_workers (int, optional): Parallel processing workers. Defaults to 10.
+ verbose (bool, optional): If True, enables progress logging. Defaults to False.
+ **kwargs (Any): Additional parameters for the AutoGraph base class.
+ """
+ super().__init__(
+ node_schema=AttackStageNode,
+ edge_schema=KillChainLink,
+ node_key_extractor=lambda x: x.stage_name.strip().lower(),
+ edge_key_extractor=lambda x: (
+ f"{x.source.strip().lower()}->{x.target.strip().lower()}"
+ ),
+ nodes_in_edge_extractor=lambda x: (
+ x.source.strip().lower(),
+ x.target.strip().lower(),
+ ),
+ 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=CYBER_KILL_CHAIN_CONSOLIDATED_PROMPT,
+ prompt_for_node_extraction=CYBER_KILL_CHAIN_NODE_PROMPT,
+ prompt_for_edge_extraction=CYBER_KILL_CHAIN_EDGE_PROMPT,
+ **kwargs,
+ )
+
+ def show(
+ self,
+ *,
+ top_k_nodes_for_search: int = 3,
+ top_k_edges_for_search: int = 3,
+ top_k_nodes_for_chat: int = 3,
+ top_k_edges_for_chat: int = 3,
+ ) -> 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: AttackStageNode) -> str:
+ return f"{node.stage_name}"
+
+ def edge_label_extractor(edge: KillChainLink) -> str:
+ return f"{edge.causality}"
+
+ super().show(
+ node_label_extractor=node_label_extractor,
+ edge_label_extractor=edge_label_extractor,
+ top_k_nodes_for_search=top_k_nodes_for_search,
+ top_k_edges_for_search=top_k_edges_for_search,
+ top_k_nodes_for_chat=top_k_nodes_for_chat,
+ top_k_edges_for_chat=top_k_edges_for_chat,
+ )
diff --git a/hyperextract/templates/en/cybersecurity/threat_intelligence_nexus.py b/hyperextract/templates/en/cybersecurity/threat_intelligence_nexus.py
new file mode 100644
index 0000000..2c28541
--- /dev/null
+++ b/hyperextract/templates/en/cybersecurity/threat_intelligence_nexus.py
@@ -0,0 +1,181 @@
+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.types import AutoHypergraph
+
+# ==============================================================================
+# 1. Schema Definitions
+# ==============================================================================
+
+
+class ThreatActorNode(BaseModel):
+ """
+ An entity (individual, group, or nation-state) responsible for cyber threats.
+ """
+
+ actor_name: str = Field(
+ description="Alias or group name (e.g., 'APT28', 'Fancy Bear', 'Lazarus Group')."
+ )
+ origin_country: Optional[str] = Field(
+ None, description="Suspected geographic origin."
+ )
+ motivation: Optional[str] = Field(
+ None,
+ description="Primary driver: 'Espionage', 'Financial Gain', 'Sabotage', 'Hacktivism'.",
+ )
+ typical_targets: List[str] = Field(
+ default_factory=list, description="Commonly targeted industries or regions."
+ )
+
+
+class IntelligenceNexus(BaseModel):
+ """
+ A hyperedge connecting an actor, specific infrastructure, and a campaign theme.
+ """
+
+ participants: List[str] = Field(
+ description="Entities involved: Actor names, Tool names, Infrastructure (IPs/Domains), Target Organizations."
+ )
+ campaign_name: str = Field(
+ description="Known name of the operation (e.g., 'SolarWinds Supply Chain Attack')."
+ )
+ shared_ttp: str = Field(
+ description="The common tactic, technique, or procedure that links these disparate entities."
+ )
+ confidence_level: str = Field(
+ "Medium", description="Analytical confidence: 'High', 'Medium', 'Low'."
+ )
+
+
+# ==============================================================================
+# 2. Prompts
+# ==============================================================================
+
+CYBER_NEXUS_CONSOLIDATED_PROMPT = (
+ "You are a Strategic Threat Intelligence Analyst. Map the high-level correlations between actors, infrastructure, and infrastructure.\n\n"
+ "Rules:\n"
+ "- Identify threat actors and their aliases as Nodes.\n"
+ "- Create Hyperedges (Nexuses) that link actors to specific tools and infrastructure under a named campaign.\n"
+ "- Focus on multi-point connections that reveal a cohesive operation rather than simple binary links.\n"
+ "- Highlight shared TTPs that unify the nexus."
+)
+
+CYBER_NEXUS_NODE_PROMPT = (
+ "Identify threat actors, malware families, and malicious infrastructure entities.\n\n"
+ "Rules:\n"
+ "- Extract actor aliases and group names.\n"
+ "- Identify motivations and target industries.\n"
+ "- DO NOT identify campaigns or relations yet."
+)
+
+CYBER_NEXUS_EDGE_PROMPT = (
+ "Identify the operational Campaigns or Nexuses that unify actors, tools, and targets.\n\n"
+ "Rules:\n"
+ "- Each hyperedge must describe a single campaign or operation.\n"
+ "- Link the actor(s) to the specific infrastructure and tools used in that operation.\n"
+ "- Only link entities from the provided list."
+)
+
+# ==============================================================================
+# 3. Template Class
+# ==============================================================================
+
+
+class ThreatIntelligenceNexusHypergraph(
+ AutoHypergraph[ThreatActorNode, IntelligenceNexus]
+):
+ """
+ Template for strategic threat intelligence mapping.
+
+ Ideal for connecting dots between infrastructure, actors, and malware in complex campaigns.
+
+ Example:
+ >>> from langchain_openai import ChatOpenAI, OpenAIEmbeddings
+ >>> nexus = ThreatIntelligenceNexusHypergraph(llm_client=llm, embedder=embedder)
+ >>> nexus.feed_text("APT28 utilized X-Tunnel and C2 at 1.2.3.4 during Operation Fancy-Phish.")
+ >>> print(nexus.nodes) # Access threat actors
+ >>> print(nexus.edges) # Access campaign hyperedges
+ """
+
+ 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 Threat Intelligence Nexus Hypergraph template.
+
+ Args:
+ llm_client (BaseChatModel): The language model client used for nexus extraction.
+ embedder (Embeddings): The embedding model used for actor deduplication.
+ extraction_mode (str, optional): 'one_stage' for joint extraction,
+ 'two_stage' for separate passes. Defaults to "one_stage".
+ chunk_size (int, optional): Maximum characters per text chunk. Defaults to 2048.
+ chunk_overlap (int, optional): Overlap between chunks. Defaults to 256.
+ max_workers (int, optional): Parallel processing workers. Defaults to 10.
+ verbose (bool, optional): If True, enables progress logging. Defaults to False.
+ **kwargs (Any): Additional parameters for the AutoHypergraph base class.
+ """
+ super().__init__(
+ node_schema=ThreatActorNode,
+ edge_schema=IntelligenceNexus,
+ node_key_extractor=lambda x: x.actor_name.strip().lower(),
+ edge_key_extractor=lambda x: (
+ f"{x.campaign_name.strip().lower()}_linking_{sorted(x.participants)}"
+ ),
+ nodes_in_edge_extractor=lambda x: [
+ p.strip().lower() for p in x.participants
+ ],
+ 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=CYBER_NEXUS_CONSOLIDATED_PROMPT,
+ prompt_for_node_extraction=CYBER_NEXUS_NODE_PROMPT,
+ prompt_for_edge_extraction=CYBER_NEXUS_EDGE_PROMPT,
+ **kwargs,
+ )
+
+ def show(
+ self,
+ *,
+ top_k_nodes_for_search: int = 3,
+ top_k_edges_for_search: int = 3,
+ top_k_nodes_for_chat: int = 3,
+ top_k_edges_for_chat: int = 3,
+ ) -> 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: ThreatActorNode) -> str:
+ return f"{node.actor_name}"
+
+ def edge_label_extractor(edge: IntelligenceNexus) -> str:
+ return f"{edge.campaign_name}"
+
+ super().show(
+ node_label_extractor=node_label_extractor,
+ edge_label_extractor=edge_label_extractor,
+ top_k_nodes_for_search=top_k_nodes_for_search,
+ top_k_edges_for_search=top_k_edges_for_search,
+ top_k_nodes_for_chat=top_k_nodes_for_chat,
+ top_k_edges_for_chat=top_k_edges_for_chat,
+ )
diff --git a/hyperextract/templates/en/cybersecurity/vulnerability_catalog.py b/hyperextract/templates/en/cybersecurity/vulnerability_catalog.py
new file mode 100644
index 0000000..cbb6616
--- /dev/null
+++ b/hyperextract/templates/en/cybersecurity/vulnerability_catalog.py
@@ -0,0 +1,123 @@
+from typing import Optional, List, Any
+from pydantic import BaseModel, Field
+from langchain_core.language_models.chat_models import BaseChatModel
+from langchain_core.embeddings import Embeddings
+from hyperextract.types import AutoSet
+
+# ==============================================================================
+# 1. Schema Definitions
+# ==============================================================================
+
+
+class VulnerabilityEntry(BaseModel):
+ """
+ A specific security vulnerability or weakness identified in a system or software.
+ """
+
+ vulnerability_id: str = Field(
+ description="Unique identifier (e.g., CVE-2023-1234, CWE-79)."
+ )
+ title: str = Field(description="Short descriptive name of the vulnerability.")
+ severity: str = Field(
+ description="Severity rating: 'Critical', 'High', 'Medium', 'Low', 'Informational'."
+ )
+ cvss_score: Optional[float] = Field(
+ None, description="CVSS base score (0.0 to 10.0)."
+ )
+ affected_components: List[str] = Field(
+ default_factory=list, description="Software or hardware components impacted."
+ )
+ remediation: Optional[str] = Field(
+ None, description="Recommended fix or mitigation steps."
+ )
+
+
+# ==============================================================================
+# 2. Prompts
+# ==============================================================================
+
+CYBER_VULN_PROMPT = (
+ "You are a Senior Security Analyst and Vulnerability Researcher. Extract and consolidate security vulnerabilities from threat reports, advisories, or scan results.\n\n"
+ "Rules:\n"
+ "- Identify unique vulnerabilities by their CVE/CWE ID or title.\n"
+ "- Capture severity levels and CVSS scores accurately.\n"
+ "- Merge duplicate mentions of the same vulnerability into a single entry, combining information about affected components and remediation."
+)
+
+# ==============================================================================
+# 3. Template Class
+# ==============================================================================
+
+
+class VulnerabilityUnifiedCatalog(AutoSet[VulnerabilityEntry]):
+ """
+ Template for building a deduplicated catalog of security vulnerabilities.
+
+ Ideal for processing multiple security advisories and creating a master list for patching.
+
+ Example:
+ >>> from langchain_openai import ChatOpenAI, OpenAIEmbeddings
+ >>> catalog = VulnerabilityUnifiedCatalog(llm_client=llm, embedder=embedder)
+ >>> catalog.feed_text("CVE-2024-0001 is a critical RCE in the kernel.")
+ >>> catalog.feed_text("The kernel RCE (CVE-2024-0001) requires an immediate patch.")
+ >>> print(catalog.items) # Duplicate entries for CVE-2024-0001 will be merged
+ """
+
+ def __init__(
+ self,
+ llm_client: BaseChatModel,
+ embedder: Embeddings,
+ *,
+ chunk_size: int = 2048,
+ chunk_overlap: int = 256,
+ max_workers: int = 10,
+ verbose: bool = False,
+ **kwargs: Any,
+ ):
+ """
+ Initialize the Vulnerability Unified Catalog template.
+
+ Args:
+ llm_client (BaseChatModel): The language model client used for vulnerability extraction.
+ embedder (Embeddings): The embedding model used for entry deduplication.
+ chunk_size (int, optional): Maximum characters per text chunk. Defaults to 2048.
+ chunk_overlap (int, optional): Overlap between chunks. Defaults to 256.
+ max_workers (int, optional): Parallel processing workers. Defaults to 10.
+ verbose (bool, optional): If True, enables progress logging. Defaults to False.
+ **kwargs (Any): Additional parameters for the AutoSet base class.
+ """
+ super().__init__(
+ item_schema=VulnerabilityEntry,
+ key_extractor=lambda x: x.vulnerability_id.strip().upper(),
+ llm_client=llm_client,
+ embedder=embedder,
+ chunk_size=chunk_size,
+ chunk_overlap=chunk_overlap,
+ max_workers=max_workers,
+ verbose=verbose,
+ prompt=CYBER_VULN_PROMPT,
+ **kwargs,
+ )
+
+ def show(
+ self,
+ *,
+ top_k_for_search: int = 3,
+ top_k_for_chat: int = 3,
+ ) -> None:
+ """
+ Visualize the collection using OntoSight.
+
+ Args:
+ top_k_for_search (int): Number of items to retrieve for search context. Default 3.
+ top_k_for_chat (int): Number of items to retrieve for chat context. Default 3.
+ """
+
+ def item_label_extractor(item: VulnerabilityEntry) -> str:
+ return f"{item.title}"
+
+ super().show(
+ item_label_extractor=item_label_extractor,
+ top_k_for_search=top_k_for_search,
+ top_k_for_chat=top_k_for_chat,
+ )
diff --git a/hyperextract/templates/en/education/academic_lexicon.py b/hyperextract/templates/en/education/academic_lexicon.py
index c016360..32e8b34 100644
--- a/hyperextract/templates/en/education/academic_lexicon.py
+++ b/hyperextract/templates/en/education/academic_lexicon.py
@@ -8,10 +8,12 @@ from hyperextract.types import AutoSet
# 1. Schema Definitions
# ==============================================================================
+
class AcademicTerm(BaseModel):
"""
A standardized academic term, concept, or definition aggregated from multiple sources.
"""
+
term: str = Field(
description="The primary name of the academic term/concept. Acts as the unique identifier."
)
@@ -22,13 +24,14 @@ class AcademicTerm(BaseModel):
description="A comprehensive, synthesized definition of the term."
)
synonyms: List[str] = Field(
- default_factory=list,
- description="Alternative names or closely related terms."
+ default_factory=list, description="Alternative names or closely related terms."
)
key_expression: Optional[str] = Field(
- None, description="A formula, notation, or iconic expression associated with this term."
+ None,
+ description="A formula, notation, or iconic expression associated with this term.",
)
+
# ==============================================================================
# 2. Prompts
# ==============================================================================
@@ -45,6 +48,7 @@ ACADEMIC_LEXICON_PROMPT = (
# 3. Template Class
# ==============================================================================
+
class AcademicLexiconSet(AutoSet[AcademicTerm]):
"""
Educational template for building a deduplicated, synthesized dictionary of academic terms.
@@ -64,7 +68,7 @@ class AcademicLexiconSet(AutoSet[AcademicTerm]):
llm_client: BaseChatModel,
embedder: Embeddings,
*,
- extraction_mode: str = "one_stage", # For AutoSet, this is standard
+ extraction_mode: str = "one_stage", # For AutoSet, this is standard
chunk_size: int = 2048,
chunk_overlap: int = 256,
max_workers: int = 10,
@@ -97,3 +101,26 @@ class AcademicLexiconSet(AutoSet[AcademicTerm]):
prompt=ACADEMIC_LEXICON_PROMPT,
**kwargs,
)
+
+ def show(
+ self,
+ *,
+ top_k_for_search: int = 3,
+ top_k_for_chat: int = 3,
+ ) -> None:
+ """
+ Visualize the collection using OntoSight.
+
+ Args:
+ top_k_for_search (int): Number of items to retrieve for search context. Default 3.
+ top_k_for_chat (int): Number of items to retrieve for chat context. Default 3.
+ """
+
+ def item_label_extractor(item: AcademicTerm) -> str:
+ return f"{item.term}"
+
+ super().show(
+ item_label_extractor=item_label_extractor,
+ top_k_for_search=top_k_for_search,
+ top_k_for_chat=top_k_for_chat,
+ )
diff --git a/hyperextract/templates/en/education/interdisciplinary_nexus.py b/hyperextract/templates/en/education/interdisciplinary_nexus.py
index 714d87e..ace46d6 100644
--- a/hyperextract/templates/en/education/interdisciplinary_nexus.py
+++ b/hyperextract/templates/en/education/interdisciplinary_nexus.py
@@ -8,17 +8,23 @@ from hyperextract.types import AutoHypergraph
# 1. Schema Definitions
# ==============================================================================
+
class ConceptNode(BaseModel):
"""
A fundamental academic concept that might belong to multiple disciplines.
"""
+
name: str = Field(description="Standard name of the concept.")
- primary_field: str = Field(description="The primary discipline this concept originates from.")
+ primary_field: str = Field(
+ description="The primary discipline this concept originates from."
+ )
+
class InterdisciplinaryEdge(BaseModel):
"""
A hyperedge representing a connection between multiple concepts across different fields.
"""
+
participants: List[str] = Field(
description="List of nodes (concept names) participating in this interdisciplinary nexus."
)
@@ -29,6 +35,7 @@ class InterdisciplinaryEdge(BaseModel):
description="The scientific or pedagogical value of connecting these specific dots."
)
+
# ==============================================================================
# 2. Prompts
# ==============================================================================
@@ -59,7 +66,10 @@ NEXUS_EDGE_PROMPT = (
# 3. Template Class
# ==============================================================================
-class InterdisciplinaryNexusHypergraph(AutoHypergraph[ConceptNode, InterdisciplinaryEdge]):
+
+class InterdisciplinaryNexusHypergraph(
+ AutoHypergraph[ConceptNode, InterdisciplinaryEdge]
+):
"""
Educational template for mapping non-binary, multi-point academic connections.
Ideal for research synthesis, curriculum cross-linking, and holistic knowledge management.
@@ -103,7 +113,9 @@ class InterdisciplinaryNexusHypergraph(AutoHypergraph[ConceptNode, Interdiscipli
edge_key_extractor=lambda x: (
f"{x.unifying_theme.strip().lower()}_linking_{sorted(x.participants)}"
),
- nodes_in_edge_extractor=lambda x: [p.strip().lower() for p in x.participants],
+ nodes_in_edge_extractor=lambda x: [
+ p.strip().lower() for p in x.participants
+ ],
llm_client=llm_client,
embedder=embedder,
extraction_mode=extraction_mode,
@@ -116,3 +128,36 @@ class InterdisciplinaryNexusHypergraph(AutoHypergraph[ConceptNode, Interdiscipli
prompt_for_edge_extraction=NEXUS_EDGE_PROMPT,
**kwargs,
)
+
+ def show(
+ self,
+ *,
+ top_k_nodes_for_search: int = 3,
+ top_k_edges_for_search: int = 3,
+ top_k_nodes_for_chat: int = 3,
+ top_k_edges_for_chat: int = 3,
+ ) -> 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: ConceptNode) -> str:
+ return f"{node.name}"
+
+ def edge_label_extractor(edge: InterdisciplinaryEdge) -> str:
+ return f"{edge.unifying_theme}"
+
+ super().show(
+ node_label_extractor=node_label_extractor,
+ edge_label_extractor=edge_label_extractor,
+ top_k_nodes_for_search=top_k_nodes_for_search,
+ top_k_edges_for_search=top_k_edges_for_search,
+ top_k_nodes_for_chat=top_k_nodes_for_chat,
+ top_k_edges_for_chat=top_k_edges_for_chat,
+ )
diff --git a/hyperextract/templates/en/education/prerequisite_map.py b/hyperextract/templates/en/education/prerequisite_map.py
index 2e1da13..c7a7030 100644
--- a/hyperextract/templates/en/education/prerequisite_map.py
+++ b/hyperextract/templates/en/education/prerequisite_map.py
@@ -8,10 +8,12 @@ from hyperextract.types import AutoGraph
# 1. Schema Definitions
# ==============================================================================
+
class KnowledgePointNode(BaseModel):
"""
A discrete unit of knowledge, concept, or skill within a specific curriculum.
"""
+
name: str = Field(
description="Standardized name of the knowledge point or concept."
)
@@ -25,19 +27,22 @@ class KnowledgePointNode(BaseModel):
None, description="A brief pedagogical summary of what this concept entails."
)
+
class PrerequisiteRelation(BaseModel):
"""
Directional dependency indicating that one concept must be learned before another.
"""
- source: str = Field(description="The prerequisite knowledge point (must be learned first).")
+
+ source: str = Field(
+ description="The prerequisite knowledge point (must be learned first)."
+ )
target: str = Field(description="The successor knowledge point (learned after).")
dependency_type: str = Field(
"Prerequisite",
- description="Nature of link: 'Prerequisite' (strict), 'Corequisite' (together), 'Recommended' (helpful)."
- )
- weight: float = Field(
- 1.0, description="Strength of dependency from 0.0 to 1.0."
+ description="Nature of link: 'Prerequisite' (strict), 'Corequisite' (together), 'Recommended' (helpful).",
)
+ weight: float = Field(1.0, description="Strength of dependency from 0.0 to 1.0.")
+
# ==============================================================================
# 2. Prompts
@@ -71,6 +76,7 @@ EDUCATION_EDGE_PROMPT = (
# 3. Template Class
# ==============================================================================
+
class PrerequisiteMapGraph(AutoGraph[KnowledgePointNode, PrerequisiteRelation]):
"""
Educational template for building Directed Acyclic Graphs (DAG) of knowledge dependencies.
@@ -119,7 +125,10 @@ class PrerequisiteMapGraph(AutoGraph[KnowledgePointNode, PrerequisiteRelation]):
edge_key_extractor=lambda x: (
f"{x.source.strip().lower()}->{x.target.strip().lower()}"
),
- nodes_in_edge_extractor=lambda x: (x.source.strip().lower(), x.target.strip().lower()),
+ nodes_in_edge_extractor=lambda x: (
+ x.source.strip().lower(),
+ x.target.strip().lower(),
+ ),
llm_client=llm_client,
embedder=embedder,
extraction_mode=extraction_mode,
@@ -132,3 +141,36 @@ class PrerequisiteMapGraph(AutoGraph[KnowledgePointNode, PrerequisiteRelation]):
prompt_for_edge_extraction=EDUCATION_EDGE_PROMPT,
**kwargs,
)
+
+ def show(
+ self,
+ *,
+ top_k_nodes_for_search: int = 3,
+ top_k_edges_for_search: int = 3,
+ top_k_nodes_for_chat: int = 3,
+ top_k_edges_for_chat: int = 3,
+ ) -> 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: KnowledgePointNode) -> str:
+ return f"{node.name}"
+
+ def edge_label_extractor(edge: PrerequisiteRelation) -> str:
+ return f"{edge.dependency_type}"
+
+ super().show(
+ node_label_extractor=node_label_extractor,
+ edge_label_extractor=edge_label_extractor,
+ top_k_nodes_for_search=top_k_nodes_for_search,
+ top_k_edges_for_search=top_k_edges_for_search,
+ top_k_nodes_for_chat=top_k_nodes_for_chat,
+ top_k_edges_for_chat=top_k_edges_for_chat,
+ )
diff --git a/hyperextract/templates/en/general/concept_map.py b/hyperextract/templates/en/general/concept_map.py
index dbc9c78..78f2dac 100644
--- a/hyperextract/templates/en/general/concept_map.py
+++ b/hyperextract/templates/en/general/concept_map.py
@@ -115,3 +115,34 @@ class ConceptMap(AutoGraph[Concept, ConceptRelation]):
prompt_for_edge_extraction=CONCEPT_MAP_EDGE_PROMPT,
**kwargs
)
+ def show(
+ self,
+ *,
+ top_k_nodes_for_search: int = 3,
+ top_k_edges_for_search: int = 3,
+ top_k_nodes_for_chat: int = 3,
+ top_k_edges_for_chat: int = 3,
+ ) -> 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 }"
+
+ def edge_label_extractor(edge: ConceptRelation) -> str:
+ return f"{ edge.source }"
+
+ super().show(
+ node_label_extractor=node_label_extractor,
+ edge_label_extractor=edge_label_extractor,
+ top_k_nodes_for_search=top_k_nodes_for_search,
+ top_k_edges_for_search=top_k_edges_for_search,
+ top_k_nodes_for_chat=top_k_nodes_for_chat,
+ top_k_edges_for_chat=top_k_edges_for_chat,
+ )
diff --git a/hyperextract/templates/en/general/knowledge_graph.py b/hyperextract/templates/en/general/knowledge_graph.py
index 5f76954..fb59066 100644
--- a/hyperextract/templates/en/general/knowledge_graph.py
+++ b/hyperextract/templates/en/general/knowledge_graph.py
@@ -117,3 +117,35 @@ class KnowledgeGraph(AutoGraph[Entity, Relation]):
prompt_for_edge_extraction=KNOWLEDGE_GRAPH_EDGE_PROMPT,
**kwargs
)
+ def show(
+ self,
+ *,
+ top_k_nodes_for_search: int = 3,
+ top_k_edges_for_search: int = 3,
+ top_k_nodes_for_chat: int = 3,
+ top_k_edges_for_chat: int = 3,
+ ) -> 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}"
+
+ def edge_label_extractor(edge: Relation) -> str:
+ return f"{ edge.source }"
+
+ super().show(
+ node_label_extractor=node_label_extractor,
+ edge_label_extractor=edge_label_extractor,
+ top_k_nodes_for_search=top_k_nodes_for_search,
+ top_k_edges_for_search=top_k_edges_for_search,
+ top_k_nodes_for_chat=top_k_nodes_for_chat,
+ top_k_edges_for_chat=top_k_edges_for_chat,
+ )
diff --git a/hyperextract/templates/en/general/logic_graph.py b/hyperextract/templates/en/general/logic_graph.py
index e15ea60..d864794 100644
--- a/hyperextract/templates/en/general/logic_graph.py
+++ b/hyperextract/templates/en/general/logic_graph.py
@@ -114,3 +114,34 @@ class LogicGraph(AutoGraph[LogicNode, LogicRelation]):
prompt_for_edge_extraction=LOGIC_GRAPH_EDGE_PROMPT,
**kwargs
)
+ def show(
+ self,
+ *,
+ top_k_nodes_for_search: int = 3,
+ top_k_edges_for_search: int = 3,
+ top_k_nodes_for_chat: int = 3,
+ top_k_edges_for_chat: int = 3,
+ ) -> 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 }"
+
+ def edge_label_extractor(edge: LogicRelation) -> str:
+ return f"{ edge.source }"
+
+ super().show(
+ node_label_extractor=node_label_extractor,
+ edge_label_extractor=edge_label_extractor,
+ top_k_nodes_for_search=top_k_nodes_for_search,
+ top_k_edges_for_search=top_k_edges_for_search,
+ top_k_nodes_for_chat=top_k_nodes_for_chat,
+ top_k_edges_for_chat=top_k_edges_for_chat,
+ )
diff --git a/hyperextract/templates/zh/agriculture/crop_cycle.py b/hyperextract/templates/zh/agriculture/crop_cycle.py
index 2fe6546..4087181 100644
--- a/hyperextract/templates/zh/agriculture/crop_cycle.py
+++ b/hyperextract/templates/zh/agriculture/crop_cycle.py
@@ -118,3 +118,36 @@ class CropCycleGraph(AutoGraph[AgriEntity, AgriRelation]):
prompt_for_edge_extraction=CROP_CYCLE_EDGE_PROMPT,
**kwargs
)
+ def show(
+ self,
+ *,
+ top_k_nodes_for_search: int = 3,
+ top_k_edges_for_search: int = 3,
+ top_k_nodes_for_chat: int = 3,
+ top_k_edges_for_chat: int = 3,
+ ) -> 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: AgriEntity) -> str:
+ info = f" ({ node.category })" if getattr(node, "category", None) else ""
+ return f"{ node.name }{info}"
+
+ def edge_label_extractor(edge: AgriRelation) -> str:
+ info = f" ({ edge.specification })" if getattr(edge, "specification", None) else ""
+ return f"{ edge.source }{info}"
+
+ super().show(
+ node_label_extractor=node_label_extractor,
+ edge_label_extractor=edge_label_extractor,
+ top_k_nodes_for_search=top_k_nodes_for_search,
+ top_k_edges_for_search=top_k_edges_for_search,
+ top_k_nodes_for_chat=top_k_nodes_for_chat,
+ top_k_edges_for_chat=top_k_edges_for_chat,
+ )
diff --git a/hyperextract/templates/zh/agriculture/livestock.py b/hyperextract/templates/zh/agriculture/livestock.py
index ebf9ae3..2e1499c 100644
--- a/hyperextract/templates/zh/agriculture/livestock.py
+++ b/hyperextract/templates/zh/agriculture/livestock.py
@@ -107,3 +107,35 @@ class LivestockGraph(AutoGraph[AnimalNode, BreedingRelation]):
prompt_for_edge_extraction=LIVESTOCK_EDGE_PROMPT,
**kwargs
)
+ def show(
+ self,
+ *,
+ top_k_nodes_for_search: int = 3,
+ top_k_edges_for_search: int = 3,
+ top_k_nodes_for_chat: int = 3,
+ top_k_edges_for_chat: int = 3,
+ ) -> 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: AnimalNode) -> str:
+ info = f" ({ node.category })" if getattr(node, "category", None) else ""
+ return f"{ node.name }{info}"
+
+ def edge_label_extractor(edge: BreedingRelation) -> str:
+ return f"{ edge.source }"
+
+ super().show(
+ node_label_extractor=node_label_extractor,
+ edge_label_extractor=edge_label_extractor,
+ top_k_nodes_for_search=top_k_nodes_for_search,
+ top_k_edges_for_search=top_k_edges_for_search,
+ top_k_nodes_for_chat=top_k_nodes_for_chat,
+ top_k_edges_for_chat=top_k_edges_for_chat,
+ )
diff --git a/hyperextract/templates/zh/agriculture/supply_chain.py b/hyperextract/templates/zh/agriculture/supply_chain.py
index 3377645..a3cbe83 100644
--- a/hyperextract/templates/zh/agriculture/supply_chain.py
+++ b/hyperextract/templates/zh/agriculture/supply_chain.py
@@ -107,3 +107,36 @@ class AgriSupplyChain(AutoGraph[SupplyEntity, SupplyFlow]):
prompt_for_edge_extraction=AGRI_SUPPLY_CHAIN_EDGE_PROMPT,
**kwargs
)
+ def show(
+ self,
+ *,
+ top_k_nodes_for_search: int = 3,
+ top_k_edges_for_search: int = 3,
+ top_k_nodes_for_chat: int = 3,
+ top_k_edges_for_chat: int = 3,
+ ) -> 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: SupplyEntity) -> str:
+ info = f" ({ node.category })" if getattr(node, "category", None) else ""
+ return f"{ node.name }{info}"
+
+ def edge_label_extractor(edge: SupplyFlow) -> str:
+ info = f" ({ edge.specification })" if getattr(edge, "specification", None) else ""
+ return f"{ edge.source }{info}"
+
+ super().show(
+ node_label_extractor=node_label_extractor,
+ edge_label_extractor=edge_label_extractor,
+ top_k_nodes_for_search=top_k_nodes_for_search,
+ top_k_edges_for_search=top_k_edges_for_search,
+ top_k_nodes_for_chat=top_k_nodes_for_chat,
+ top_k_edges_for_chat=top_k_edges_for_chat,
+ )
diff --git a/hyperextract/templates/zh/art/exhibition.py b/hyperextract/templates/zh/art/exhibition.py
index e2dfaa0..09523b6 100644
--- a/hyperextract/templates/zh/art/exhibition.py
+++ b/hyperextract/templates/zh/art/exhibition.py
@@ -107,3 +107,35 @@ class ExhibitionGraph(AutoGraph[ExhibitionEntity, ExhibitionRelation]):
prompt_for_edge_extraction=EXHIBITION_EDGE_PROMPT,
**kwargs
)
+ def show(
+ self,
+ *,
+ top_k_nodes_for_search: int = 3,
+ top_k_edges_for_search: int = 3,
+ top_k_nodes_for_chat: int = 3,
+ top_k_edges_for_chat: int = 3,
+ ) -> 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: ExhibitionEntity) -> str:
+ info = f" ({ node.type })" if getattr(node, "type", None) else ""
+ return f"{ node.name }{info}"
+
+ def edge_label_extractor(edge: ExhibitionRelation) -> str:
+ return f"{ edge.source }"
+
+ super().show(
+ node_label_extractor=node_label_extractor,
+ edge_label_extractor=edge_label_extractor,
+ top_k_nodes_for_search=top_k_nodes_for_search,
+ top_k_edges_for_search=top_k_edges_for_search,
+ top_k_nodes_for_chat=top_k_nodes_for_chat,
+ top_k_edges_for_chat=top_k_edges_for_chat,
+ )
diff --git a/hyperextract/templates/zh/art/movement.py b/hyperextract/templates/zh/art/movement.py
index a00b594..149af30 100644
--- a/hyperextract/templates/zh/art/movement.py
+++ b/hyperextract/templates/zh/art/movement.py
@@ -107,3 +107,35 @@ class ArtMovementGraph(AutoGraph[MovementEntity, ArtInfluenceEdge]):
prompt_for_edge_extraction=MOVEMENT_EDGE_PROMPT,
**kwargs
)
+ def show(
+ self,
+ *,
+ top_k_nodes_for_search: int = 3,
+ top_k_edges_for_search: int = 3,
+ top_k_nodes_for_chat: int = 3,
+ top_k_edges_for_chat: int = 3,
+ ) -> 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: MovementEntity) -> str:
+ info = f" ({ node.category })" if getattr(node, "category", None) else ""
+ return f"{ node.name }{info}"
+
+ def edge_label_extractor(edge: ArtInfluenceEdge) -> str:
+ return f"{ edge.source }"
+
+ super().show(
+ node_label_extractor=node_label_extractor,
+ edge_label_extractor=edge_label_extractor,
+ top_k_nodes_for_search=top_k_nodes_for_search,
+ top_k_edges_for_search=top_k_edges_for_search,
+ top_k_nodes_for_chat=top_k_nodes_for_chat,
+ top_k_edges_for_chat=top_k_edges_for_chat,
+ )
diff --git a/hyperextract/templates/zh/art/provenance.py b/hyperextract/templates/zh/art/provenance.py
index 6337f88..aa39fa5 100644
--- a/hyperextract/templates/zh/art/provenance.py
+++ b/hyperextract/templates/zh/art/provenance.py
@@ -107,3 +107,36 @@ class ProvenanceGraph(AutoGraph[ArtEntity, ProvenanceEdge]):
prompt_for_edge_extraction=PROVENANCE_EDGE_PROMPT,
**kwargs
)
+ def show(
+ self,
+ *,
+ top_k_nodes_for_search: int = 3,
+ top_k_edges_for_search: int = 3,
+ top_k_nodes_for_chat: int = 3,
+ top_k_edges_for_chat: int = 3,
+ ) -> 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: ArtEntity) -> str:
+ info = f" ({ node.category })" if getattr(node, "category", None) else ""
+ return f"{ node.name }{info}"
+
+ def edge_label_extractor(edge: ProvenanceEdge) -> str:
+ info = f" ({ edge.specification })" if getattr(edge, "specification", None) else ""
+ return f"{ edge.source }{info}"
+
+ super().show(
+ node_label_extractor=node_label_extractor,
+ edge_label_extractor=edge_label_extractor,
+ top_k_nodes_for_search=top_k_nodes_for_search,
+ top_k_edges_for_search=top_k_edges_for_search,
+ top_k_nodes_for_chat=top_k_nodes_for_chat,
+ top_k_edges_for_chat=top_k_edges_for_chat,
+ )
diff --git a/hyperextract/templates/zh/automotive/car_spec.py b/hyperextract/templates/zh/automotive/car_spec.py
index 8b2bdba..ed2e891 100644
--- a/hyperextract/templates/zh/automotive/car_spec.py
+++ b/hyperextract/templates/zh/automotive/car_spec.py
@@ -107,3 +107,35 @@ class CarSpecGraph(AutoGraph[CarComponent, CarSystemRelation]):
prompt_for_edge_extraction=CAR_SPEC_EDGE_PROMPT,
**kwargs
)
+ def show(
+ self,
+ *,
+ top_k_nodes_for_search: int = 3,
+ top_k_edges_for_search: int = 3,
+ top_k_nodes_for_chat: int = 3,
+ top_k_edges_for_chat: int = 3,
+ ) -> 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: CarComponent) -> str:
+ info = f" ({ node.category })" if getattr(node, "category", None) else ""
+ return f"{ node.name }{info}"
+
+ def edge_label_extractor(edge: CarSystemRelation) -> str:
+ return f"{ edge.source }"
+
+ super().show(
+ node_label_extractor=node_label_extractor,
+ edge_label_extractor=edge_label_extractor,
+ top_k_nodes_for_search=top_k_nodes_for_search,
+ top_k_edges_for_search=top_k_edges_for_search,
+ top_k_nodes_for_chat=top_k_nodes_for_chat,
+ top_k_edges_for_chat=top_k_edges_for_chat,
+ )
diff --git a/hyperextract/templates/zh/automotive/maintenance.py b/hyperextract/templates/zh/automotive/maintenance.py
index df98ffd..b2b5155 100644
--- a/hyperextract/templates/zh/automotive/maintenance.py
+++ b/hyperextract/templates/zh/automotive/maintenance.py
@@ -103,3 +103,35 @@ class CarMaintenanceGraph(AutoGraph[MaintenanceItem, MaintenanceSequence]):
prompt_for_edge_extraction=MAINTENANCE_EDGE_PROMPT,
**kwargs
)
+ def show(
+ self,
+ *,
+ top_k_nodes_for_search: int = 3,
+ top_k_edges_for_search: int = 3,
+ top_k_nodes_for_chat: int = 3,
+ top_k_edges_for_chat: int = 3,
+ ) -> 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: MaintenanceItem) -> str:
+ info = f" ({ node.category })" if getattr(node, "category", None) else ""
+ return f"{ node.item_name }{info}"
+
+ def edge_label_extractor(edge: MaintenanceSequence) -> str:
+ return f"{ edge.source }"
+
+ super().show(
+ node_label_extractor=node_label_extractor,
+ edge_label_extractor=edge_label_extractor,
+ top_k_nodes_for_search=top_k_nodes_for_search,
+ top_k_edges_for_search=top_k_edges_for_search,
+ top_k_nodes_for_chat=top_k_nodes_for_chat,
+ top_k_edges_for_chat=top_k_edges_for_chat,
+ )
diff --git a/hyperextract/templates/zh/automotive/manufacturing.py b/hyperextract/templates/zh/automotive/manufacturing.py
index fb0f851..1301067 100644
--- a/hyperextract/templates/zh/automotive/manufacturing.py
+++ b/hyperextract/templates/zh/automotive/manufacturing.py
@@ -107,3 +107,35 @@ class AutomotiveMfgGraph(AutoGraph[ManufacturingEntity, SupplyChainRelation]):
prompt_for_edge_extraction=MFG_EDGE_PROMPT,
**kwargs
)
+ def show(
+ self,
+ *,
+ top_k_nodes_for_search: int = 3,
+ top_k_edges_for_search: int = 3,
+ top_k_nodes_for_chat: int = 3,
+ top_k_edges_for_chat: int = 3,
+ ) -> 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: ManufacturingEntity) -> str:
+ info = f" ({ node.category })" if getattr(node, "category", None) else ""
+ return f"{ node.name }{info}"
+
+ def edge_label_extractor(edge: SupplyChainRelation) -> str:
+ return f"{ edge.source }"
+
+ super().show(
+ node_label_extractor=node_label_extractor,
+ edge_label_extractor=edge_label_extractor,
+ top_k_nodes_for_search=top_k_nodes_for_search,
+ top_k_edges_for_search=top_k_edges_for_search,
+ top_k_nodes_for_chat=top_k_nodes_for_chat,
+ top_k_edges_for_chat=top_k_edges_for_chat,
+ )
diff --git a/hyperextract/templates/zh/biology/metabolic.py b/hyperextract/templates/zh/biology/metabolic.py
index dcf9101..949f33c 100644
--- a/hyperextract/templates/zh/biology/metabolic.py
+++ b/hyperextract/templates/zh/biology/metabolic.py
@@ -96,3 +96,35 @@ class MetabolicHypergraph(AutoHypergraph[MetaboliteNode, MetabolicReactionHypere
prompt_for_edge_extraction=METABOLIC_EDGE_PROMPT,
**kwargs
)
+ def show(
+ self,
+ *,
+ top_k_nodes_for_search: int = 3,
+ top_k_edges_for_search: int = 3,
+ top_k_nodes_for_chat: int = 3,
+ top_k_edges_for_chat: int = 3,
+ ) -> 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: MetaboliteNode) -> str:
+ info = f" ({ node.formula })" if getattr(node, "formula", None) else ""
+ return f"{ node.name }{info}"
+
+ def edge_label_extractor(edge: MetabolicReactionHyperedge) -> str:
+ return f"{ edge.reaction_id }"
+
+ super().show(
+ node_label_extractor=node_label_extractor,
+ edge_label_extractor=edge_label_extractor,
+ top_k_nodes_for_search=top_k_nodes_for_search,
+ top_k_edges_for_search=top_k_edges_for_search,
+ top_k_nodes_for_chat=top_k_nodes_for_chat,
+ top_k_edges_for_chat=top_k_edges_for_chat,
+ )
diff --git a/hyperextract/templates/zh/biology/network.py b/hyperextract/templates/zh/biology/network.py
index 932b2ab..ce24dad 100644
--- a/hyperextract/templates/zh/biology/network.py
+++ b/hyperextract/templates/zh/biology/network.py
@@ -96,3 +96,35 @@ class BiologicalNetwork(AutoGraph[BioEntityNode, BioInteractionEdge]):
prompt_for_edge_extraction=NETWORK_EDGE_PROMPT,
**kwargs
)
+ def show(
+ self,
+ *,
+ top_k_nodes_for_search: int = 3,
+ top_k_edges_for_search: int = 3,
+ top_k_nodes_for_chat: int = 3,
+ top_k_edges_for_chat: int = 3,
+ ) -> 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: BioEntityNode) -> str:
+ info = f" ({ node.category })" if getattr(node, "category", None) else ""
+ return f"{ node.name }{info}"
+
+ def edge_label_extractor(edge: BioInteractionEdge) -> str:
+ return f"{ edge.source }"
+
+ super().show(
+ node_label_extractor=node_label_extractor,
+ edge_label_extractor=edge_label_extractor,
+ top_k_nodes_for_search=top_k_nodes_for_search,
+ top_k_edges_for_search=top_k_edges_for_search,
+ top_k_nodes_for_chat=top_k_nodes_for_chat,
+ top_k_edges_for_chat=top_k_edges_for_chat,
+ )
diff --git a/hyperextract/templates/zh/biology/taxonomy.py b/hyperextract/templates/zh/biology/taxonomy.py
index 39e340d..e718385 100644
--- a/hyperextract/templates/zh/biology/taxonomy.py
+++ b/hyperextract/templates/zh/biology/taxonomy.py
@@ -94,3 +94,36 @@ class TaxonomyGraph(AutoGraph[TaxonNode, TaxonomyRelation]):
prompt_for_edge_extraction=TAXONOMY_EDGE_PROMPT,
**kwargs
)
+ def show(
+ self,
+ *,
+ top_k_nodes_for_search: int = 3,
+ top_k_edges_for_search: int = 3,
+ top_k_nodes_for_chat: int = 3,
+ top_k_edges_for_chat: int = 3,
+ ) -> 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: TaxonNode) -> str:
+ info = f" ({ node.description })" if getattr(node, "description", None) else ""
+ return f"{ node.name }{info}"
+
+ def edge_label_extractor(edge: TaxonomyRelation) -> str:
+ info = f" ({ edge.type })" if getattr(edge, "type", None) else ""
+ return f"{ edge.source }{info}"
+
+ super().show(
+ node_label_extractor=node_label_extractor,
+ edge_label_extractor=edge_label_extractor,
+ top_k_nodes_for_search=top_k_nodes_for_search,
+ top_k_edges_for_search=top_k_edges_for_search,
+ top_k_nodes_for_chat=top_k_nodes_for_chat,
+ top_k_edges_for_chat=top_k_edges_for_chat,
+ )
diff --git a/hyperextract/templates/zh/business/ecosystem.py b/hyperextract/templates/zh/business/ecosystem.py
index 5b0c32c..8ec4a84 100644
--- a/hyperextract/templates/zh/business/ecosystem.py
+++ b/hyperextract/templates/zh/business/ecosystem.py
@@ -106,3 +106,34 @@ class MarketEcosystemHyper(AutoHypergraph[IndustryParticipant, CollectiveAllianc
prompt_for_edge_extraction=ECOSYSTEM_EDGE_PROMPT,
**kwargs,
)
+ def show(
+ self,
+ *,
+ top_k_nodes_for_search: int = 3,
+ top_k_edges_for_search: int = 3,
+ top_k_nodes_for_chat: int = 3,
+ top_k_edges_for_chat: int = 3,
+ ) -> 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: IndustryParticipant) -> str:
+ return f"{ node.name }"
+
+ def edge_label_extractor(edge: CollectiveAlliance) -> str:
+ return f"{ edge.alliance_id }"
+
+ super().show(
+ node_label_extractor=node_label_extractor,
+ edge_label_extractor=edge_label_extractor,
+ top_k_nodes_for_search=top_k_nodes_for_search,
+ top_k_edges_for_search=top_k_edges_for_search,
+ top_k_nodes_for_chat=top_k_nodes_for_chat,
+ top_k_edges_for_chat=top_k_edges_for_chat,
+ )
diff --git a/hyperextract/templates/zh/business/equity.py b/hyperextract/templates/zh/business/equity.py
index 8e4c68f..c57d710 100644
--- a/hyperextract/templates/zh/business/equity.py
+++ b/hyperextract/templates/zh/business/equity.py
@@ -114,3 +114,35 @@ class EquityStructureGraph(AutoGraph[StakeholderNode, OwnershipRelation]):
prompt_for_edge_extraction=EQUITY_EDGE_PROMPT,
**kwargs,
)
+ def show(
+ self,
+ *,
+ top_k_nodes_for_search: int = 3,
+ top_k_edges_for_search: int = 3,
+ top_k_nodes_for_chat: int = 3,
+ top_k_edges_for_chat: int = 3,
+ ) -> 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: StakeholderNode) -> str:
+ info = f" ({ node.type })" if getattr(node, "type", None) else ""
+ return f"{ node.name }{info}"
+
+ def edge_label_extractor(edge: OwnershipRelation) -> str:
+ return f"{ edge.source }"
+
+ super().show(
+ node_label_extractor=node_label_extractor,
+ edge_label_extractor=edge_label_extractor,
+ top_k_nodes_for_search=top_k_nodes_for_search,
+ top_k_edges_for_search=top_k_edges_for_search,
+ top_k_nodes_for_chat=top_k_nodes_for_chat,
+ top_k_edges_for_chat=top_k_edges_for_chat,
+ )
diff --git a/hyperextract/templates/zh/business/strategy.py b/hyperextract/templates/zh/business/strategy.py
index 46d66ea..6b7ba7c 100644
--- a/hyperextract/templates/zh/business/strategy.py
+++ b/hyperextract/templates/zh/business/strategy.py
@@ -113,3 +113,35 @@ class StrategicChainGraph(AutoTemporalGraph[StrategicNode, StrategicPivot]):
prompt_for_edge_extraction=STRATEGY_EDGE_PROMPT,
**kwargs,
)
+ def show(
+ self,
+ *,
+ top_k_nodes_for_search: int = 3,
+ top_k_edges_for_search: int = 3,
+ top_k_nodes_for_chat: int = 3,
+ top_k_edges_for_chat: int = 3,
+ ) -> 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: StrategicNode) -> str:
+ return f"{ node.name }"
+
+ def edge_label_extractor(edge: StrategicPivot) -> str:
+ info = f" ({ edge.timestamp })" if getattr(edge, "timestamp", None) else ""
+ return f"{ edge.source }{info}"
+
+ super().show(
+ node_label_extractor=node_label_extractor,
+ edge_label_extractor=edge_label_extractor,
+ top_k_nodes_for_search=top_k_nodes_for_search,
+ top_k_edges_for_search=top_k_edges_for_search,
+ top_k_nodes_for_chat=top_k_nodes_for_chat,
+ top_k_edges_for_chat=top_k_edges_for_chat,
+ )
diff --git a/hyperextract/templates/zh/chemistry/molecular.py b/hyperextract/templates/zh/chemistry/molecular.py
index c4840b0..882e2d4 100644
--- a/hyperextract/templates/zh/chemistry/molecular.py
+++ b/hyperextract/templates/zh/chemistry/molecular.py
@@ -96,3 +96,34 @@ class MolecularStructureGraph(AutoSpatialGraph[AtomNode, ChemicalBond]):
prompt_for_edge_extraction=MOLECULAR_EDGE_PROMPT,
**kwargs
)
+ def show(
+ self,
+ *,
+ top_k_nodes_for_search: int = 3,
+ top_k_edges_for_search: int = 3,
+ top_k_nodes_for_chat: int = 3,
+ top_k_edges_for_chat: int = 3,
+ ) -> 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: AtomNode) -> str:
+ return f"{ node.name }"
+
+ def edge_label_extractor(edge: ChemicalBond) -> str:
+ return f"{ edge.source }"
+
+ super().show(
+ node_label_extractor=node_label_extractor,
+ edge_label_extractor=edge_label_extractor,
+ top_k_nodes_for_search=top_k_nodes_for_search,
+ top_k_edges_for_search=top_k_edges_for_search,
+ top_k_nodes_for_chat=top_k_nodes_for_chat,
+ top_k_edges_for_chat=top_k_edges_for_chat,
+ )
diff --git a/hyperextract/templates/zh/chemistry/protocol.py b/hyperextract/templates/zh/chemistry/protocol.py
index cea1003..d508d7d 100644
--- a/hyperextract/templates/zh/chemistry/protocol.py
+++ b/hyperextract/templates/zh/chemistry/protocol.py
@@ -98,3 +98,35 @@ class LabProtocolTemporal(AutoTemporalGraph[LabOperation, ProtocolTransition]):
prompt_for_edge_extraction=PROTOCOL_EDGE_PROMPT,
**kwargs
)
+ def show(
+ self,
+ *,
+ top_k_nodes_for_search: int = 3,
+ top_k_edges_for_search: int = 3,
+ top_k_nodes_for_chat: int = 3,
+ top_k_edges_for_chat: int = 3,
+ ) -> 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: LabOperation) -> str:
+ return f"{ node.op_id }"
+
+ def edge_label_extractor(edge: ProtocolTransition) -> str:
+ info = f" ({ edge.timestamp })" if getattr(edge, "timestamp", None) else ""
+ return f"{ edge.transition_id }{info}"
+
+ super().show(
+ node_label_extractor=node_label_extractor,
+ edge_label_extractor=edge_label_extractor,
+ top_k_nodes_for_search=top_k_nodes_for_search,
+ top_k_edges_for_search=top_k_edges_for_search,
+ top_k_nodes_for_chat=top_k_nodes_for_chat,
+ top_k_edges_for_chat=top_k_edges_for_chat,
+ )
diff --git a/hyperextract/templates/zh/chemistry/reaction.py b/hyperextract/templates/zh/chemistry/reaction.py
index 3738684..4b5ac5e 100644
--- a/hyperextract/templates/zh/chemistry/reaction.py
+++ b/hyperextract/templates/zh/chemistry/reaction.py
@@ -98,3 +98,35 @@ class ChemicalReactionHyper(AutoHypergraph[ChemicalEntity, ReactionEvent]):
prompt_for_edge_extraction=REACTION_EDGE_PROMPT,
**kwargs
)
+ def show(
+ self,
+ *,
+ top_k_nodes_for_search: int = 3,
+ top_k_edges_for_search: int = 3,
+ top_k_nodes_for_chat: int = 3,
+ top_k_edges_for_chat: int = 3,
+ ) -> 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: ChemicalEntity) -> str:
+ info = f" ({ node.phase })" if getattr(node, "phase", None) else ""
+ return f"{ node.name }{info}"
+
+ def edge_label_extractor(edge: ReactionEvent) -> str:
+ return f"{ edge.reaction_id }"
+
+ super().show(
+ node_label_extractor=node_label_extractor,
+ edge_label_extractor=edge_label_extractor,
+ top_k_nodes_for_search=top_k_nodes_for_search,
+ top_k_edges_for_search=top_k_edges_for_search,
+ top_k_nodes_for_chat=top_k_nodes_for_chat,
+ top_k_edges_for_chat=top_k_edges_for_chat,
+ )
diff --git a/hyperextract/templates/zh/construction/project_timeline.py b/hyperextract/templates/zh/construction/project_timeline.py
new file mode 100644
index 0000000..44a3228
--- /dev/null
+++ b/hyperextract/templates/zh/construction/project_timeline.py
@@ -0,0 +1,161 @@
+from typing import Optional, Any
+from pydantic import BaseModel, Field
+from langchain_core.language_models.chat_models import BaseChatModel
+from langchain_core.embeddings import Embeddings
+from hyperextract.types import AutoTemporalGraph
+
+# ==============================================================================
+# 1. Schema Definitions
+# ==============================================================================
+
+class MilestoneTaskNode(BaseModel):
+ """
+ 建筑项目进度计划中的特定任务、里程碑或阶段。
+ """
+ task_name: str = Field(description="任务操作名称(例如:'基础浇筑')。")
+ phase: str = Field(description="项目阶段:'前期准备'、'下部结构'、'主体结构'、'装修装饰'、'竣工验收'。")
+ duration_estimate: Optional[str] = Field(None, description="预估时间(例如:'14天'、'2个月')。")
+ responsible_party: Optional[str] = Field(None, description="负责的分包商或团队负责人。")
+
+class TemporalDependency(BaseModel):
+ """
+ 表示任务权序的基于时间的连接。
+ """
+ source: str = Field(description="前置任务。")
+ target: str = Field(description="后置任务。")
+ timestamp: str = Field(
+ description="时间标记或步骤编号(例如:'阶段1'、'步骤2'或具体日期/时间)。"
+ )
+ dependency_type: str = Field(
+ "完成到开始",
+ description="类型:'完成到开始 (FS)'、'开始到开始 (SS)'、'完成到完成 (FF)'、'开始到完成 (SF)'。"
+ )
+ lag_time: Optional[str] = Field(None, description="任务间的等待时间(例如:'混凝土养护3天')。")
+
+# ==============================================================================
+# 2. Prompts
+# ==============================================================================
+
+CONSTRUCTION_TEMPORAL_CONSOLIDATED_PROMPT = (
+ "你是一位资深项目管理计划员和计划工程师。请从文本中提取建筑项目的时间进度表。\n\n"
+ "提取规则:\n"
+ "- 识别细化的任务和重大里程碑作为节点。\n"
+ "- 使用行业标准逻辑(FS, SS等)建立清晰的时间依赖关系(边)。\n"
+ "- 捕获提到的持续时间和养护/滞后时间。\n"
+ "- 重点关注关键路径和测序逻辑。"
+)
+
+CONSTRUCTION_TEMPORAL_NODE_PROMPT = (
+ "识别所有任务、计划活动和项目里程碑。\n\n"
+ "提取规则:\n"
+ "- 提取以动词开头的任务名称。\n"
+ "- 将任务归类到项目阶段并识别负责方。\n"
+ "- 在此阶段不要识别顺序或依赖关系。"
+)
+
+CONSTRUCTION_TEMPORAL_EDGE_PROMPT = (
+ "根据任务在时间上的逻辑顺序进行连接。\n\n"
+ "提取规则:\n"
+ "- 为每个链接定义前导任务和后续任务。\n"
+ "- 分类链接类型(如果未指定,默认为FS)。\n"
+ "- 识别任何要求的滞后时间(例如:等待检验或干燥)。\n"
+ "- 仅在已识别的列表任务之间创建连接。"
+)
+
+# ==============================================================================
+# 3. Template Class
+# ==============================================================================
+
+class ProjectSchedulingTimeline(AutoTemporalGraph[MilestoneTaskNode, TemporalDependency]):
+ """
+ 用于提取项目计划和关键路径法(CPM)网络的模板。
+
+ 适用于进度风险分析、延期跟踪和进度监控。
+
+ Example:
+ >>> from langchain_openai import ChatOpenAI, OpenAIEmbeddings
+ >>> timeline = ProjectSchedulingTimeline(llm_client=llm, embedder=embedder)
+ >>> timeline.feed_text("土方开挖必须在浇筑基础底板之前完成。")
+ >>> print(timeline.nodes) # 获取任务和里程碑
+ >>> print(timeline.edges) # 获取时间依赖关系
+ """
+
+ 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,
+ ):
+ """
+ 初始化项目进度时间轴模板。
+
+ Args:
+ llm_client (BaseChatModel): 用于进度提取的大模型客户端。
+ embedder (Embeddings): 用于任务和依赖项去重的嵌入模型。
+ extraction_mode (str, optional): 'one_stage' 为联合提取,
+ 'two_stage' 为分步提取。默认为 "one_stage"。
+ chunk_size (int, optional): 单个文本块的最大字符数。默认为 2048。
+ chunk_overlap (int, optional): 文本块之间的重叠字符数。默认为 256。
+ max_workers (int, optional): 并行处理的工作线程数。默认为 10。
+ verbose (bool, optional): 是否启用详细日记记录。默认为 False。
+ **kwargs (Any): 传递给 AutoTemporalGraph 基类的其他参数。
+ """
+ super().__init__(
+ node_schema=MilestoneTaskNode,
+ edge_schema=TemporalDependency,
+ node_key_extractor=lambda x: x.task_name.strip().lower(),
+ edge_key_extractor=lambda x: f"{x.source.strip().lower()}--[{x.dependency_type}]-->{x.target.strip().lower()}",
+ nodes_in_edge_extractor=lambda x: (x.source.strip().lower(), x.target.strip().lower()),
+ time_in_edge_extractor=lambda x: x.timestamp.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=CONSTRUCTION_TEMPORAL_CONSOLIDATED_PROMPT,
+ prompt_for_node_extraction=CONSTRUCTION_TEMPORAL_NODE_PROMPT,
+ prompt_for_edge_extraction=CONSTRUCTION_TEMPORAL_EDGE_PROMPT,
+ **kwargs,
+ )
+
+ def show(
+ self,
+ *,
+ top_k_nodes_for_search: int = 3,
+ top_k_edges_for_search: int = 3,
+ top_k_nodes_for_chat: int = 3,
+ top_k_edges_for_chat: int = 3,
+ ) -> 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: MilestoneTaskNode) -> str:
+ info = f" ({ node.phase })" if getattr(node, "phase", None) else ""
+ return f"{ node.task_name }{info}"
+
+ def edge_label_extractor(edge: TemporalDependency) -> str:
+ info = f" ({ edge.timestamp })" if getattr(edge, "timestamp", None) else ""
+ return f"{ edge.source }{info}"
+
+ super().show(
+ node_label_extractor=node_label_extractor,
+ edge_label_extractor=edge_label_extractor,
+ top_k_nodes_for_search=top_k_nodes_for_search,
+ top_k_edges_for_search=top_k_edges_for_search,
+ top_k_nodes_for_chat=top_k_nodes_for_chat,
+ top_k_edges_for_chat=top_k_edges_for_chat,
+ )
\ No newline at end of file
diff --git a/hyperextract/templates/zh/construction/resource_registry.py b/hyperextract/templates/zh/construction/resource_registry.py
new file mode 100644
index 0000000..8afb9b1
--- /dev/null
+++ b/hyperextract/templates/zh/construction/resource_registry.py
@@ -0,0 +1,108 @@
+from typing import Optional, Any
+from pydantic import BaseModel, Field
+from langchain_core.language_models.chat_models import BaseChatModel
+from langchain_core.embeddings import Embeddings
+from hyperextract.types import AutoSet
+
+# ==============================================================================
+# 1. Schema Definitions
+# ==============================================================================
+
+class ConstructionResource(BaseModel):
+ """
+ 分配给建筑项目的特定资产、材料或劳动力类别。
+ """
+ resource_name: str = Field(description="资源名称(例如:'塔吊 TC-01'、'粉煤灰混凝土')。")
+ category: str = Field(description="类别:'重型机械'、'散装材料'、'技工'、'固定设备'、'易耗品'。")
+ quantity: Optional[str] = Field(None, description="现场当前数量或容量。")
+ supplier_or_team: Optional[str] = Field(None, description="提供或管理此资源的实体。")
+ status: str = Field("在用", description="当前运营状态:'在用'、'闲置'、'维护'、'用尽'。")
+
+# ==============================================================================
+# 2. Prompts
+# ==============================================================================
+
+CONSTRUCTION_RESOURCE_PROMPT = (
+ "你是一位建筑物流和采购经理。请从文本中提取所有项目资源和资产。\n\n"
+ "提取规则:\n"
+ "- 识别机械、材料和劳动力类型。\n"
+ "- 捕获提到的具体数量和供应商名称。\n"
+ "- 自动合并相似项(例如:'挖掘机 A' 和 '那台主要的挖掘机' 如果指代同一个单元,应合并)。"
+)
+
+# ==============================================================================
+# 3. Template Class
+# ==============================================================================
+
+class ResourceRegistrySet(AutoSet[ConstructionResource]):
+ """
+ 用于管理统一的工地资产和材料清单的模板。
+
+ 使用 AutoSet 自动去重并合并多段文本中提到的资源信息。
+
+ Example:
+ >>> from langchain_openai import ChatOpenAI, OpenAIEmbeddings
+ >>> registry = ResourceRegistrySet(llm_client=llm, embedder=embedder)
+ >>> registry.feed_text("现场有两台来自利勃海尔的50吨起重机。")
+ >>> registry.feed_text("这些利勃海尔起重机目前正在进行维修。")
+ >>> print(registry.items) # 信息将合并到单个起重机资源中
+ """
+
+ def __init__(
+ self,
+ llm_client: BaseChatModel,
+ embedder: Embeddings,
+ *,
+ chunk_size: int = 2048,
+ chunk_overlap: int = 256,
+ max_workers: int = 10,
+ verbose: bool = False,
+ **kwargs: Any,
+ ):
+ """
+ 初始化资源登记表模板。
+
+ Args:
+ llm_client (BaseChatModel): 用于资源提取的大模型客户端。
+ embedder (Embeddings): 用于资源去重的嵌入模型。
+ chunk_size (int, optional): 单个文本块的最大字符数。默认为 2048。
+ chunk_overlap (int, optional): 文本块之间的重叠字符数。默认为 256。
+ max_workers (int, optional): 并行处理的工作线程数。默认为 10。
+ verbose (bool, optional): 是否启用详细日记记录。默认为 False。
+ **kwargs (Any): 传递给 AutoSet 基类的其他参数。
+ """
+ super().__init__(
+ item_schema=ConstructionResource,
+ key_extractor=lambda x: x.resource_name.strip().lower(),
+ llm_client=llm_client,
+ embedder=embedder,
+ chunk_size=chunk_size,
+ chunk_overlap=chunk_overlap,
+ max_workers=max_workers,
+ verbose=verbose,
+ prompt=CONSTRUCTION_RESOURCE_PROMPT,
+ **kwargs,
+ )
+
+ def show(
+ self,
+ *,
+ top_k_for_search: int = 3,
+ top_k_for_chat: int = 3,
+ ) -> None:
+ """
+ Visualize the collection using OntoSight.
+
+ Args:
+ top_k_for_search (int): Number of items to retrieve for search context. Default 3.
+ top_k_for_chat (int): Number of items to retrieve for chat context. Default 3.
+ """
+ def item_label_extractor(item: ConstructionResource) -> str:
+ info = f" ({ item.category })" if getattr(item, "category", None) else ""
+ return f"{ item.resource_name }{info}"
+
+ super().show(
+ item_label_extractor=item_label_extractor,
+ top_k_for_search=top_k_for_search,
+ top_k_for_chat=top_k_for_chat,
+ )
\ No newline at end of file
diff --git a/hyperextract/templates/zh/construction/site_topology.py b/hyperextract/templates/zh/construction/site_topology.py
new file mode 100644
index 0000000..b9ec8e3
--- /dev/null
+++ b/hyperextract/templates/zh/construction/site_topology.py
@@ -0,0 +1,157 @@
+from typing import Optional, Any
+from pydantic import BaseModel, Field
+from langchain_core.language_models.chat_models import BaseChatModel
+from langchain_core.embeddings import Embeddings
+from hyperextract.types import AutoSpatialGraph
+
+# ==============================================================================
+# 1. Schema Definitions
+# ==============================================================================
+
+class PhysicalElementNode(BaseModel):
+ """
+ 建筑工地内的特定物理构件或区域。
+ """
+ name: str = Field(description="元素的唯一标识符或名称(例如:'A1柱'、'B区')。")
+ element_type: str = Field(description="类型:'结构件'、'机电构件'、'建筑构件'、'临时设施'、'安全区域'。")
+ location_coordinates: Optional[str] = Field(None, description="空间坐标或相对轴线位置(例如:'X:10, Y:20, Z:5')。")
+ material_properties: Optional[str] = Field(None, description="关键材料信息(例如:'C30混凝土'、'S355钢材')。")
+
+class SpatialRelation(BaseModel):
+ """
+ 两个物理元素之间的空间或拓扑连接关系。
+ """
+ source: str = Field(description="参考元素。")
+ target: str = Field(description="关联元素。")
+ location_marker: str = Field(
+ description="该关系所在的物理位置或区域(例如:'3层'、'北翼'、'A-1轴线')。"
+ )
+ relation_type: str = Field(
+ description="类型:'支撑'、'相邻'、'包含于'、'相交'、'对齐'。"
+ )
+ offset_distance: Optional[float] = Field(None, description="元素间的物理距离(单位:米)。")
+
+# ==============================================================================
+# 2. Prompts
+# ==============================================================================
+
+CONSTRUCTION_SPATIAL_CONSOLIDATED_PROMPT = (
+ "你是一位资深BIM(建筑信息模型)工程师。请从文本中提取建筑项目的空间拓扑结构。\n\n"
+ "提取规则:\n"
+ "- 识别固定的物理构件及其属性作为节点。\n"
+ "- 映射这些构件之间的结构和空间关系(边)。\n"
+ "- 如果文本提到了坐标和距离,请务必捕获。\n"
+ "- 重点关注物体是如何物理连接或相对定位的。"
+)
+
+CONSTRUCTION_SPATIAL_NODE_PROMPT = (
+ "识别所有物理建筑构件和工地区域。\n\n"
+ "提取规则:\n"
+ "- 提取明确的结构、机械或建筑构件。\n"
+ "- 捕获任何提到的位置、楼层或材料信息。\n"
+ "- 在此阶段不要识别连接关系。"
+)
+
+CONSTRUCTION_SPATIAL_EDGE_PROMPT = (
+ "映射已识别建筑构件之间的空间和结构关系。\n\n"
+ "提取规则:\n"
+ "- 确定元素A与元素B的关系(例如:A支撑B,A与B相邻)。\n"
+ "- 提取提供的物理偏移或距离。\n"
+ "- 仅在已识别的列表元素之间创建连接。"
+)
+
+# ==============================================================================
+# 3. Template Class
+# ==============================================================================
+
+class SiteTopologyGraph(AutoSpatialGraph[PhysicalElementNode, SpatialRelation]):
+ """
+ 用于构建建筑工地空间数字孪生的模板。
+
+ 适用于结构分析、BIM转图谱以及工地物流规划。
+
+ Example:
+ >>> from langchain_openai import ChatOpenAI, OpenAIEmbeddings
+ >>> graph = SiteTopologyGraph(llm_client=llm, embedder=embedder)
+ >>> graph.feed_text("基础底板支撑着四个主要的转角柱。")
+ >>> print(graph.nodes) # 获取结构构件
+ >>> print(graph.edges) # 获取空间关系
+ """
+
+ 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,
+ ):
+ """
+ 初始化工地拓扑图谱模板。
+
+ Args:
+ llm_client (BaseChatModel): 用于拓扑提取的大模型客户端。
+ embedder (Embeddings): 用于元素去重的嵌入模型。
+ extraction_mode (str, optional): 'one_stage' 为联合提取,
+ 'two_stage' 为分步提取。默认为 "one_stage"。
+ chunk_size (int, optional): 单个文本块的最大字符数。默认为 2048。
+ chunk_overlap (int, optional): 文本块之间的重叠字符数。默认为 256。
+ max_workers (int, optional): 并行处理的工作线程数。默认为 10。
+ verbose (bool, optional): 是否启用详细日记记录。默认为 False。
+ **kwargs (Any): 传递给 AutoSpatialGraph 基类的其他参数。
+ """
+ super().__init__(
+ node_schema=PhysicalElementNode,
+ edge_schema=SpatialRelation,
+ node_key_extractor=lambda x: x.name.strip().lower(),
+ edge_key_extractor=lambda x: f"{x.source.strip().lower()}--{x.relation_type.lower()}--{x.target.strip().lower()}",
+ nodes_in_edge_extractor=lambda x: (x.source.strip().lower(), x.target.strip().lower()),
+ location_in_edge_extractor=lambda x: x.location_marker.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=CONSTRUCTION_SPATIAL_CONSOLIDATED_PROMPT,
+ prompt_for_node_extraction=CONSTRUCTION_SPATIAL_NODE_PROMPT,
+ prompt_for_edge_extraction=CONSTRUCTION_SPATIAL_EDGE_PROMPT,
+ **kwargs,
+ )
+
+ def show(
+ self,
+ *,
+ top_k_nodes_for_search: int = 3,
+ top_k_edges_for_search: int = 3,
+ top_k_nodes_for_chat: int = 3,
+ top_k_edges_for_chat: int = 3,
+ ) -> 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: PhysicalElementNode) -> str:
+ return f"{ node.name }"
+
+ def edge_label_extractor(edge: SpatialRelation) -> str:
+ return f"{ edge.source }"
+
+ super().show(
+ node_label_extractor=node_label_extractor,
+ edge_label_extractor=edge_label_extractor,
+ top_k_nodes_for_search=top_k_nodes_for_search,
+ top_k_edges_for_search=top_k_edges_for_search,
+ top_k_nodes_for_chat=top_k_nodes_for_chat,
+ top_k_edges_for_chat=top_k_edges_for_chat,
+ )
\ No newline at end of file
diff --git a/hyperextract/templates/zh/cybersecurity/intrusion_kill_chain.py b/hyperextract/templates/zh/cybersecurity/intrusion_kill_chain.py
new file mode 100644
index 0000000..7cae783
--- /dev/null
+++ b/hyperextract/templates/zh/cybersecurity/intrusion_kill_chain.py
@@ -0,0 +1,176 @@
+from typing import Optional, List, Any
+from pydantic import BaseModel, Field
+from langchain_core.language_models.chat_models import BaseChatModel
+from langchain_core.embeddings import Embeddings
+from hyperextract.types import AutoGraph
+
+# ==============================================================================
+# 1. Schema Definitions
+# ==============================================================================
+
+
+class AttackStageNode(BaseModel):
+ """
+ 网络攻击中的特定阶段或行动(符合 MITRE ATT&CK 或 Lockheed Kill Chain)。
+ """
+
+ stage_name: str = Field(
+ description="攻击阶段名称(例如:'初始访问'、'权限维持'、'横向移动')。"
+ )
+ tactics_used: List[str] = Field(
+ default_factory=list,
+ description="所使用的具体技术或战术(例如:'鱼叉式网络钓鱼'、'哈希传递')。",
+ )
+ adversary_tools: List[str] = Field(
+ default_factory=list,
+ description="该阶段使用的恶意软件或工具(例如:'Cobalt Strike'、'Mimikatz')。",
+ )
+ timestamp: Optional[str] = Field(None, description="行动的时间点或顺序标记。")
+
+
+class KillChainLink(BaseModel):
+ """
+ 表示攻击从一个阶段向另一个阶段演进的有向链接。
+ """
+
+ source: str = Field(description="前置攻击阶段。")
+ target: str = Field(description="后续攻击阶段。")
+ causality: str = Field(
+ "演进",
+ description="类型:'演进' (标准)、'触发' (自动执行)、'跳转' (基础设施切换)。",
+ )
+ observables: List[str] = Field(
+ default_factory=list, description="连接各阶段的入侵指标 (IOCs,如IP、Hash)。"
+ )
+
+
+# ==============================================================================
+# 2. Prompts
+# ==============================================================================
+
+CYBER_KILL_CHAIN_CONSOLIDATED_PROMPT = (
+ "你是一位数字取证专家和事件响应人员。请从事件报告中重建攻击时间线和杀伤链。\n\n"
+ "提取规则:\n"
+ "- 识别不同的攻击阶段作为节点。\n"
+ "- 映射攻击在时间上和逻辑上的演进过程(边)。\n"
+ "- 捕获每个阶段的具体战术、工具 (TTPs) 和时间戳。\n"
+ "- 重点关注攻击者是如何移动以及在何时移动的。"
+)
+
+CYBER_KILL_CHAIN_NODE_PROMPT = (
+ "识别所有可疑活动和网络入侵的不同阶段。\n\n"
+ "提取规则:\n"
+ "- 提取以行动为导向的阶段(例如:'投放载荷'、'建立C2')。\n"
+ "- 识别与每个发现相关的工具和技术。\n"
+ "- 在此阶段不要建立各阶段之间的连接。"
+)
+
+CYBER_KILL_CHAIN_EDGE_PROMPT = (
+ "建立已识别攻击阶段之间的顺序和演进关系。\n\n"
+ "提取规则:\n"
+ "- 定义哪个阶段导致或促成了下一个阶段。\n"
+ "- 识别连接这些阶段的入侵指标(IP、哈希等)。\n"
+ "- 仅在已识别的列表阶段之间创建连接。"
+)
+
+# ==============================================================================
+# 3. Template Class
+# ==============================================================================
+
+
+class IntrusionKillChainGraph(AutoGraph[AttackStageNode, KillChainLink]):
+ """
+ 用于重建网络攻击生命周期的模板。
+
+ 适用于事件响应 (IR)、取证报告和威胁狩猎。
+
+ Example:
+ >>> from langchain_openai import ChatOpenAI, OpenAIEmbeddings
+ >>> kill_chain = IntrusionKillChainGraph(llm_client=llm, embedder=embedder)
+ >>> kill_chain.feed_text("攻击者通过鱼叉式网络钓鱼邮件获得了初始访问。")
+ >>> print(kill_chain.nodes) # 获取攻击阶段
+ >>> print(kill_chain.edges) # 获取演进链接
+ """
+
+ 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,
+ ):
+ """
+ 初始化入侵杀伤链图谱模板。
+
+ Args:
+ llm_client (BaseChatModel): 用于杀伤链提取的大模型客户端。
+ embedder (Embeddings): 用于阶段去重的嵌入模型。
+ extraction_mode (str, optional): 'one_stage' 为联合提取,
+ 'two_stage' 为分步提取。默认为 "one_stage"。
+ chunk_size (int, optional): 单个文本块的最大字符数。默认为 2048。
+ chunk_overlap (int, optional): 文本块之间的重叠字符数。默认为 256。
+ max_workers (int, optional): 并行处理的工作线程数。默认为 10。
+ verbose (bool, optional): 是否启用详细日记记录。默认为 False。
+ **kwargs (Any): 传递给 AutoGraph 基类的其他参数。
+ """
+ super().__init__(
+ node_schema=AttackStageNode,
+ edge_schema=KillChainLink,
+ node_key_extractor=lambda x: x.stage_name.strip().lower(),
+ edge_key_extractor=lambda x: (
+ f"{x.source.strip().lower()}->{x.target.strip().lower()}"
+ ),
+ nodes_in_edge_extractor=lambda x: (
+ x.source.strip().lower(),
+ x.target.strip().lower(),
+ ),
+ 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=CYBER_KILL_CHAIN_CONSOLIDATED_PROMPT,
+ prompt_for_node_extraction=CYBER_KILL_CHAIN_NODE_PROMPT,
+ prompt_for_edge_extraction=CYBER_KILL_CHAIN_EDGE_PROMPT,
+ **kwargs,
+ )
+
+ def show(
+ self,
+ *,
+ top_k_nodes_for_search: int = 3,
+ top_k_edges_for_search: int = 3,
+ top_k_nodes_for_chat: int = 3,
+ top_k_edges_for_chat: int = 3,
+ ) -> 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: AttackStageNode) -> str:
+ info = f" ({ node.timestamp })" if getattr(node, "timestamp", None) else ""
+ return f"{ node.stage_name }{info}"
+
+ def edge_label_extractor(edge: KillChainLink) -> str:
+ return f"{ edge.source }"
+
+ super().show(
+ node_label_extractor=node_label_extractor,
+ edge_label_extractor=edge_label_extractor,
+ top_k_nodes_for_search=top_k_nodes_for_search,
+ top_k_edges_for_search=top_k_edges_for_search,
+ top_k_nodes_for_chat=top_k_nodes_for_chat,
+ top_k_edges_for_chat=top_k_edges_for_chat,
+ )
\ No newline at end of file
diff --git a/hyperextract/templates/zh/cybersecurity/threat_intelligence_nexus.py b/hyperextract/templates/zh/cybersecurity/threat_intelligence_nexus.py
new file mode 100644
index 0000000..cbcf124
--- /dev/null
+++ b/hyperextract/templates/zh/cybersecurity/threat_intelligence_nexus.py
@@ -0,0 +1,176 @@
+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.types import AutoHypergraph
+
+# ==============================================================================
+# 1. Schema Definitions
+# ==============================================================================
+
+
+class ThreatActorNode(BaseModel):
+ """
+ 负责网络威胁的实体(个人、团体或国家级组织)。
+ """
+
+ actor_name: str = Field(
+ description="别名或团体名称(例如:'APT28'、'Fancy Bear'、'Lazarus Group')。"
+ )
+ origin_country: Optional[str] = Field(None, description="疑似地理来源。")
+ motivation: Optional[str] = Field(
+ None,
+ description="主要驱动因素:'间谍活动'、'经济利益'、'破坏'、'黑客行动主义'。",
+ )
+ typical_targets: List[str] = Field(
+ default_factory=list, description="通常的目标行业或地区。"
+ )
+
+
+class IntelligenceNexus(BaseModel):
+ """
+ 连接攻击者、特定基础设施和战役主题的超边。
+ """
+
+ participants: List[str] = Field(
+ description="涉及的实体:攻击者名称、工具名称、基础设施(IP/域名)、受害组织。"
+ )
+ campaign_name: str = Field(
+ description="已知的行动名称(例如:'SolarWinds 供应链攻击')。"
+ )
+ shared_ttp: str = Field(
+ description="连接这些不同实体的共同战术、技术或过程(TTP)。"
+ )
+ confidence_level: str = Field("中", description="分析置信度:'高'、'中'、'低'。")
+
+
+# ==============================================================================
+# 2. Prompts
+# ==============================================================================
+
+CYBER_NEXUS_CONSOLIDATED_PROMPT = (
+ "你是一位战略威胁情报分析师。请映射攻击者、基础设施及其战役之间的高层级关联。\n\n"
+ "提取规则:\n"
+ "- 识别威胁攻击者及其别名作为节点。\n"
+ "- 创建超边 (Nexuses),在命名的战役下,将攻击者与特定工具和基础设施关联起来。\n"
+ "- 重点关注揭示协同行动的多点连接,而非简单的二元链接。\n"
+ "- 突出显示统一该 Nexus 的共享 TTPs。"
+)
+
+CYBER_NEXUS_NODE_PROMPT = (
+ "识别威胁攻击者、恶意软件家族和恶意基础设施实体。\n\n"
+ "提取规则:\n"
+ "- 提取攻击者别名和团体名称。\n"
+ "- 识别攻击动机和目标行业。\n"
+ "- 在此阶段不要建立战役关联。"
+)
+
+CYBER_NEXUS_EDGE_PROMPT = (
+ "识别统一了攻击者、工具和目标的行动战役 (Campaigns) 或关联 (Nexuses)。\n\n"
+ "提取规则:\n"
+ "- 每个超边必须描述一个单一的战役或行动。\n"
+ "- 将攻击者关联到在该行动中使用的特定基础设施和工具。\n"
+ "- 仅在已识别的列表实体之间创建连接。"
+)
+
+# ==============================================================================
+# 3. Template Class
+# ==============================================================================
+
+
+class ThreatIntelligenceNexusHypergraph(
+ AutoHypergraph[ThreatActorNode, IntelligenceNexus]
+):
+ """
+ 用于战略威胁情报映射的模板。
+
+ 适用于在复杂战役中连接基础设施、攻击者和恶意软件。
+
+ Example:
+ >>> from langchain_openai import ChatOpenAI, OpenAIEmbeddings
+ >>> nexus = ThreatIntelligenceNexusHypergraph(llm_client=llm, embedder=embedder)
+ >>> nexus.feed_text("APT28 在 'Fancy-Phish' 行动中使用了 X-Tunnel。")
+ >>> print(nexus.nodes) # 获取威胁攻击者
+ >>> print(nexus.edges) # 获取战役超边
+ """
+
+ 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,
+ ):
+ """
+ 初始化威胁情报关联超图模板。
+
+ Args:
+ llm_client (BaseChatModel): 用于关联提取的大模型客户端。
+ embedder (Embeddings): 用于攻击者去重的嵌入模型。
+ extraction_mode (str, optional): 'one_stage' 为联合提取,
+ 'two_stage' 为分步提取。默认为 "one_stage"。
+ chunk_size (int, optional): 单个文本块的最大字符数。默认为 2048。
+ chunk_overlap (int, optional): 文本块之间的重叠字符数。默认为 256。
+ max_workers (int, optional): 并行处理的工作线程数。默认为 10。
+ verbose (bool, optional): 是否启用详细日记记录。默认为 False。
+ **kwargs (Any): 传递给 AutoHypergraph 基类的其他参数。
+ """
+ super().__init__(
+ node_schema=ThreatActorNode,
+ edge_schema=IntelligenceNexus,
+ node_key_extractor=lambda x: x.actor_name.strip().lower(),
+ edge_key_extractor=lambda x: (
+ f"{x.campaign_name.strip().lower()}_linking_{sorted(x.participants)}"
+ ),
+ nodes_in_edge_extractor=lambda x: [
+ p.strip().lower() for p in x.participants
+ ],
+ 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=CYBER_NEXUS_CONSOLIDATED_PROMPT,
+ prompt_for_node_extraction=CYBER_NEXUS_NODE_PROMPT,
+ prompt_for_edge_extraction=CYBER_NEXUS_EDGE_PROMPT,
+ **kwargs,
+ )
+
+ def show(
+ self,
+ *,
+ top_k_nodes_for_search: int = 3,
+ top_k_edges_for_search: int = 3,
+ top_k_nodes_for_chat: int = 3,
+ top_k_edges_for_chat: int = 3,
+ ) -> 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: ThreatActorNode) -> str:
+ return f"{ node.actor_name }"
+
+ def edge_label_extractor(edge: IntelligenceNexus) -> str:
+ return f"{ edge.participants }"
+
+ super().show(
+ node_label_extractor=node_label_extractor,
+ edge_label_extractor=edge_label_extractor,
+ top_k_nodes_for_search=top_k_nodes_for_search,
+ top_k_edges_for_search=top_k_edges_for_search,
+ top_k_nodes_for_chat=top_k_nodes_for_chat,
+ top_k_edges_for_chat=top_k_edges_for_chat,
+ )
\ No newline at end of file
diff --git a/hyperextract/templates/zh/cybersecurity/vulnerability_catalog.py b/hyperextract/templates/zh/cybersecurity/vulnerability_catalog.py
new file mode 100644
index 0000000..ff42aaa
--- /dev/null
+++ b/hyperextract/templates/zh/cybersecurity/vulnerability_catalog.py
@@ -0,0 +1,120 @@
+from typing import Optional, List, Any
+from pydantic import BaseModel, Field
+from langchain_core.language_models.chat_models import BaseChatModel
+from langchain_core.embeddings import Embeddings
+from hyperextract.types import AutoSet
+
+# ==============================================================================
+# 1. Schema Definitions
+# ==============================================================================
+
+
+class VulnerabilityEntry(BaseModel):
+ """
+ 系统中识别出的特定安全漏洞或弱点。
+ """
+
+ vulnerability_id: str = Field(
+ description="唯一标识符(例如:CVE-2023-1234, CWE-79)。"
+ )
+ title: str = Field(description="漏洞的简短描述性名称。")
+ severity: str = Field(
+ description="严重程度:'危急'、'高危'、'中危'、'低危'、'提示'。"
+ )
+ cvss_score: Optional[float] = Field(
+ None, description="CVSS 基础评分(0.0 到 10.0)。"
+ )
+ affected_components: List[str] = Field(
+ default_factory=list, description="受影响的软件或硬件组件。"
+ )
+ remediation: Optional[str] = Field(None, description="建议的修复或缓解措施。")
+
+
+# ==============================================================================
+# 2. Prompts
+# ==============================================================================
+
+CYBER_VULN_PROMPT = (
+ "你是一位资深安全分析师和漏洞研究员。请从威胁报告、安全公告或扫描结果中提取并整合安全漏洞。\n\n"
+ "提取规则:\n"
+ "- 通过 CVE/CWE ID 或名称识别唯一的漏洞。\n"
+ "- 准确捕获严重级别和 CVSS 评分。\n"
+ "- 将同一漏洞的多次提及合并为单个条目,并汇总受影响组件和修复建议的信息。"
+)
+
+# ==============================================================================
+# 3. Template Class
+# ==============================================================================
+
+
+class VulnerabilityUnifiedCatalog(AutoSet[VulnerabilityEntry]):
+ """
+ 用于构建去重后的安全漏洞目录的模板。
+
+ 适用于处理多个安全公告并创建用于补丁管理的清单。
+
+ Example:
+ >>> from langchain_openai import ChatOpenAI, OpenAIEmbeddings
+ >>> catalog = VulnerabilityUnifiedCatalog(llm_client=llm, embedder=embedder)
+ >>> catalog.feed_text("CVE-2024-0001 是内核中的一个危急 RCE 漏洞。")
+ >>> catalog.feed_text("该内核 RCE (CVE-2024-0001) 需要立即修复。")
+ >>> print(catalog.items) # CVE-2024-0001 的重复条目将被合并
+ """
+
+ def __init__(
+ self,
+ llm_client: BaseChatModel,
+ embedder: Embeddings,
+ *,
+ chunk_size: int = 2048,
+ chunk_overlap: int = 256,
+ max_workers: int = 10,
+ verbose: bool = False,
+ **kwargs: Any,
+ ):
+ """
+ 初始化安全漏洞目录模板。
+
+ Args:
+ llm_client (BaseChatModel): 用于漏洞提取的大模型客户端。
+ embedder (Embeddings): 用于条目去重的嵌入模型。
+ chunk_size (int, optional): 单个文本块的最大字符数。默认为 2048。
+ chunk_overlap (int, optional): 文本块之间的重叠字符数。默认为 256。
+ max_workers (int, optional): 并行处理的工作线程数。默认为 10。
+ verbose (bool, optional): 是否启用详细日记记录。默认为 False。
+ **kwargs (Any): 传递给 AutoSet 基类的其他参数。
+ """
+ super().__init__(
+ item_schema=VulnerabilityEntry,
+ key_extractor=lambda x: x.vulnerability_id.strip().upper(),
+ llm_client=llm_client,
+ embedder=embedder,
+ chunk_size=chunk_size,
+ chunk_overlap=chunk_overlap,
+ max_workers=max_workers,
+ verbose=verbose,
+ prompt=CYBER_VULN_PROMPT,
+ **kwargs,
+ )
+
+ def show(
+ self,
+ *,
+ top_k_for_search: int = 3,
+ top_k_for_chat: int = 3,
+ ) -> None:
+ """
+ Visualize the collection using OntoSight.
+
+ Args:
+ top_k_for_search (int): Number of items to retrieve for search context. Default 3.
+ top_k_for_chat (int): Number of items to retrieve for chat context. Default 3.
+ """
+ def item_label_extractor(item: VulnerabilityEntry) -> str:
+ return f"{ item.title }"
+
+ super().show(
+ item_label_extractor=item_label_extractor,
+ top_k_for_search=top_k_for_search,
+ top_k_for_chat=top_k_for_chat,
+ )
\ No newline at end of file
diff --git a/hyperextract/templates/zh/education/academic_lexicon.py b/hyperextract/templates/zh/education/academic_lexicon.py
index 68b80cd..12f0313 100644
--- a/hyperextract/templates/zh/education/academic_lexicon.py
+++ b/hyperextract/templates/zh/education/academic_lexicon.py
@@ -95,3 +95,24 @@ class AcademicLexiconSet(AutoSet[AcademicTerm]):
prompt=ACADEMIC_LEXICON_PROMPT,
**kwargs,
)
+ def show(
+ self,
+ *,
+ top_k_for_search: int = 3,
+ top_k_for_chat: int = 3,
+ ) -> None:
+ """
+ Visualize the collection using OntoSight.
+
+ Args:
+ top_k_for_search (int): Number of items to retrieve for search context. Default 3.
+ top_k_for_chat (int): Number of items to retrieve for chat context. Default 3.
+ """
+ def item_label_extractor(item: AcademicTerm) -> str:
+ return f"{ item.term }"
+
+ super().show(
+ item_label_extractor=item_label_extractor,
+ top_k_for_search=top_k_for_search,
+ top_k_for_chat=top_k_for_chat,
+ )
diff --git a/hyperextract/templates/zh/education/interdisciplinary_nexus.py b/hyperextract/templates/zh/education/interdisciplinary_nexus.py
index cd2086f..85f62cf 100644
--- a/hyperextract/templates/zh/education/interdisciplinary_nexus.py
+++ b/hyperextract/templates/zh/education/interdisciplinary_nexus.py
@@ -116,3 +116,34 @@ class InterdisciplinaryNexusHypergraph(AutoHypergraph[ConceptNode, Interdiscipli
prompt_for_edge_extraction=NEXUS_EDGE_PROMPT,
**kwargs,
)
+ def show(
+ self,
+ *,
+ top_k_nodes_for_search: int = 3,
+ top_k_edges_for_search: int = 3,
+ top_k_nodes_for_chat: int = 3,
+ top_k_edges_for_chat: int = 3,
+ ) -> 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: ConceptNode) -> str:
+ return f"{ node.name }"
+
+ def edge_label_extractor(edge: InterdisciplinaryEdge) -> str:
+ return f"{ edge.participants }"
+
+ super().show(
+ node_label_extractor=node_label_extractor,
+ edge_label_extractor=edge_label_extractor,
+ top_k_nodes_for_search=top_k_nodes_for_search,
+ top_k_edges_for_search=top_k_edges_for_search,
+ top_k_nodes_for_chat=top_k_nodes_for_chat,
+ top_k_edges_for_chat=top_k_edges_for_chat,
+ )
diff --git a/hyperextract/templates/zh/education/prerequisite_map.py b/hyperextract/templates/zh/education/prerequisite_map.py
index 19fca73..bbd0fe5 100644
--- a/hyperextract/templates/zh/education/prerequisite_map.py
+++ b/hyperextract/templates/zh/education/prerequisite_map.py
@@ -130,3 +130,35 @@ class PrerequisiteMapGraph(AutoGraph[KnowledgePointNode, PrerequisiteRelation]):
prompt_for_edge_extraction=EDUCATION_EDGE_PROMPT,
**kwargs,
)
+ def show(
+ self,
+ *,
+ top_k_nodes_for_search: int = 3,
+ top_k_edges_for_search: int = 3,
+ top_k_nodes_for_chat: int = 3,
+ top_k_edges_for_chat: int = 3,
+ ) -> 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: KnowledgePointNode) -> str:
+ info = f" ({ node.description })" if getattr(node, "description", None) else ""
+ return f"{ node.name }{info}"
+
+ def edge_label_extractor(edge: PrerequisiteRelation) -> str:
+ return f"{ edge.source }"
+
+ super().show(
+ node_label_extractor=node_label_extractor,
+ edge_label_extractor=edge_label_extractor,
+ top_k_nodes_for_search=top_k_nodes_for_search,
+ top_k_edges_for_search=top_k_edges_for_search,
+ top_k_nodes_for_chat=top_k_nodes_for_chat,
+ top_k_edges_for_chat=top_k_edges_for_chat,
+ )
diff --git a/hyperextract/templates/zh/general/concept_map.py b/hyperextract/templates/zh/general/concept_map.py
index 0e43f6d..6bdbcd0 100644
--- a/hyperextract/templates/zh/general/concept_map.py
+++ b/hyperextract/templates/zh/general/concept_map.py
@@ -111,3 +111,34 @@ class ConceptMap(AutoGraph[Concept, ConceptRelation]):
prompt_for_edge_extraction=CONCEPT_MAP_EDGE_PROMPT,
**kwargs
)
+ def show(
+ self,
+ *,
+ top_k_nodes_for_search: int = 3,
+ top_k_edges_for_search: int = 3,
+ top_k_nodes_for_chat: int = 3,
+ top_k_edges_for_chat: int = 3,
+ ) -> 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 }"
+
+ def edge_label_extractor(edge: ConceptRelation) -> str:
+ return f"{ edge.source }"
+
+ super().show(
+ node_label_extractor=node_label_extractor,
+ edge_label_extractor=edge_label_extractor,
+ top_k_nodes_for_search=top_k_nodes_for_search,
+ top_k_edges_for_search=top_k_edges_for_search,
+ top_k_nodes_for_chat=top_k_nodes_for_chat,
+ top_k_edges_for_chat=top_k_edges_for_chat,
+ )
diff --git a/hyperextract/templates/zh/general/knowledge_graph.py b/hyperextract/templates/zh/general/knowledge_graph.py
index 39e626f..8971779 100644
--- a/hyperextract/templates/zh/general/knowledge_graph.py
+++ b/hyperextract/templates/zh/general/knowledge_graph.py
@@ -114,3 +114,35 @@ class KnowledgeGraph(AutoGraph[Entity, Relation]):
prompt_for_edge_extraction=KNOWLEDGE_GRAPH_EDGE_PROMPT,
**kwargs
)
+ def show(
+ self,
+ *,
+ top_k_nodes_for_search: int = 3,
+ top_k_edges_for_search: int = 3,
+ top_k_nodes_for_chat: int = 3,
+ top_k_edges_for_chat: int = 3,
+ ) -> 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}"
+
+ def edge_label_extractor(edge: Relation) -> str:
+ return f"{ edge.source }"
+
+ super().show(
+ node_label_extractor=node_label_extractor,
+ edge_label_extractor=edge_label_extractor,
+ top_k_nodes_for_search=top_k_nodes_for_search,
+ top_k_edges_for_search=top_k_edges_for_search,
+ top_k_nodes_for_chat=top_k_nodes_for_chat,
+ top_k_edges_for_chat=top_k_edges_for_chat,
+ )
diff --git a/hyperextract/templates/zh/general/logic_graph.py b/hyperextract/templates/zh/general/logic_graph.py
index c7873d5..d507aae 100644
--- a/hyperextract/templates/zh/general/logic_graph.py
+++ b/hyperextract/templates/zh/general/logic_graph.py
@@ -110,3 +110,34 @@ class LogicGraph(AutoGraph[LogicNode, LogicRelation]):
prompt_for_edge_extraction=LOGIC_GRAPH_EDGE_PROMPT,
**kwargs
)
+ def show(
+ self,
+ *,
+ top_k_nodes_for_search: int = 3,
+ top_k_edges_for_search: int = 3,
+ top_k_nodes_for_chat: int = 3,
+ top_k_edges_for_chat: int = 3,
+ ) -> 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 }"
+
+ def edge_label_extractor(edge: LogicRelation) -> str:
+ return f"{ edge.source }"
+
+ super().show(
+ node_label_extractor=node_label_extractor,
+ edge_label_extractor=edge_label_extractor,
+ top_k_nodes_for_search=top_k_nodes_for_search,
+ top_k_edges_for_search=top_k_edges_for_search,
+ top_k_nodes_for_chat=top_k_nodes_for_chat,
+ top_k_edges_for_chat=top_k_edges_for_chat,
+ )
diff --git a/hyperextract/types/list.py b/hyperextract/types/list.py
index 44b159f..bf778ec 100644
--- a/hyperextract/types/list.py
+++ b/hyperextract/types/list.py
@@ -18,21 +18,22 @@ from langchain_core.documents import Document
from langchain_core.embeddings import Embeddings
from langchain_core.language_models.chat_models import BaseChatModel
from langchain_community.vectorstores import FAISS
+from ontosight import view_graph
from .base import BaseAutoType
from hyperextract.utils.logging import logger
-Item = TypeVar("Item", bound=BaseModel)
+ItemSchema = TypeVar("ItemSchema", bound=BaseModel)
-class AutoListSchema(BaseModel, Generic[Item]):
+class AutoListSchema(BaseModel, Generic[ItemSchema]):
"""Generic schema container for list-based knowledge patterns."""
- items: List[Item] = Field(default_factory=list, description="Item list")
+ items: List[ItemSchema] = Field(default_factory=list, description="Item list")
-class AutoList(BaseAutoType[AutoListSchema[Item]], Generic[Item]):
+class AutoList(BaseAutoType[AutoListSchema[ItemSchema]], Generic[ItemSchema]):
"""AutoList - extracts a collection of objects from text.
This pattern extracts multiple independent objects from a document, suitable for
@@ -50,11 +51,11 @@ class AutoList(BaseAutoType[AutoListSchema[Item]], Generic[Item]):
if TYPE_CHECKING:
# Use generic version during type checking to maintain complete type hints
- item_list_schema: Type[AutoListSchema[Item]]
+ item_list_schema: Type[AutoListSchema[ItemSchema]]
def __init__(
self,
- item_schema: Type[Item],
+ item_schema: Type[ItemSchema],
llm_client: BaseChatModel,
embedder: Embeddings,
*,
@@ -121,11 +122,11 @@ class AutoList(BaseAutoType[AutoListSchema[Item]], Generic[Item]):
)
@property
- def items(self) -> List[Item]:
+ def items(self) -> List[ItemSchema]:
"""Returns the internal list of extracted items."""
return getattr(self._data, "items", [])
- def _create_empty_instance(self) -> "AutoList[Item]":
+ def _create_empty_instance(self) -> "AutoList[ItemSchema]":
"""Creates a new empty instance with the same configuration.
Overrides base class method to handle AutoList's item_schema parameter.
@@ -264,7 +265,7 @@ class AutoList(BaseAutoType[AutoListSchema[Item]], Generic[Item]):
logger.error("FAISS not available. Install with: pip install faiss-cpu")
raise
- def search(self, query: str, top_k: int = 3) -> List[Item]:
+ def search(self, query: str, top_k: int = 3) -> List[ItemSchema]:
"""Searches items in the list using semantic similarity.
Args:
@@ -282,7 +283,7 @@ class AutoList(BaseAutoType[AutoListSchema[Item]], Generic[Item]):
raise Exception("Vector store not initialized")
docs = self._index.similarity_search(query, k=top_k)
- results: List[Item] = []
+ results: List[ItemSchema] = []
for doc in docs:
# Attempt to restore as object
try:
@@ -313,13 +314,67 @@ class AutoList(BaseAutoType[AutoListSchema[Item]], Generic[Item]):
str(folder), self.embedder, allow_dangerous_deserialization=True
)
+ def show(
+ self,
+ item_label_extractor: Callable[[ItemSchema], str],
+ *,
+ top_k_for_search: int = 3,
+ top_k_for_chat: int = 3,
+ ) -> None:
+ """Visualize the list using OntoSight.
+
+ Args:
+ item_label_extractor: A function that takes an ItemSchema and returns a string label for visualization.
+ top_k_for_search: Number of items to retrieve for search callback (default: 3).
+ top_k_for_chat: Number of items to retrieve for chat callback (default: 3).
+ """
+
+ if self._index is not None:
+ logger.info(
+ "Visualizing list with search and chat capabilities (indices detected)."
+ )
+
+ def search_callback(query: str) -> None:
+ related_items = self.search(query, top_k=top_k_for_search)
+ return related_items, []
+
+ def chat_callback(question: str) -> None:
+ response = self.chat(question, top_k=top_k_for_chat)
+ content = response.content
+ retrieved_items = response.additional_kwargs.get("retrieved_items", [])
+ return content, (retrieved_items, [])
+ else:
+ logger.info(
+ "Visualizing list without search and chat capabilities (no indices detected)."
+ )
+ search_callback = None
+ chat_callback = None
+
+ from hashlib import md5
+
+ def item_id_extractor(item: ItemSchema) -> str:
+ return md5(str(item.model_dump()).encode()).hexdigest()[:8]
+
+ view_graph(
+ node_list=self.items,
+ edge_list=[],
+ node_schema=self.item_schema,
+ edge_schema=None,
+ node_id_extractor=item_id_extractor,
+ node_ids_in_edge_extractor=None,
+ node_label_extractor=item_label_extractor,
+ edge_label_extractor=None,
+ on_search=search_callback,
+ on_chat=chat_callback,
+ )
+
# ==================== Pythonic Sequence Operations ====================
def __len__(self) -> int:
"""Returns the number of elements in the list."""
return len(self.items)
- def __getitem__(self, key: int | slice) -> Item | "AutoList[Item]":
+ def __getitem__(self, key: int | slice) -> ItemSchema | "AutoList[ItemSchema]":
"""Support index access and slicing.
Args:
@@ -353,7 +408,7 @@ class AutoList(BaseAutoType[AutoListSchema[Item]], Generic[Item]):
f"List indices must be integers or slices, not {type(key).__name__}"
)
- def __setitem__(self, index: int, item: Item) -> None:
+ def __setitem__(self, index: int, item: ItemSchema) -> None:
"""Support index assignment.
Args:
@@ -377,7 +432,7 @@ class AutoList(BaseAutoType[AutoListSchema[Item]], Generic[Item]):
self.clear_index()
self.metadata["updated_at"] = datetime.now()
- def __add__(self, other: "BaseAutoType[Item]") -> "AutoList[Item]":
+ def __add__(self, other: "BaseAutoType[ItemSchema]") -> "AutoList[ItemSchema]":
"""Operator overload for '+' to combine knowledge instances.
Supports multiple combination patterns:
@@ -459,7 +514,7 @@ class AutoList(BaseAutoType[AutoListSchema[Item]], Generic[Item]):
self.clear_index()
self.metadata["updated_at"] = datetime.now()
- def __iter__(self) -> Iterator[Item]:
+ def __iter__(self) -> Iterator[ItemSchema]:
"""Support iteration over items.
Returns:
@@ -474,7 +529,7 @@ class AutoList(BaseAutoType[AutoListSchema[Item]], Generic[Item]):
"""
return iter(self.items)
- def __contains__(self, item: Item) -> bool:
+ def __contains__(self, item: ItemSchema) -> bool:
"""Support 'in' operator for membership testing.
Args:
@@ -524,7 +579,7 @@ class AutoList(BaseAutoType[AutoListSchema[Item]], Generic[Item]):
# ==================== List Modification Methods ====================
- def append(self, item: Item) -> None:
+ def append(self, item: ItemSchema) -> None:
"""Append a single item to the end of the list.
Args:
@@ -545,7 +600,7 @@ class AutoList(BaseAutoType[AutoListSchema[Item]], Generic[Item]):
self.clear_index()
self.metadata["updated_at"] = datetime.now()
- def extend(self, items: Iterable[Item] | "AutoList[Item]") -> None:
+ def extend(self, items: Iterable[ItemSchema] | "AutoList[ItemSchema]") -> None:
"""Extend the list by appending multiple items.
Args:
@@ -586,7 +641,7 @@ class AutoList(BaseAutoType[AutoListSchema[Item]], Generic[Item]):
self.clear_index()
self.metadata["updated_at"] = datetime.now()
- def insert(self, index: int, item: Item) -> None:
+ def insert(self, index: int, item: ItemSchema) -> None:
"""Insert an item at a specific position.
Args:
@@ -609,7 +664,7 @@ class AutoList(BaseAutoType[AutoListSchema[Item]], Generic[Item]):
self.clear_index()
self.metadata["updated_at"] = datetime.now()
- def remove(self, item: Item) -> None:
+ def remove(self, item: ItemSchema) -> None:
"""Remove the first occurrence of an item from the list.
Args:
@@ -638,7 +693,7 @@ class AutoList(BaseAutoType[AutoListSchema[Item]], Generic[Item]):
# Item not found
raise ValueError(f"{item} is not in list")
- def pop(self, index: int = -1) -> Item:
+ def pop(self, index: int = -1) -> ItemSchema:
"""Remove and return an item at the given position.
Args:
@@ -668,7 +723,7 @@ class AutoList(BaseAutoType[AutoListSchema[Item]], Generic[Item]):
# ==================== Query and Utility Methods ====================
- def index(self, item: Item, start: int = 0, stop: int | None = None) -> int:
+ def index(self, item: ItemSchema, start: int = 0, stop: int | None = None) -> int:
"""Return the index of the first occurrence of item.
Args:
@@ -696,7 +751,7 @@ class AutoList(BaseAutoType[AutoListSchema[Item]], Generic[Item]):
raise ValueError(f"{item} is not in list")
- def count(self, item: Item) -> int:
+ def count(self, item: ItemSchema) -> int:
"""Return the number of times item appears in the list.
Args:
@@ -717,7 +772,7 @@ class AutoList(BaseAutoType[AutoListSchema[Item]], Generic[Item]):
count += 1
return count
- def copy(self) -> "AutoList[Item]":
+ def copy(self) -> "AutoList[ItemSchema]":
"""Create a deep copy of this AutoList instance.
Returns:
@@ -756,7 +811,7 @@ class AutoList(BaseAutoType[AutoListSchema[Item]], Generic[Item]):
self.metadata["updated_at"] = datetime.now()
def sort(
- self, key: Callable[[Item], Any] | None = None, reverse: bool = False
+ self, key: Callable[[ItemSchema], Any] | None = None, reverse: bool = False
) -> None:
"""Sort the items in place.
diff --git a/hyperextract/types/set.py b/hyperextract/types/set.py
index 773023e..12bac58 100644
--- a/hyperextract/types/set.py
+++ b/hyperextract/types/set.py
@@ -16,26 +16,29 @@ from typing import (
)
from pathlib import Path
from datetime import datetime
-from ontomem import OMem
-from ontomem.merger import MergeStrategy, create_merger, BaseMerger
from pydantic import BaseModel, Field, create_model
from langchain_core.embeddings import Embeddings
from langchain_core.language_models.chat_models import BaseChatModel
+from ontomem import OMem
+from ontomem.merger import MergeStrategy, create_merger, BaseMerger
+from ontosight import view_graph
from .base import BaseAutoType
from hyperextract.utils.logging import logger
-Item = TypeVar("Item", bound=BaseModel)
+ItemSchema = TypeVar("ItemSchema", bound=BaseModel)
-class AutoSetSchema(BaseModel, Generic[Item]):
+class AutoSetSchema(BaseModel, Generic[ItemSchema]):
"""Generic schema container for set-based knowledge patterns."""
- items: List[Item] = Field(default_factory=list, description="Set of unique items")
+ items: List[ItemSchema] = Field(
+ default_factory=list, description="Set of unique items"
+ )
-class AutoSet(BaseAutoType[AutoSetSchema[Item]], Generic[Item]):
+class AutoSet(BaseAutoType[AutoSetSchema[ItemSchema]], Generic[ItemSchema]):
"""AutoSet - extracts a unique collection of objects.
This pattern automatically deduplicates items based on a user-specified
@@ -81,14 +84,14 @@ class AutoSet(BaseAutoType[AutoSetSchema[Item]], Generic[Item]):
if TYPE_CHECKING:
# Use generic version during type checking to maintain complete type hints
- item_set_schema: Type[AutoSetSchema[Item]]
+ item_set_schema: Type[AutoSetSchema[ItemSchema]]
def __init__(
self,
- item_schema: Type[Item],
+ item_schema: Type[ItemSchema],
llm_client: BaseChatModel,
embedder: Embeddings,
- key_extractor: Callable[[Item], Any],
+ key_extractor: Callable[[ItemSchema], Any],
*,
strategy_or_merger: MergeStrategy | BaseMerger = MergeStrategy.LLM.BALANCED,
prompt: str = "",
@@ -161,7 +164,7 @@ class AutoSet(BaseAutoType[AutoSetSchema[Item]], Generic[Item]):
)
# Initialize OMem instance BEFORE calling super().__init__ so _init_internal_state can use it
- self._data_memory: OMem[Item] = OMem(
+ self._data_memory: OMem[ItemSchema] = OMem(
memory_schema=item_schema,
key_extractor=key_extractor,
llm_client=llm_client,
@@ -184,7 +187,7 @@ class AutoSet(BaseAutoType[AutoSetSchema[Item]], Generic[Item]):
# ==================== Override Instance Creation ====================
- def _create_empty_instance(self) -> "AutoSet[Item]":
+ def _create_empty_instance(self) -> "AutoSet[ItemSchema]":
"""Creates a new empty instance with the same configuration.
Overrides parent method to include AutoSet-specific parameters.
@@ -218,7 +221,7 @@ class AutoSet(BaseAutoType[AutoSetSchema[Item]], Generic[Item]):
)
@property
- def data(self) -> AutoSetSchema[Item]:
+ def data(self) -> AutoSetSchema[ItemSchema]:
"""Returns all stored knowledge (read-only access).
Returns:
@@ -235,7 +238,7 @@ class AutoSet(BaseAutoType[AutoSetSchema[Item]], Generic[Item]):
return self._data_memory.empty()
@property
- def items(self) -> List[Item]:
+ def items(self) -> List[ItemSchema]:
"""Returns the internal items as a list (for external interface compatibility).
Returns:
@@ -265,7 +268,7 @@ class AutoSet(BaseAutoType[AutoSetSchema[Item]], Generic[Item]):
"""Initialize vector index to empty state."""
self._data_memory.clear_index()
- def _set_data_state(self, data: AutoSetSchema[Item]) -> None:
+ def _set_data_state(self, data: AutoSetSchema[ItemSchema]) -> None:
"""
SET: Full Reset. Wipe OMem and refill from data (e.g., load from disk).
Called by extract() or load() where data IS the new state.
@@ -275,7 +278,7 @@ class AutoSet(BaseAutoType[AutoSetSchema[Item]], Generic[Item]):
self._data_memory.add(data.items)
self.clear_index()
- def _update_data_state(self, incoming_data: AutoSetSchema[Item]) -> None:
+ def _update_data_state(self, incoming_data: AutoSetSchema[ItemSchema]) -> None:
"""
UPDATE: Incremental merge. Add to OMem efficiently (called by feed()).
@@ -292,8 +295,8 @@ class AutoSet(BaseAutoType[AutoSetSchema[Item]], Generic[Item]):
# ==================== Core Override Methods ====================
def merge_batch_data(
- self, data_list: List[AutoSetSchema[Item]]
- ) -> AutoSetSchema[Item]:
+ self, data_list: List[AutoSetSchema[ItemSchema]]
+ ) -> AutoSetSchema[ItemSchema]:
"""Merges multiple data containers with automatic deduplication.
Pure function: Does not modify internal state.
@@ -332,7 +335,7 @@ class AutoSet(BaseAutoType[AutoSetSchema[Item]], Generic[Item]):
"""
self._data_memory.build_index(force=force)
- def search(self, query: str, top_k: int = 3) -> List[Item]:
+ def search(self, query: str, top_k: int = 3) -> List[ItemSchema]:
"""Searches items in the set using semantic similarity.
Args:
@@ -359,6 +362,57 @@ class AutoSet(BaseAutoType[AutoSetSchema[Item]], Generic[Item]):
"""Loads FAISS vector index from disk."""
self._data_memory.load_index(Path(folder_path))
+ def show(
+ self,
+ item_label_extractor: Callable[[ItemSchema], str],
+ *,
+ top_k_for_search: int = 3,
+ top_k_for_chat: int = 3,
+ ) -> None:
+ """Visualize the set using OntoSight.
+
+ Args:
+ item_label_extractor: A function that takes an ItemSchema and returns a string label for visualization.
+ top_k_for_search: Number of items to retrieve for search callback (default: 3).
+ top_k_for_chat: Number of items to retrieve for chat callback (default: 3).
+ """
+
+ if self._data_memory.has_index():
+ logger.info(
+ "Visualizing set with search and chat capabilities (indices detected)."
+ )
+
+ def search_callback(query: str) -> None:
+ related_items = self.search(query, top_k=top_k_for_search)
+ return related_items, []
+
+ def chat_callback(question: str) -> None:
+ response = self.chat(question, top_k=top_k_for_chat)
+ content = response.content
+ retrieved_items = response.additional_kwargs.get("retrieved_items", [])
+ return content, (retrieved_items, [])
+ else:
+ logger.info(
+ "Visualizing set without search and chat capabilities (no indices detected)."
+ )
+ search_callback = None
+ chat_callback = None
+
+ view_graph(
+ node_list=self.items,
+ edge_list=[],
+ node_schema=self.item_schema,
+ edge_schema=None,
+ node_id_extractor=self.key_extractor,
+ node_ids_in_edge_extractor=None,
+ node_label_extractor=item_label_extractor,
+ edge_label_extractor=None,
+ on_search=search_callback,
+ on_chat=chat_callback,
+ )
+
+ # ==================== Set Interface Methods ====================
+
def __len__(self) -> int:
"""Returns the number of unique items in the set."""
return len(self._data_memory.items)
@@ -397,7 +451,7 @@ class AutoSet(BaseAutoType[AutoSetSchema[Item]], Generic[Item]):
# ==================== Set-Specific Methods ====================
- def add(self, item: Item) -> None:
+ def add(self, item: ItemSchema) -> None:
"""Adds a single item to the set with automatic deduplication.
Args:
@@ -408,7 +462,7 @@ class AutoSet(BaseAutoType[AutoSetSchema[Item]], Generic[Item]):
self.clear_index()
self.metadata["updated_at"] = datetime.now()
- def remove(self, key: Any) -> Optional[Item]:
+ def remove(self, key: Any) -> Optional[ItemSchema]:
"""Removes an item by its unique key value.
Args:
@@ -441,7 +495,9 @@ class AutoSet(BaseAutoType[AutoSetSchema[Item]], Generic[Item]):
"""
return self._data_memory.get(key) is not None
- def get(self, key: Any, default: Optional[Item] = None) -> Optional[Item]:
+ def get(
+ self, key: Any, default: Optional[ItemSchema] = None
+ ) -> Optional[ItemSchema]:
"""Gets an item by its unique key value.
Args:
@@ -454,7 +510,7 @@ class AutoSet(BaseAutoType[AutoSetSchema[Item]], Generic[Item]):
result = self._data_memory.get(key)
return result if result is not None else default
- def update(self, items: List[Item]) -> None:
+ def update(self, items: List[ItemSchema]) -> None:
"""Batch adds multiple items.
Args:
@@ -485,7 +541,7 @@ class AutoSet(BaseAutoType[AutoSetSchema[Item]], Generic[Item]):
except (KeyError, Exception):
pass
- def pop(self) -> Item:
+ def pop(self) -> ItemSchema:
"""Removes and returns an arbitrary item from the set.
Args:
@@ -517,7 +573,7 @@ class AutoSet(BaseAutoType[AutoSetSchema[Item]], Generic[Item]):
return item
- def copy(self) -> "AutoSet[Item]":
+ def copy(self) -> "AutoSet[ItemSchema]":
"""Creates a deep copy of the set.
Returns:
@@ -540,7 +596,7 @@ class AutoSet(BaseAutoType[AutoSetSchema[Item]], Generic[Item]):
# ==================== Set Operations ====================
- def __or__(self, other: "AutoSet[Item]") -> "AutoSet[Item]":
+ def __or__(self, other: "AutoSet[ItemSchema]") -> "AutoSet[ItemSchema]":
"""Union operation: set1 | set2.
Returns a new set containing all items from both sets.
@@ -572,7 +628,7 @@ class AutoSet(BaseAutoType[AutoSetSchema[Item]], Generic[Item]):
return new_set
- def __and__(self, other: "AutoSet[Item]") -> "AutoSet[Item]":
+ def __and__(self, other: "AutoSet[ItemSchema]") -> "AutoSet[ItemSchema]":
"""Intersection operation: set1 & set2.
Returns a new set containing only items present in both sets.
@@ -611,7 +667,7 @@ class AutoSet(BaseAutoType[AutoSetSchema[Item]], Generic[Item]):
return new_set
- def __sub__(self, other: "AutoSet[Item]") -> "AutoSet[Item]":
+ def __sub__(self, other: "AutoSet[ItemSchema]") -> "AutoSet[ItemSchema]":
"""Difference operation: set1 - set2.
Returns a new set containing items in self but not in other.
@@ -650,7 +706,7 @@ class AutoSet(BaseAutoType[AutoSetSchema[Item]], Generic[Item]):
return new_set
- def __xor__(self, other: "AutoSet[Item]") -> "AutoSet[Item]":
+ def __xor__(self, other: "AutoSet[ItemSchema]") -> "AutoSet[ItemSchema]":
"""Symmetric difference operation: set1 ^ set2.
Returns a new set containing items in either set but not in both.
@@ -700,19 +756,21 @@ class AutoSet(BaseAutoType[AutoSetSchema[Item]], Generic[Item]):
# ==================== Named Set Operations ====================
- def union(self, other: "AutoSet[Item]") -> "AutoSet[Item]":
+ def union(self, other: "AutoSet[ItemSchema]") -> "AutoSet[ItemSchema]":
"""Union operation (named method)."""
return self | other
- def intersection(self, other: "AutoSet[Item]") -> "AutoSet[Item]":
+ def intersection(self, other: "AutoSet[ItemSchema]") -> "AutoSet[ItemSchema]":
"""Intersection operation (named method)."""
return self & other
- def difference(self, other: "AutoSet[Item]") -> "AutoSet[Item]":
+ def difference(self, other: "AutoSet[ItemSchema]") -> "AutoSet[ItemSchema]":
"""Difference operation (named method)."""
return self - other
- def symmetric_difference(self, other: "AutoSet[Item]") -> "AutoSet[Item]":
+ def symmetric_difference(
+ self, other: "AutoSet[ItemSchema]"
+ ) -> "AutoSet[ItemSchema]":
"""Symmetric difference operation (named method)."""
return self ^ other
@@ -749,7 +807,7 @@ class AutoSet(BaseAutoType[AutoSetSchema[Item]], Generic[Item]):
"""
return not self.__eq__(other)
- def __le__(self, other: "AutoSet[Item]") -> bool:
+ def __le__(self, other: "AutoSet[ItemSchema]") -> bool:
"""Subset comparison: set1 <= set2.
Args:
@@ -775,7 +833,7 @@ class AutoSet(BaseAutoType[AutoSetSchema[Item]], Generic[Item]):
return set(self.keys).issubset(set(other.keys))
- def __lt__(self, other: "AutoSet[Item]") -> bool:
+ def __lt__(self, other: "AutoSet[ItemSchema]") -> bool:
"""Proper subset comparison: set1 < set2.
Args:
@@ -792,7 +850,7 @@ class AutoSet(BaseAutoType[AutoSetSchema[Item]], Generic[Item]):
return self <= other and self != other
- def __ge__(self, other: "AutoSet[Item]") -> bool:
+ def __ge__(self, other: "AutoSet[ItemSchema]") -> bool:
"""Superset comparison: set1 >= set2.
Args:
@@ -815,7 +873,7 @@ class AutoSet(BaseAutoType[AutoSetSchema[Item]], Generic[Item]):
return set(self.keys).issuperset(set(other.keys))
- def __gt__(self, other: "AutoSet[Item]") -> bool:
+ def __gt__(self, other: "AutoSet[ItemSchema]") -> bool:
"""Proper superset comparison: set1 > set2.
Args:
@@ -832,7 +890,7 @@ class AutoSet(BaseAutoType[AutoSetSchema[Item]], Generic[Item]):
return self >= other and self != other
- def issubset(self, other: "AutoSet[Item]") -> bool:
+ def issubset(self, other: "AutoSet[ItemSchema]") -> bool:
"""Test whether every key in the set is in other.
Args:
@@ -846,7 +904,7 @@ class AutoSet(BaseAutoType[AutoSetSchema[Item]], Generic[Item]):
"""
return self <= other
- def issuperset(self, other: "AutoSet[Item]") -> bool:
+ def issuperset(self, other: "AutoSet[ItemSchema]") -> bool:
"""Test whether every key in other is in the set.
Args:
@@ -860,7 +918,7 @@ class AutoSet(BaseAutoType[AutoSetSchema[Item]], Generic[Item]):
"""
return self >= other
- def isdisjoint(self, other: "AutoSet[Item]") -> bool:
+ def isdisjoint(self, other: "AutoSet[ItemSchema]") -> bool:
"""Test whether the set has no keys in common with other.
Args: