Skip to content

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

fix(lint): resolve 584 ruff 0.16.1 errors

yifanfeng978e2abeb32d71
diff --git a/hyperextract/__init__.py b/hyperextract/__init__.py
index 6a673ac..e8d746d 100644
--- a/hyperextract/__init__.py
+++ b/hyperextract/__init__.py
@@ -22,55 +22,55 @@ Usage:
"""
# Core AutoType primitives
+from importlib.metadata import version
+
from .types import (
- BaseAutoType,
- AutoModel,
- AutoList,
- AutoSet,
AutoGraph,
AutoHypergraph,
- AutoTemporalGraph,
+ AutoList,
+ AutoModel,
+ AutoSet,
AutoSpatialGraph,
AutoSpatioTemporalGraph,
+ AutoTemporalGraph,
+ BaseAutoType,
)
-# Template engine API
-from .utils.template_engine import Template
-
# Client factory
-from .utils.client import create_client, create_llm, create_embedder, get_client
+from .utils.client import create_client, create_embedder, create_llm, get_client
# Logging utilities
from .utils.logging import configure_logging, get_logger, set_log_level
-from importlib.metadata import version
+# Template engine API
+from .utils.template_engine import Template
__version__ = version("hyperextract")
__author__ = "Yifan Feng"
__email__ = "[email protected]"
__all__ = [
- # Base class
- "BaseAutoType",
- # Scalar types
- "AutoModel",
- "AutoList",
- "AutoSet",
# Graph types
"AutoGraph",
"AutoHypergraph",
- "AutoTemporalGraph",
+ "AutoList",
+ # Scalar types
+ "AutoModel",
+ "AutoSet",
"AutoSpatialGraph",
"AutoSpatioTemporalGraph",
+ "AutoTemporalGraph",
+ # Base class
+ "BaseAutoType",
# Template engine
"Template",
+ # Logging utilities
+ "configure_logging",
# Client factory
"create_client",
- "create_llm",
"create_embedder",
+ "create_llm",
"get_client",
- # Logging utilities
- "configure_logging",
"get_logger",
"set_log_level",
]
diff --git a/hyperextract/cli/cli.py b/hyperextract/cli/cli.py
index 5dd04c7..b62c29e 100644
--- a/hyperextract/cli/cli.py
+++ b/hyperextract/cli/cli.py
@@ -1,32 +1,30 @@
"""CLI entry point for Hyper-Extract."""
from pathlib import Path
-from typing import Optional
import typer
from rich.console import Console
-from rich.table import Table
-from rich.text import Text
from rich.progress import Progress, SpinnerColumn, TextColumn
from rich.prompt import Prompt
+from rich.table import Table
+from rich.text import Text
-from hyperextract.utils.template_engine import Gallery, Template
from hyperextract.utils.logging import configure_logging, get_logger
+from hyperextract.utils.template_engine import Gallery, Template
+from .commands import config_app, list_app
+from .config import (
+ load_ka_metadata,
+)
from .utils import (
LOGO,
+ get_template_from_ka,
read_input,
validate_config,
validate_ka_path,
validate_ka_with_data,
validate_ka_with_index,
- get_template_from_ka,
)
-from .config import (
- load_ka_metadata,
-)
-
-from .commands import list_app, config_app
console = Console()
logger = get_logger("he")
@@ -158,7 +156,7 @@ def main(
raise typer.Exit()
-def select_template_interactive() -> Optional[str]:
+def select_template_interactive() -> str | None:
"""Interactive template selection when user doesn't specify one."""
templates = Gallery.list()
@@ -223,13 +221,13 @@ def parse(
..., help="Input file path, directory, or '-' for stdin"
),
output: str = typer.Option(..., "--output", "-o", help="Output directory"),
- template: Optional[str] = typer.Option(
+ template: str | None = typer.Option(
None, "--template", "-t", help="Template (omit for interactive selection)"
),
- method: Optional[str] = typer.Option(
+ method: str | None = typer.Option(
None, "--method", "-m", help="Method template (e.g., light_rag, hyper_rag)"
),
- lang: Optional[str] = typer.Option(
+ lang: str | None = typer.Option(
None,
"--lang",
"-l",
@@ -348,15 +346,14 @@ def parse(
progress.update(task, description="Saving data...")
template_config = Template.get(template)
- if template_config is None:
- if template.endswith(".yaml"):
- import shutil
+ if template_config is None and template.endswith(".yaml"):
+ import shutil
- filename = Path(template).name
- shutil.copy(template, output_path / filename)
- console.print(
- f"[dim]Custom template '{filename}' saved to KA directory[/dim]"
- )
+ filename = Path(template).name
+ shutil.copy(template, output_path / filename)
+ console.print(
+ f"[dim]Custom template '{filename}' saved to KA directory[/dim]"
+ )
ka.dump(output_path)
logger.info("stage=data_saved output=%s", output_path)
@@ -454,7 +451,7 @@ export_app = typer.Typer(
def export_obsidian_cmd(
ka_path: str = typer.Argument(..., help="Knowledge Abstract directory"),
output: str = typer.Option(..., "--output", "-o", help="Output vault directory"),
- name: Optional[str] = typer.Option(
+ name: str | None = typer.Option(
None, "--name", help="Vault name used for the index note"
),
no_index: bool = typer.Option(
@@ -691,7 +688,7 @@ def chat_loop(ka, ka_path: str):
@app.command(name="talk")
def talk(
ka_path: str = typer.Argument(..., help="Knowledge Abstract directory"),
- query: Optional[str] = typer.Option(None, "--query", "-q", help="Question to ask"),
+ query: str | None = typer.Option(None, "--query", "-q", help="Question to ask"),
top_k: int = typer.Option(3, "--top-k", "-n", help="Number of context items"),
interactive: bool = typer.Option(
False, "--interactive", "-i", help="Interactive mode"
@@ -773,8 +770,8 @@ def talk(
def feed(
ka_path: str = typer.Argument(..., help="Knowledge Abstract directory"),
input: str = typer.Argument(..., help="Input file path or '-' for stdin"),
- template: Optional[str] = typer.Option(None, "--template", "-t", help="Template"),
- lang: Optional[str] = typer.Option(None, "--lang", "-l", help="Language"),
+ template: str | None = typer.Option(None, "--template", "-t", help="Template"),
+ lang: str | None = typer.Option(None, "--lang", "-l", help="Language"),
):
"""Append knowledge to an existing Knowledge Abstract."""
logger.info("command=feed ka_path=%s input=%s", ka_path, input)
diff --git a/hyperextract/cli/commands/__init__.py b/hyperextract/cli/commands/__init__.py
index 82766b2..23fc2ed 100644
--- a/hyperextract/cli/commands/__init__.py
+++ b/hyperextract/cli/commands/__init__.py
@@ -1,6 +1,6 @@
"""Command modules for Hyper-Extract CLI."""
-from .list import app as list_app
from .config import app as config_app
+from .list import app as list_app
-__all__ = ["list_app", "config_app"]
+__all__ = ["config_app", "list_app"]
diff --git a/hyperextract/cli/commands/config.py b/hyperextract/cli/commands/config.py
index 205e1b9..4329dfa 100644
--- a/hyperextract/cli/commands/config.py
+++ b/hyperextract/cli/commands/config.py
@@ -1,13 +1,13 @@
"""Config command for Hyper-Extract CLI."""
-from typing import Optional
+import typer
from rich.console import Console
from rich.table import Table
from rich.text import Text
-from ..config import ConfigManager
from hyperextract.utils.logging import get_logger
-import typer
+
+from ..config import ConfigManager
logger = get_logger("he.config")
console = Console()
@@ -147,25 +147,25 @@ def show(
@app.command(name="llm")
def llm(
- provider: Optional[str] = typer.Option(
+ provider: str | None = typer.Option(
None,
"--provider",
"-p",
help="Provider preset: openai, anthropic, deepseek, bailian, vllm",
),
- api_key: Optional[str] = typer.Option(
+ api_key: str | None = typer.Option(
None,
"--api-key",
"-k",
help="LLM API key",
),
- model: Optional[str] = typer.Option(
+ model: str | None = typer.Option(
None,
"--model",
"-m",
help="LLM model name",
),
- base_url: Optional[str] = typer.Option(
+ base_url: str | None = typer.Option(
None,
"--base-url",
"-u",
@@ -208,25 +208,25 @@ def llm(
@app.command(name="embedder")
def embedder(
- provider: Optional[str] = typer.Option(
+ provider: str | None = typer.Option(
None,
"--provider",
"-p",
help="Provider preset: openai, anthropic, deepseek, bailian, vllm",
),
- api_key: Optional[str] = typer.Option(
+ api_key: str | None = typer.Option(
None,
"--api-key",
"-k",
help="Embedder API key",
),
- model: Optional[str] = typer.Option(
+ model: str | None = typer.Option(
None,
"--model",
"-m",
help="Embedder model name",
),
- base_url: Optional[str] = typer.Option(
+ base_url: str | None = typer.Option(
None,
"--base-url",
"-u",
@@ -271,19 +271,19 @@ def embedder(
@app.command(name="init")
def init(
- provider: Optional[str] = typer.Option(
+ provider: str | None = typer.Option(
None,
"--provider",
"-p",
help="Provider preset: openai, anthropic, deepseek, bailian, vllm",
),
- api_key: Optional[str] = typer.Option(
+ api_key: str | None = typer.Option(
None,
"--api-key",
"-k",
help="API key for both LLM and Embedder",
),
- base_url: Optional[str] = typer.Option(
+ base_url: str | None = typer.Option(
None,
"--base-url",
"-u",
@@ -323,9 +323,7 @@ def init(
)
else:
console.print(
- "[yellow]Warning: Provider '{}' has no default embedder. Please configure embedder separately.[/yellow]".format(
- provider
- )
+ f"[yellow]Warning: Provider '{provider}' has no default embedder. Please configure embedder separately.[/yellow]"
)
console.print("[bold green]Configuration saved successfully![/bold green]")
diff --git a/hyperextract/cli/commands/list.py b/hyperextract/cli/commands/list.py
index 6728229..8f9fc12 100644
--- a/hyperextract/cli/commands/list.py
+++ b/hyperextract/cli/commands/list.py
@@ -1,13 +1,12 @@
"""List command for Hyper-Extract CLI."""
-from typing import Optional
+import typer
from rich.console import Console
from rich.table import Table
from rich.text import Text
-import typer
-from hyperextract.utils.template_engine import Gallery
from hyperextract.utils.logging import get_logger
+from hyperextract.utils.template_engine import Gallery
logger = get_logger("he.list")
console = Console()
@@ -28,13 +27,13 @@ def _get_description(cfg, lang: str = "en") -> str:
@app.command(name="template")
def template(
- query: Optional[str] = typer.Option(
+ query: str | None = typer.Option(
None, "--query", "-q", help="Query to search templates"
),
- autotype: Optional[str] = typer.Option(
+ autotype: str | None = typer.Option(
None, "--autotype", "-a", help="Filter by autotype"
),
- language: Optional[str] = typer.Option(
+ language: str | None = typer.Option(
None, "--lang", "-l", help="Language filter (en/zh/all, default: en)"
),
include_methods: bool = typer.Option(
@@ -113,7 +112,7 @@ def template(
@app.command(name="method")
def method(
- query: Optional[str] = typer.Option(
+ query: str | None = typer.Option(
None, "--query", "-q", help="Query to search methods"
),
):
diff --git a/hyperextract/cli/config.py b/hyperextract/cli/config.py
index c723871..30d7a24 100644
--- a/hyperextract/cli/config.py
+++ b/hyperextract/cli/config.py
@@ -1,19 +1,19 @@
"""Configuration management for Hyper-Extract CLI."""
-import os
import json
+import os
import tomllib
-import tomli_w
-from pathlib import Path
-from typing import Optional, Dict, Any
from dataclasses import dataclass
+from pathlib import Path
+from typing import Any
+import tomli_w
DEFAULT_CONFIG_DIR = Path.home() / ".he"
DEFAULT_CONFIG_FILE = DEFAULT_CONFIG_DIR / "config.toml"
# Provider presets: base_url and default models for each provider
-PROVIDER_PRESETS: Dict[str, Dict[str, str | None]] = {
+PROVIDER_PRESETS: dict[str, dict[str, str | None]] = {
"openai": {
"base_url": "https://api.openai.com/v1",
"default_llm": "gpt-4o-mini",
@@ -49,7 +49,7 @@ PROVIDER_PRESETS: Dict[str, Dict[str, str | None]] = {
}
# Environment variables checked (in order) for each provider's API key.
-PROVIDER_API_KEY_ENV: Dict[str, tuple] = {
+PROVIDER_API_KEY_ENV: dict[str, tuple] = {
"anthropic": ("ANTHROPIC_API_KEY", "CLAUDE_API_KEY"),
"claude": ("ANTHROPIC_API_KEY", "CLAUDE_API_KEY"),
"deepseek": ("DEEPSEEK_API_KEY",),
@@ -75,7 +75,7 @@ class LLMConfig:
api_key: str = ""
base_url: str = ""
- def to_dict(self) -> Dict[str, Any]:
+ def to_dict(self) -> dict[str, Any]:
return {
"provider": self.provider,
"model": self.model,
@@ -84,7 +84,7 @@ class LLMConfig:
}
@classmethod
- def from_dict(cls, data: Dict[str, Any]) -> "LLMConfig":
+ def from_dict(cls, data: dict[str, Any]) -> "LLMConfig":
return cls(
provider=data.get("provider", ""),
model=data.get("model", "gpt-4o-mini"),
@@ -100,7 +100,7 @@ class EmbedderConfig:
api_key: str = ""
base_url: str = ""
- def to_dict(self) -> Dict[str, Any]:
+ def to_dict(self) -> dict[str, Any]:
return {
"provider": self.provider,
"model": self.model,
@@ -109,7 +109,7 @@ class EmbedderConfig:
}
@classmethod
- def from_dict(cls, data: Dict[str, Any]) -> "EmbedderConfig":
+ def from_dict(cls, data: dict[str, Any]) -> "EmbedderConfig":
return cls(
provider=data.get("provider", ""),
model=data.get("model", "text-embedding-3-small"),
@@ -121,7 +121,7 @@ class EmbedderConfig:
class ConfigManager:
"""Manages Hyper-Extract CLI configuration."""
- def __init__(self, config_path: Optional[Path] = None):
+ def __init__(self, config_path: Path | None = None):
self.config_path = config_path or DEFAULT_CONFIG_FILE
self.llm = LLMConfig()
self.embedder = EmbedderConfig()
@@ -196,10 +196,10 @@ class ConfigManager:
def set_llm(
self,
- provider: Optional[str] = None,
- model: Optional[str] = None,
- api_key: Optional[str] = None,
- base_url: Optional[str] = None,
+ provider: str | None = None,
+ model: str | None = None,
+ api_key: str | None = None,
+ base_url: str | None = None,
) -> None:
"""Set LLM configuration."""
if provider is not None:
@@ -214,10 +214,10 @@ class ConfigManager:
def set_embedder(
self,
- provider: Optional[str] = None,
- model: Optional[str] = None,
- api_key: Optional[str] = None,
- base_url: Optional[str] = None,
+ provider: str | None = None,
+ model: str | None = None,
+ api_key: str | None = None,
+ base_url: str | None = None,
) -> None:
"""Set Embedder configuration."""
if provider is not None:
@@ -240,7 +240,7 @@ class ConfigManager:
self.embedder = EmbedderConfig()
self._save()
- def show(self) -> Dict[str, Any]:
+ def show(self) -> dict[str, Any]:
"""Show current configuration."""
return {
"llm": self.get_llm_config().to_dict(),
@@ -274,7 +274,7 @@ class ConfigManager:
return True, "Configuration is valid"
-def load_ka_metadata(ka_path: Path) -> Optional[Dict[str, Any]]:
+def load_ka_metadata(ka_path: Path) -> dict[str, Any] | None:
"""Load knowledge abstract metadata from directory."""
metadata_path = ka_path / "metadata.json"
if not metadata_path.exists():
diff --git a/hyperextract/cli/utils.py b/hyperextract/cli/utils.py
index 6dea896..1c24366 100644
--- a/hyperextract/cli/utils.py
+++ b/hyperextract/cli/utils.py
@@ -1,9 +1,9 @@
"""Common utilities for Hyper-Extract CLI."""
import sys
-import typer
from pathlib import Path
-from typing import Tuple
+
+import typer
from rich.console import Console
from .config import ConfigManager
@@ -106,7 +106,7 @@ def validate_ka_with_index(ka_path: str) -> Path:
return path
-def get_template_from_ka(ka_path: Path) -> Tuple[str, str]:
+def get_template_from_ka(ka_path: Path) -> tuple[str, str]:
"""Get template path for Knowledge Abstract.
Load priority:
@@ -116,9 +116,10 @@ def get_template_from_ka(ka_path: Path) -> Tuple[str, str]:
Raises:
ValueError: If template not found and no local yaml file exists
"""
- from .config import load_ka_metadata
from hyperextract.utils.template_engine import Gallery
+ from .config import load_ka_metadata
+
metadata = load_ka_metadata(ka_path)
if metadata is None:
raise ValueError(f"No metadata.json found in Knowledge Abstract: {ka_path}")
diff --git a/hyperextract/mcp_server.py b/hyperextract/mcp_server.py
index 89551db..effe520 100644
--- a/hyperextract/mcp_server.py
+++ b/hyperextract/mcp_server.py
@@ -25,7 +25,7 @@ from __future__ import annotations
import json
from pathlib import Path
-from typing import Any, Tuple
+from typing import Any
from langchain_core.embeddings import Embeddings
from langchain_core.language_models.chat_models import BaseChatModel
@@ -38,14 +38,14 @@ SERVER_NAME = "hyper-extract"
# ---------------------------------------------------------------------------
-def _get_clients() -> Tuple[BaseChatModel, Embeddings]:
+def _get_clients() -> tuple[BaseChatModel, Embeddings]:
"""Return (llm, embedder) from the user's ~/.he/config.toml."""
from hyperextract.utils.client import get_client
return get_client()
-def _template_and_lang(path: Path) -> Tuple[str, str]:
+def _template_and_lang(path: Path) -> tuple[str, str]:
"""Read the template name and language from a KA's metadata.json."""
meta_path = path / "metadata.json"
if not meta_path.exists():
diff --git a/hyperextract/methods/__init__.py b/hyperextract/methods/__init__.py
index 0290331..7d6a961 100644
--- a/hyperextract/methods/__init__.py
+++ b/hyperextract/methods/__init__.py
@@ -6,24 +6,23 @@ the AutoTypes primitives. It's divided into:
- typical: Typical/canonical graph construction pipelines
"""
-from . import rag
-from . import typical
+from . import rag, typical
from .registry import (
- register_method,
+ MethodCfg,
get_method,
- list_methods,
get_method_cfg,
list_method_cfgs,
- MethodCfg,
+ list_methods,
+ register_method,
)
__all__ = [
- "rag",
- "typical",
- "register_method",
+ "MethodCfg",
"get_method",
- "list_methods",
"get_method_cfg",
"list_method_cfgs",
- "MethodCfg",
+ "list_methods",
+ "rag",
+ "register_method",
+ "typical",
]
diff --git a/hyperextract/methods/rag/__init__.py b/hyperextract/methods/rag/__init__.py
index 435303d..e84621b 100644
--- a/hyperextract/methods/rag/__init__.py
+++ b/hyperextract/methods/rag/__init__.py
@@ -4,16 +4,16 @@ This module contains various RAG implementations that leverage graph structures
(both binary and hypergraphs) for context retrieval and augmented generation.
"""
+from .cog_rag import Cog_RAG
+from .graph_rag import Graph_RAG
from .hyper_rag import Hyper_RAG
from .hypergraph_rag import HyperGraph_RAG
-from .cog_rag import Cog_RAG
from .light_rag import Light_RAG
-from .graph_rag import Graph_RAG
__all__ = [
- "Hyper_RAG",
- "HyperGraph_RAG",
"Cog_RAG",
- "Light_RAG",
"Graph_RAG",
+ "HyperGraph_RAG",
+ "Hyper_RAG",
+ "Light_RAG",
]
diff --git a/hyperextract/methods/rag/cog_rag.py b/hyperextract/methods/rag/cog_rag.py
index 84ebe17..6b17e55 100644
--- a/hyperextract/methods/rag/cog_rag.py
+++ b/hyperextract/methods/rag/cog_rag.py
@@ -3,18 +3,18 @@ COG_RAG: Cognitive-Inspired Dual-Hypergraph RAG System Pattern
Extracts and manages Theme-Entity relationships where Themes act as Hyperedges connecting multiple Entities.
"""
-import os
import json
+import os
from datetime import datetime
-from typing import List
from hashlib import md5
-from pydantic import BaseModel, Field
-from hyperextract.types.hypergraph import AutoHypergraph, AutoHypergraphSchema
-from langchain_core.prompts import ChatPromptTemplate
+
from langchain_core.embeddings import Embeddings
from langchain_core.language_models import BaseChatModel
+from langchain_core.prompts import ChatPromptTemplate
from ontomem.merger import CustomRuleMerger
+from pydantic import BaseModel, Field
+from hyperextract.types.hypergraph import AutoHypergraph, AutoHypergraphSchema
# ============================================================================
# Node Schema
@@ -44,7 +44,7 @@ class ThemeSchema(BaseModel):
# participants: List[str] = Field(
# description="Names of key entities involved in this theme"
# )
- participants: List[NodeSchema] = Field(
+ participants: list[NodeSchema] = Field(
description="List of key entities involved in this theme, with their details."
)
description: str = Field(
@@ -60,13 +60,13 @@ class ThemeSchema(BaseModel):
class EdgeSchema(BaseModel):
"""Represents a relationship or event connecting multiple entities (low-order or high-order)."""
- participants: List[str] = Field(
+ participants: list[str] = Field(
description="Names of entities involved in this relationship"
)
description: str = Field(
description="Detailed explanation of the relationship or event"
)
- keywords: List[str] = Field(
+ keywords: list[str] = Field(
description="List of keywords summarizing the relationship themes (e.g., ['conflict', 'trade', 'alliance'])",
)
strength: int = Field(
diff --git a/hyperextract/methods/rag/graph_rag.py b/hyperextract/methods/rag/graph_rag.py
index f91a8e8..8b9bd74 100644
--- a/hyperextract/methods/rag/graph_rag.py
+++ b/hyperextract/methods/rag/graph_rag.py
@@ -5,13 +5,15 @@ Extracts and manages entity-relationship knowledge graphs with standard binary e
import json
from pathlib import Path
-from typing import List, Dict, Optional, Any, Tuple
-from pydantic import BaseModel, Field
+from typing import Any
+
from langchain_core.embeddings import Embeddings
from langchain_core.language_models import BaseChatModel
from langchain_core.prompts import ChatPromptTemplate
-from hyperextract.types import AutoGraph
from ontomem.merger import CustomRuleMerger
+from pydantic import BaseModel, Field
+
+from hyperextract.types import AutoGraph
try:
from graspologic.partition import hierarchical_leiden
@@ -89,10 +91,10 @@ class CommunityReport(BaseModel):
description="Detailed executive summary of the community's structure and key entities"
)
rating: float = Field(description="Impact severity rating (0-10)")
- findings: List[CommunityFinding] = Field(description="List of key findings")
+ findings: list[CommunityFinding] = Field(description="List of key findings")
# Optional fields (filled programmatically)
- id: Optional[str] = Field(default=None, description="Community ID")
- key_entities: Optional[List[str]] = Field(
+ id: str | None = Field(default=None, description="Community ID")
+ key_entities: list[str] | None = Field(
default_factory=list, description="List of key entities in this community"
)
@@ -285,13 +287,13 @@ class Graph_RAG(AutoGraph[NodeSchema, EdgeSchema]):
)
# Community Management
- self.community_reports: Dict[str, CommunityReport] = {}
- self._community_graph: Optional[Any] = None
- self._community_hierarchy: Dict[
- int, Dict[str, List[str]]
+ self.community_reports: dict[str, CommunityReport] = {}
+ self._community_graph: Any | None = None
+ self._community_hierarchy: dict[
+ int, dict[str, list[str]]
] = {} # level -> {community_id: [node_names]}
- self._node_to_community: Dict[
- str, Dict[str, Any]
+ self._node_to_community: dict[
+ str, dict[str, Any]
] = {} # node_name -> {level: community_id}
def dump(self, folder_path: str | Path) -> None:
@@ -541,7 +543,7 @@ class Graph_RAG(AutoGraph[NodeSchema, EdgeSchema]):
top_k_edges: int = 3,
top_k: int | None = None,
use_community: bool = False,
- ) -> Tuple[List, List, Optional[Dict]]:
+ ) -> tuple[list, list, dict | None]:
"""Unified graph search interface with optional community enhancement.
Args:
@@ -565,7 +567,7 @@ class Graph_RAG(AutoGraph[NodeSchema, EdgeSchema]):
query: str,
top_k_nodes: int = 3,
top_k_edges: int = 3,
- ) -> Tuple[List, List, Dict]:
+ ) -> tuple[list, list, dict]:
"""Internal implementation for community-enhanced search."""
if not self.community_reports:
if self.verbose:
@@ -577,7 +579,7 @@ class Graph_RAG(AutoGraph[NodeSchema, EdgeSchema]):
return nodes, edges, community_context
- def _get_community_context_for_query(self, query: str) -> Dict:
+ def _get_community_context_for_query(self, query: str) -> dict:
"""Get community context related to the query."""
if not self.community_reports:
return {}
diff --git a/hyperextract/methods/rag/hyper_rag.py b/hyperextract/methods/rag/hyper_rag.py
index 41bf8c7..aa52322 100644
--- a/hyperextract/methods/rag/hyper_rag.py
+++ b/hyperextract/methods/rag/hyper_rag.py
@@ -3,12 +3,12 @@ Hyper-RAG: Hypergraph-based Retrieval-Augmented Generation
Extracts and manages multi-entity relationships with support for n-ary hyperedges.
"""
-from typing import List
-from pydantic import BaseModel, Field
-from hyperextract.types import AutoHypergraph
from langchain_core.embeddings import Embeddings
from langchain_core.language_models import BaseChatModel
from ontomem.merger import CustomRuleMerger
+from pydantic import BaseModel, Field
+
+from hyperextract.types import AutoHypergraph
# ============================================================================
# Node Schema
@@ -35,13 +35,13 @@ class NodeSchema(BaseModel):
class EdgeSchema(BaseModel):
"""Represents a relationship or event connecting multiple entities (low-order or high-order)."""
- participants: List[str] = Field(
+ participants: list[str] = Field(
description="Names of entities involved in this relationship"
)
description: str = Field(
description="Detailed explanation of the relationship or event"
)
- keywords: List[str] = Field(
+ keywords: list[str] = Field(
description="List of keywords summarizing the relationship themes (e.g., ['conflict', 'trade', 'alliance'])",
)
strength: int = Field(
diff --git a/hyperextract/methods/rag/hypergraph_rag.py b/hyperextract/methods/rag/hypergraph_rag.py
index d6e1d30..4327690 100644
--- a/hyperextract/methods/rag/hypergraph_rag.py
+++ b/hyperextract/methods/rag/hypergraph_rag.py
@@ -10,12 +10,12 @@ each hyperedge encodes a complete semantic unit (sentence or event), maintaining
the natural information flow and contextual dependencies of the source material.
"""
-from typing import List
from hashlib import md5
-from pydantic import BaseModel, Field
+
from langchain_core.embeddings import Embeddings
from langchain_core.language_models import BaseChatModel
from ontomem.merger import MergeStrategy
+from pydantic import BaseModel, Field
from hyperextract.types.hypergraph import AutoHypergraph, AutoHypergraphSchema
@@ -58,7 +58,7 @@ class EdgeSchema(BaseModel):
# related_entities: List[str] = Field(
# description="A list of all entity names involved in or referenced by this knowledge segment."
# )
- related_entities: List[NodeSchema] = Field(
+ related_entities: list[NodeSchema] = Field(
description="A list of all entities involved in or referenced by this knowledge segment."
)
diff --git a/hyperextract/methods/rag/light_rag.py b/hyperextract/methods/rag/light_rag.py
index 17370d9..e859fc6 100644
--- a/hyperextract/methods/rag/light_rag.py
+++ b/hyperextract/methods/rag/light_rag.py
@@ -3,11 +3,12 @@ Light-RAG: Lightweight Graph-based Retrieval-Augmented Generation
Extracts and manages entity-relationship knowledge graphs with standard binary edges.
"""
-from pydantic import BaseModel, Field
-from hyperextract.types import AutoGraph
from langchain_core.embeddings import Embeddings
from langchain_core.language_models import BaseChatModel
from ontomem.merger import CustomRuleMerger
+from pydantic import BaseModel, Field
+
+from hyperextract.types import AutoGraph
# ============================================================================
# Node Schema
diff --git a/hyperextract/methods/registry.py b/hyperextract/methods/registry.py
index 8e3a4fa..17a9498 100644
--- a/hyperextract/methods/registry.py
+++ b/hyperextract/methods/registry.py
@@ -4,16 +4,16 @@ This module provides a unified registry for method templates, enabling
consistent creation and management of method-based knowledge extraction.
"""
-from typing import Dict, Any, Type, Optional
-from pydantic import BaseModel
+from typing import Any
+from pydantic import BaseModel
-_METHOD_REGISTRY: Dict[str, Dict[str, Any]] = {}
+_METHOD_REGISTRY: dict[str, dict[str, Any]] = {}
def register_method(
name: str,
- method_class: Type,
+ method_class: type,
autotype: str,
description: str = "",
) -> None:
@@ -32,7 +32,7 @@ def register_method(
}
-def get_method(name: str) -> Optional[Dict[str, Any]]:
+def get_method(name: str) -> dict[str, Any] | None:
"""Get method info by name.
Args:
@@ -44,7 +44,7 @@ def get_method(name: str) -> Optional[Dict[str, Any]]:
return _METHOD_REGISTRY.get(name)
-def list_methods() -> Dict[str, Dict[str, Any]]:
+def list_methods() -> dict[str, dict[str, Any]]:
"""List all registered methods.
Returns:
@@ -61,7 +61,7 @@ class MethodCfg(BaseModel):
description: str = ""
-def get_method_cfg(name: str) -> Optional[MethodCfg]:
+def get_method_cfg(name: str) -> MethodCfg | None:
"""Get method configuration as MethodCfg.
Args:
@@ -80,7 +80,7 @@ def get_method_cfg(name: str) -> Optional[MethodCfg]:
)
-def list_method_cfgs() -> Dict[str, MethodCfg]:
+def list_method_cfgs() -> dict[str, MethodCfg]:
"""List all method configurations.
Returns:
@@ -99,13 +99,13 @@ def list_method_cfgs() -> Dict[str, MethodCfg]:
def _init_registry():
"""Initialize the registry with built-in methods."""
from hyperextract.methods.rag import (
- Light_RAG,
- Hyper_RAG,
- HyperGraph_RAG,
Cog_RAG,
Graph_RAG,
+ Hyper_RAG,
+ HyperGraph_RAG,
+ Light_RAG,
)
- from hyperextract.methods.typical import iText2KG, iText2KG_Star, KG_Gen, Atom
+ from hyperextract.methods.typical import Atom, KG_Gen, iText2KG, iText2KG_Star
register_method(
name="graph_rag",
diff --git a/hyperextract/methods/typical/__init__.py b/hyperextract/methods/typical/__init__.py
index 2923aa2..b54d45f 100644
--- a/hyperextract/methods/typical/__init__.py
+++ b/hyperextract/methods/typical/__init__.py
@@ -4,14 +4,14 @@ This module contains typical and canonical methods for constructing knowledge gr
from text, including iText2KG, and other established graph-building techniques.
"""
+from .atom import Atom
from .itext2kg import iText2KG
from .itext2kg_star import iText2KG_Star
from .kg_gen import KG_Gen
-from .atom import Atom
__all__ = [
+ "Atom",
+ "KG_Gen",
"iText2KG",
"iText2KG_Star",
- "KG_Gen",
- "Atom",
]
diff --git a/hyperextract/methods/typical/atom.py b/hyperextract/methods/typical/atom.py
index 82cb2b3..991ef9b 100644
--- a/hyperextract/methods/typical/atom.py
+++ b/hyperextract/methods/typical/atom.py
@@ -7,17 +7,17 @@ standardized triple-based knowledge graphs.
Prompts and schemas are adapted from the original Atom implementation.
"""
-from typing import Optional, List
from datetime import datetime
-from pydantic import BaseModel, Field
-from semhash import SemHash
+
+from langchain_core.embeddings import Embeddings
from langchain_core.language_models import BaseChatModel
from langchain_core.prompts import ChatPromptTemplate
-from langchain_core.embeddings import Embeddings
-from ontomem.merger import MergeStrategy, CustomRuleMerger
+from ontomem.merger import CustomRuleMerger, MergeStrategy
+from pydantic import BaseModel, Field
+from semhash import SemHash
-from hyperextract.utils.logging import get_logger
from hyperextract.types.graph import AutoGraph, AutoGraphSchema
+from hyperextract.utils.logging import get_logger
logger = get_logger(__name__)
@@ -82,7 +82,7 @@ class EdgeSchema(BaseModel):
"AVOID preposition-only relation names like 'of', 'in', 'at' - use descriptive present-tense verbs instead."
)
)
- t_start: Optional[list[str]] = Field(
+ t_start: list[str] | None = Field(
default_factory=list,
description=(
"A time or interval indicating when this relationship begins or is active. "
@@ -101,7 +101,7 @@ class EdgeSchema(BaseModel):
"Leave it [] if not specified."
),
)
- t_end: Optional[list[str]] = Field(
+ t_end: list[str] | None = Field(
default_factory=list,
description=(
"A time or interval indicating when this relationship ceases to hold. "
@@ -117,14 +117,14 @@ class EdgeSchema(BaseModel):
"Leave it [] if no end date/time is given."
),
)
- t_obs: Optional[list[str]] = Field(
+ t_obs: list[str] | None = Field(
default=None,
description=(
"DO NOT EXTRACT OR INFER THIS FIELD. Leave it null/None. "
"This field represents the extraction timestamp and will be populated manually by the user post-processing."
),
)
- atomic_facts: Optional[list[str]] = Field(
+ atomic_facts: list[str] | None = Field(
default_factory=list,
description=(
"A list of exact string copies of the atomic facts or sentences from the source text that provide evidence for this relationship. "
@@ -441,7 +441,7 @@ class Atom(AutoGraph[NodeSchema, EdgeSchema]):
{"source_text": chunk, "observation_time": obs_date_str}
for chunk in raw_chunks
]
- chunk_fact_lists: List[AtomicFactSchema] = fact_chain.batch(
+ chunk_fact_lists: list[AtomicFactSchema] = fact_chain.batch(
fact_inputs, config={"max_concurrency": self.max_workers}
)
diff --git a/hyperextract/methods/typical/itext2kg.py b/hyperextract/methods/typical/itext2kg.py
index a478936..6b78c7c 100644
--- a/hyperextract/methods/typical/itext2kg.py
+++ b/hyperextract/methods/typical/itext2kg.py
@@ -7,10 +7,10 @@ standardized triple-based knowledge graphs.
Prompts and schemas are adapted from the original iText2KG* implementation.
"""
-from pydantic import BaseModel, Field
-from langchain_core.language_models import BaseChatModel
from langchain_core.embeddings import Embeddings
+from langchain_core.language_models import BaseChatModel
from ontomem.merger import MergeStrategy
+from pydantic import BaseModel, Field
from hyperextract.types import AutoGraph
from hyperextract.utils.logging import get_logger
diff --git a/hyperextract/methods/typical/itext2kg_star.py b/hyperextract/methods/typical/itext2kg_star.py
index 70af0a8..1246602 100644
--- a/hyperextract/methods/typical/itext2kg_star.py
+++ b/hyperextract/methods/typical/itext2kg_star.py
@@ -7,13 +7,13 @@ standardized triple-based knowledge graphs.
Prompts and schemas are adapted from the original iText2KG_Star implementation.
"""
-from typing import Optional
from datetime import datetime
-from pydantic import BaseModel, Field
-from semhash import SemHash
-from langchain_core.language_models import BaseChatModel
+
from langchain_core.embeddings import Embeddings
+from langchain_core.language_models import BaseChatModel
from ontomem.merger import MergeStrategy
+from pydantic import BaseModel, Field
+from semhash import SemHash
from hyperextract.types.graph import AutoGraph, AutoGraphSchema
from hyperextract.utils.logging import get_logger
@@ -48,7 +48,7 @@ class NodeSchema(BaseModel):
class EdgeProperties(BaseModel):
"""Properties for Knowledge Graph Edge"""
- observation_date: Optional[str] = Field(
+ observation_date: str | None = Field(
default=None,
description=(
"The date when the relationship was observed or recorded. "
diff --git a/hyperextract/methods/typical/kg_gen.py b/hyperextract/methods/typical/kg_gen.py
index 94c8205..cd0bd2c 100644
--- a/hyperextract/methods/typical/kg_gen.py
+++ b/hyperextract/methods/typical/kg_gen.py
@@ -7,11 +7,11 @@ Prompts are adapted from kg_gen/steps to ensure consistency with the original
knowledge graph extraction logic.
"""
-from semhash import SemHash
-from pydantic import BaseModel, Field
-from langchain_core.language_models import BaseChatModel
from langchain_core.embeddings import Embeddings
+from langchain_core.language_models import BaseChatModel
from ontomem.merger import MergeStrategy
+from pydantic import BaseModel, Field
+from semhash import SemHash
from hyperextract.types.graph import AutoGraph, AutoGraphSchema
from hyperextract.utils.logging import get_logger
diff --git a/hyperextract/templates/README.md b/hyperextract/templates/README.md
index 2378d5c..5651507 100644
--- a/hyperextract/templates/README.md
+++ b/hyperextract/templates/README.md
@@ -430,9 +430,9 @@ embedder = OpenAIEmbeddings()
# Load custom template by file path
template = Template.create(
"/path/to/my_template.yaml", # Template file path
- "zh", # Language
+ "zh", # Language
llm,
- embedder
+ embedder,
)
result = template.parse("Your document text here...")
diff --git a/hyperextract/templates/README_ZH.md b/hyperextract/templates/README_ZH.md
index 4dc1a7b..5ee64df 100644
--- a/hyperextract/templates/README_ZH.md
+++ b/hyperextract/templates/README_ZH.md
@@ -430,9 +430,9 @@ embedder = OpenAIEmbeddings()
# 使用文件路径加载自定义模板
template = Template.create(
"/path/to/my_template.yaml", # 模板文件路径
- "zh", # 语言
+ "zh", # 语言
llm,
- embedder
+ embedder,
)
result = template.parse("您的文档文本...")
diff --git a/hyperextract/types/__init__.py b/hyperextract/types/__init__.py
index 36abe1c..3cc126e 100644
--- a/hyperextract/types/__init__.py
+++ b/hyperextract/types/__init__.py
@@ -52,28 +52,28 @@ import os
os.environ["TRANSFORMERS_NO_ADVISORY_WARNINGS"] = "1"
from .base import BaseAutoType
-from .model import AutoModel
-from .list import AutoList
-from .set import AutoSet
from .graph import AutoGraph
from .hypergraph import AutoHypergraph
-from .temporal_graph import AutoTemporalGraph
+from .list import AutoList
+from .model import AutoModel
+from .set import AutoSet
from .spatial_graph import AutoSpatialGraph
from .spatio_temporal_graph import AutoSpatioTemporalGraph
+from .temporal_graph import AutoTemporalGraph
__all__ = [
- # Base class
- "BaseAutoType",
- # Scalar types
- "AutoModel",
+ # Graph types
+ "AutoGraph",
+ # Hypergraph type
+ "AutoHypergraph",
# Collection types
"AutoList",
+ # Scalar types
+ "AutoModel",
"AutoSet",
- # Graph types
- "AutoGraph",
- "AutoTemporalGraph",
"AutoSpatialGraph",
"AutoSpatioTemporalGraph",
- # Hypergraph type
- "AutoHypergraph",
+ "AutoTemporalGraph",
+ # Base class
+ "BaseAutoType",
]
diff --git a/hyperextract/types/base.py b/hyperextract/types/base.py
index 3edb0b1..d89d3bf 100644
--- a/hyperextract/types/base.py
+++ b/hyperextract/types/base.py
@@ -1,14 +1,15 @@
import json
-from pathlib import Path
-from datetime import datetime
-from pydantic import BaseModel
from abc import ABC, abstractmethod
-from typing import TypeVar, Generic, Any, Dict, Type, List
-from langchain_core.messages import AIMessage
+from datetime import datetime
+from pathlib import Path
+from typing import Any, Generic, TypeVar
+
from langchain_core.embeddings import Embeddings
-from langchain_core.prompts import ChatPromptTemplate
from langchain_core.language_models.chat_models import BaseChatModel
+from langchain_core.messages import AIMessage
+from langchain_core.prompts import ChatPromptTemplate
from langchain_text_splitters import RecursiveCharacterTextSplitter
+from pydantic import BaseModel
from hyperextract.utils.logging import get_logger
@@ -41,7 +42,7 @@ class BaseAutoType(ABC, Generic[T]):
def __init__(
self,
- data_schema: Type[T],
+ data_schema: type[T],
llm_client: BaseChatModel,
embedder: Embeddings,
*,
@@ -92,7 +93,7 @@ class BaseAutoType(ABC, Generic[T]):
self._init_internal_state()
# Internal state storing the extracted knowledge
- self.metadata: Dict[str, Any] = {
+ self.metadata: dict[str, Any] = {
"created_at": datetime.now(),
"updated_at": datetime.now(),
}
@@ -122,12 +123,11 @@ class BaseAutoType(ABC, Generic[T]):
Subclasses must implement this to provide a prompt tailored to their extraction pattern.
"""
- pass
# ==================== Data Access Interface ====================
@property
- def data_schema(self) -> Type[T]:
+ def data_schema(self) -> type[T]:
"""Returns the Pydantic schema class used by this knowledge instance.
Returns:
@@ -147,7 +147,6 @@ class BaseAutoType(ABC, Generic[T]):
Returns:
The internal knowledge data as a Pydantic model instance (or converted form).
"""
- pass
@abstractmethod
def empty(self) -> bool:
@@ -156,7 +155,6 @@ class BaseAutoType(ABC, Generic[T]):
Returns:
True if no data is stored, False otherwise.
"""
- pass
# ==================== Data Management Operations ====================
@@ -196,7 +194,6 @@ class BaseAutoType(ABC, Generic[T]):
Subclasses must implement this to set up internal structures that may be optimized
beyond the standard Pydantic schema (e.g., OMem for AutoSet, dict-based for others).
"""
- pass
@abstractmethod
def _set_data_state(self, data: T) -> None:
@@ -212,7 +209,6 @@ class BaseAutoType(ABC, Generic[T]):
Args:
data: The new data object to set.
"""
- pass
@abstractmethod
def _update_data_state(self, incoming_data: T) -> None:
@@ -230,7 +226,6 @@ class BaseAutoType(ABC, Generic[T]):
Args:
incoming_data: The incremental data to merge into the current state.
"""
- pass
@abstractmethod
def _init_index_state(self) -> None:
@@ -243,7 +238,6 @@ class BaseAutoType(ABC, Generic[T]):
This separation allows index implementation to be decoupled from base class.
"""
- pass
# ==================== Extraction & Merge ====================
@@ -420,7 +414,7 @@ class BaseAutoType(ABC, Generic[T]):
return self
@abstractmethod
- def merge_batch_data(self, data_list: List[T]) -> T:
+ def merge_batch_data(self, data_list: list[T]) -> T:
"""Merges multiple knowledge data objects into a single unified object.
This is a pure data transformation method that does not modify internal state.
@@ -439,7 +433,6 @@ class BaseAutoType(ABC, Generic[T]):
Returns:
A new merged knowledge object.
"""
- pass
# ==================== Indexing & Search & Chat ====================
@@ -450,10 +443,9 @@ class BaseAutoType(ABC, Generic[T]):
Subclasses must implement this method to define how the vector index is constructed
from the knowledge data. Uses FAISS as the vector store backend.
"""
- pass
@abstractmethod
- def search(self, query: str, top_k: int = 3) -> List[Any]:
+ def search(self, query: str, top_k: int = 3) -> list[Any]:
"""Performs semantic search over the knowledge abstract.
Standard search workflow:
@@ -470,7 +462,6 @@ class BaseAutoType(ABC, Generic[T]):
Returns:
List of relevant knowledge items.
"""
- pass
def chat(self, query: str, top_k: int = 3) -> AIMessage:
"""Performs a chat-like interaction with the knowledge abstract.
@@ -672,7 +663,6 @@ class BaseAutoType(ABC, Generic[T]):
Args:
folder_path: Target folder path for saving index files.
"""
- pass
@abstractmethod
def load_index(self, folder_path: str | Path) -> None:
@@ -684,7 +674,6 @@ class BaseAutoType(ABC, Generic[T]):
Args:
folder_path: Source folder path containing index files.
"""
- pass
# ==================== Operator Overloads ====================
diff --git a/hyperextract/types/graph.py b/hyperextract/types/graph.py
index a36821b..97ce1c3 100644
--- a/hyperextract/types/graph.py
+++ b/hyperextract/types/graph.py
@@ -4,29 +4,28 @@ Provides automatic deduplication for both nodes and edges using OMem.
Supports single-stage and two-stage extraction strategies with consistency validation.
"""
+from collections.abc import Callable
+from pathlib import Path
from typing import (
+ TYPE_CHECKING,
Any,
- List,
- Type,
- Tuple,
- Callable,
- TypeVar,
Generic,
- TYPE_CHECKING,
+ TypeVar,
)
-from pathlib import Path
-from pydantic import BaseModel, Field, create_model
-from langchain_core.messages import AIMessage
+
from langchain_core.embeddings import Embeddings
from langchain_core.language_models.chat_models import BaseChatModel
+from langchain_core.messages import AIMessage
from langchain_core.prompts import ChatPromptTemplate
from ontomem import OMem
-from ontomem.merger import MergeStrategy, create_merger, BaseMerger
+from ontomem.merger import BaseMerger, MergeStrategy, create_merger
from ontosight import view_graph
+from pydantic import BaseModel, Field, create_model
-from .base import BaseAutoType
from hyperextract.utils.logging import get_logger
+from .base import BaseAutoType
+
logger = get_logger(__name__)
@@ -78,10 +77,10 @@ DEFAULT_EDGE_PROMPT = (
class AutoGraphSchema(BaseModel, Generic[NodeSchema, EdgeSchema]):
"""Generic schema container for graph-based knowledge patterns."""
- nodes: List[NodeSchema] = Field(
+ nodes: list[NodeSchema] = Field(
default_factory=list, description="Graph nodes/entities"
)
- edges: List[EdgeSchema] = Field(
+ edges: list[EdgeSchema] = Field(
default_factory=list, description="Graph edges/relationships"
)
@@ -89,7 +88,7 @@ class AutoGraphSchema(BaseModel, Generic[NodeSchema, EdgeSchema]):
class NodeListSchema(BaseModel, Generic[NodeSchema]):
"""Intermediate schema for batch node extraction."""
- items: List[NodeSchema] = Field(
+ items: list[NodeSchema] = Field(
default_factory=list,
description="List of identified entities or nodes found in the text.",
)
@@ -98,7 +97,7 @@ class NodeListSchema(BaseModel, Generic[NodeSchema]):
class EdgeListSchema(BaseModel, Generic[EdgeSchema]):
"""Intermediate schema for batch edge extraction."""
- items: List[EdgeSchema] = Field(
+ items: list[EdgeSchema] = Field(
default_factory=list,
description="List of identified relationships or edges found in the text.",
)
@@ -148,15 +147,15 @@ class AutoGraph(
"""
if TYPE_CHECKING:
- graph_schema: Type[AutoGraphSchema[NodeSchema, EdgeSchema]]
+ graph_schema: type[AutoGraphSchema[NodeSchema, EdgeSchema]]
def __init__(
self,
- node_schema: Type[NodeSchema],
- edge_schema: Type[EdgeSchema],
+ node_schema: type[NodeSchema],
+ edge_schema: type[EdgeSchema],
node_key_extractor: Callable[[NodeSchema], str],
edge_key_extractor: Callable[[EdgeSchema], str],
- nodes_in_edge_extractor: Callable[[EdgeSchema], Tuple[str, str]],
+ nodes_in_edge_extractor: Callable[[EdgeSchema], tuple[str, str]],
llm_client: BaseChatModel,
embedder: Embeddings,
*,
@@ -168,14 +167,14 @@ class AutoGraph(
prompt: str = "",
prompt_for_node_extraction: str = "",
prompt_for_edge_extraction: str = "",
- node_label_extractor: Callable[[NodeSchema], str] = None,
- edge_label_extractor: Callable[[EdgeSchema], str] = None,
+ node_label_extractor: Callable[[NodeSchema], str] | None = None,
+ edge_label_extractor: Callable[[EdgeSchema], str] | None = None,
chunk_size: int = 2048,
chunk_overlap: int = 256,
max_workers: int = 10,
verbose: bool = False,
- node_fields_for_index: List[str] | None = None,
- edge_fields_for_index: List[str] | None = None,
+ node_fields_for_index: list[str] | None = None,
+ edge_fields_for_index: list[str] | None = None,
**kwargs: Any,
):
"""Initialize AutoGraph with node/edge schemas and configuration.
@@ -227,22 +226,22 @@ class AutoGraph(
graph_schema_name = f"{node_schema.__name__}{edge_schema.__name__}Graph"
self.graph_schema = create_model(
graph_schema_name,
- nodes=(List[node_schema], Field(default_factory=list)),
- edges=(List[edge_schema], Field(default_factory=list)),
+ nodes=(list[node_schema], Field(default_factory=list)),
+ edges=(list[edge_schema], Field(default_factory=list)),
)
# Create schema for list extraction (two-stage mode) with 'items' field
self.node_list_schema = create_model(
"NodeList",
items=(
- List[node_schema],
+ list[node_schema],
Field(default_factory=list, description="Extracted nodes"),
),
)
self.edge_list_schema = create_model(
"EdgeList",
items=(
- List[edge_schema],
+ list[edge_schema],
Field(default_factory=list, description="Extracted edges"),
),
)
@@ -378,7 +377,7 @@ class AutoGraph(
)
@property
- def nodes(self) -> List[NodeSchema]:
+ def nodes(self) -> list[NodeSchema]:
"""Returns the current node collection.
Returns:
@@ -387,7 +386,7 @@ class AutoGraph(
return self._node_memory.items
@property
- def edges(self) -> List[EdgeSchema]:
+ def edges(self) -> list[EdgeSchema]:
"""Returns the current edge collection.
Returns:
@@ -575,8 +574,8 @@ class AutoGraph(
return result
def _extract_nodes_batch(
- self, chunks: List[str]
- ) -> List[NodeListSchema[NodeSchema]]:
+ self, chunks: list[str]
+ ) -> list[NodeListSchema[NodeSchema]]:
"""Batch extract nodes from multiple text chunks.
Args:
@@ -595,8 +594,8 @@ class AutoGraph(
)
def _extract_edges_batch(
- self, chunks: List[str], node_lists: List[NodeListSchema[NodeSchema]]
- ) -> List[EdgeListSchema[EdgeSchema]]:
+ self, chunks: list[str], node_lists: list[NodeListSchema[NodeSchema]]
+ ) -> list[EdgeListSchema[EdgeSchema]]:
"""Batch extract edges using corresponding node lists as context.
Args:
@@ -672,8 +671,8 @@ class AutoGraph(
def merge_batch_data(
self,
- data_list_or_tuple: List[AutoGraphSchema[NodeSchema, EdgeSchema]]
- | Tuple[List[List[NodeSchema]], List[List[EdgeSchema]]],
+ data_list_or_tuple: list[AutoGraphSchema[NodeSchema, EdgeSchema]]
+ | tuple[list[list[NodeSchema]], list[list[EdgeSchema]]],
) -> AutoGraphSchema[NodeSchema, EdgeSchema]:
"""Merge multiple graphs or node/edge tuples into one.
@@ -784,7 +783,7 @@ class AutoGraph(
top_k_nodes: int = 3,
top_k_edges: int = 3,
top_k: int | None = None,
- ) -> Tuple[List[NodeSchema], List[EdgeSchema]]:
+ ) -> tuple[list[NodeSchema], list[EdgeSchema]]:
"""Unified graph search interface.
Retrieves nodes and edges semantically related to the query.
@@ -814,8 +813,8 @@ class AutoGraph(
"At least one of top_k_nodes or top_k_edges must be positive."
)
- nodes: List[NodeSchema] = []
- edges: List[EdgeSchema] = []
+ nodes: list[NodeSchema] = []
+ edges: list[EdgeSchema] = []
if top_k_nodes > 0:
if not self._node_memory.has_index():
@@ -829,7 +828,7 @@ class AutoGraph(
return nodes, edges
- def search_nodes(self, query: str, top_k: int = 3) -> List[NodeSchema]:
+ def search_nodes(self, query: str, top_k: int = 3) -> list[NodeSchema]:
"""Semantic search for nodes/entities only.
Args:
@@ -841,7 +840,7 @@ class AutoGraph(
"""
return self._node_memory.search(query=query, top_k=top_k)
- def search_edges(self, query: str, top_k: int = 3) -> List[EdgeSchema]:
+ def search_edges(self, query: str, top_k: int = 3) -> list[EdgeSchema]:
"""Semantic search for edges/relationships only.
Args:
@@ -986,8 +985,8 @@ class AutoGraph(
def show(
self,
- node_label_extractor: Callable[[NodeSchema], str] = None,
- edge_label_extractor: Callable[[EdgeSchema], str] = None,
+ node_label_extractor: Callable[[NodeSchema], str] | None = None,
+ edge_label_extractor: Callable[[EdgeSchema], str] | None = None,
*,
top_k_nodes_for_search: int = 3,
top_k_edges_for_search: int = 3,
@@ -1059,8 +1058,8 @@ class AutoGraph(
self,
folder_path: str | Path,
*,
- node_label_extractor: Callable[[NodeSchema], str] = None,
- edge_label_extractor: Callable[[EdgeSchema], str] = None,
+ node_label_extractor: Callable[[NodeSchema], str] | None = None,
+ edge_label_extractor: Callable[[EdgeSchema], str] | None = None,
vault_name: str = "Knowledge Vault",
include_index: bool = True,
overwrite: bool = False,
diff --git a/hyperextract/types/hypergraph.py b/hyperextract/types/hypergraph.py
index a4eb10f..a3fb046 100644
--- a/hyperextract/types/hypergraph.py
+++ b/hyperextract/types/hypergraph.py
@@ -5,29 +5,28 @@ Key Difference from AutoGraph:
- Consistency validation checks if *ALL* participants in a hyperedge exist in the node registry.
"""
+from collections.abc import Callable
+from pathlib import Path
from typing import (
+ TYPE_CHECKING,
Any,
- List,
- Type,
- Tuple,
- Callable,
- TypeVar,
Generic,
- TYPE_CHECKING,
+ TypeVar,
)
-from pathlib import Path
-from pydantic import BaseModel, Field, create_model
+
from langchain_core.embeddings import Embeddings
-from langchain_core.messages import AIMessage
from langchain_core.language_models.chat_models import BaseChatModel
+from langchain_core.messages import AIMessage
from langchain_core.prompts import ChatPromptTemplate
from ontomem import OMem
-from ontomem.merger import MergeStrategy, create_merger, BaseMerger
+from ontomem.merger import BaseMerger, MergeStrategy, create_merger
from ontosight import view_hypergraph
+from pydantic import BaseModel, Field, create_model
-from .base import BaseAutoType
from hyperextract.utils.logging import get_logger
+from .base import BaseAutoType
+
logger = get_logger(__name__)
@@ -80,10 +79,10 @@ CRITICAL RULES:
class AutoHypergraphSchema(BaseModel, Generic[NodeSchema, EdgeSchema]):
"""Generic schema container for hypergraph data."""
- nodes: List[NodeSchema] = Field(
+ nodes: list[NodeSchema] = Field(
default_factory=list, description="Graph nodes/entities"
)
- edges: List[EdgeSchema] = Field(
+ edges: list[EdgeSchema] = Field(
default_factory=list, description="Hyperedges connecting multiple entities"
)
@@ -91,7 +90,7 @@ class AutoHypergraphSchema(BaseModel, Generic[NodeSchema, EdgeSchema]):
class NodeListSchema(BaseModel, Generic[NodeSchema]):
"""Intermediate schema for batch node extraction."""
- items: List[NodeSchema] = Field(
+ items: list[NodeSchema] = Field(
default_factory=list,
description="List of identified entities or nodes found in the text.",
)
@@ -100,7 +99,7 @@ class NodeListSchema(BaseModel, Generic[NodeSchema]):
class EdgeListSchema(BaseModel, Generic[EdgeSchema]):
"""Intermediate schema for batch hyperedge extraction."""
- items: List[EdgeSchema] = Field(
+ items: list[EdgeSchema] = Field(
default_factory=list,
description="List of identified hyperedges found in the text.",
)
@@ -146,16 +145,16 @@ class AutoHypergraph(
"""
if TYPE_CHECKING:
- graph_schema: Type[AutoHypergraphSchema[NodeSchema, EdgeSchema]]
+ graph_schema: type[AutoHypergraphSchema[NodeSchema, EdgeSchema]]
def __init__(
self,
- node_schema: Type[NodeSchema],
- edge_schema: Type[EdgeSchema],
+ node_schema: type[NodeSchema],
+ edge_schema: type[EdgeSchema],
node_key_extractor: Callable[[NodeSchema], str],
edge_key_extractor: Callable[[EdgeSchema], str],
# Returns a tuple of ALL node keys involved in this hyperedge
- nodes_in_edge_extractor: Callable[[EdgeSchema], Tuple[str, ...]],
+ nodes_in_edge_extractor: Callable[[EdgeSchema], tuple[str, ...]],
llm_client: BaseChatModel,
embedder: Embeddings,
*,
@@ -167,14 +166,14 @@ class AutoHypergraph(
prompt: str = "",
prompt_for_node_extraction: str = "",
prompt_for_edge_extraction: str = "",
- node_label_extractor: Callable[[NodeSchema], str] = None,
- edge_label_extractor: Callable[[EdgeSchema], str] = None,
+ node_label_extractor: Callable[[NodeSchema], str] | None = None,
+ edge_label_extractor: Callable[[EdgeSchema], str] | None = None,
chunk_size: int = 2048,
chunk_overlap: int = 256,
max_workers: int = 10,
verbose: bool = False,
- node_fields_for_index: List[str] | None = None,
- edge_fields_for_index: List[str] | None = None,
+ node_fields_for_index: list[str] | None = None,
+ edge_fields_for_index: list[str] | None = None,
**kwargs: Any,
):
"""Initialize AutoHypergraph with node/edge schemas and configuration.
@@ -238,18 +237,18 @@ class AutoHypergraph(
graph_schema_name = f"{node_schema.__name__}{edge_schema.__name__}Hypergraph"
self.graph_schema = create_model(
graph_schema_name,
- nodes=(List[node_schema], Field(default_factory=list)),
- edges=(List[edge_schema], Field(default_factory=list)),
+ nodes=(list[node_schema], Field(default_factory=list)),
+ edges=(list[edge_schema], Field(default_factory=list)),
)
# Helper Schemas for Batch Extraction
self.node_list_schema = create_model(
f"{node_schema.__name__}List",
- items=(List[node_schema], Field(default_factory=list)),
+ items=(list[node_schema], Field(default_factory=list)),
)
self.edge_list_schema = create_model(
f"{edge_schema.__name__}List",
- items=(List[edge_schema], Field(default_factory=list)),
+ items=(list[edge_schema], Field(default_factory=list)),
)
# Initialize Node Merger
@@ -418,12 +417,12 @@ class AutoHypergraph(
)
@property
- def nodes(self) -> List[NodeSchema]:
+ def nodes(self) -> list[NodeSchema]:
"""Returns the current node collection."""
return self._node_memory.items
@property
- def edges(self) -> List[EdgeSchema]:
+ def edges(self) -> list[EdgeSchema]:
"""Returns the current hyperedge collection."""
return self._edge_memory.items
@@ -506,8 +505,8 @@ class AutoHypergraph(
return self.merge_batch_data(partial_hypergraphs)
def _extract_nodes_batch(
- self, chunks: List[str]
- ) -> List[NodeListSchema[NodeSchema]]:
+ self, chunks: list[str]
+ ) -> list[NodeListSchema[NodeSchema]]:
"""Batch extract nodes from multiple text chunks."""
inputs = [{"source_text": chunk} for chunk in chunks]
results = self.node_extractor.batch(
@@ -519,8 +518,8 @@ class AutoHypergraph(
)
def _extract_edges_batch(
- self, chunks: List[str], node_lists: List[NodeListSchema[NodeSchema]]
- ) -> List[EdgeListSchema[EdgeSchema]]:
+ self, chunks: list[str], node_lists: list[NodeListSchema[NodeSchema]]
+ ) -> list[EdgeListSchema[EdgeSchema]]:
"""Batch extract hyperedges using corresponding node lists as context."""
inputs = []
for chunk, node_list in zip(chunks, node_lists):
@@ -593,8 +592,8 @@ class AutoHypergraph(
def merge_batch_data(
self,
- data_list_or_tuple: List[AutoHypergraphSchema]
- | Tuple[List[List[NodeSchema]], List[List[EdgeSchema]]],
+ data_list_or_tuple: list[AutoHypergraphSchema]
+ | tuple[list[list[NodeSchema]], list[list[EdgeSchema]]],
) -> AutoHypergraphSchema:
"""Merge multiple hypergraphs or node/edge tuples into one.
@@ -689,7 +688,7 @@ class AutoHypergraph(
top_k_nodes: int = 3,
top_k_edges: int = 3,
top_k: int | None = None,
- ) -> Tuple[List[NodeSchema], List[EdgeSchema]]:
+ ) -> tuple[list[NodeSchema], list[EdgeSchema]]:
"""Unified hypergraph search interface.
Retrieves nodes and hyperedges semantically related to the query.
@@ -719,8 +718,8 @@ class AutoHypergraph(
"At least one of top_k_nodes or top_k_edges must be positive."
)
- nodes: List[NodeSchema] = []
- edges: List[EdgeSchema] = []
+ nodes: list[NodeSchema] = []
+ edges: list[EdgeSchema] = []
if top_k_nodes > 0:
if not self._node_memory.has_index():
@@ -734,11 +733,11 @@ class AutoHypergraph(
return nodes, edges
- def search_nodes(self, query: str, top_k: int = 3) -> List[NodeSchema]:
+ def search_nodes(self, query: str, top_k: int = 3) -> list[NodeSchema]:
"""Semantic search for nodes/entities only."""
return self._node_memory.search(query=query, top_k=top_k)
- def search_edges(self, query: str, top_k: int = 3) -> List[EdgeSchema]:
+ def search_edges(self, query: str, top_k: int = 3) -> list[EdgeSchema]:
"""Semantic search for hyperedges/relationships only."""
return self._edge_memory.search(query=query, top_k=top_k)
@@ -874,8 +873,8 @@ class AutoHypergraph(
def show(
self,
- node_label_extractor: Callable[[NodeSchema], str] = None,
- edge_label_extractor: Callable[[EdgeSchema], str] = None,
+ node_label_extractor: Callable[[NodeSchema], str] | None = None,
+ edge_label_extractor: Callable[[EdgeSchema], str] | None = None,
*,
top_k_nodes_for_search: int = 3,
top_k_edges_for_search: int = 3,
@@ -947,8 +946,8 @@ class AutoHypergraph(
self,
folder_path: str | Path,
*,
- node_label_extractor: Callable[[NodeSchema], str] = None,
- edge_label_extractor: Callable[[EdgeSchema], str] = None,
+ node_label_extractor: Callable[[NodeSchema], str] | None = None,
+ edge_label_extractor: Callable[[EdgeSchema], str] | None = None,
vault_name: str = "Knowledge Vault",
include_index: bool = True,
overwrite: bool = False,
diff --git a/hyperextract/types/list.py b/hyperextract/types/list.py
index a2942ae..9b9e432 100644
--- a/hyperextract/types/list.py
+++ b/hyperextract/types/list.py
@@ -1,28 +1,28 @@
"""List Knowledge Pattern - extracts a collection of objects from text."""
+from __future__ import annotations
+
+from collections.abc import Callable, Iterable, Iterator
+from datetime import datetime
+from pathlib import Path
from typing import (
+ TYPE_CHECKING,
Any,
- List,
- Type,
- TypeVar,
Generic,
- Iterator,
- Callable,
- Iterable,
- TYPE_CHECKING,
+ TypeVar,
)
-from pathlib import Path
-from datetime import datetime
-from pydantic import BaseModel, Field, create_model
+
+from langchain_community.vectorstores import FAISS
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_nodes
+from pydantic import BaseModel, Field, create_model
-from .base import BaseAutoType
from hyperextract.utils.logging import get_logger
+from .base import BaseAutoType
+
logger = get_logger(__name__)
@@ -42,7 +42,7 @@ DEFAULT_LIST_PROMPT = (
class AutoListSchema(BaseModel, Generic[ItemSchema]):
"""Generic schema container for list-based knowledge patterns."""
- items: List[ItemSchema] = Field(default_factory=list, description="Item list")
+ items: list[ItemSchema] = Field(default_factory=list, description="Item list")
class AutoList(BaseAutoType[AutoListSchema[ItemSchema]], Generic[ItemSchema]):
@@ -63,21 +63,21 @@ class AutoList(BaseAutoType[AutoListSchema[ItemSchema]], Generic[ItemSchema]):
if TYPE_CHECKING:
# Use generic version during type checking to maintain complete type hints
- item_list_schema: Type[AutoListSchema[ItemSchema]]
+ item_list_schema: type[AutoListSchema[ItemSchema]]
def __init__(
self,
- item_schema: Type[ItemSchema],
+ item_schema: type[ItemSchema],
llm_client: BaseChatModel,
embedder: Embeddings,
*,
prompt: str = "",
- item_label_extractor: Callable[[ItemSchema], str] = None,
+ item_label_extractor: Callable[[ItemSchema], str] | None = None,
chunk_size: int = 2048,
chunk_overlap: int = 256,
max_workers: int = 10,
verbose: bool = False,
- fields_for_index: List[str] | None = None,
+ fields_for_index: list[str] | None = None,
):
"""Initialize AutoList with item schema and configuration.
@@ -101,7 +101,7 @@ class AutoList(BaseAutoType[AutoListSchema[ItemSchema]], Generic[ItemSchema]):
self.item_list_schema = create_model(
container_name,
items=(
- List[item_schema],
+ list[item_schema],
Field(default_factory=list, description="Item list"),
),
)
@@ -132,7 +132,7 @@ class AutoList(BaseAutoType[AutoListSchema[ItemSchema]], Generic[ItemSchema]):
return DEFAULT_LIST_PROMPT
@property
- def items(self) -> List[ItemSchema]:
+ def items(self) -> list[ItemSchema]:
"""Returns the internal list of extracted items."""
return getattr(self._data, "items", [])
@@ -209,7 +209,7 @@ class AutoList(BaseAutoType[AutoListSchema[ItemSchema]], Generic[ItemSchema]):
# ==================== Core Methods ====================
- def merge_batch_data(self, data_list: List[AutoListSchema]) -> AutoListSchema:
+ def merge_batch_data(self, data_list: list[AutoListSchema]) -> AutoListSchema:
"""Pure data merge method implementing list append strategy.
Merge strategy: Collects all items from all container objects and merges them
@@ -275,7 +275,7 @@ class AutoList(BaseAutoType[AutoListSchema[ItemSchema]], Generic[ItemSchema]):
logger.error("FAISS not available. Install with: pip install faiss-cpu")
raise
- def search(self, query: str, top_k: int = 3) -> List[ItemSchema]:
+ def search(self, query: str, top_k: int = 3) -> list[ItemSchema]:
"""Searches items in the list using semantic similarity.
Args:
@@ -293,7 +293,7 @@ class AutoList(BaseAutoType[AutoListSchema[ItemSchema]], Generic[ItemSchema]):
raise Exception("Vector store not initialized")
docs = self._index.similarity_search(query, k=top_k)
- results: List[ItemSchema] = []
+ results: list[ItemSchema] = []
for doc in docs:
# Attempt to restore as object
try:
@@ -326,7 +326,7 @@ class AutoList(BaseAutoType[AutoListSchema[ItemSchema]], Generic[ItemSchema]):
def show(
self,
- item_label_extractor: Callable[[ItemSchema], str] = None,
+ item_label_extractor: Callable[[ItemSchema], str] | None = None,
*,
top_k_for_search: int = 3,
top_k_for_chat: int = 3,
@@ -891,8 +891,10 @@ class AutoList(BaseAutoType[AutoListSchema[ItemSchema]], Generic[ItemSchema]):
extra = got_field_names - expected_field_names
error_parts = [
- f"Item schema mismatch. Expected {self.item_schema.__name__}, "
- f"but got {type(item).__name__}."
+ (
+ f"Item schema mismatch. Expected {self.item_schema.__name__}, "
+ f"but got {type(item).__name__}."
+ )
]
if missing:
diff --git a/hyperextract/types/model.py b/hyperextract/types/model.py
index b7a6530..c2971a9 100644
--- a/hyperextract/types/model.py
+++ b/hyperextract/types/model.py
@@ -1,21 +1,23 @@
"""Unit Knowledge Pattern - extracts a single structured object from text."""
-from pathlib import Path
+from collections.abc import Callable
from datetime import datetime
-from typing import List, Any, Type, Callable, Union, TYPE_CHECKING
+from pathlib import Path
+from typing import TYPE_CHECKING, Any, Union
if TYPE_CHECKING:
from .list import AutoList
-from ontomem.merger import MergeStrategy, create_merger, BaseMerger
-from langchain_core.language_models.chat_models import BaseChatModel
-from langchain_core.embeddings import Embeddings
-from langchain_core.documents import Document
from langchain_community.vectorstores import FAISS
+from langchain_core.documents import Document
+from langchain_core.embeddings import Embeddings
+from langchain_core.language_models.chat_models import BaseChatModel
+from ontomem.merger import BaseMerger, MergeStrategy, create_merger
from ontosight import view_nodes
-from .base import BaseAutoType, T
from hyperextract.utils.logging import get_logger
+from .base import BaseAutoType, T
+
logger = get_logger(__name__)
@@ -50,13 +52,13 @@ class AutoModel(BaseAutoType[T]):
def __init__(
self,
- data_schema: Type[T],
+ data_schema: type[T],
llm_client: BaseChatModel,
embedder: Embeddings,
*,
strategy_or_merger: MergeStrategy | BaseMerger = MergeStrategy.LLM.BALANCED,
prompt: str = "",
- label_extractor: Callable[[T], str] = None,
+ label_extractor: Callable[[T], str] | None = None,
chunk_size: int = 2048,
chunk_overlap: int = 256,
max_workers: int = 10,
@@ -206,7 +208,7 @@ class AutoModel(BaseAutoType[T]):
# ==================== Core Methods ====================
- def merge_batch_data(self, data_list: List[T]) -> T:
+ def merge_batch_data(self, data_list: list[T]) -> T:
"""Merge multiple extracted objects using configured strategy.
Leverages ontomem's merge strategies to intelligently combine results from
@@ -264,7 +266,7 @@ class AutoModel(BaseAutoType[T]):
self._index = FAISS.from_documents(documents, self.embedder)
logger.info(f"Built FAISS index with {len(documents)} documents")
- def search(self, query: str, top_k: int = 3) -> List[Any]:
+ def search(self, query: str, top_k: int = 3) -> list[Any]:
"""Searches all indexed fields using semantic similarity.
Args:
@@ -315,7 +317,7 @@ class AutoModel(BaseAutoType[T]):
def show(
self,
- label_extractor: Callable[[T], str] = None,
+ label_extractor: Callable[[T], str] | None = None,
*,
top_k: int = 3,
) -> None:
diff --git a/hyperextract/types/set.py b/hyperextract/types/set.py
index 1589802..744388f 100644
--- a/hyperextract/types/set.py
+++ b/hyperextract/types/set.py
@@ -4,29 +4,27 @@ Provides automatic deduplication based on a user-specified unique key field.
Supports multiple merge strategies including LLM-powered intelligent merging.
"""
+from collections.abc import Callable, Iterator
+from datetime import datetime
+from pathlib import Path
from typing import (
+ TYPE_CHECKING,
Any,
- List,
- Type,
- TypeVar,
Generic,
- Optional,
- Callable,
- Iterator,
- TYPE_CHECKING,
+ TypeVar,
)
-from pathlib import Path
-from datetime import datetime
-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 ontomem.merger import BaseMerger, MergeStrategy, create_merger
from ontosight import view_nodes
+from pydantic import BaseModel, Field, create_model
-from .base import BaseAutoType
from hyperextract.utils.logging import get_logger
+from .base import BaseAutoType
+
logger = get_logger(__name__)
@@ -46,7 +44,7 @@ DEFAULT_SET_PROMPT = (
class AutoSetSchema(BaseModel, Generic[ItemSchema]):
"""Generic schema container for set-based knowledge patterns."""
- items: List[ItemSchema] = Field(
+ items: list[ItemSchema] = Field(
default_factory=list, description="Set of unique items"
)
@@ -97,23 +95,23 @@ class AutoSet(BaseAutoType[AutoSetSchema[ItemSchema]], Generic[ItemSchema]):
if TYPE_CHECKING:
# Use generic version during type checking to maintain complete type hints
- item_set_schema: Type[AutoSetSchema[ItemSchema]]
+ item_set_schema: type[AutoSetSchema[ItemSchema]]
def __init__(
self,
- item_schema: Type[ItemSchema],
+ item_schema: type[ItemSchema],
llm_client: BaseChatModel,
embedder: Embeddings,
key_extractor: Callable[[ItemSchema], Any],
*,
strategy_or_merger: MergeStrategy | BaseMerger = MergeStrategy.LLM.BALANCED,
prompt: str = "",
- item_label_extractor: Callable[[ItemSchema], str] = None,
+ item_label_extractor: Callable[[ItemSchema], str] | None = None,
chunk_size: int = 2048,
chunk_overlap: int = 256,
max_workers: int = 10,
verbose: bool = False,
- fields_for_index: List[str] | None = None,
+ fields_for_index: list[str] | None = None,
**kwargs: Any,
):
"""Initialize AutoSet with key extractor and merge strategy.
@@ -150,7 +148,7 @@ class AutoSet(BaseAutoType[AutoSetSchema[ItemSchema]], Generic[ItemSchema]):
self.item_set_schema = create_model(
container_name,
items=(
- List[item_schema],
+ list[item_schema],
Field(default_factory=list, description="Set of unique items"),
),
)
@@ -249,7 +247,7 @@ class AutoSet(BaseAutoType[AutoSetSchema[ItemSchema]], Generic[ItemSchema]):
return self._data_memory.empty()
@property
- def items(self) -> List[ItemSchema]:
+ def items(self) -> list[ItemSchema]:
"""Returns the internal items as a list (for external interface compatibility).
Returns:
@@ -258,7 +256,7 @@ class AutoSet(BaseAutoType[AutoSetSchema[ItemSchema]], Generic[ItemSchema]):
return self._data_memory.items
@property
- def keys(self) -> List[Any]:
+ def keys(self) -> list[Any]:
"""Returns all unique key values.
Returns:
@@ -306,7 +304,7 @@ class AutoSet(BaseAutoType[AutoSetSchema[ItemSchema]], Generic[ItemSchema]):
# ==================== Core Override Methods ====================
def merge_batch_data(
- self, data_list: List[AutoSetSchema[ItemSchema]]
+ self, data_list: list[AutoSetSchema[ItemSchema]]
) -> AutoSetSchema[ItemSchema]:
"""Merges multiple data containers with automatic deduplication.
@@ -346,7 +344,7 @@ class AutoSet(BaseAutoType[AutoSetSchema[ItemSchema]], Generic[ItemSchema]):
"""
self._data_memory.build_index(force=force)
- def search(self, query: str, top_k: int = 3) -> List[ItemSchema]:
+ def search(self, query: str, top_k: int = 3) -> list[ItemSchema]:
"""Searches items in the set using semantic similarity.
Args:
@@ -375,7 +373,7 @@ class AutoSet(BaseAutoType[AutoSetSchema[ItemSchema]], Generic[ItemSchema]):
def show(
self,
- item_label_extractor: Callable[[ItemSchema], str] = None,
+ item_label_extractor: Callable[[ItemSchema], str] | None = None,
*,
top_k_for_search: int = 3,
top_k_for_chat: int = 3,
@@ -476,7 +474,7 @@ class AutoSet(BaseAutoType[AutoSetSchema[ItemSchema]], Generic[ItemSchema]):
self.clear_index()
self.metadata["updated_at"] = datetime.now()
- def remove(self, key: Any) -> Optional[ItemSchema]:
+ def remove(self, key: Any) -> ItemSchema | None:
"""Removes an item by its unique key value.
Args:
@@ -509,9 +507,7 @@ class AutoSet(BaseAutoType[AutoSetSchema[ItemSchema]], Generic[ItemSchema]):
"""
return self._data_memory.get(key) is not None
- def get(
- self, key: Any, default: Optional[ItemSchema] = None
- ) -> Optional[ItemSchema]:
+ def get(self, key: Any, default: ItemSchema | None = None) -> ItemSchema | None:
"""Gets an item by its unique key value.
Args:
@@ -524,7 +520,7 @@ class AutoSet(BaseAutoType[AutoSetSchema[ItemSchema]], Generic[ItemSchema]):
result = self._data_memory.get(key)
return result if result is not None else default
- def update(self, items: List[ItemSchema]) -> None:
+ def update(self, items: list[ItemSchema]) -> None:
"""Batch adds multiple items.
Args:
@@ -793,7 +789,7 @@ class AutoSet(BaseAutoType[AutoSetSchema[ItemSchema]], Generic[ItemSchema]):
# ==================== Set Comparison Operations ====================
- def __eq__(self, other: Any) -> bool:
+ def __eq__(self, other: object) -> bool:
"""Equality comparison: set1 == set2.
Two sets are equal if they have the same schema and key set.
@@ -816,7 +812,7 @@ class AutoSet(BaseAutoType[AutoSetSchema[ItemSchema]], Generic[ItemSchema]):
return self.keys == other.keys
- def __ne__(self, other: Any) -> bool:
+ def __ne__(self, other: object) -> bool:
"""Inequality comparison: set1 != set2.
Returns:
diff --git a/hyperextract/types/spatial_graph.py b/hyperextract/types/spatial_graph.py
index b2f8a82..70e410c 100644
--- a/hyperextract/types/spatial_graph.py
+++ b/hyperextract/types/spatial_graph.py
@@ -1,16 +1,18 @@
"""Generic spatial graph implementation supporting custom schemas with location-aware extraction."""
-from typing import Type, Callable, Tuple, Any, List
-from langchain_core.language_models import BaseChatModel
+from collections.abc import Callable
+from typing import Any
+
from langchain_core.embeddings import Embeddings
-from ontomem.merger import MergeStrategy, BaseMerger
+from langchain_core.language_models import BaseChatModel
+from ontomem.merger import BaseMerger, MergeStrategy
from .graph import (
AutoGraph,
- NodeSchema,
+ EdgeListSchema,
EdgeSchema,
NodeListSchema,
- EdgeListSchema,
+ NodeSchema,
)
# ==============================================================================
@@ -128,12 +130,12 @@ class AutoSpatialGraph(AutoGraph[NodeSchema, EdgeSchema]):
def __init__(
self,
- node_schema: Type[NodeSchema],
- edge_schema: Type[EdgeSchema],
+ node_schema: type[NodeSchema],
+ edge_schema: type[EdgeSchema],
node_key_extractor: Callable[[NodeSchema], str],
edge_key_extractor: Callable[[EdgeSchema], str],
location_in_edge_extractor: Callable[[EdgeSchema], str],
- nodes_in_edge_extractor: Callable[[EdgeSchema], Tuple[str, str]],
+ nodes_in_edge_extractor: Callable[[EdgeSchema], tuple[str, str]],
llm_client: BaseChatModel,
embedder: Embeddings,
*,
@@ -148,8 +150,8 @@ class AutoSpatialGraph(AutoGraph[NodeSchema, EdgeSchema]):
chunk_overlap: int = 256,
max_workers: int = 10,
verbose: bool = False,
- node_fields_for_index: List[str] | None = None,
- edge_fields_for_index: List[str] | None = None,
+ node_fields_for_index: list[str] | None = None,
+ edge_fields_for_index: list[str] | None = None,
**kwargs: Any,
):
"""
@@ -249,8 +251,8 @@ class AutoSpatialGraph(AutoGraph[NodeSchema, EdgeSchema]):
# ==============================================================================
def _extract_edges_batch(
- self, chunks: List[str], node_lists: List[NodeListSchema[NodeSchema]]
- ) -> List[EdgeListSchema[EdgeSchema]]:
+ self, chunks: list[str], node_lists: list[NodeListSchema[NodeSchema]]
+ ) -> list[EdgeListSchema[EdgeSchema]]:
"""Override: Inject observation_location into edge extraction during two-stage extraction."""
inputs = []
for chunk, node_list in zip(chunks, node_lists):
diff --git a/hyperextract/types/spatio_temporal_graph.py b/hyperextract/types/spatio_temporal_graph.py
index fc7ac91..7473a08 100644
--- a/hyperextract/types/spatio_temporal_graph.py
+++ b/hyperextract/types/spatio_temporal_graph.py
@@ -1,17 +1,19 @@
"""Generic temporal graph implementation supporting custom schemas with time-aware deduplication."""
-from typing import Type, Callable, Tuple, Any, List
+from collections.abc import Callable
from datetime import datetime
-from langchain_core.language_models import BaseChatModel
+from typing import Any
+
from langchain_core.embeddings import Embeddings
-from ontomem.merger import MergeStrategy, BaseMerger
+from langchain_core.language_models import BaseChatModel
+from ontomem.merger import BaseMerger, MergeStrategy
from .graph import (
AutoGraph,
- NodeSchema,
+ EdgeListSchema,
EdgeSchema,
NodeListSchema,
- EdgeListSchema,
+ NodeSchema,
)
# ==============================================================================
@@ -143,13 +145,13 @@ class AutoSpatioTemporalGraph(AutoGraph[NodeSchema, EdgeSchema]):
def __init__(
self,
- node_schema: Type[NodeSchema],
- edge_schema: Type[EdgeSchema],
+ node_schema: type[NodeSchema],
+ edge_schema: type[EdgeSchema],
node_key_extractor: Callable[[NodeSchema], str],
edge_key_extractor: Callable[[EdgeSchema], str],
time_in_edge_extractor: Callable[[EdgeSchema], str],
location_in_edge_extractor: Callable[[EdgeSchema], str],
- nodes_in_edge_extractor: Callable[[EdgeSchema], Tuple[str, str]],
+ nodes_in_edge_extractor: Callable[[EdgeSchema], tuple[str, str]],
llm_client: BaseChatModel,
embedder: Embeddings,
observation_time: str | None = None,
@@ -164,8 +166,8 @@ class AutoSpatioTemporalGraph(AutoGraph[NodeSchema, EdgeSchema]):
chunk_overlap: int = 256,
max_workers: int = 10,
verbose: bool = False,
- node_fields_for_index: List[str] | None = None,
- edge_fields_for_index: List[str] | None = None,
+ node_fields_for_index: list[str] | None = None,
+ edge_fields_for_index: list[str] | None = None,
**kwargs: Any,
):
"""
@@ -265,8 +267,8 @@ class AutoSpatioTemporalGraph(AutoGraph[NodeSchema, EdgeSchema]):
# ==============================================================================
def _extract_edges_batch(
- self, chunks: List[str], node_lists: List[NodeListSchema[NodeSchema]]
- ) -> List[EdgeListSchema[EdgeSchema]]:
+ self, chunks: list[str], node_lists: list[NodeListSchema[NodeSchema]]
+ ) -> list[EdgeListSchema[EdgeSchema]]:
"""Inject observation_time and observation_location into edge extraction."""
inputs = []
for chunk, node_list in zip(chunks, node_lists):
diff --git a/hyperextract/types/temporal_graph.py b/hyperextract/types/temporal_graph.py
index 41adac9..96134dd 100644
--- a/hyperextract/types/temporal_graph.py
+++ b/hyperextract/types/temporal_graph.py
@@ -1,17 +1,19 @@
"""Generic temporal graph implementation supporting custom schemas with time-aware deduplication."""
-from typing import Type, Callable, Tuple, Any, List
+from collections.abc import Callable
from datetime import datetime
-from langchain_core.language_models import BaseChatModel
+from typing import Any
+
from langchain_core.embeddings import Embeddings
-from ontomem.merger import MergeStrategy, BaseMerger
+from langchain_core.language_models import BaseChatModel
+from ontomem.merger import BaseMerger, MergeStrategy
from .graph import (
AutoGraph,
- NodeSchema,
+ EdgeListSchema,
EdgeSchema,
NodeListSchema,
- EdgeListSchema,
+ NodeSchema,
)
# ==============================================================================
@@ -143,12 +145,12 @@ class AutoTemporalGraph(AutoGraph[NodeSchema, EdgeSchema]):
def __init__(
self,
- node_schema: Type[NodeSchema],
- edge_schema: Type[EdgeSchema],
+ node_schema: type[NodeSchema],
+ edge_schema: type[EdgeSchema],
node_key_extractor: Callable[[NodeSchema], str],
edge_key_extractor: Callable[[EdgeSchema], str],
time_in_edge_extractor: Callable[[EdgeSchema], str],
- nodes_in_edge_extractor: Callable[[EdgeSchema], Tuple[str, str]],
+ nodes_in_edge_extractor: Callable[[EdgeSchema], tuple[str, str]],
llm_client: BaseChatModel,
embedder: Embeddings,
observation_time: str | None = None,
@@ -162,8 +164,8 @@ class AutoTemporalGraph(AutoGraph[NodeSchema, EdgeSchema]):
chunk_overlap: int = 256,
max_workers: int = 10,
verbose: bool = False,
- node_fields_for_index: List[str] | None = None,
- edge_fields_for_index: List[str] | None = None,
+ node_fields_for_index: list[str] | None = None,
+ edge_fields_for_index: list[str] | None = None,
**kwargs: Any,
):
"""
@@ -266,8 +268,8 @@ class AutoTemporalGraph(AutoGraph[NodeSchema, EdgeSchema]):
# ==============================================================================
def _extract_edges_batch(
- self, chunks: List[str], node_lists: List[NodeListSchema[NodeSchema]]
- ) -> List[EdgeListSchema[EdgeSchema]]:
+ self, chunks: list[str], node_lists: list[NodeListSchema[NodeSchema]]
+ ) -> list[EdgeListSchema[EdgeSchema]]:
"""Override: Inject observation_time into edge extraction during two-stage extraction."""
inputs = []
for chunk, node_list in zip(chunks, node_lists):
diff --git a/hyperextract/utils/__init__.py b/hyperextract/utils/__init__.py
index f6a1b54..8c0e759 100644
--- a/hyperextract/utils/__init__.py
+++ b/hyperextract/utils/__init__.py
@@ -1,14 +1,14 @@
"""Hyperextract utilities module."""
-from .logging import get_logger, configure_logging, set_log_level
from .client import get_client
+from .logging import configure_logging, get_logger, set_log_level
from .obsidian import export_to_obsidian, sanitize_filename
__all__ = [
- "get_logger",
"configure_logging",
- "set_log_level",
- "get_client",
"export_to_obsidian",
+ "get_client",
+ "get_logger",
"sanitize_filename",
+ "set_log_level",
]
diff --git a/hyperextract/utils/client.py b/hyperextract/utils/client.py
index 8114524..dc0354c 100644
--- a/hyperextract/utils/client.py
+++ b/hyperextract/utils/client.py
@@ -11,10 +11,10 @@ String shorthand format: provider:model@url
- "vllm:Qwen3.5-9B@http://localhost:8000/v1" → full specification
"""
-import os
import logging
+import os
from pathlib import Path
-from typing import Any, Dict, List, Optional, Tuple
+from typing import Any
from langchain_core.embeddings import Embeddings
from langchain_core.language_models.chat_models import BaseChatModel
@@ -28,7 +28,7 @@ DEFAULT_CONFIG_FILE = DEFAULT_CONFIG_DIR / "config.toml"
OPENAI_API_URL = "https://api.openai.com/v1"
# Provider presets: base_url and default models for each provider
-PROVIDER_PRESETS: Dict[str, Dict[str, str | None]] = {
+PROVIDER_PRESETS: dict[str, dict[str, str | None]] = {
"openai": {
"base_url": "https://api.openai.com/v1",
"default_llm": "gpt-4o-mini",
@@ -75,7 +75,7 @@ PROVIDER_PRESETS: Dict[str, Dict[str, str | None]] = {
ANTHROPIC_PROVIDERS = ("anthropic", "claude")
# Environment variables checked (in order) for each provider's API key.
-PROVIDER_API_KEY_ENV: Dict[str, Tuple[str, ...]] = {
+PROVIDER_API_KEY_ENV: dict[str, tuple[str, ...]] = {
"anthropic": ("ANTHROPIC_API_KEY", "CLAUDE_API_KEY"),
"claude": ("ANTHROPIC_API_KEY", "CLAUDE_API_KEY"),
"deepseek": ("DEEPSEEK_API_KEY",),
@@ -107,10 +107,10 @@ class CompatibleEmbeddings(Embeddings):
def __init__(
self,
model: str = "text-embedding-ada-002",
- api_key: Optional[str] = None,
- base_url: Optional[str] = None,
+ api_key: str | None = None,
+ base_url: str | None = None,
max_batch_size: int = 10,
- chunk_size: Optional[int] = None,
+ chunk_size: int | None = None,
max_retries: int = 2,
**kwargs: Any,
):
@@ -146,12 +146,12 @@ class CompatibleEmbeddings(Embeddings):
# Max tokens per request (8191 is the limit for most OpenAI embedders)
self._max_tokens = kwargs.get("embedding_ctx_length", 8191)
- def _split_texts(self, texts: List[str]) -> List[Tuple[str, int]]:
+ def _split_texts(self, texts: list[str]) -> list[tuple[str, int]]:
"""Split texts into chunks that fit within token limits.
Returns list of (text_chunk, original_index) tuples.
"""
- chunks: List[Tuple[str, int]] = []
+ chunks: list[tuple[str, int]] = []
for i, text in enumerate(texts):
tokens = self._encoding.encode(text)
if len(tokens) <= self._max_tokens:
@@ -164,7 +164,7 @@ class CompatibleEmbeddings(Embeddings):
chunks.append((chunk_text, i))
return chunks
- def embed_documents(self, texts: List[str]) -> List[List[float]]:
+ def embed_documents(self, texts: list[str]) -> list[list[float]]:
if not texts:
return []
@@ -173,16 +173,16 @@ class CompatibleEmbeddings(Embeddings):
return []
# Group chunks into batches no larger than max_batch_size
- all_embeddings: List[Optional[List[float]]] = [None] * len(texts)
- batch: List[Tuple[str, int]] = []
+ all_embeddings: list[list[float] | None] = [None] * len(texts)
+ batch: list[tuple[str, int]] = []
# Accumulate a running sum and count per text so multi-chunk texts get a
# true mean. A pairwise (prev + curr) / 2 only yields the mean for two
# chunks; for three or more it biases toward later chunks.
- sums: Dict[int, List[float]] = {}
- counts: Dict[int, int] = {}
+ sums: dict[int, list[float]] = {}
+ counts: dict[int, int] = {}
- def _embed_batch(b: List[Tuple[str, int]]) -> None:
+ def _embed_batch(b: list[tuple[str, int]]) -> None:
# Skip blank chunks: many OpenAI-compatible providers (e.g. Bailian /
# DashScope) reject empty-string input with a 400. Blank texts are
# backfilled with a zero vector after all batches complete.
@@ -215,7 +215,7 @@ class CompatibleEmbeddings(Embeddings):
_embed_batch(batch)
# Divide each running sum by its chunk count to get the mean embedding.
- dim: Optional[int] = None
+ dim: int | None = None
for orig_idx, running in sums.items():
count = counts[orig_idx]
mean = [v / count for v in running]
@@ -242,7 +242,7 @@ class CompatibleEmbeddings(Embeddings):
return all_embeddings # type: ignore[return-value]
- def embed_query(self, text: str) -> List[float]:
+ def embed_query(self, text: str) -> list[float]:
return self.embed_documents([text])[0]
@@ -251,7 +251,7 @@ def _parse_client_spec(
*,
api_key: str = "",
default_kind: str = "llm",
-) -> Dict[str, Any]:
+) -> dict[str, Any]:
"""Parse a client specification string or dict into a config dict.
String formats:
@@ -377,12 +377,12 @@ def create_llm(
if provider == "deepseek" and "thinking" not in extra_body:
extra_body["thinking"] = {"type": "disabled"}
- chat_kwargs: dict[str, Any] = dict(
- model=config["model"],
- api_key=config["api_key"] or os.environ.get("OPENAI_API_KEY", ""),
- base_url=config.get("base_url") or None,
- temperature=config.get("temperature", 0),
- )
+ chat_kwargs: dict[str, Any] = {
+ "model": config["model"],
+ "api_key": config["api_key"] or os.environ.get("OPENAI_API_KEY", ""),
+ "base_url": config.get("base_url") or None,
+ "temperature": config.get("temperature", 0),
+ }
if extra_body:
chat_kwargs["extra_body"] = extra_body
return ChatOpenAI(**chat_kwargs)
@@ -445,7 +445,7 @@ def create_client(
provider: str = "",
api_key: str = "",
**kwargs: Any,
-) -> Tuple[BaseChatModel, Embeddings]:
+) -> tuple[BaseChatModel, Embeddings]:
"""Create both LLM and Embedder clients in one call.
Supports three patterns:
@@ -513,7 +513,9 @@ def create_client(
return llm_client, embedder_client
-def get_client(config_path: str | Path = None) -> Tuple[BaseChatModel, Embeddings]:
+def get_client(
+ config_path: str | Path | None = None,
+) -> tuple[BaseChatModel, Embeddings]:
"""Get OpenAI LLM client and Embedder from config.
Backward-compatible: reads ~/.he/config.toml.
diff --git a/hyperextract/utils/logging.py b/hyperextract/utils/logging.py
index c39ec36..d136303 100644
--- a/hyperextract/utils/logging.py
+++ b/hyperextract/utils/logging.py
@@ -2,7 +2,6 @@
import logging
import sys
-from typing import Optional
import structlog
@@ -13,7 +12,7 @@ ENV_LOG_FILE = "HYPER_EXTRACT_LOG_FILE"
def configure_logging(
level: str = "WARNING",
json_output: bool = False,
- output_file: Optional[str] = None,
+ output_file: str | None = None,
) -> None:
"""Configure structlog for hyper-extract.
@@ -70,7 +69,7 @@ def configure_logging(
root_logger.setLevel(level_value)
-def get_logger(name: str = None) -> structlog.stdlib.BoundLogger:
+def get_logger(name: str | None = None) -> structlog.stdlib.BoundLogger:
"""Get a configured structlog logger.
Args:
@@ -92,4 +91,4 @@ def set_log_level(level: str) -> None:
logging.getLogger().setLevel(level_value)
-__all__ = ["get_logger", "configure_logging", "set_log_level"]
+__all__ = ["configure_logging", "get_logger", "set_log_level"]
diff --git a/hyperextract/utils/obsidian.py b/hyperextract/utils/obsidian.py
index 805cf4e..aee79ae 100644
--- a/hyperextract/utils/obsidian.py
+++ b/hyperextract/utils/obsidian.py
@@ -17,8 +17,9 @@ Only the Python standard library is used; no extra dependency is introduced.
import json
import re
+from collections.abc import Callable, Sequence
from pathlib import Path
-from typing import Any, Callable, Dict, List, Optional, Sequence, Tuple
+from typing import Any
from pydantic import BaseModel
@@ -83,7 +84,7 @@ def _is_scalar(value: Any) -> bool:
return not isinstance(value, (dict, list, tuple))
-def _emit_kv(key: str, value: Any, indent: int) -> List[str]:
+def _emit_kv(key: str, value: Any, indent: int) -> list[str]:
"""Emit ``key: value`` as YAML block-style lines."""
pad = " " * indent
key_str = _yaml_scalar(key)
@@ -112,7 +113,7 @@ def _emit_kv(key: str, value: Any, indent: int) -> List[str]:
return [f"{pad}{key_str}: {_yaml_scalar(value)}"]
-def _render_frontmatter(mapping: Dict[str, Any]) -> str:
+def _render_frontmatter(mapping: dict[str, Any]) -> str:
"""Render a mapping as a YAML front-matter block (with ``---`` fences)."""
lines = ["---"]
for key, value in mapping.items():
@@ -126,7 +127,7 @@ def _render_frontmatter(mapping: Dict[str, Any]) -> str:
# ----------------------------------------------------------------------------
-def _first_field(model: BaseModel, candidates: Sequence[str]) -> Optional[str]:
+def _first_field(model: BaseModel, candidates: Sequence[str]) -> str | None:
"""Return the first present, non-empty candidate field as a string."""
model_fields = type(model).model_fields
for field in candidates:
@@ -137,13 +138,13 @@ def _first_field(model: BaseModel, candidates: Sequence[str]) -> Optional[str]:
return None
-def _safe_call(func: Optional[Callable], arg: Any) -> Optional[str]:
+def _safe_call(func: Callable | None, arg: Any) -> str | None:
"""Call ``func(arg)`` defensively, returning a string or None."""
if func is None:
return None
try:
result = func(arg)
- except Exception as exc: # noqa: BLE001 - extractor is user-supplied
+ except Exception as exc:
logger.debug("obsidian: extractor raised %s", exc)
return None
if result in (None, ""):
@@ -162,7 +163,7 @@ def sanitize_filename(name: str, fallback: str = "untitled") -> str:
return cleaned[:120].strip()
-def _wikilink(stem: str, display: Optional[str] = None) -> str:
+def _wikilink(stem: str, display: str | None = None) -> str:
"""Build an Obsidian wikilink targeting ``stem`` with optional display text."""
if display and display != stem:
return f"[[{stem}|{display}]]"
@@ -177,7 +178,7 @@ def _wikilink(stem: str, display: Optional[str] = None) -> str:
class _NoteEntry:
"""Resolved metadata for a single node note."""
- __slots__ = ("node_id", "title", "stem", "model")
+ __slots__ = ("model", "node_id", "stem", "title")
def __init__(self, node_id: str, title: str, stem: str, model: BaseModel):
self.node_id = node_id
@@ -189,18 +190,18 @@ class _NoteEntry:
def _resolve_notes(
nodes: Sequence[BaseModel],
node_id_extractor: Callable[[Any], str],
- node_label_extractor: Optional[Callable[[Any], str]],
+ node_label_extractor: Callable[[Any], str] | None,
reserved_stems: set,
-) -> Tuple[List[_NoteEntry], Dict[str, _NoteEntry]]:
+) -> tuple[list[_NoteEntry], dict[str, _NoteEntry]]:
"""Resolve titles and unique, collision-free filename stems for all nodes."""
used_stems = set(reserved_stems)
- entries: List[_NoteEntry] = []
- by_id: Dict[str, _NoteEntry] = {}
+ entries: list[_NoteEntry] = []
+ by_id: dict[str, _NoteEntry] = {}
for index, node in enumerate(nodes):
try:
node_id = str(node_id_extractor(node))
- except Exception as exc: # noqa: BLE001
+ except Exception as exc:
logger.debug("obsidian: node_id_extractor raised %s; skipping node", exc)
continue
@@ -232,11 +233,11 @@ def _resolve_notes(
def _incident_ids(
edge: Any, incident_nodes_extractor: Callable[[Any], Sequence[str]]
-) -> List[str]:
+) -> list[str]:
"""Normalize an edge's incident node keys to a list of strings."""
try:
raw = incident_nodes_extractor(edge)
- except Exception as exc: # noqa: BLE001
+ except Exception as exc:
logger.debug("obsidian: incident_nodes_extractor raised %s; skipping edge", exc)
return []
if raw is None:
@@ -251,15 +252,15 @@ def _incident_ids(
def _build_relationships(
edges: Sequence[BaseModel],
incident_nodes_extractor: Callable[[Any], Sequence[str]],
- edge_label_extractor: Optional[Callable[[Any], str]],
- by_id: Dict[str, _NoteEntry],
-) -> Tuple[Dict[str, List[str]], int]:
+ edge_label_extractor: Callable[[Any], str] | None,
+ by_id: dict[str, _NoteEntry],
+) -> tuple[dict[str, list[str]], int]:
"""Group edges into per-source-note relationship lines.
Returns a mapping ``{source_node_id: [markdown bullet, ...]}`` and the count
of edges that were skipped because no incident node was known.
"""
- relationships: Dict[str, List[str]] = {}
+ relationships: dict[str, list[str]] = {}
skipped = 0
for edge in edges:
@@ -302,9 +303,9 @@ def _build_relationships(
# ----------------------------------------------------------------------------
-def _build_frontmatter(entry: _NoteEntry) -> Dict[str, Any]:
+def _build_frontmatter(entry: _NoteEntry) -> dict[str, Any]:
"""Build the YAML front-matter mapping for a node note."""
- data: Dict[str, Any] = dict(entry.model.model_dump())
+ data: dict[str, Any] = dict(entry.model.model_dump())
# Ensure the original id / title remain resolvable as aliases, so wikilinks
# written against either still point at this note.
@@ -327,7 +328,7 @@ def _build_frontmatter(entry: _NoteEntry) -> Dict[str, Any]:
return data
-def _render_note(entry: _NoteEntry, relationship_lines: List[str]) -> str:
+def _render_note(entry: _NoteEntry, relationship_lines: list[str]) -> str:
"""Render a single node note's full Markdown content."""
parts = [_render_frontmatter(_build_frontmatter(entry)), "", f"# {entry.title}", ""]
@@ -344,7 +345,7 @@ def _render_note(entry: _NoteEntry, relationship_lines: List[str]) -> str:
return "\n".join(parts).rstrip() + "\n"
-def _render_index(entries: List[_NoteEntry], vault_name: str, edge_count: int) -> str:
+def _render_index(entries: list[_NoteEntry], vault_name: str, edge_count: int) -> str:
"""Render the vault index / map-of-content note."""
parts = [
_render_frontmatter({"tags": ["index"], "title": vault_name}),
@@ -356,7 +357,7 @@ def _render_index(entries: List[_NoteEntry], vault_name: str, edge_count: int) -
]
# Group notes by a guessed "type" field for a tidy table of contents.
- groups: Dict[str, List[_NoteEntry]] = {}
+ groups: dict[str, list[_NoteEntry]] = {}
for entry in entries:
group = _first_field(entry.model, _TYPE_FIELDS) or "Notes"
groups.setdefault(group, []).append(entry)
@@ -383,8 +384,8 @@ def export_to_obsidian(
node_id_extractor: Callable[[Any], str],
incident_nodes_extractor: Callable[[Any], Sequence[str]],
folder_path: str | Path,
- node_label_extractor: Optional[Callable[[Any], str]] = None,
- edge_label_extractor: Optional[Callable[[Any], str]] = None,
+ node_label_extractor: Callable[[Any], str] | None = None,
+ edge_label_extractor: Callable[[Any], str] | None = None,
vault_name: str = "Knowledge Vault",
include_index: bool = True,
overwrite: bool = False,
diff --git a/hyperextract/utils/template_engine/__init__.py b/hyperextract/utils/template_engine/__init__.py
index b68a49e..f2f9fab 100644
--- a/hyperextract/utils/template_engine/__init__.py
+++ b/hyperextract/utils/template_engine/__init__.py
@@ -12,21 +12,20 @@ Main Components:
- parse_identifiers: Parses identifier extractors from config
"""
-from .template import Template
-from .gallery import Gallery
from .factory import TemplateFactory
+from .gallery import Gallery
from .parsers import (
TemplateCfg,
load_template,
localize_template,
)
-
+from .template import Template
__all__ = [
- "Template",
"Gallery",
- "TemplateFactory",
+ "Template",
"TemplateCfg",
+ "TemplateFactory",
"load_template",
"localize_template",
]
diff --git a/hyperextract/utils/template_engine/factory.py b/hyperextract/utils/template_engine/factory.py
index 313fc0d..77e43ca 100644
--- a/hyperextract/utils/template_engine/factory.py
+++ b/hyperextract/utils/template_engine/factory.py
@@ -3,33 +3,32 @@
Supports all 8 AutoType dynamic generation.
"""
-from typing import TYPE_CHECKING, Union, Optional
from pathlib import Path
+from typing import TYPE_CHECKING
-from langchain_core.language_models import BaseChatModel
from langchain_core.embeddings import Embeddings
+from langchain_core.language_models import BaseChatModel
from .parsers import (
TemplateCfg,
localize_template,
- parse_output,
- parse_identifiers,
+ parse_display,
parse_guideline,
+ parse_identifiers,
parse_option,
- parse_display,
+ parse_output,
)
-
if TYPE_CHECKING:
from hyperextract.types import (
- AutoModel,
- AutoList,
- AutoSet,
AutoGraph,
AutoHypergraph,
- AutoTemporalGraph,
+ AutoList,
+ AutoModel,
+ AutoSet,
AutoSpatialGraph,
AutoSpatioTemporalGraph,
+ AutoTemporalGraph,
)
@@ -379,8 +378,8 @@ class TemplateFactory:
@classmethod
def create(
cls,
- source: Union[str, TemplateCfg],
- language: Optional[str] = None,
+ source: str | TemplateCfg,
+ language: str | None = None,
llm_client: BaseChatModel = None,
embedder: Embeddings = None,
**kwargs,
diff --git a/hyperextract/utils/template_engine/gallery.py b/hyperextract/utils/template_engine/gallery.py
index 5cd595c..3c54b33 100644
--- a/hyperextract/utils/template_engine/gallery.py
+++ b/hyperextract/utils/template_engine/gallery.py
@@ -4,7 +4,7 @@ Auto-loads templates from presets directory.
"""
from pathlib import Path
-from typing import Dict, Optional
+from typing import Optional
from .parsers import TemplateCfg, load_template
@@ -29,10 +29,10 @@ class Gallery:
_instance: Optional["Gallery"] = None
def __init__(self):
- self._configs: Dict[str, TemplateCfg] = {}
+ self._configs: dict[str, TemplateCfg] = {}
@classmethod
- def get(cls, path: str) -> Optional[TemplateCfg]:
+ def get(cls, path: str) -> TemplateCfg | None:
"""Get template configuration by path.
Args:
@@ -53,11 +53,11 @@ class Gallery:
@classmethod
def list(
cls,
- filter_by_query: str = None,
- filter_by_type: str = None,
- filter_by_tag: str = None,
- filter_by_language: str = None,
- ) -> Dict[str, "TemplateCfg"]:
+ filter_by_query: str | None = None,
+ filter_by_type: str | None = None,
+ filter_by_tag: str | None = None,
+ filter_by_language: str | None = None,
+ ) -> dict[str, "TemplateCfg"]:
"""List templates with optional filters.
Args:
diff --git a/hyperextract/utils/template_engine/parsers/__init__.py b/hyperextract/utils/template_engine/parsers/__init__.py
index 07e97c2..d25216d 100644
--- a/hyperextract/utils/template_engine/parsers/__init__.py
+++ b/hyperextract/utils/template_engine/parsers/__init__.py
@@ -1,24 +1,23 @@
"""Builder module - Configuration models, loaders and builders."""
+from .display import parse_display
+from .guideline import parse_guideline
+from .identifiers import parse_identifiers
from .loader import (
TemplateCfg,
load_template,
localize_template,
)
-from .output import parse_output
-from .guideline import parse_guideline
-from .identifiers import parse_identifiers
from .options import parse_option
-from .display import parse_display
-
+from .output import parse_output
__all__ = [
"TemplateCfg",
"load_template",
"localize_template",
- "parse_output",
+ "parse_display",
"parse_guideline",
"parse_identifiers",
"parse_option",
- "parse_display",
+ "parse_output",
]
diff --git a/hyperextract/utils/template_engine/parsers/display.py b/hyperextract/utils/template_engine/parsers/display.py
index aa76feb..35a6d93 100644
--- a/hyperextract/utils/template_engine/parsers/display.py
+++ b/hyperextract/utils/template_engine/parsers/display.py
@@ -1,13 +1,14 @@
"""Display parser - generates label extraction functions from YAML config."""
-from typing import Any, Callable
+from collections.abc import Callable
+from typing import Any
+from .identifiers import _extractor
from .schemas import (
VALID_AUTOTYPES,
- NaiveDisplaySchema,
GraphDisplaySchema,
+ NaiveDisplaySchema,
)
-from .identifiers import _extractor
def parse_display(
diff --git a/hyperextract/utils/template_engine/parsers/guideline.py b/hyperextract/utils/template_engine/parsers/guideline.py
index d9dfa69..9e59349 100644
--- a/hyperextract/utils/template_engine/parsers/guideline.py
+++ b/hyperextract/utils/template_engine/parsers/guideline.py
@@ -1,8 +1,5 @@
"""Extraction prompt parser from guideline."""
-from typing import Tuple
-
-
LABEL_MAPPING = {
"zh": {
"role_and_task": "角色与任务",
@@ -29,7 +26,7 @@ LABEL_MAPPING = {
def parse_guideline(
guideline, autotype: str, language: str = "en"
-) -> str | Tuple[str, str, str]:
+) -> str | tuple[str, str, str]:
"""Parse guideline and return prompts based on autotype (config is already localized).
Args:
diff --git a/hyperextract/utils/template_engine/parsers/identifiers.py b/hyperextract/utils/template_engine/parsers/identifiers.py
index ea4f9a1..e68738b 100644
--- a/hyperextract/utils/template_engine/parsers/identifiers.py
+++ b/hyperextract/utils/template_engine/parsers/identifiers.py
@@ -1,12 +1,13 @@
"""Identifier parser - generates extraction functions from YAML config."""
import re
-from typing import Any, Callable, Tuple
+from collections.abc import Callable
+from typing import Any
from .schemas import (
VALID_AUTOTYPES,
- NaiveIdentifierSchema,
GraphIdentifiersSchema,
+ NaiveIdentifierSchema,
)
@@ -42,7 +43,7 @@ def _extractor(field_or_template: str) -> Callable[[Any], str]:
def _members_extractor(
members: dict[str, str] | str | list[str],
-) -> Callable[[Any], Tuple[str, ...]]:
+) -> Callable[[Any], tuple[str, ...]]:
"""Relation members extractor:
Graph: {source: 's', target: 't'} -> lambda x: (x.s, x.t)
Hypergraph: 'members' -> lambda x: tuple(sorted(x.members)) or
@@ -51,12 +52,12 @@ def _members_extractor(
# Handle Graph
if isinstance(members, dict):
- def extractor(item: Any) -> Tuple[str, str]:
+ def extractor(item: Any) -> tuple[str, str]:
return tuple(
sorted(
[
- str(getattr(item, "source")),
- str(getattr(item, "target")),
+ str(item.source),
+ str(item.target),
]
)
)
@@ -66,12 +67,12 @@ def _members_extractor(
# Handle Hypergraph
if isinstance(members, str):
- def extractor(item: Any) -> Tuple[str, ...]:
+ def extractor(item: Any) -> tuple[str, ...]:
return tuple(sorted(getattr(item, members)))
return extractor
- def extractor(item: str | list[str]) -> Tuple[str, ...]:
+ def extractor(item: str | list[str]) -> tuple[str, ...]:
result = []
for m in members:
result.append(tuple(sorted(getattr(item, m))))
@@ -85,8 +86,8 @@ def parse_identifiers(
autotype: VALID_AUTOTYPES,
) -> (
Callable[[Any], str]
- | Tuple[
- Callable[[Any], str], Callable[[Any], str], Callable[[Any], Tuple[str, ...]]
+ | tuple[
+ Callable[[Any], str], Callable[[Any], str], Callable[[Any], tuple[str, ...]]
]
):
"""Parse identifiers config and return extractors based on autotype.
diff --git a/hyperextract/utils/template_engine/parsers/loader.py b/hyperextract/utils/template_engine/parsers/loader.py
index eaa4740..09d49a4 100644
--- a/hyperextract/utils/template_engine/parsers/loader.py
+++ b/hyperextract/utils/template_engine/parsers/loader.py
@@ -1,35 +1,35 @@
"""Config loader and template configuration models."""
-import yaml
from pathlib import Path
-from typing import Dict, List, Union
+
+import yaml
from pydantic import BaseModel
from .schemas.base import VALID_AUTOTYPES, FieldSchema
-from .schemas.naive import (
- NaiveGuidelineSchema,
- NaiveOutputSchema,
- NaiveOptionsSchema,
- NaiveDisplaySchema,
- NaiveIdentifierSchema,
-)
from .schemas.graph import (
- GraphGuidelineSchema,
- GraphOutputSchema,
- GraphOptionsSchema,
GraphDisplaySchema,
+ GraphGuidelineSchema,
GraphIdentifiersSchema,
+ GraphOptionsSchema,
+ GraphOutputSchema,
+)
+from .schemas.naive import (
+ NaiveDisplaySchema,
+ NaiveGuidelineSchema,
+ NaiveIdentifierSchema,
+ NaiveOptionsSchema,
+ NaiveOutputSchema,
)
class TemplateCfg(BaseModel):
"""Template configuration loaded from YAML."""
- language: str | List[str] = "en"
+ language: str | list[str] = "en"
name: str
type: VALID_AUTOTYPES
- tags: List[str]
- description: str | Dict[str, str]
+ tags: list[str]
+ description: str | dict[str, str]
output: NaiveOutputSchema | GraphOutputSchema
guideline: NaiveGuidelineSchema | GraphGuidelineSchema
identifiers: NaiveIdentifierSchema | GraphIdentifiersSchema | None = None
@@ -38,7 +38,7 @@ class TemplateCfg(BaseModel):
def _localize_data(
- value: str | List[str] | Dict[str, str | List[str]],
+ value: str | list[str] | dict[str, str | list[str]],
language: str,
) -> str:
"""Get multilingual text value, supports list format
@@ -155,7 +155,7 @@ def localize_template(config: TemplateCfg, language: str) -> TemplateCfg:
)
-def load_template(file_path: Union[str, Path]) -> TemplateCfg:
+def load_template(file_path: str | Path) -> TemplateCfg:
"""Load and validate template configuration."""
path = Path(file_path)
if not path.exists():
diff --git a/hyperextract/utils/template_engine/parsers/options.py b/hyperextract/utils/template_engine/parsers/options.py
index 869d17c..5525f31 100644
--- a/hyperextract/utils/template_engine/parsers/options.py
+++ b/hyperextract/utils/template_engine/parsers/options.py
@@ -1,14 +1,13 @@
"""Options models and parser."""
-from typing import List
+from ontomem.merger import MergeStrategy
from pydantic import BaseModel
-from ontomem.merger import MergeStrategy
from .schemas import (
VALID_AUTOTYPES,
VALID_MERGE_STRATEGIES,
- NaiveOptionsSchema,
GraphOptionsSchema,
+ NaiveOptionsSchema,
)
@@ -31,13 +30,13 @@ class Options(BaseModel):
verbose: bool | None = None
strategy_or_merger: str | None = None
- fields_for_index: List[str] | None = None
+ fields_for_index: list[str] | None = None
extraction_mode: str | None = None
node_strategy_or_merger: str | None = None
edge_strategy_or_merger: str | None = None
- node_fields_for_index: List[str] | None = None
- edge_fields_for_index: List[str] | None = None
+ node_fields_for_index: list[str] | None = None
+ edge_fields_for_index: list[str] | None = None
observation_time: str | None = None
observation_location: str | None = None
diff --git a/hyperextract/utils/template_engine/parsers/output.py b/hyperextract/utils/template_engine/parsers/output.py
index dd32bfa..e76269f 100644
--- a/hyperextract/utils/template_engine/parsers/output.py
+++ b/hyperextract/utils/template_engine/parsers/output.py
@@ -1,26 +1,25 @@
"""Output schema parser."""
-from typing import List, Optional
+from pydantic import BaseModel, Field, create_model
+
from .schemas import (
FieldSchema,
- NaiveOutputSchema,
GraphOutputSchema,
+ NaiveOutputSchema,
)
-from pydantic import Field, create_model, BaseModel
-
TYPE_MAPPING = {
"str": str,
"int": int,
"float": float,
"bool": bool,
- "list": List[str],
+ "list": list[str],
}
def build_naive_schema(
name: str,
- fields: List[FieldSchema],
+ fields: list[FieldSchema],
description: str,
) -> BaseModel:
"""Build Pydantic schema for naive types (model, list, set)."""
@@ -36,7 +35,7 @@ def build_naive_schema(
# Use Optional type for non-required fields so None values pass validation
base_type = TYPE_MAPPING[field.type]
if field.required is False:
- field_type = Optional[base_type]
+ field_type = base_type | None
else:
field_type = base_type
schema_fields[field.name] = (
diff --git a/hyperextract/utils/template_engine/parsers/schemas/__init__.py b/hyperextract/utils/template_engine/parsers/schemas/__init__.py
index 79c1fd0..2a3d42b 100644
--- a/hyperextract/utils/template_engine/parsers/schemas/__init__.py
+++ b/hyperextract/utils/template_engine/parsers/schemas/__init__.py
@@ -1,35 +1,35 @@
from .base import (
- VALID_MERGE_STRATEGIES,
VALID_AUTOTYPES,
+ VALID_MERGE_STRATEGIES,
FieldSchema,
)
-from .naive import (
- NaiveGuidelineSchema,
- NaiveOutputSchema,
- NaiveOptionsSchema,
- NaiveDisplaySchema,
- NaiveIdentifierSchema,
-)
from .graph import (
- GraphGuidelineSchema,
- GraphOutputSchema,
- GraphOptionsSchema,
GraphDisplaySchema,
+ GraphGuidelineSchema,
GraphIdentifiersSchema,
+ GraphOptionsSchema,
+ GraphOutputSchema,
+)
+from .naive import (
+ NaiveDisplaySchema,
+ NaiveGuidelineSchema,
+ NaiveIdentifierSchema,
+ NaiveOptionsSchema,
+ NaiveOutputSchema,
)
__all__ = [
- "VALID_MERGE_STRATEGIES",
"VALID_AUTOTYPES",
+ "VALID_MERGE_STRATEGIES",
"FieldSchema",
- "NaiveGuidelineSchema",
- "NaiveOutputSchema",
- "NaiveOptionsSchema",
- "NaiveDisplaySchema",
- "NaiveIdentifierSchema",
- "GraphGuidelineSchema",
- "GraphOutputSchema",
- "GraphOptionsSchema",
"GraphDisplaySchema",
+ "GraphGuidelineSchema",
"GraphIdentifiersSchema",
+ "GraphOptionsSchema",
+ "GraphOutputSchema",
+ "NaiveDisplaySchema",
+ "NaiveGuidelineSchema",
+ "NaiveIdentifierSchema",
+ "NaiveOptionsSchema",
+ "NaiveOutputSchema",
]
diff --git a/hyperextract/utils/template_engine/parsers/schemas/base.py b/hyperextract/utils/template_engine/parsers/schemas/base.py
index 535e670..fa7a6d3 100644
--- a/hyperextract/utils/template_engine/parsers/schemas/base.py
+++ b/hyperextract/utils/template_engine/parsers/schemas/base.py
@@ -1,6 +1,6 @@
-from typing import Any, Dict, Literal
-from pydantic import BaseModel
+from typing import Any, Literal
+from pydantic import BaseModel
VALID_MERGE_STRATEGIES = Literal[
"merge_field",
@@ -27,6 +27,6 @@ VALID_FIELD_TYPES = Literal["str", "int", "float", "bool", "list"]
class FieldSchema(BaseModel):
name: str
type: VALID_FIELD_TYPES
- description: str | Dict[str, str]
+ description: str | dict[str, str]
required: bool | None = None
default: Any | None = None
diff --git a/hyperextract/utils/template_engine/parsers/schemas/graph.py b/hyperextract/utils/template_engine/parsers/schemas/graph.py
index 56fb173..4cc5645 100644
--- a/hyperextract/utils/template_engine/parsers/schemas/graph.py
+++ b/hyperextract/utils/template_engine/parsers/schemas/graph.py
@@ -1,21 +1,21 @@
-from typing import Dict, List
from pydantic import BaseModel
+
from .base import VALID_MERGE_STRATEGIES
from .naive import NaiveOutputSchema
class GraphGuidelineSchema(BaseModel):
- target: str | List[str] | Dict[str, str]
- rules_for_entities: str | List[str] | Dict[str, str | List[str]]
- rules_for_relations: str | List[str] | Dict[str, str | List[str]]
+ target: str | list[str] | dict[str, str]
+ rules_for_entities: str | list[str] | dict[str, str | list[str]]
+ rules_for_relations: str | list[str] | dict[str, str | list[str]]
# Only for AutoTemporalGraph, AutoSpatioTemporalGraph
- rules_for_time: str | List[str] | Dict[str, str | List[str]] | None = None
+ rules_for_time: str | list[str] | dict[str, str | list[str]] | None = None
# Only for AutoSpatialGraph, AutoSpatioTemporalGraph
- rules_for_location: str | List[str] | Dict[str, str | List[str]] | None = None
+ rules_for_location: str | list[str] | dict[str, str | list[str]] | None = None
class GraphOutputSchema(BaseModel):
- description: str | Dict[str, str]
+ description: str | dict[str, str]
entities: NaiveOutputSchema
relations: NaiveOutputSchema
@@ -28,8 +28,8 @@ class GraphOptionsSchema(BaseModel):
entity_merge_strategy: VALID_MERGE_STRATEGIES | None = None
relation_merge_strategy: VALID_MERGE_STRATEGIES | None = None
extraction_mode: str | None = None
- entity_fields_for_search: List[str] | None = None
- relation_fields_for_search: List[str] | None = None
+ entity_fields_for_search: list[str] | None = None
+ relation_fields_for_search: list[str] | None = None
# Only for AutoTemporalGraph, AutoSpatioTemporalGraph
observation_time: str | None = None
# Only for AutoSpatialGraph, AutoSpatioTemporalGraph
@@ -44,7 +44,7 @@ class GraphDisplaySchema(BaseModel):
class GraphIdentifiersSchema(BaseModel):
entity_id: str = None
relation_id: str = None
- relation_members: str | Dict[str, str] | List[str] = None
+ relation_members: str | dict[str, str] | list[str] = None
# Only for AutoTemporalGraph, AutoSpatioTemporalGraph
time_field: str | None = None
# Only for AutoSpatialGraph, AutoSpatioTemporalGraph
diff --git a/hyperextract/utils/template_engine/parsers/schemas/naive.py b/hyperextract/utils/template_engine/parsers/schemas/naive.py
index 6b06c3f..7c97305 100644
--- a/hyperextract/utils/template_engine/parsers/schemas/naive.py
+++ b/hyperextract/utils/template_engine/parsers/schemas/naive.py
@@ -1,16 +1,16 @@
-from typing import Dict, List
from pydantic import BaseModel
+
from .base import VALID_MERGE_STRATEGIES, FieldSchema
class NaiveGuidelineSchema(BaseModel):
- target: str | List[str] | Dict[str, str]
- rules: str | List[str] | Dict[str, str | List[str]]
+ target: str | list[str] | dict[str, str]
+ rules: str | list[str] | dict[str, str | list[str]]
class NaiveOutputSchema(BaseModel):
- description: str | Dict[str, str]
- fields: List[FieldSchema]
+ description: str | dict[str, str]
+ fields: list[FieldSchema]
class NaiveOptionsSchema(BaseModel):
@@ -19,7 +19,7 @@ class NaiveOptionsSchema(BaseModel):
max_workers: int | None = None
verbose: bool | None = None
merge_strategy: VALID_MERGE_STRATEGIES | None = None
- fields_for_search: List[str] | None = None
+ fields_for_search: list[str] | None = None
class NaiveDisplaySchema(BaseModel):
diff --git a/hyperextract/utils/template_engine/template.py b/hyperextract/utils/template_engine/template.py
index 291b322..1e90e2a 100644
--- a/hyperextract/utils/template_engine/template.py
+++ b/hyperextract/utils/template_engine/template.py
@@ -3,13 +3,13 @@
Provides a clean API for searching and creating knowledge extraction templates.
"""
-from typing import Dict, Optional, Any
+from typing import Any
-from langchain_core.language_models import BaseChatModel
from langchain_core.embeddings import Embeddings
+from langchain_core.language_models import BaseChatModel
-from hyperextract.types.base import BaseAutoType
from hyperextract.methods import get_method_cfg
+from hyperextract.types.base import BaseAutoType
from .gallery import Gallery
from .parsers import TemplateCfg
@@ -39,9 +39,9 @@ class Template:
@staticmethod
def create(
source: str,
- language: Optional[str] = None,
- llm_client: Optional[BaseChatModel] = None,
- embedder: Optional[Embeddings] = None,
+ language: str | None = None,
+ llm_client: BaseChatModel | None = None,
+ embedder: Embeddings | None = None,
**kwargs: Any,
) -> "BaseAutoType":
"""Create template instance.
@@ -89,7 +89,7 @@ class Template:
return TemplateFactory.create(source, language, llm_client, embedder, **kwargs)
@staticmethod
- def get(path: str) -> Optional[TemplateCfg]:
+ def get(path: str) -> TemplateCfg | None:
"""Get template configuration by path.
Args:
@@ -105,12 +105,12 @@ class Template:
@staticmethod
def list(
- filter_by_query: Optional[str] = None,
- filter_by_type: Optional[str] = None,
- filter_by_tag: Optional[str] = None,
- filter_by_language: Optional[str] = None,
+ filter_by_query: str | None = None,
+ filter_by_type: str | None = None,
+ filter_by_tag: str | None = None,
+ filter_by_language: str | None = None,
include_methods: bool = True,
- ) -> Dict[str, TemplateCfg]:
+ ) -> dict[str, TemplateCfg]:
"""List templates with optional filters.
Args:
diff --git a/pyproject.toml b/pyproject.toml
index bd4ff6c..ab76439 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -94,4 +94,13 @@ exclude = [
packages = ["hyperextract"]
[tool.ruff.lint]
-ignore = ["E731"]
+ignore = [
+ "E731", # Lambda assignment is fine
+ "BLE001", # Blind except Exception — common pattern in this codebase
+ "DTZ005", # datetime.now() without tz — not needed for this project
+ "SIM102", # Nested if/else style preference
+ "TRY002", # Custom exception class design preference
+ "TRY004", # TypeError vs ValueError preference
+ "S110", # try/except/pass — intentional in some places
+ "TC010", # String forward-ref in X | Y union — needed for recursive types
+]
diff --git a/uv.lock b/uv.lock
index c40c426..109915d 100644
--- a/uv.lock
+++ b/uv.lock
@@ -903,7 +903,7 @@ wheels = [
[[package]]
name = "hyperextract"
-version = "0.3.1"
+version = "0.4.0"
source = { editable = "." }
dependencies = [
{ name = "faiss-cpu" },