Skip to content

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

refactor default_prompt

yifanfeng974040a9ad9b36
diff --git a/hyperextract/core/base.py b/hyperextract/core/base.py
index 6672854..0fb7306 100644
--- a/hyperextract/core/base.py
+++ b/hyperextract/core/base.py
@@ -109,16 +109,13 @@ class BaseAutoType(ABC, Generic[T]):
show_progress=self.show_progress,
)
- @staticmethod
- def _default_prompt() -> str:
- """Returns the default extraction prompt template."""
- return (
- "You are an expert knowledge extraction assistant. "
- "Your task is to carefully analyze the following text and extract structured information "
- "according to the specified schema. Be precise, comprehensive, and faithful to the source text. "
- "Extract all relevant details without adding information not present in the text.\n\n"
- "### Source Text:\n"
- )
+ @abstractmethod
+ def _default_prompt(self) -> str:
+ """Returns the default extraction prompt template.
+
+ Subclasses must implement this to provide a prompt tailored to their extraction pattern.
+ """
+ pass
# ==================== Data Management ====================
diff --git a/hyperextract/core/list.py b/hyperextract/core/list.py
index 3bd3a37..f3da7d4 100644
--- a/hyperextract/core/list.py
+++ b/hyperextract/core/list.py
@@ -106,8 +106,7 @@ class AutoList(BaseAutoType[AutoListSchema[Item]], Generic[Item]):
**kwargs,
)
- @staticmethod
- def _default_prompt() -> str:
+ def _default_prompt(self) -> str:
"""Returns the default extraction prompt for list-based extraction."""
return (
"You are an expert knowledge extraction assistant. "
diff --git a/hyperextract/core/model.py b/hyperextract/core/model.py
index b7654d7..a2565da 100644
--- a/hyperextract/core/model.py
+++ b/hyperextract/core/model.py
@@ -69,8 +69,7 @@ class AutoModel(BaseAutoType[T]):
**kwargs,
)
- @staticmethod
- def _default_prompt() -> str:
+ def _default_prompt(self) -> str:
"""Returns the default extraction prompt for single-object extraction."""
return (
"You are an expert knowledge extraction assistant. "
@@ -80,72 +79,6 @@ class AutoModel(BaseAutoType[T]):
"### Source Text:\n"
)
- # ==================== Extraction & Aggregation ====================
-
- def extract(self, text: str, *, store: bool = True) -> T:
- """Extracts knowledge using LangChain native batch processing.
-
- Automatically handles text chunking for long documents and aggregates results
- using the field-level merge strategy.
-
- Args:
- text: Input text to extract knowledge from.
- store: Controls whether to store extracted knowledge internally.
- - True (default): Store mode - merge with existing knowledge and update internal state
- - False: Temporary mode - return extracted data without modifying internal state
-
- Returns:
- The extracted knowledge object.
- """
- start_time = datetime.now()
-
- # Determine if chunking is needed based on text length
- if len(text) <= self.chunk_size:
- # Short text: direct extraction without chunking
- if self.show_progress:
- logger.info(f"Processing single text (length: {len(text)})...")
-
- extracted_data = self.llm_chain_extract.invoke({"chunk_text": text})
- extracted_data_list = [extracted_data]
-
- else:
- # Long text: extract by chunking
- chunks = self.text_splitter.split_text(text)
- logger.info(f"Split text into {len(chunks)} chunks")
-
- if self.show_progress:
- logger.info(f"Processing {len(chunks)} chunk(s)...")
-
- inputs = [{"chunk_text": chunk} for chunk in chunks]
- extracted_data_list = self.llm_chain_extract.batch(
- inputs, config={"max_concurrency": self.max_workers}
- )
-
- if self.show_progress:
- logger.info(f"Extracted {len(extracted_data_list)} chunks")
-
- logger.info("Merging extracted knowledge...")
- merged_data = self.merge(extracted_data_list)
-
- # If store=True, merge with existing data and update internal state
- if store:
- if self._data and any(
- v is not None for v in self._data.model_dump().values()
- ):
- self._data = self.merge([self._data, merged_data])
- else:
- self._data = merged_data
-
- self.clear_index()
- self.metadata["updated_at"] = datetime.now()
-
- logger.info("Knowledge extraction completed")
- logger.info(
- f"Duration: {(datetime.now() - start_time).total_seconds():.2f} seconds"
- )
-
- return self._data if store else merged_data
-
def merge(self, data_list: List[T]) -> T:
"""Pure data merge method implementing field-level update strategy.
@@ -191,6 +124,18 @@ class AutoModel(BaseAutoType[T]):
self._index = FAISS.from_documents(documents, self.embedder)
logger.info(f"Built FAISS index with {len(documents)} documents")
+ def __len__(self) -> int:
+ """Returns 1 if there is data (non-empty fields), 0 otherwise.
+
+ AutoModel always represents a single unit of knowledge, so it's either
+ empty (0) or contains one item (1).
+ """
+ if not self._data:
+ return 0
+ # Check if any field is non-null
+ has_data = any(v is not None for v in self._data.model_dump().values())
+ return 1 if has_data else 0
+
def search(self, query: str, top_k: int = 3) -> List[Any]:
"""Searches all indexed fields using semantic similarity.
diff --git a/hyperextract/core/set.py b/hyperextract/core/set.py
index 70153c7..b6aefa0 100644
--- a/hyperextract/core/set.py
+++ b/hyperextract/core/set.py
@@ -170,10 +170,7 @@ class AutoSet(BaseAutoType[AutoSetSchema[Item]], Generic[Item]):
llm_batch_size=self.llm_batch_size,
)
- # ==================== Validation ====================
-
- @staticmethod
- def _default_prompt() -> str:
+ def _default_prompt(self) -> str:
"""Returns the default extraction prompt for set-based extraction."""
return (
"You are an expert knowledge extraction assistant. "
diff --git a/hyperextract/graphs/base.py b/hyperextract/graphs/base.py
index 74bbe2f..a97c30f 100644
--- a/hyperextract/graphs/base.py
+++ b/hyperextract/graphs/base.py
@@ -1,71 +1,2 @@
-from ..core.base import BaseAutoType
-from typing import List, Optional, Dict, Any
-from pydantic import BaseModel, Field
+from ..core import BaseAutoType, AutoSet
-
-class Entity(BaseModel):
- """Represents a node in the knowledge graph."""
-
- id: str = Field(
- ...,
- description="The unique name or identifier of the entity (e.g., 'Elon Musk').",
- )
- type: str = Field(
- "Unknown",
- description="The category of the entity (e.g., 'Person', 'Organization', 'Concept').",
- )
- description: Optional[str] = Field(
- None, description="A brief summary of the entity found in the text."
- )
-
-
-class Relationship(BaseModel):
- """Represents an edge between two entities."""
-
- source: str = Field(..., description="The id of the source entity.")
- target: str = Field(..., description="The id of the target entity.")
- type: str = Field(
- ..., description="The type of the relationship (e.g., 'FOUNDED', 'LOCATED_IN')."
- )
- description: Optional[str] = Field(
- None, description="Contextual details about this relationship."
- )
-
-
-class GraphSchema(BaseModel):
- """The container for the entire graph structure."""
-
- nodes: List[Entity] = Field(default_factory=list)
- edges: List[Relationship] = Field(default_factory=list)
-
- def get_node_ids(self) -> set[str]:
- """Helper to get all current node IDs for quick lookup."""
- return {n.id for n in self.nodes}
-
-
-class SimpleGraph(BaseAutoType[GraphSchema]):
- def _get_schema_class(self):
- return GraphSchema
-
- async def aextract(self, text: str, **kwargs):
- # 1. 构造 prompt
- prompt = f"Extract nodes and edges from: {text}"
-
- # 2. 调用自带的 LLM
- response = await self.llm_client.ainvoke(prompt)
-
- # 3. 解析结果 (假设 parsed_data 是解析后的 GraphSchema 对象)
- parsed_data = self._parse(response)
-
- # 4. 【直接在这里做合并逻辑】
- # 简单的去重合并
- existing_node_ids = {n.id for n in self._data.nodes}
- for node in parsed_data.nodes:
- if node.id not in existing_node_ids:
- self._data.nodes.append(node)
- self._data.edges.extend(parsed_data.edges)
-
- def dump(self, format="json"):
- if format == "json":
- return self._data.model_dump_json()
- return self._data.model_dump()