Skip to content

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

Remove chemistry and environment templates from Chinese localization, updating README to reflect the changes in available domains.

yifanfeng97527841151f53
diff --git a/hyperextract/templates/en/README.md b/hyperextract/templates/en/README.md
index 1a8ba3b..db6d303 100644
--- a/hyperextract/templates/en/README.md
+++ b/hyperextract/templates/en/README.md
@@ -13,9 +13,7 @@ Welcome to the **Hyper-Extract Domain Templates** library. This directory contai
| :--- | :--- | :--- |
| **`agriculture`** | Farming, livestock, and crop management. | Crop rotation, irrigation systems. |
| **`biology`** | Life sciences, genetics, and taxonomy. | Protein interactions, species classification. |
-| **`chemistry`** | Molecular structures and reactions. | Chemical compounds, lab experiments. |
| **`education`** | Knowledge points and curriculum. | Course prerequisites, learning paths. |
-| **`environment`** | Climate, ecology, and sustainability. | Weather patterns, carbon footprint tracking. |
| **`finance`** | Banking, stocks, and investments. | Earnings reports, stock analysis. |
| **`food`** | Culinary, recipes, and nutrition. | Menu planning, recipe breakdown. |
| **`game`** | Game lore, mechanics, and quests. | NPC relations, quest trees, item crafting. |
diff --git a/hyperextract/templates/en/chemistry/__init__.py b/hyperextract/templates/en/chemistry/__init__.py
deleted file mode 100644
index eb82d62..0000000
--- a/hyperextract/templates/en/chemistry/__init__.py
+++ /dev/null
@@ -1,9 +0,0 @@
-from .molecular import MolecularStructureGraph
-from .reaction import ChemicalReactionHyper
-from .protocol import LabProtocolTemporal
-
-__all__ = [
- "MolecularStructureGraph",
- "ChemicalReactionHyper",
- "LabProtocolTemporal",
-]
diff --git a/hyperextract/templates/en/chemistry/molecular.py b/hyperextract/templates/en/chemistry/molecular.py
deleted file mode 100644
index 749e800..0000000
--- a/hyperextract/templates/en/chemistry/molecular.py
+++ /dev/null
@@ -1,152 +0,0 @@
-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 AtomNode(BaseModel):
- """
- An individual atom in a chemical structure.
- """
-
- name: str = Field(
- description="Atom identifier (e.g., 'C1', 'O2') or element symbol."
- )
- element: str = Field(description="The chemical element (e.g., 'Carbon', 'H').")
- hybridization: Optional[str] = Field(
- None, description="Electronic state: 'sp3', 'sp2', 'sp'."
- )
- formal_charge: int = Field(
- 0, description="The electrical charge assigned to the atom."
- )
-
-
-class ChemicalBond(BaseModel):
- """
- A chemical bond between two atoms, including spatial/positional metadata.
- """
-
- source: str = Field(description="The originating atom (e.g., 'C1').")
- target: str = Field(description="The connected atom (e.g., 'C2').")
- bond_type: str = Field(
- description="Type: 'Single', 'Double', 'Triple', 'Aromatic', 'Hydrogen'."
- )
- spatial_position: str = Field(
- description="The physical or logical position in the molecule (e.g., 'C4 position', 'Axial', 'Equatorial')."
- )
- bond_length: Optional[float] = Field(
- None, description="Distance between atoms in Angstroms."
- )
-
-
-# ==============================================================================
-# 2. Prompts
-# ==============================================================================
-
-_PROMPT = (
- "You are a computational chemist and crystallographer. Extract the 3D molecular topology.\n\n"
- "Rules:\n"
- "- Identify every atom as a node.\n"
- "- Define bonds including their specific spatial orientation (e.g., 'Alpha', 'Beta', 'Cis', 'Trans').\n"
- "- Capture the formal charge and hybridization of each atom."
-)
-
-_NODE_PROMPT = (
- "You are a computational chemist. Your task is to identify and extract all individual atoms (Nodes) from the molecular structure.\n\n"
- "Extraction Rules:\n"
- "- Identify the unique atom identifier (e.g., C1) and its chemical element symbol.\n"
- "- Capture the electronic hybridization state (sp, sp2, sp3) and formal charge for each atom.\n"
- "- Focus on maintaining the exact nomenclature present in the source text.\n"
- "- DO NOT identify bonds or spatial connectivity at this stage."
-)
-
-_EDGE_PROMPT = (
- "You are a computational chemist. Given the list of atoms, map the chemical bonds and spatial connectivity (Edges).\n\n"
- "Extraction Rules:\n"
- "- Define the bond type (Single, Double, Aromatic, etc.) between a source and target atom.\n"
- "- Precisely identify the spatial position or stereochemical orientation (e.g., Axial, Equatorial, Cis, Trans).\n"
- "- Only create bonds between atoms that are present in the provided atom list.\n"
- "- Capture bond lengths if numerical data is provided."
-)
-
-# ==============================================================================
-# 3. Template Class
-# ==============================================================================
-
-
-class MolecularStructureGraph(AutoSpatialGraph[AtomNode, ChemicalBond]):
- """
- Spatial graph template for high-resolution molecular modeling and structural biology.
- """
-
- 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,
- ):
- super().__init__(
- node_schema=AtomNode,
- edge_schema=ChemicalBond,
- node_key_extractor=lambda x: x.name.strip(),
- edge_key_extractor=lambda x: (
- f"{x.source.strip()}--({x.bond_type})-->{x.target.strip()}"
- ),
- location_in_edge_extractor=lambda x: x.spatial_position.strip(),
- nodes_in_edge_extractor=lambda x: (x.source.strip(), x.target.strip()),
- llm_client=llm_client,
- embedder=embedder,
- extraction_mode=extraction_mode,
- chunk_size=chunk_size,
- chunk_overlap=chunk_overlap,
- max_workers=max_workers,
- verbose=verbose,
- prompt=_PROMPT,
- prompt_for_node_extraction=_NODE_PROMPT,
- prompt_for_edge_extraction=_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
deleted file mode 100644
index 0964fdb..0000000
--- a/hyperextract/templates/en/chemistry/protocol.py
+++ /dev/null
@@ -1,157 +0,0 @@
-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 AutoTemporalGraph
-
-# ==============================================================================
-# 1. Schema Definitions
-# ==============================================================================
-
-
-class LabOperation(BaseModel):
- """
- A specific step or action in a chemical or biological protocol (e.g., 'Centrifugation', 'Titration').
- """
-
- op_id: str = Field(description="Action name (e.g., 'Centrifuge', 'Heat', 'Add').")
- equipment: Optional[str] = Field(
- None, description="Device used (e.g., 'Microcentrifuge', 'Bunsen burner')."
- )
- reagents_added: List[str] = Field(
- default_factory=list,
- description="Chemicals introduced during this specific step.",
- )
- precautions: Optional[str] = Field(
- None, description="Safety or accuracy warnings (e.g., 'Keep on ice')."
- )
-
-
-class ProtocolTransition(BaseModel):
- """
- The sequential transition between protocol steps, capturing duration and order.
- """
-
- transition_id: str = Field(description="Unique id for the step transition.")
- source_step: str = Field(description="The preceding operation.")
- target_step: str = Field(description="The succeeding operation.")
- duration: Optional[str] = Field(
- None,
- description="Time spent in or between steps (e.g., '15 mins', 'Overnight').",
- )
- timestamp: str = Field(
- description="The sequence order or relative time marker (e.g., 'T+0', 'Step 2')."
- )
- wait_condition: Optional[str] = Field(
- None, description="Condition to proceed (e.g., 'Until precipitate forms')."
- )
-
-
-# ==============================================================================
-# 2. Prompts
-# ==============================================================================
-
-_PROMPT = (
- "You are a laboratory technician specializing in protocol documentation. Extract sequential lab steps.\n\n"
- "Rules:\n"
- "- Represent each operation as a Node.\n"
- "- Represent the sequence and duration between operations as Temporal Edges.\n"
- "- Capture specific reagents/equipment associated with each step.\n"
- "- Map the timeline precisely (e.g., 'Step 1' -> 'Step 2')."
-)
-
-_NODE_PROMPT = (
- "You are a laboratory technician. Your task is to identify individual protocol operations (Nodes).\n\n"
- "Extraction Rules:\n"
- "- Identify discrete actions (e.g., Centrifuge, Incubate, Titrate).\n"
- "- Note the equipment used and any reagents added during that specific step.\n"
- "- Capture safety precautions associated with the operation.\n"
- "- DO NOT establish the sequence or timing between steps at this stage."
-)
-
-_EDGE_PROMPT = (
- "You are a laboratory technician. Given the list of laboratory operations, extract the sequential flow (Edges).\n\n"
- "Extraction Rules:\n"
- "- Connect the source step to the target step in chronological order.\n"
- "- Identify the duration of the transition and the specific timestamp or sequence number.\n"
- "- Record any wait conditions required before moving to the next operation.\n"
- "- Only reference operations that exist in the provided operations list."
-)
-
-# ==============================================================================
-# 3. Template Class
-# ==============================================================================
-
-
-class LabProtocolTemporal(AutoTemporalGraph[LabOperation, ProtocolTransition]):
- """
- Temporal graph template for capturing step-by-step laboratory procedures and experimental workflows.
- """
-
- 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,
- ):
- super().__init__(
- node_schema=LabOperation,
- edge_schema=ProtocolTransition,
- node_key_extractor=lambda x: x.op_id.strip(),
- edge_key_extractor=lambda x: x.transition_id.strip(),
- nodes_in_edge_extractor=lambda x: (
- x.source_step.strip(),
- x.target_step.strip(),
- ),
- 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=_PROMPT,
- prompt_for_node_extraction=_NODE_PROMPT,
- prompt_for_edge_extraction=_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
deleted file mode 100644
index 055aa7d..0000000
--- a/hyperextract/templates/en/chemistry/reaction.py
+++ /dev/null
@@ -1,160 +0,0 @@
-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 ChemicalEntity(BaseModel):
- """
- A chemical substance (Reagent, Product, Catalyst, Solvent).
- """
-
- name: str = Field(description="IUPAC name or common name (e.g., 'Sulfuric Acid').")
- formula: Optional[str] = Field(
- None, description="Chemical formula (e.g., 'H2SO4')."
- )
- cas_number: Optional[str] = Field(None, description="CAS Registry Number.")
- phase: str = Field(
- "Unknown", description="Physical state: 'Gas', 'Liquid', 'Solid', 'Aqueous'."
- )
-
-
-class ReactionEvent(BaseModel):
- """
- A chemical reaction involving multiple reagents, specific conditions, and resulting products.
- """
-
- reaction_id: str = Field(
- description="Name or short description of the reaction (e.g., 'Haber Process')."
- )
- reactants: List[str] = Field(description="List of starting chemical entity names.")
- products: List[str] = Field(description="List of resulting chemical entity names.")
- catalysts: List[str] = Field(
- default_factory=list,
- description="Names of substances facilitating the reaction.",
- )
- solvents: List[str] = Field(
- default_factory=list, description="The medium in which the reaction occurs."
- )
- temperature: Optional[str] = Field(
- None, description="Operating temperature (e.g., '250°C')."
- )
- pressure: Optional[str] = Field(
- None, description="Operating pressure (e.g., '200 atm')."
- )
- yield_info: Optional[str] = Field(
- None, description="Percentage/amount of product obtained."
- )
-
-
-# ==============================================================================
-# 2. Prompts
-# ==============================================================================
-
-_PROMPT = (
- "You are an expert organic and inorganic chemist. Extract multi-component reaction events.\n\n"
- "Rules:\n"
- "- Represent each reaction as a Hyperedge connecting all reactants, products, and catalysts.\n"
- "- Capture detailed conditions like temperature, pressure, and specific solvents.\n"
- "- Identify the role of each chemical entity correctly in the reaction context."
-)
-
-_NODE_PROMPT = (
- "You are an organic chemist. Your task is to identify and extract all chemical entities (Nodes) mentioned in the text.\n\n"
- "Extraction Rules:\n"
- "- Identify reagents, products, catalysts, and solvents.\n"
- "- Capture IUPAC names, chemical formulas, and CAS numbers where available.\n"
- "- Note the physical phase (Gas, Liquid, Solid) of each entity.\n"
- "- DO NOT construct reaction sequences or grouping participants at this stage."
-)
-
-_EDGE_PROMPT = (
- "You are an organic chemist. Given the list of chemical entities, extract multi-component reaction events (Hyperedges).\n\n"
- "Extraction Rules:\n"
- "- Group all participants (reactants, products, catalysts, solvents) into a single reaction hyperedge.\n"
- "- Clearly specify the roles (Reactant vs. Product) for each involved entity in the metadata.\n"
- "- Only include entities that are present in the provided entity list.\n"
- "- Capture specific conditions like temperature, pressure, and yield info for the reaction event."
-)
-
-# ==============================================================================
-# 3. Template Class
-# ==============================================================================
-
-
-class ChemicalReactionHyper(AutoHypergraph[ChemicalEntity, ReactionEvent]):
- """
- Hypergraph template for complex chemical synthesis, industrial processes, and reaction pathways.
- """
-
- 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,
- ):
- super().__init__(
- node_schema=ChemicalEntity,
- edge_schema=ReactionEvent,
- 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.reactants + x.products + x.catalysts + x.solvents)
- ),
- 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=_PROMPT,
- prompt_for_node_extraction=_NODE_PROMPT,
- prompt_for_edge_extraction=_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/environment/__init__.py b/hyperextract/templates/en/environment/__init__.py
deleted file mode 100644
index 50df726..0000000
--- a/hyperextract/templates/en/environment/__init__.py
+++ /dev/null
@@ -1,11 +0,0 @@
-"""Environment domain templates for ecological and environmental analysis."""
-
-from .species_interaction import SpeciesInteractionNetwork
-from .pollutant_impact import PollutantImpactMap
-from .endangered_species import EndangeredSpeciesList
-
-__all__ = [
- "SpeciesInteractionNetwork",
- "PollutantImpactMap",
- "EndangeredSpeciesList",
-]
diff --git a/hyperextract/templates/en/environment/endangered_species.py b/hyperextract/templates/en/environment/endangered_species.py
deleted file mode 100644
index aec13fb..0000000
--- a/hyperextract/templates/en/environment/endangered_species.py
+++ /dev/null
@@ -1,127 +0,0 @@
-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.set import AutoSet
-
-# ==============================================================================
-# 1. Schema Definitions
-# ==============================================================================
-
-
-class SpeciesStatus(BaseModel):
- """Conservation and endangerment status of a species."""
-
- name: str = Field(description="Scientific or common name of the species.")
- endangered_level: str = Field(
- description="Conservation status: 'Extinct', 'Extinct in the Wild', 'Critically Endangered', 'Endangered', 'Vulnerable', 'Near Threatened', 'Least Concern'."
- )
- main_threats: Optional[str] = Field(
- None,
- description="Primary threats to the species (e.g., 'habitat loss', 'poaching', 'climate change', 'pollution')."
- )
-
-
-# ==============================================================================
-# 2. Prompts
-# ==============================================================================
-
-_PROMPT = """
-You are a conservation biologist and wildlife protection expert.
-Your task is to extract information about endangered and threatened species from conservation reports, IUCN Red List summaries, and biodiversity assessments.
-
-Guidelines:
-1. Identify all species mentioned with their conservation or endangerment status.
-2. For each species, classify it according to standard conservation levels: Extinct, Extinct in the Wild, Critically Endangered, Endangered, Vulnerable, Near Threatened, or Least Concern.
-3. Extract the primary threats that endanger the species (habitat loss, poaching, climate change, disease, pollution, etc.).
-4. Ensure scientific accuracy in species names and classification.
-5. Avoid duplication: if a species appears multiple times with the same status and threats, treat as a single entry.
-
-Output Format:
-- Each species should have a name, endangered_level, and main_threats.
-- Consolidate multiple mentions of the same species into a single comprehensive entry.
-"""
-
-# ==============================================================================
-# 3. Template Class
-# ==============================================================================
-
-
-class EndangeredSpeciesList(AutoSet[SpeciesStatus]):
- """
- Unique collection template for endangered and threatened species documentation.
-
- Transforms conservation assessments and biodiversity reports into a deduplicated
- registry of species with their endangerment status and key threats, supporting
- conservation priority-setting and resource allocation.
-
- Example:
- >>> from langchain_openai import ChatOpenAI, OpenAIEmbeddings
- >>> llm = ChatOpenAI(model="gpt-4")
- >>> embedder = OpenAIEmbeddings()
- >>>
- >>> endangered_list = EndangeredSpeciesList(llm_client=llm, embedder=embedder)
- >>> text = "The Giant Panda (Vulnerable) faces habitat loss. The Javan Rhinoceros (Critically Endangered) is threatened by poaching."
- >>> endangered_list.feed_text(text)
- >>> endangered_list.show() # Display deduplicated endangered species
- """
-
- 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 EndangeredSpeciesList template.
-
- Args:
- llm_client (BaseChatModel): Language model client for extracting species and threat information.
- embedder (Embeddings): Embedding model for semantic search and knowledge graph visualization. Note: Species accumulation is based on exact species name matching, not semantic similarity.
- chunk_size (int, optional): Maximum characters per text chunk. Defaults to 2048.
- chunk_overlap (int, optional): Overlapping characters between chunks for context preservation. Defaults to 256.
- max_workers (int, optional): Maximum concurrent extraction workers. Defaults to 10.
- verbose (bool, optional): If True, prints detailed extraction and accumulation progress logs. Defaults to False.
- **kwargs: Additional arguments passed to the AutoSet base class.
- """
- super().__init__(
- item_schema=SpeciesStatus,
- key_extractor=lambda x: x.name.strip().lower(),
- llm_client=llm_client,
- embedder=embedder,
- chunk_size=chunk_size,
- chunk_overlap=chunk_overlap,
- max_workers=max_workers,
- verbose=verbose,
- prompt=_PROMPT,
- **kwargs,
- )
-
- def show(
- self,
- *,
- top_k_for_search: int = 3,
- top_k_for_chat: int = 3,
- ) -> None:
- """Visualize the endangered species list for conservation planning.
-
- Displays the deduplicated unique collection of species with endangerment levels and threats using OntoSight.
- Internally defines frontend labels for species (name + endangered_level for conservation priority visualization).
-
- Args:
- top_k_for_search (int, optional): Number of species to retrieve from the deduplicated collection for search-triggered context. Defaults to 3.
- top_k_for_chat (int, optional): Number of species to retrieve from the deduplicated collection for chat-triggered context. Defaults to 3.
- """
-
- def item_label_extractor(item: SpeciesStatus) -> str:
- return f"{item.name} - {item.endangered_level}"
-
- 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/environment/pollutant_impact.py b/hyperextract/templates/en/environment/pollutant_impact.py
deleted file mode 100644
index 1f50d75..0000000
--- a/hyperextract/templates/en/environment/pollutant_impact.py
+++ /dev/null
@@ -1,191 +0,0 @@
-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.graph import AutoGraph
-
-# ==============================================================================
-# 1. Schema Definitions
-# ==============================================================================
-
-
-class EnvironmentalEntity(BaseModel):
- """An entity in the environmental pollution domain."""
-
- name: str = Field(description="Name of the pollutant, contaminated site, or impacted entity.")
- entity_type: str = Field(
- description="Type: 'Pollutant' (chemical, particle, etc.), 'Source' (factory, vehicle), 'Target' (water, air, soil, organism)."
- )
- description: Optional[str] = Field(
- None, description="Additional details such as chemical properties, location, or characteristics."
- )
-
-
-class PollutantImpact(BaseModel):
- """The impact relationship between a source/pollutant and a target/receptor."""
-
- source: str = Field(description="The pollutant or contamination source.")
- target: str = Field(description="The impacted entity (ecosystem, organism, location).")
- impact_type: str = Field(
- description="Type of impact: 'bioaccumulation', 'toxicity', 'contamination', 'ecosystem damage', 'health effect'."
- )
- severity: Optional[str] = Field(
- None, description="Severity level: 'critical', 'high', 'moderate', 'low'. Include any quantitative measures if available."
- )
-
-
-# ==============================================================================
-# 2. Prompts
-# ==============================================================================
-
-_PROMPT = """
-You are an environmental scientist specializing in pollution assessment and ecosystem impact analysis.
-Your task is to extract pollution sources, pollutants, and their environmental impacts from reports, monitoring data, and news articles.
-
-Guidelines:
-1. Identify all pollutants mentioned (e.g., Mercury, PM2.5, Plastic, Carbon dioxide).
-2. Identify pollution sources (factories, vehicles, agricultural runoff, etc.) and impacted targets (rivers, air, soil, human populations, wildlife).
-3. Extract the causal relationships: which pollutant affects which entity and how.
-4. Capture severity or quantitative impact information if mentioned (e.g., "50 mg/L concentration", "critical contamination level").
-5. Be precise about the directionality: pollution flows FROM source TO target.
-
-Output Format:
-- Pollutants and impacted entities should be distinct nodes.
-- Impacts should have a clear source (pollutant/source), target (receptor), impact type, and severity if available.
-"""
-
-_NODE_PROMPT = """
-You are an environmental scientist specializing in pollution assessment.
-Your task is to extract ONLY environmental entities (pollutants, sources, and targets) during the first extraction stage.
-Do not identify pollution impacts yet - focus solely on finding all entities.
-
-Guidelines:
-1. Identify all pollutants mentioned (chemicals, particles, compounds).
-2. Identify all pollution sources (factories, vehicles, sites).
-3. Identify all impacted targets (water bodies, organisms, human populations, ecosystems).
-4. For each entity, capture: name, entity_type, and any relevant characteristics.
-5. Avoid duplicates: consolidate references to the same entity.
-
-Output Format:
-- Each entity should be a distinct node with name, type (Pollutant/Source/Target), and description.
-"""
-
-_EDGE_PROMPT = """
-You are an environmental scientist analyzing pollution impacts.
-Your task is to extract ONLY the causal relationships between given entities during the second extraction stage.
-You will be provided with environmental entities (nodes). Your job is to identify pollution impacts between them.
-
-Guidelines:
-1. Given the list of confirmed entities, find all pollution impact relationships in the text.
-2. For each impact, capture: source (pollutant/source entity), target (impacted entity), impact_type, and severity.
-3. Preserve directionality: pollutant flows FROM source TO target.
-4. Only extract impacts between given entities; ignore stands-alone entity mentions.
-5. Include quantitative measures (concentrations, severity levels) if available.
-
-Output Format:
-- Each impact is an edge: Source → Target with impact_type and severity.
-"""
-
-# ==============================================================================
-# 3. Template Class
-# =============================================================================="
-
-
-class PollutantImpactMap(AutoGraph[EnvironmentalEntity, PollutantImpact]):
- """
- Knowledge graph template for environmental pollution tracking and impact assessment.
-
- Transforms environmental monitoring reports and sustainability analyses into structured
- pollution source-target relationships, enabling environmental health risk analysis.
-
- Example:
- >>> from langchain_openai import ChatOpenAI, OpenAIEmbeddings
- >>> llm = ChatOpenAI(model="gpt-4")
- >>> embedder = OpenAIEmbeddings()
- >>>
- >>> pollutant_map = PollutantImpactMap(llm_client=llm, embedder=embedder)
- >>> text = "Mercury discharged from the factory contaminated the river, causing fish toxicity at critical levels."
- >>> pollutant_map.feed_text(text)
- >>> pollutant_map.show() # Visualize pollution impacts and severity
- """
-
- 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 PollutantImpactMap template.
-
- Args:
- llm_client (BaseChatModel): Language model client for extracting pollutants and impacts.
- embedder (Embeddings): Embedding model for indexing pollutants, sources, and impacted targets.
- extraction_mode (str, optional): Extraction strategy. Defaults to "one_stage".
- - "one_stage": Extract pollutants and impacts simultaneously (faster).
- - "two_stage": Extract pollutants first, then impacts (higher accuracy).
- chunk_size (int, optional): Maximum characters per text chunk. Defaults to 2048.
- chunk_overlap (int, optional): Overlapping characters between chunks for context preservation. Defaults to 256.
- max_workers (int, optional): Maximum concurrent extraction workers for parallel processing. Defaults to 10.
- verbose (bool, optional): If True, prints detailed extraction progress and pollution tracking logs. Defaults to False.
- **kwargs: Additional arguments passed to the AutoGraph base class.
- """
- super().__init__(
- node_schema=EnvironmentalEntity,
- edge_schema=PollutantImpact,
- node_key_extractor=lambda x: x.name.strip(),
- edge_key_extractor=lambda x: f"{x.source.strip()}--({x.impact_type.lower()})-->{x.target.strip()}",
- nodes_in_edge_extractor=lambda x: (x.source.strip(), x.target.strip()),
- llm_client=llm_client,
- embedder=embedder,
- extraction_mode=extraction_mode,
- chunk_size=chunk_size,
- chunk_overlap=chunk_overlap,
- max_workers=max_workers,
- verbose=verbose,
- prompt=_PROMPT,
- prompt_for_node_extraction=_NODE_PROMPT,
- prompt_for_edge_extraction=_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 pollutant impact network for environmental assessment.
-
- Displays pollution sources, pollutants, and impacted targets with severity information using OntoSight.
- Internally defines frontend labels for entities (name + entity_type) and impacts (impact_type + severity).
-
- Args:
- top_k_nodes_for_search (int, optional): Number of environmental entities (pollutants/sources/targets) to retrieve for search context. Defaults to 3.
- top_k_edges_for_search (int, optional): Number of impact relationships to retrieve for search context. Defaults to 3.
- top_k_nodes_for_chat (int, optional): Number of environmental entities to retrieve for chat context. Defaults to 3.
- top_k_edges_for_chat (int, optional): Number of impact relationships to retrieve for chat context. Defaults to 3.
- """
-
- def node_label_extractor(node: EnvironmentalEntity) -> str:
- return f"{node.name} ({node.entity_type})"
-
- def edge_label_extractor(edge: PollutantImpact) -> str:
- severity = f" [{edge.severity}]" if edge.severity else ""
- return f"{edge.impact_type}{severity}"
-
- 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/environment/species_interaction.py b/hyperextract/templates/en/environment/species_interaction.py
deleted file mode 100644
index 57b77c2..0000000
--- a/hyperextract/templates/en/environment/species_interaction.py
+++ /dev/null
@@ -1,191 +0,0 @@
-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.graph import AutoGraph
-
-# ==============================================================================
-# 1. Schema Definitions
-# ==============================================================================
-
-
-class Species(BaseModel):
- """An organism entity in the ecosystem."""
-
- name: str = Field(description="Scientific or common name of the species.")
- category: str = Field(
- description="Taxonomic classification: 'Mammal', 'Bird', 'Insect', 'Plant', 'Microorganism', etc."
- )
- protection_level: Optional[str] = Field(
- None, description="Conservation status: 'Endangered', 'Vulnerable', 'Least Concern', etc."
- )
-
-
-class Interaction(BaseModel):
- """The biological interaction between two species."""
-
- source: str = Field(description="The initiating species (predator, competitor, etc.).")
- target: str = Field(description="The receiving species (prey, competitor, host, etc.).")
- interaction_type: str = Field(
- description="Type of interaction: 'predation', 'parasitism', 'mutualism', 'commensalism', 'competition', 'symbiosis'."
- )
- detail: Optional[str] = Field(
- None, description="Specific details about the interaction (e.g., frequency, seasonal pattern, ecological impact)."
- )
-
-
-# ==============================================================================
-# 2. Prompts
-# ==============================================================================
-
-_PROMPT = """
-You are an expert ecologist and biodiversity specialist.
-Your task is to extract the ecological interaction network from scientific texts, field reports, or nature documentaries.
-
-Guidelines:
-1. Identify all organisms mentioned in the text and classify them by category (species, type).
-2. Extract the biological relationships between organisms (predation, parasitism, competition, mutualism, etc.).
-3. For each interaction, capture the interaction type and any specific ecological details (timing, frequency, conditions).
-4. Preserve the directionality of interactions (e.g., "Lion hunts Zebra" is different from "Zebra avoids Lion").
-5. Include conservation status information if mentioned.
-
-Output Format:
-- Each species should be a distinct entity with name, category, and conservation status if available.
-- Each interaction should clearly state the source (initiating organism), target (receiving organism), and interaction type.
-"""
-
-_NODE_PROMPT = """
-You are an expert ecologist and biodiversity specialist.
-Your task is to extract ONLY species/organisms from the text during the first extraction stage.
-Do not identify interactions yet - focus solely on finding all distinct organisms mentioned.
-
-Guidelines:
-1. List all species, organisms, or populations mentioned in the text.
-2. For each organism, capture: name, category (e.g., predator, prey, plant, decomposer), and conservation status if available.
-3. Ensure no duplicates: if "Lion" and "African Lion" refer to the same species, consolidate to the most specific name.
-4. Ignore vague references; extract only explicitly mentioned organisms.
-
-Output Format:
-- Each organism should be a distinct node with clear name, category, and status.
-"""
-
-_EDGE_PROMPT = """
-You are an expert ecologist analyzing species interactions.
-Your task is to extract ONLY the biological relationships between given species during the second extraction stage.
-You will be provided with a list of already-extracted species (nodes). Your job is to identify interactions between them.
-
-Guidelines:
-1. Given the list of confirmed species, find all pairwise interactions mentioned in the text.
-2. For each interaction, capture: source species, target species, interaction type (predation, mutualism, competition, etc.), and details.
-3. Preserve directionality: "A eats B" is predation (A→B), different from "B avoids A".
-4. Only extract interactions that connect the given species; ignore mentions of species not in the provided list.
-5. Include ecological context (seasonal, frequency, conditions) as details.
-
-Output Format:
-- Each interaction is an edge: Source → Target with relation type and details.
-"""
-
-# ==============================================================================
-# 3. Template Class
-# =============================================================================="
-
-
-class SpeciesInteractionNetwork(AutoGraph[Species, Interaction]):
- """
- Knowledge graph template for ecological species interactions.
-
- Transforms scientific texts and field studies into structured interaction networks,
- enabling analysis of ecosystem biodiversity and food webs.
-
- Example:
- >>> from langchain_openai import ChatOpenAI, OpenAIEmbeddings
- >>> llm = ChatOpenAI(model="gpt-4")
- >>> embedder = OpenAIEmbeddings()
- >>>
- >>> network = SpeciesInteractionNetwork(llm_client=llm, embedder=embedder)
- >>> text = "Lions hunt zebras in the African savanna. Parasitic wasps are eaten by birds."
- >>> network.feed_text(text)
- >>> network.show() # Visualize the species interaction network
- """
-
- 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 SpeciesInteractionNetwork template.
-
- Args:
- llm_client (BaseChatModel): Language model client for species and interaction extraction.
- embedder (Embeddings): Embedding model for vector indexing of species nodes and interaction edges.
- extraction_mode (str, optional): Extraction strategy. Defaults to "one_stage".
- - "one_stage": Extract species and interactions simultaneously (faster).
- - "two_stage": Extract species first, then interactions (higher accuracy).
- chunk_size (int, optional): Maximum characters per text chunk. Defaults to 2048.
- chunk_overlap (int, optional): Overlapping characters between chunks for context preservation. Defaults to 256.
- max_workers (int, optional): Maximum concurrent extraction workers. Defaults to 10.
- verbose (bool, optional): If True, prints detailed extraction progress logs. Defaults to False.
- **kwargs: Additional arguments passed to the AutoGraph base class.
- """
- super().__init__(
- node_schema=Species,
- edge_schema=Interaction,
- node_key_extractor=lambda x: x.name.strip(),
- edge_key_extractor=lambda x: f"{x.source.strip()}--({x.interaction_type.lower()})-->{x.target.strip()}",
- nodes_in_edge_extractor=lambda x: (x.source.strip(), x.target.strip()),
- llm_client=llm_client,
- embedder=embedder,
- extraction_mode=extraction_mode,
- chunk_size=chunk_size,
- chunk_overlap=chunk_overlap,
- max_workers=max_workers,
- verbose=verbose,
- prompt=_PROMPT,
- prompt_for_node_extraction=_NODE_PROMPT,
- prompt_for_edge_extraction=_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 species interaction network for ecological analysis.
-
- Displays the extracted species (nodes) and their interactions (edges) using OntoSight visualization.
- Internally defines frontend visualization labels for species (name + category + protection status)
- and interactions (interaction type).
-
- Args:
- top_k_nodes_for_search (int, optional): Number of species nodes to retrieve for search-triggered context. Defaults to 3.
- top_k_edges_for_search (int, optional): Number of interaction edges to retrieve for search-triggered context. Defaults to 3.
- top_k_nodes_for_chat (int, optional): Number of species nodes to retrieve for chat-triggered context. Defaults to 3.
- top_k_edges_for_chat (int, optional): Number of interaction edges to retrieve for chat-triggered context. Defaults to 3.
- """
-
- def node_label_extractor(node: Species) -> str:
- status = f" [{node.protection_level}]" if node.protection_level else ""
- return f"{node.name} ({node.category}){status}"
-
- def edge_label_extractor(edge: Interaction) -> 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/zh/README.md b/hyperextract/templates/zh/README.md
index 64316a7..99713d7 100644
--- a/hyperextract/templates/zh/README.md
+++ b/hyperextract/templates/zh/README.md
@@ -22,14 +22,12 @@
| **`game`** | 游戏娱乐 | 游戏世界观设定、任务流向树、物品合成与掉落表。 |
| **`agriculture`** | 农业与养殖 | 作物生长周期、病虫害防治、畜牧谱系与饲养标准。 |
| **`biology`** | 生物科学 | 生物分类学、蛋白质相互作用、基因调控网络。 |
-| **`chemistry`** | 化学科学 | 分子结构、化学反应方程式路径、材料物化特性。 |
-| **`environment`** | 环境与气候 | 气象变化趋势、碳排放追踪、生态系统食物网。 |
| **`movie`** | 影视 | 剧本节拍器拆解、演员与幕后人员网络、影评情感。 |
| **`news`** | 新闻传媒 | 事件因果链、新闻专题聚类、深度报道事实板。 |
| **`tcm`** | **中医 (特色)** | 中草药方剂、经络穴位、辨证施治过程等。 |
| **`wuxia`** | **武侠/修真 (特色)** | 门派恩怨、功法境界、修仙等级体系、法宝神兵。 |
-*(注:英文版对应的 15 个通用领域在中文版中均有映射文件夹,中医与武侠为中文版独有)*
+*(注:英文版对应的 13 个通用领域在中文版中均有映射文件夹,中医与武侠为中文版独有)*
## 快速上手
```python
diff --git a/hyperextract/templates/zh/chemistry/__init__.py b/hyperextract/templates/zh/chemistry/__init__.py
deleted file mode 100644
index eb82d62..0000000
--- a/hyperextract/templates/zh/chemistry/__init__.py
+++ /dev/null
@@ -1,9 +0,0 @@
-from .molecular import MolecularStructureGraph
-from .reaction import ChemicalReactionHyper
-from .protocol import LabProtocolTemporal
-
-__all__ = [
- "MolecularStructureGraph",
- "ChemicalReactionHyper",
- "LabProtocolTemporal",
-]
diff --git a/hyperextract/templates/zh/chemistry/molecular.py b/hyperextract/templates/zh/chemistry/molecular.py
deleted file mode 100644
index 679397b..0000000
--- a/hyperextract/templates/zh/chemistry/molecular.py
+++ /dev/null
@@ -1,129 +0,0 @@
-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 AtomNode(BaseModel):
- """
- 化学结构中的单个原子。
- """
- name: str = Field(description="原子标识符(如:'C1'、'O2')或元素符号。")
- element: str = Field(description="化学元素(如:'碳'、'H')。")
- hybridization: Optional[str] = Field(None, description="电子状态:'sp3'、'sp2'、'sp'。")
- formal_charge: int = Field(0, description="分配给原子的形式电荷。")
-
-class ChemicalBond(BaseModel):
- """
- 两个原子之间的化学键,包括空间/位置元数据。
- """
- source: str = Field(description="起始原子(如:'C1')。")
- target: str = Field(description="连接原子(如:'C2')。")
- bond_type: str = Field(description="类型:'单键'、'双键'、'三键'、'芳香键'、'氢键'。")
- spatial_position: str = Field(description="在分子中的物理或逻辑位置(如:'C4位'、'轴向'、'赤道向')。")
- bond_length: Optional[float] = Field(None, description="原子间的距离(单位:埃)。")
-
-# ==============================================================================
-# 2. Prompts
-# ==============================================================================
-
-_PROMPT = (
- "你是一位计算化学家和晶体学家。请提取 3D 分子拓扑结构。\n\n"
- "规则:\n"
- "- 将每个原子识别为一个节点。\n"
- "- 定义化学键,包括它们的特定空间取向(例如:'Alpha'、'Beta'、'顺式'、'反式')。\n"
- "- 捕获每个原子的形式电荷和杂化轨道。"
-)
-
-_NODE_PROMPT = (
- "你是一位计算化学家。你的任务是从分子结构中识别并提取所有单个原子(节点)。\n\n"
- "提取规则:\n"
- "- 识别唯一的原子标识符(如:C1)及其化学元素符号。\n"
- "- 捕捉每个原子的电子杂化状态(sp, sp2, sp3)和形式电荷。\n"
- "- 专注于保持源文本中出现的准确命名方式。\n"
- "- 在此阶段请勿识别化学键或空间连接性。"
-)
-
-_EDGE_PROMPT = (
- "你是一位计算化学家。根据原子清单,映射化学键和空间连接性(边)。\n\n"
- "提取规则:\n"
- "- 定义源原子与目标原子之间的化学键类型(单键、双键、芳香键等)。\n"
- "- 精确识别空间位置或立体化学取向(如:轴向、赤道向、顺式、反式)。\n"
- "- 仅在提供的原子清单中存在的原子之间建立化学键。\n"
- "- 如果提供了数值数据,请捕捉键长。"
-)
-
-# ==============================================================================
-# 3. Template Class
-# ==============================================================================
-
-class MolecularStructureGraph(AutoSpatialGraph[AtomNode, ChemicalBond]):
- """
- 用于高分辨率分子建模和结构生物学的空间图模板。
- """
- 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
- ):
- super().__init__(
- node_schema=AtomNode,
- edge_schema=ChemicalBond,
- node_key_extractor=lambda x: x.name.strip(),
- edge_key_extractor=lambda x: f"{x.source.strip()}--({x.bond_type})-->{x.target.strip()}",
- location_in_edge_extractor=lambda x: x.spatial_position.strip(),
- nodes_in_edge_extractor=lambda x: (x.source.strip(), x.target.strip()),
- llm_client=llm_client,
- embedder=embedder,
- extraction_mode=extraction_mode,
- chunk_size=chunk_size,
- chunk_overlap=chunk_overlap,
- max_workers=max_workers,
- verbose=verbose,
- prompt=_PROMPT,
- prompt_for_node_extraction=_NODE_PROMPT,
- prompt_for_edge_extraction=_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/zh/chemistry/protocol.py b/hyperextract/templates/zh/chemistry/protocol.py
deleted file mode 100644
index 45d2a45..0000000
--- a/hyperextract/templates/zh/chemistry/protocol.py
+++ /dev/null
@@ -1,131 +0,0 @@
-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 AutoTemporalGraph
-
-# ==============================================================================
-# 1. Schema Definitions
-# ==============================================================================
-
-class LabOperation(BaseModel):
- """
- 化学或生物实验方案中的具体步骤或动作(如:'离心'、'滴定')。
- """
- op_id: str = Field(description="动作名称(如:'离心'、'加热'、'加入')。")
- equipment: Optional[str] = Field(None, description="使用的设备(如:'微量离心机'、'本生灯')。")
- reagents_added: List[str] = Field(default_factory=list, description="此具体步骤中引入的化学物质。")
- precautions: Optional[str] = Field(None, description="安全或准确性警告(如:'在冰上操作')。")
-
-class ProtocolTransition(BaseModel):
- """
- 实验步骤之间的顺序转换,捕获持续时间和顺序。
- """
- transition_id: str = Field(description="步骤转换的唯一标识符。")
- source_step: str = Field(description="前序操作。")
- target_step: str = Field(description="后续操作。")
- duration: Optional[str] = Field(None, description="步骤内或步骤间的耗时(如:'15 分钟'、'过夜')。")
- timestamp: str = Field(description="顺序编号或相对时间标记(如:'T+0'、'步骤 2')。")
- wait_condition: Optional[str] = Field(None, description="继续操作的条件(如:'直到形成沉淀')。")
-
-# ==============================================================================
-# 2. Prompts
-# ==============================================================================
-
-_PROMPT = (
- "你是一位擅长实验方案记录的实验室技术员。请提取顺序性的实验步骤。\n\n"
- "规则:\n"
- "- 将每个操作表示为一个节点(Node)。\n"
- "- 将操作之间的顺序和持续时间表示为时间边(Temporal Edges)。\n"
- "- 捕获与每个步骤相关的特定试剂/设备。\n"
- "- 精确映射时间线(例如:'步骤 1' -> '步骤 2')。"
-)
-
-_NODE_PROMPT = (
- "你是一位实验室技术员。你的任务是识别出单个实验方案操作(节点)。\n\n"
- "提取规则:\n"
- "- 识别离散的动作(如:离心、孵育、滴定)。\n"
- "- 记录使用的设备以及在该特定步骤中添加的任何试剂。\n"
- "- 捕捉与该操作相关的安全预防措施。\n"
- "- 在此阶段请勿建立步骤之间的顺序或时间关系。"
-)
-
-_EDGE_PROMPT = (
- "你是一位实验室技术员。根据实验室操作清单,提取顺序流向(边)。\n\n"
- "提取规则:\n"
- "- 按时间顺序将源步骤连接到目标步骤。\n"
- "- 识别转换的持续时间以及特定的时间戳或顺序编号。\n"
- "- 记录在进入下一步操作之前所需的任何等待条件。\n"
- "- 仅引用提供的操作清单中存在的操作。"
-)
-
-# ==============================================================================
-# 3. Template Class
-# ==============================================================================
-
-class LabProtocolTemporal(AutoTemporalGraph[LabOperation, ProtocolTransition]):
- """
- 用于捕获逐步实验室程序和实验工作流的时间图模板。
- """
- 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
- ):
- super().__init__(
- node_schema=LabOperation,
- edge_schema=ProtocolTransition,
- node_key_extractor=lambda x: x.op_id.strip(),
- edge_key_extractor=lambda x: x.transition_id.strip(),
- nodes_in_edge_extractor=lambda x: (x.source_step.strip(), x.target_step.strip()),
- 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=_PROMPT,
- prompt_for_node_extraction=_NODE_PROMPT,
- prompt_for_edge_extraction=_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/zh/chemistry/reaction.py b/hyperextract/templates/zh/chemistry/reaction.py
deleted file mode 100644
index 4c21603..0000000
--- a/hyperextract/templates/zh/chemistry/reaction.py
+++ /dev/null
@@ -1,132 +0,0 @@
-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 ChemicalEntity(BaseModel):
- """
- 一种化学物质(试剂、产物、催化剂、溶剂)。
- """
- name: str = Field(description="IUPAC 名称或常用名称(如:'硫酸')。")
- formula: Optional[str] = Field(None, description="化学式(如:'H2SO4')。")
- cas_number: Optional[str] = Field(None, description="CAS 登记号。")
- phase: str = Field("Unknown", description="物理状态:'气态'、'液态'、'固态'、'水溶液'。")
-
-class ReactionEvent(BaseModel):
- """
- 涉及多种试剂、特定条件和所得产物的化学反应。
- """
- reaction_id: str = Field(description="反应的名称或简短描述(如:'哈伯制氨法')。")
- reactants: List[str] = Field(description="起始化学实体名称列表。")
- products: List[str] = Field(description="所得化学实体名称列表。")
- catalysts: List[str] = Field(default_factory=list, description="促进反应的物质名称。")
- solvents: List[str] = Field(default_factory=list, description="反应发生的介质。")
- temperature: Optional[str] = Field(None, description="操作温度(如:'250°C')。")
- pressure: Optional[str] = Field(None, description="操作压力(如:'200 atm')。")
- yield_info: Optional[str] = Field(None, description="获得的产率/产量。")
-
-# ==============================================================================
-# 2. Prompts
-# ==============================================================================
-
-_PROMPT = (
- "你是一位专业的有机和无机化学家。请提取多组分的反应事件。\n\n"
- "规则:\n"
- "- 将每个反应表示为一个超边(Hyperedge),连接所有反应物、产物和催化剂。\n"
- "- 捕获详细的反应条件,如温度、压力和特定溶剂。\n"
- "- 在反应语境中正确识别每个化学实体的角色。"
-)
-
-_NODE_PROMPT = (
- "你是一位有机化学家。你的任务是识别出文本中提及的所有化学实体(节点)。\n\n"
- "提取规则:\n"
- "- 识别反应试剂、产物、催化剂和溶剂。\n"
- "- 在可用时捕捉 IUPAC 名称、化学式和 CAS 号。\n"
- "- 记录每个实体的物理状态(气态、液态、固态)。\n"
- "- 在此阶段请勿构建反应序列或对参与者进行分组。"
-)
-
-_EDGE_PROMPT = (
- "你是一位有机化学家。根据化学实体清单,提取多组分反应事件(超边)。\n\n"
- "提取规则:\n"
- "- 将所有参与者(反应物、产物、催化剂、溶剂)归入单个反应超边中。\n"
- "- 在元数据中清晰指定每个相关实体的角色(反应物与产物)。\n"
- "- 仅包含已提供的实体清单中存在的实体。\n"
- "- 捕捉反应事件的特定条件,如温度、压力和产率信息。"
-)
-
-# ==============================================================================
-# 3. Template Class
-# ==============================================================================
-
-class ChemicalReactionHyper(AutoHypergraph[ChemicalEntity, ReactionEvent]):
- """
- 用于复杂化学合成、工业流程和反应路径的超图模板。
- """
- 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
- ):
- super().__init__(
- node_schema=ChemicalEntity,
- edge_schema=ReactionEvent,
- 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.reactants + x.products + x.catalysts + x.solvents)),
- 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=_PROMPT,
- prompt_for_node_extraction=_NODE_PROMPT,
- prompt_for_edge_extraction=_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/environment/__init__.py b/hyperextract/templates/zh/environment/__init__.py
deleted file mode 100644
index 3af7f41..0000000
--- a/hyperextract/templates/zh/environment/__init__.py
+++ /dev/null
@@ -1,11 +0,0 @@
-"""环境领域模板,用于生态和环保分析。"""
-
-from .species_interaction import SpeciesInteractionNetwork
-from .pollutant_impact import PollutantImpactMap
-from .endangered_species import EndangeredSpeciesList
-
-__all__ = [
- "SpeciesInteractionNetwork",
- "PollutantImpactMap",
- "EndangeredSpeciesList",
-]
diff --git a/hyperextract/templates/zh/environment/endangered_species.py b/hyperextract/templates/zh/environment/endangered_species.py
deleted file mode 100644
index 04e5d7d..0000000
--- a/hyperextract/templates/zh/environment/endangered_species.py
+++ /dev/null
@@ -1,126 +0,0 @@
-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.set import AutoSet
-
-# ==============================================================================
-# 1. Schema 定义
-# ==============================================================================
-
-
-class SpeciesStatus(BaseModel):
- """物种的保护和濒危状态。"""
-
- name: str = Field(description="物种的学名或常用名。")
- endangered_level: str = Field(
- description="保护状态:'Extinct'(灭绝), 'Extinct in the Wild'(野外灭绝), 'Critically Endangered'(极危), 'Endangered'(濒危), 'Vulnerable'(易危), 'Near Threatened'(近危), 'Least Concern'(无危)。"
- )
- main_threats: Optional[str] = Field(
- None,
- description="物种面临的主要威胁(如'habitat loss'(栖息地丧失), 'poaching'(偷猎), 'climate change'(气候变化), 'pollution'(污染))。"
- )
-
-
-# ==============================================================================
-# 2. Prompts
-# ==============================================================================
-
-_PROMPT = """
-你是一位保护生物学和野生动物保护专家。
-你的任务是从保护报告、IUCN红色名录摘要或生物多样性评估中提取濒危物种的信息。
-
-指导原则:
-1. 识别文本中提到的所有物种及其保护或濒危状态。
-2. 对于每个物种,根据标准保护等级进行分类:灭绝、野外灭绝、极危、濒危、易危、近危或无危。
-3. 提取危害物种的主要威胁(栖息地丧失、偷猎、气候变化、疾病、污染等)。
-4. 确保物种名称和分类的科学准确性。
-5. 避免重复:如果同一物种多次出现且状态和威胁相同,应合并为一个条目。
-
-输出格式:
-- 每个物种应该有名称、濒危等级和主要威胁。
-- 将同一物种的多个提及合并为一个综合条目。
-"""
-
-# ==============================================================================
-# 3. 模板类
-# ==============================================================================
-
-
-class EndangeredSpeciesList(AutoSet[SpeciesStatus]):
- """
- 濒危和受威胁物种文档的唯一集合模板。
-
- 将保护评估和生物多样性报告转化为一个去重的物种清单,
- 包含其濒危状态和主要威胁,支持保护优先级设定和资源配置。
-
- Example:
- >>> from langchain_openai import ChatOpenAI, OpenAIEmbeddings
- >>> llm = ChatOpenAI(model="gpt-4")
- >>> embedder = OpenAIEmbeddings()
- >>>
- >>> endangered_list = EndangeredSpeciesList(llm_client=llm, embedder=embedder)
- >>> text = "大熊猫(易危)面临栖息地丧失。爪哇犀牛(极危)受到偷猎威胁。"
- >>> endangered_list.feed_text(text)
- >>> endangered_list.show() # 显示去重的濒危物种
- """
-
- 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):如果为 True,则打印详细的提取和累加进度日志。默认为 False。
- **kwargs:传递给 AutoSet 基类的参数。
- """
- super().__init__(
- item_schema=SpeciesStatus,
- key_extractor=lambda x: x.name.strip().lower(),
- llm_client=llm_client,
- embedder=embedder,
- chunk_size=chunk_size,
- chunk_overlap=chunk_overlap,
- max_workers=max_workers,
- verbose=verbose,
- prompt=_PROMPT,
- **kwargs,
- )
-
- def show(
- self,
- *,
- top_k_for_search: int = 3,
- top_k_for_chat: int = 3,
- ) -> None:
- """可视化濒危物种清单用于保护规划。
-
- 使用 OntoSight 展示去重的唯一物种集合,包含濒危等级和威胁信息。
- 在内部定义物种的前端展示标签(名称 + 濒危等级,用于保护优先级可视化)。
-
- Args:
- top_k_for_search (int, optional):搜索触发时从去重集合中检索的物种数量。默认为 3。
- top_k_for_chat (int, optional):聊天触发时从去重集合中检索的物种数量。默认为 3。
- """
-
- def item_label_extractor(item: SpeciesStatus) -> str:
- return f"{item.name} - {item.endangered_level}"
-
- 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/environment/pollutant_impact.py b/hyperextract/templates/zh/environment/pollutant_impact.py
deleted file mode 100644
index 839de18..0000000
--- a/hyperextract/templates/zh/environment/pollutant_impact.py
+++ /dev/null
@@ -1,191 +0,0 @@
-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.graph import AutoGraph
-
-# ==============================================================================
-# 1. Schema 定义
-# ==============================================================================
-
-
-class EnvironmentalEntity(BaseModel):
- """环境污染领域中的实体。"""
-
- name: str = Field(description="污染物、污染源或受影响实体的名称。")
- entity_type: str = Field(
- description="实体类型:'Pollutant'(污染物-化学物质、颗粒等), 'Source'(源头-工厂、车辆等), 'Target'(受体-水体、空气、土壤、生物)。"
- )
- description: Optional[str] = Field(
- None, description="额外信息,如化学性质、位置或特征。"
- )
-
-
-class PollutantImpact(BaseModel):
- """污染物或污染源与受影响实体之间的影响关系。"""
-
- source: str = Field(description="污染物或污染源。")
- target: str = Field(description="受影响的实体(生态系统、生物、地点)。")
- impact_type: str = Field(
- description="影响类型:'bioaccumulation'(生物富集), 'toxicity'(毒性), 'contamination'(污染), 'ecosystem damage'(生态破坏), 'health effect'(健康影响)。"
- )
- severity: Optional[str] = Field(
- None, description="严重程度:'critical'(严重), 'high'(较高), 'moderate'(中等), 'low'(轻微)。如果有,包含定量数据。"
- )
-
-
-# ==============================================================================
-# 2. Prompts
-# ==============================================================================
-
-_PROMPT = """
-你是一位环境科学家,专长于污染评估和生态影响分析。
-你的任务是从报告、监测数据和新闻文章中提取污染源、污染物及其环保影响。
-
-指导原则:
-1. 识别文本中提到的所有污染物(如汞、PM2.5、塑料、二氧化碳等)。
-2. 识别污染源(工厂、车辆、农业径流等)和受影响的对象(河流、空气、土壤、人口、野生动物)。
-3. 提取因果关系:哪种污染物影响了哪个实体以及如何影响。
-4. 记录严重程度或定量影响信息(如"50毫克/升浓度"、"污染等级为严重")。
-5. 精确表示污染流向:从污染源指向受体。
-
-输出格式:
-- 污染物和受影响的实体应该是不同的节点。
-- 影响应该有明确的源头(污染物/污染源)、目标(受体)、影响类型和严重程度(如果有)。
-"""
-
-_NODE_PROMPT = """
-你是一位专注于污染评估的环境科学家。
-你的任务是在第一个提取阶段只提取环境实体(污染物、污染源、受体),不提取污染影响。
-专注于找到所有实体。
-
-指导原则:
-1. 识别提到的所有污染物(化学物质、颗粒、化合物)。
-2. 识别所有污染源(工厂、车辆、场所)。
-3. 识别所有受体目标(水体、生物体、人口、生态系统)。
-4. 对于每个实体,捕获:名称、实体_类型,以及任何相关特征。
-5. 避免重复:整合对同一实体的引用。
-
-输出格式:
-- 每个实体应为不同的节点,包含名称、类型(污染物/污染源/受体)和描述。
-"""
-
-_EDGE_PROMPT = """
-你是一位分析污染影响的环境科学家。
-你的任务是在第二个提取阶段只提取已给定实体间的因果关系。
-你将获得环境实体(节点)列表。你的工作是识别它们之间的污染影响关系。
-
-指导原则:
-1. 根据确认的实体列表,找出文本中的所有污染影响关系。
-2. 对于每个影响,捕获:源(污染物/污染源实体)、目标(受影响实体)、影响_类型和严重程度。
-3. 保留方向性:污染物从污染源流向目标。
-4. 仅提取给定实体间的影响;忽略独立的实体提及。
-5. 包括定量测量(浓度、严重程度)如有。
-
-输出格式:
-- 每个影响是一条边:源 → 目标,包含影响_类型和严重程度。
-"""
-
-# ==============================================================================
-# 3. 模板类
-# =============================================================================="
-
-
-class PollutantImpactMap(AutoGraph[EnvironmentalEntity, PollutantImpact]):
- """
- 环境污染追踪与影响评估知识图谱模板。
-
- 将环境监测报告和可持续性分析转化为结构化的污染源-受体关系,
- 用于环保健康风险分析。
-
- Example:
- >>> from langchain_openai import ChatOpenAI, OpenAIEmbeddings
- >>> llm = ChatOpenAI(model="gpt-4")
- >>> embedder = OpenAIEmbeddings()
- >>>
- >>> pollutant_map = PollutantImpactMap(llm_client=llm, embedder=embedder)
- >>> text = "工厂排放的汞污染了河流,导致鱼类受到严重毒性影响。"
- >>> pollutant_map.feed_text(text)
- >>> pollutant_map.show() # 可视化污染物影响及严重程度
- """
-
- 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"。
- - "one_stage":同时提取污染物和影响(更快)。
- - "two_stage":先提取污染物,再提取影响(准确度更高)。
- chunk_size (int, optional):每个文本块的最大字符数。默认为 2048。
- chunk_overlap (int, optional):块之间的重叠字符数,用于保持上下文连贯性。默认为 256。
- max_workers (int, optional):用于并行处理的最大并发提取工作进程数。默认为 10。
- verbose (bool, optional):如果为 True,则打印详细的提取进度和污染物追踪日志。默认为 False。
- **kwargs:传递给 AutoGraph 基类的其他参数。
- """
- super().__init__(
- node_schema=EnvironmentalEntity,
- edge_schema=PollutantImpact,
- node_key_extractor=lambda x: x.name.strip(),
- edge_key_extractor=lambda x: f"{x.source.strip()}--({x.impact_type.lower()})-->{x.target.strip()}",
- nodes_in_edge_extractor=lambda x: (x.source.strip(), x.target.strip()),
- llm_client=llm_client,
- embedder=embedder,
- extraction_mode=extraction_mode,
- chunk_size=chunk_size,
- chunk_overlap=chunk_overlap,
- max_workers=max_workers,
- verbose=verbose,
- prompt=_PROMPT,
- prompt_for_node_extraction=_NODE_PROMPT,
- prompt_for_edge_extraction=_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:
- """可视化污染物影响网络用于环境评估。
-
- 使用 OntoSight 展示污染源、污染物及受影响对象和严重程度信息。
- 在内部定义实体的前端展示标签(名称 + 实体类型)和影响标签(影响类型 + 严重程度)。
-
- Args:
- top_k_nodes_for_search (int, optional):搜索触发时要检索的环境实体(污染物/污染源/受体)数量。默认为 3。
- top_k_edges_for_search (int, optional):搜索触发时要检索的影响关系数。默认为 3。
- top_k_nodes_for_chat (int, optional):聊天触发时要检索的环境实体数量。默认为 3。
- top_k_edges_for_chat (int, optional):聊天触发时要检索的影响关系数。默认为 3。
- """
-
- def node_label_extractor(node: EnvironmentalEntity) -> str:
- return f"{node.name} ({node.entity_type})"
-
- def edge_label_extractor(edge: PollutantImpact) -> str:
- severity = f" [{edge.severity}]" if edge.severity else ""
- return f"{edge.impact_type}{severity}"
-
- 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/environment/species_interaction.py b/hyperextract/templates/zh/environment/species_interaction.py
deleted file mode 100644
index c545de7..0000000
--- a/hyperextract/templates/zh/environment/species_interaction.py
+++ /dev/null
@@ -1,191 +0,0 @@
-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.graph import AutoGraph
-
-# ==============================================================================
-# 1. Schema 定义
-# ==============================================================================
-
-
-class Species(BaseModel):
- """生态系统中的生物体。"""
-
- name: str = Field(description="生物的学名或常用名。")
- category: str = Field(
- description="分类学分类:'Mammal'(哺乳动物), 'Bird'(鸟类), 'Insect'(昆虫), 'Plant'(植物), 'Microorganism'(微生物)等。"
- )
- protection_level: Optional[str] = Field(
- None, description="保护等级:'Endangered'(濒危), 'Vulnerable'(易危), 'Least Concern'(无危)等。"
- )
-
-
-class Interaction(BaseModel):
- """两个物种之间的生物互作关系。"""
-
- source: str = Field(description="初始物种(捕食者、竞争者等)。")
- target: str = Field(description="接收物种(猎物、竞争者、寄主等)。")
- interaction_type: str = Field(
- description="互作类型:'predation'(捕食), 'parasitism'(寄生), 'mutualism'(互利共生), 'commensalism'(偏利共生), 'competition'(竞争), 'symbiosis'(共生)。"
- )
- detail: Optional[str] = Field(
- None, description="互作的具体细节(如频率、季节性规律、生态影响等)。"
- )
-
-
-# ==============================================================================
-# 2. Prompts
-# ==============================================================================
-
-_PROMPT = """
-你是一位生态学和生物多样性专家。
-你的任务是从科学文献、野外调查报告或自然纪录片中提取生物互作网络。
-
-指导原则:
-1. 识别文本中提到的所有生物,并按类别分类(物种类型)。
-2. 提取生物之间的关系(捕食、寄生、竞争、互利共生等)。
-3. 对于每个互作,捕捉互作类型和具体的生态学细节(时间、频率、条件)。
-4. 保持互作的方向性:例如"狮子捕食斑马"和"斑马躲避狮子"是不同的。
-5. 如果提到了保护等级信息的,要进行记录。
-
-输出格式:
-- 每个物种应该是一个独立的实体,包括名称、类别和保护等级(如果有)。
-- 每个互作应该清楚地表示源头(发起方生物)、目标(接收方生物)和互作类型。
-"""
-
-_NODE_PROMPT = """
-你是一位生态学和生物多样性专家。
-你的任务是在第一个提取阶段只提取文本中的物种和生物体,不提取它们之间的互作。
-专注于识别文本中提到的所有不同生物体。
-
-指导原则:
-1. 列出文本中提到的所有物种、生物体或种群。
-2. 对于每个生物体,捕获:名称、分类(例如捕食者、猎物、植物、分解者)、以及保护状态(如有)。
-3. 避免重复:如果"狮子"和"非洲狮"指的是同一物种,统一为最具体的名称。
-4. 忽略模糊的指代;仅提取明确提到的生物体。
-
-输出格式:
-- 每个生物体应为不同的节点,具有清晰的名称、分类和状态。
-"""
-
-_EDGE_PROMPT = """
-你是一位分析物种互作的生态学家。
-你的任务是在第二个提取阶段只提取已给定物种间的生物关系。
-你将获得一份已提取物种(节点)的列表。你的工作是识别它们之间的互作。
-
-指导原则:
-1. 根据确认的物种列表,找出文本中提到的所有两两互作关系。
-2. 对于每个互作,捕获:源物种、目标物种、互作类型(捕食、共生、竞争等)和详细信息。
-3. 保留方向性:"A 捕食 B" 是捕食(A→B),不同于 "B 躲避 A"。
-4. 仅提取连接已给定物种的互作;忽略不在提供列表中的物种提及。
-5. 包括生态学背景(季节性、频率、条件)作为详细信息。
-
-输出格式:
-- 每个互作是一条边:源物种 → 目标物种,包含关系类型和详细信息。
-"""
-
-# ==============================================================================
-# 3. 模板类
-# ==============================================================================
-
-
-class SpeciesInteractionNetwork(AutoGraph[Species, Interaction]):
- """
- 物种生态互作知识图谱模板。
-
- 将科学文献和野外研究转化为结构化的互作网络,
- 使得生态系统生物多样性和食物链分析成为可能。
-
- Example:
- >>> from langchain_openai import ChatOpenAI, OpenAIEmbeddings
- >>> llm = ChatOpenAI(model="gpt-4")
- >>> embedder = OpenAIEmbeddings()
- >>>
- >>> network = SpeciesInteractionNetwork(llm_client=llm, embedder=embedder)
- >>> text = "狮子在非洲草原狩猎斑马。寄生蜂被鸟类捕食。"
- >>> network.feed_text(text)
- >>> network.show() # 可视化物种互作网络
- """
-
- 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"。
- - "one_stage":同时提取物种和互作(更快)。
- - "two_stage":先提取物种,再提取互作(准确度更高)。
- chunk_size (int, optional):每个文本块的最大字符数。默认为 2048。
- chunk_overlap (int, optional):块之间的重叠字符数,用于保持上下文连贯性。默认为 256。
- max_workers (int, optional):并发提取的最大工作进程数。默认为 10。
- verbose (bool, optional):如果为 True,则打印详细的提取进度日志。默认为 False。
- **kwargs:传递给 AutoGraph 基类的其他参数。
- """
- super().__init__(
- node_schema=Species,
- edge_schema=Interaction,
- node_key_extractor=lambda x: x.name.strip(),
- edge_key_extractor=lambda x: f"{x.source.strip()}--({x.interaction_type.lower()})-->{x.target.strip()}",
- nodes_in_edge_extractor=lambda x: (x.source.strip(), x.target.strip()),
- llm_client=llm_client,
- embedder=embedder,
- extraction_mode=extraction_mode,
- chunk_size=chunk_size,
- chunk_overlap=chunk_overlap,
- max_workers=max_workers,
- verbose=verbose,
- prompt=_PROMPT,
- prompt_for_node_extraction=_NODE_PROMPT,
- prompt_for_edge_extraction=_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:
- """可视化物种互作网络用于生态分析。
-
- 使用 OntoSight 可视化已提取的物种(节点)及其互作关系(边)。
- 在内部定义物种的前端展示标签(名称 + 类别 + 保护等级)
- 和互作关系标签(互作类型)。
-
- Args:
- top_k_nodes_for_search (int, optional):搜索触发时要检索的物种节点数。默认为 3。
- top_k_edges_for_search (int, optional):搜索触发时要检索的互作边数。默认为 3。
- top_k_nodes_for_chat (int, optional):聊天触发时要检索的物种节点数。默认为 3。
- top_k_edges_for_chat (int, optional):聊天触发时要检索的互作边数。默认为 3。
- """
-
- def node_label_extractor(node: Species) -> str:
- status = f" [{node.protection_level}]" if node.protection_level else ""
- return f"{node.name} ({node.category}){status}"
-
- def edge_label_extractor(edge: Interaction) -> 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,
- )