Skip to content

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

refactor(cli): 重构CLI模块结构并优化模板引擎

yifanfeng97d6354528be66
diff --git a/examples/template_demo.py b/examples/template_demo.py
index 706c642..0a7a6db 100644
--- a/examples/template_demo.py
+++ b/examples/template_demo.py
@@ -16,7 +16,7 @@ from dotenv import load_dotenv
load_dotenv()
-from hyperextract.utils.template_engine import Template
+from hyperextract import Template
from langchain_openai import ChatOpenAI, OpenAIEmbeddings
diff --git a/hyperextract/cli/cli.py b/hyperextract/cli/cli.py
index 0a5c1c6..b8124ac 100644
--- a/hyperextract/cli/cli.py
+++ b/hyperextract/cli/cli.py
@@ -1,19 +1,32 @@
"""CLI entry point for Hyper-Extract."""
+from pathlib import Path
+from datetime import datetime
+from typing import Optional
+
import typer
from rich.console import Console
from rich.panel import Panel
from rich.table import Table
from rich.text import Text
+from rich.progress import Progress, SpinnerColumn, TextColumn
+from rich.prompt import Prompt
+
+from hyperextract.utils.template_engine import Gallery, Template
+
+from .utils import (
+ read_input,
+ validate_config,
+ validate_kb_path,
+ validate_kb_with_data,
+ validate_kb_with_index,
+ get_template_from_kb,
+)
+from .config import (
+ load_kb_metadata,
+)
-from .commands.config import app as config_app
-from .commands.list import app as list_app
-from .commands.parse import app as parse_app
-from .commands.feed import app as feed_app
-from .commands.show import app as show_app
-from .commands.build_index import app as build_index_app
-from .commands.search import app as search_app
-from .commands.talk import app as talk_app
+from .commands import list_app, config_app
console = Console()
@@ -24,14 +37,8 @@ app = typer.Typer(
invoke_without_command=True,
)
-app.add_typer(config_app, name="config")
app.add_typer(list_app, name="list")
-app.add_typer(parse_app, name="parse")
-app.add_typer(feed_app, name="feed")
-app.add_typer(show_app, name="show")
-app.add_typer(build_index_app, name="build-index")
-app.add_typer(search_app, name="search")
-app.add_typer(talk_app, name="talk")
+app.add_typer(config_app, name="config")
@app.callback()
@@ -50,31 +57,34 @@ def main(
from . import __version__
console.print(f"[bold]Hyper-Extract CLI[/bold] version {__version__}")
raise typer.Exit()
-
+
if ctx.invoked_subcommand is None:
welcome_text = Text("Welcome to Hyper-Extract CLI!", style="bold cyan")
desc_text = Text("A command-line tool for knowledge extraction from unstructured text.")
-
+
table = Table(box=None, show_header=False, pad_edge=False)
table.add_column(style="green")
table.add_column()
-
- table.add_row("he parse", "<input> -o <output>", "Extract knowledge from text")
+ table.add_column()
+
+ table.add_row("he parse", "<input> -o <output>", "Extract knowledge (auto-builds index)")
table.add_row("he list", "template", "List available templates")
+ table.add_row("he list", "method", "List extraction methods")
table.add_row("he show", "<kb_path>", "Visualize knowledge base")
+ table.add_row("he info", "<kb_path>", "View knowledge base info")
table.add_row("he search", "<query> -k <kb>", "Semantic search in knowledge base")
- table.add_row("he talk", "-k <kb>", "Chat with knowledge base")
+ table.add_row("he talk", "<kb_path>", "Chat with knowledge base")
table.add_row("he config", "--help", "Manage LLM and Embedder configuration")
table.add_row("he feed", "-k <kb>", "Feed knowledge into knowledge base")
- table.add_row("he build-index", "-k <kb>", "Build search index for knowledge base")
-
+ table.add_row("he build-index", "<kb_path>", "Build search index for knowledge base")
+
panel = Panel(
table,
title="Quick Start",
border_style="cyan",
padding=(1, 2),
)
-
+
console.print()
console.print(welcome_text)
console.print()
@@ -84,3 +94,465 @@ def main(
console.print()
console.print("Run [bold]he --help[/bold] for more information.")
raise typer.Exit()
+
+
+def select_template_interactive() -> Optional[str]:
+ """Interactive template selection when user doesn't specify one."""
+ templates = Gallery.list()
+
+ if not templates:
+ console.print("[yellow]No templates available.[/yellow]")
+ return None
+
+ template_list = list(templates.items())
+
+ console.print()
+ console.print("[bold cyan]Select a template:[/bold cyan]")
+ console.print()
+
+ for i, (path, cfg) in enumerate(template_list, 1):
+ desc = cfg.description if cfg.description else ""
+ if isinstance(desc, dict):
+ desc = desc.get("zh", desc.get("en", ""))
+ console.print(f" [{i}] {path}")
+ if desc:
+ console.print(f" {desc}")
+
+ console.print()
+
+ while True:
+ choice = Prompt.ask(
+ "Enter number or search keyword",
+ default="1",
+ show_default=True,
+ )
+
+ if choice.isdigit():
+ idx = int(choice) - 1
+ if 0 <= idx < len(template_list):
+ return template_list[idx][0]
+ else:
+ console.print(f"[red]Invalid number. Please choose 1-{len(template_list)}[/red]")
+ else:
+ query_lower = choice.lower()
+ matches = [(i, p, c) for i, (p, c) in enumerate(template_list)
+ if query_lower in p.lower() or
+ (c.description and query_lower in str(c.description).lower())]
+
+ if len(matches) == 1:
+ return matches[0][1]
+ elif len(matches) > 1:
+ console.print(f"[yellow]Found {len(matches)} matches:[/yellow]")
+ for i, path, cfg in matches:
+ console.print(f" [{i+1}] {path}")
+ continue
+ else:
+ console.print("[yellow]No matches found. Try another keyword.[/yellow]")
+
+
[email protected](name="parse")
+def parse(
+ input: str = typer.Argument(..., help="Input file path, directory, or '-' for stdin"),
+ output: str = typer.Option(..., "--output", "-o", help="Output directory"),
+ template: Optional[str] = typer.Option(None, "--template", "-t", help="Template (omit for interactive selection)"),
+ lang: str = typer.Option("zh", "--lang", "-l", help="Language (zh/en)"),
+ force: bool = typer.Option(False, "--force", "-f", help="Force overwrite"),
+ no_index: bool = typer.Option(False, "--no-index", help="Skip building search index"),
+):
+ """Extract knowledge from text to a new directory."""
+ validate_config()
+
+ if template is None:
+ template = select_template_interactive()
+ if template is None:
+ console.print("[red]No template selected. Exiting.[/red]")
+ raise typer.Exit(1)
+
+ output_path = Path(output)
+
+ if output_path.exists() and not force:
+ if any(output_path.iterdir()):
+ console.print("[red]Error:[/red] Output directory already exists and is not empty. Use --force to overwrite.")
+ raise typer.Exit(1)
+
+ output_path.mkdir(parents=True, exist_ok=True)
+
+ console.print(f"[blue]Input:[/blue] {input}")
+ console.print(f"[blue]Output:[/blue] {output}")
+ console.print(f"[blue]Template:[/blue] {template}")
+ console.print(f"[blue]Language:[/blue] {lang}")
+ console.print(f"[blue]Build Index:[/blue] {'No' if no_index else 'Yes'}")
+ console.print()
+
+ try:
+ template_config = Template.get(template)
+ if template_config is None:
+ raise ValueError(f"Template '{template}' not found")
+ console.print(f"[green]Template resolved:[/green] {template_config.name}")
+ except ValueError as e:
+ console.print(f"[red]Error:[/red] {e}")
+ raise typer.Exit(1)
+
+ input_path = Path(input)
+
+ with Progress(SpinnerColumn(), TextColumn("[progress.description]{task.description}"), console=console) as progress:
+ task = progress.add_task("Creating template instance...", total=None)
+
+ kb = Template.create(template, lang)
+
+ if input_path.is_dir():
+ progress.update(task, description="Processing directory...")
+ text_files = list(input_path.glob("*.txt")) + list(input_path.glob("*.md"))
+ if not text_files:
+ console.print(f"[red]Error:[/red] No .txt or .md files found in {input}")
+ raise typer.Exit(1)
+
+ all_text = []
+ for file_path in text_files:
+ text = read_input(str(file_path))
+ all_text.append(text)
+ console.print(f"[dim]Loaded {file_path.name}: {len(text)} chars[/dim]")
+
+ combined_text = "\n\n".join(all_text)
+ console.print(f"[dim]Total input: {len(combined_text)} characters[/dim]")
+
+ progress.update(task, description="Extracting knowledge...")
+ kb.feed_text(combined_text)
+ else:
+ progress.update(task, description="Reading input...")
+ text = read_input(input)
+ console.print(f"[dim]Input text: {len(text)} characters[/dim]")
+
+ progress.update(task, description="Extracting knowledge...")
+ kb.feed_text(text)
+
+ progress.update(task, description="Saving data...")
+ kb.dump(output_path)
+
+ if not no_index:
+ progress.update(task, description="Building search index...")
+ kb.build_index()
+ progress.update(task, description="Saving index...")
+ kb.dump(output_path)
+
+ console.print()
+ console.print(f"[bold green]Success![/bold green] Knowledge extracted to {output_path}")
+ if no_index:
+ console.print(f"[dim]Note: Index was not built. Run 'he build-index {output}' to enable search/talk.[/dim]")
+ else:
+ console.print("[dim]Index built. You can now use 'he search' and 'he talk'.[/dim]")
+ console.print(f"[dim]Use 'he show {output}' to visualize[/dim]")
+
+
[email protected](name="show")
+def show(kb_path: str = typer.Argument(..., help="Knowledge base directory")):
+ """Visualize knowledge base using OntoSight."""
+ path = validate_kb_with_data(kb_path)
+
+ template, lang = get_template_from_kb(path)
+
+ console.print(f"[blue]Template:[/blue] {template}")
+ console.print(f"[blue]Language:[/blue] {lang}")
+ console.print()
+
+ validate_config()
+
+ with console.status("[bold blue]Loading knowledge base..."):
+ try:
+ kb = Template.create(template, lang)
+ kb.load(path)
+
+ except Exception as e:
+ console.print(f"[red]Error loading knowledge base:[/red] {e}")
+ raise typer.Exit(1)
+
+ console.print("[bold blue]Visualizing with OntoSight...[/bold blue]")
+
+ try:
+ kb.show(
+ node_label_extractor=lambda n: getattr(n, "name", str(n)),
+ edge_label_extractor=lambda e: getattr(e, "relation", str(e)),
+ )
+ except Exception as e:
+ console.print(f"[red]Error during visualization:[/red] {e}")
+ raise typer.Exit(1)
+
+
[email protected](name="info")
+def info(kb_path: str = typer.Argument(..., help="Knowledge base directory")):
+ """View knowledge base information and statistics."""
+ import json
+
+ path = validate_kb_with_data(kb_path)
+
+ metadata = load_kb_metadata(path)
+
+ data_file = path / "data.json"
+ with open(data_file, "r", encoding="utf-8") as f:
+ data = json.load(f)
+
+ if isinstance(data, dict):
+ node_count = len(data.get("nodes", data.get("entities", [])))
+ edge_count = len(data.get("edges", data.get("relations", [])))
+ elif isinstance(data, list):
+ node_count = len(data)
+ edge_count = 0
+ else:
+ node_count = 0
+ edge_count = 0
+
+ index_exists = (path / "index").exists() and any((path / "index").iterdir())
+
+ table = Table(title="Knowledge Base Info", show_header=False, box=None)
+ table.add_column("Key", style="cyan", width=15)
+ table.add_column("Value", style="green")
+
+ table.add_row("Path", str(path))
+
+ if metadata:
+ table.add_row("Template", metadata.get("template", "unknown"))
+ table.add_row("Language", metadata.get("lang", "unknown"))
+ table.add_row("Created", metadata.get("created_at", "unknown"))
+ table.add_row("Updated", metadata.get("updated_at", "unknown"))
+ else:
+ table.add_row("Template", "[yellow]unknown[/yellow]")
+ table.add_row("Language", "[yellow]unknown[/yellow]")
+
+ table.add_row("Nodes", str(node_count))
+ table.add_row("Edges", str(edge_count))
+ table.add_row("Index", "[green]Built[/green]" if index_exists else "[red]Not Built[/red]")
+
+ console.print(table)
+
+
[email protected](name="search")
+def search(
+ query: str = typer.Argument(..., help="Search query"),
+ kb_path: str = typer.Option(..., "--kb", "-k", help="Knowledge base directory"),
+ top_k: int = typer.Option(3, "--top-k", "-n", help="Number of results"),
+):
+ """Semantic search in knowledge base."""
+ import json
+
+ validate_config()
+
+ path = validate_kb_with_index(kb_path)
+ template, lang = get_template_from_kb(path)
+
+ console.print(f"[blue]Query:[/blue] {query}")
+ console.print(f"[blue]Knowledge base:[/blue] {kb_path}")
+ console.print(f"[blue]Top K:[/blue] {top_k}")
+ console.print()
+
+ with Progress(SpinnerColumn(), TextColumn("[progress.description]{task.description}"), console=console) as progress:
+ task = progress.add_task("Searching...", total=None)
+
+ try:
+ kb = Template.create(template, lang)
+
+ progress.update(task, description="Loading knowledge base...")
+ kb.load(path)
+
+ progress.update(task, description="Searching...")
+ results = kb.search(query, top_k=top_k)
+
+ except Exception as e:
+ console.print(f"[red]Error:[/red] {e}")
+ raise typer.Exit(1)
+
+ console.print()
+ if not results:
+ console.print("[yellow]No results found.[/yellow]")
+ else:
+ console.print(f"[bold green]Found {len(results)} result(s):[/bold green]")
+ console.print()
+
+ for i, result in enumerate(results, 1):
+ console.print(f"[bold cyan]Result {i}:[/bold cyan]")
+ if hasattr(result, "model_dump"):
+ console.print_json(json.dumps(result.model_dump(), indent=2, ensure_ascii=False))
+ elif hasattr(result, "dict"):
+ console.print_json(json.dumps(result.dict(), indent=2, ensure_ascii=False))
+ else:
+ console.print(str(result))
+ console.print()
+
+
+def chat_loop(kb):
+ """Interactive chat loop."""
+ console.print("\n[bold green]Entering interactive mode. Type 'exit' or 'quit' to stop.[/bold green]\n")
+ while True:
+ try:
+ query = console.input("[bold cyan]>[/bold cyan] ")
+ if query.lower() in ["exit", "quit", "q"]:
+ console.print("[dim]Goodbye![/dim]")
+ break
+ if not query.strip():
+ continue
+ response = kb.chat(query)
+ console.print()
+ console.print(response.content)
+ console.print()
+ except KeyboardInterrupt:
+ console.print("\n[dim]Goodbye![/dim]")
+ break
+ except Exception as e:
+ console.print(f"[red]Error:[/red] {e}")
+
+
[email protected](name="talk")
+def talk(
+ kb_path: str = typer.Argument(..., help="Knowledge base directory"),
+ query: Optional[str] = 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"),
+):
+ """Chat with knowledge base."""
+ validate_config()
+
+ path = validate_kb_with_index(kb_path)
+ template, lang = get_template_from_kb(path)
+
+ if interactive:
+ console.print(f"[blue]Knowledge base:[/blue] {kb_path}")
+ console.print(f"[blue]Template:[/blue] {template}")
+ console.print(f"[blue]Top K:[/blue] {top_k}")
+ console.print()
+ elif query is None:
+ console.print("[red]Error:[/red] Please provide a query or use --interactive mode")
+ raise typer.Exit(1)
+ else:
+ console.print(f"[blue]Query:[/blue] {query}")
+ console.print(f"[blue]Knowledge base:[/blue] {kb_path}")
+ console.print(f"[blue]Top K:[/blue] {top_k}")
+ console.print()
+
+ with Progress(SpinnerColumn(), TextColumn("[progress.description]{task.description}"), console=console) as progress:
+ task = progress.add_task("Loading...", total=None)
+
+ try:
+ kb = Template.create(template, lang)
+
+ progress.update(task, description="Loading knowledge base...")
+ kb.load(path)
+
+ except Exception as e:
+ console.print(f"[red]Error:[/red] {e}")
+ raise typer.Exit(1)
+
+ if interactive:
+ chat_loop(kb)
+ else:
+ with console.status("[bold blue]Thinking..."):
+ try:
+ response = kb.chat(query, top_k=top_k)
+ console.print(response.content)
+
+ if response.additional_kwargs.get("retrieved_items"):
+ console.print()
+ console.print("[dim]Retrieved context:[/dim]")
+ items = response.additional_kwargs["retrieved_items"]
+ for i, item in enumerate(items, 1):
+ console.print(f"[dim]{i}. {str(item)[:100]}...[/dim]")
+ except Exception as e:
+ console.print(f"[red]Error:[/red] {e}")
+ raise typer.Exit(1)
+
+
[email protected](name="feed")
+def feed(
+ input: str = typer.Argument(..., help="Input file path or '-' for stdin"),
+ output: str = typer.Option(..., "--output", "-o", help="Knowledge base directory"),
+ template: Optional[str] = typer.Option(None, "--template", "-t", help="Template"),
+ lang: Optional[str] = typer.Option(None, "--lang", "-l", help="Language"),
+):
+ """Append knowledge to an existing directory."""
+ validate_config()
+
+ output_path = validate_kb_path(output)
+
+ metadata = load_kb_metadata(output_path)
+ if not metadata:
+ console.print(f"[red]Error:[/red] Not a valid knowledge base directory: {output}")
+ raise typer.Exit(1)
+
+ if template is None:
+ template = metadata.get("template", "knowledge_graph")
+ if lang is None:
+ lang = metadata.get("lang", "zh")
+
+ console.print(f"[blue]Input:[/blue] {input}")
+ console.print(f"[blue]Output:[/blue] {output}")
+ console.print(f"[blue]Template:[/blue] {template} (from metadata)")
+ console.print(f"[blue]Language:[/blue] {lang} (from metadata)")
+ console.print()
+
+ try:
+ kb = Template.create(template, lang)
+ console.print(f"[green]Template loaded:[/green] {template}")
+ except ValueError as e:
+ console.print(f"[red]Error:[/red] {e}")
+ raise typer.Exit(1)
+
+ with Progress(SpinnerColumn(), TextColumn("[progress.description]{task.description}"), console=console) as progress:
+ task = progress.add_task("Loading existing knowledge...", total=None)
+
+ kb.load(output_path)
+
+ progress.update(task, description="Reading input...")
+ text = read_input(input)
+ console.print(f"[dim]Input text: {len(text)} characters[/dim]")
+
+ progress.update(task, description="Appending knowledge...")
+ kb.feed_text(text)
+
+ progress.update(task, description="Saving data...")
+ kb.dump(output_path)
+
+ console.print()
+ console.print(f"[bold green]Success![/bold green] Knowledge appended to {output_path}")
+
+
[email protected](name="build-index")
+def build_index(
+ kb_path: str = typer.Argument(..., help="Knowledge base directory"),
+ force: bool = typer.Option(False, "--force", "-f", help="Force rebuild"),
+):
+ """Build vector index for knowledge base."""
+ validate_config()
+
+ path = validate_kb_with_data(kb_path)
+
+ index_dir = path / "index"
+ if index_dir.exists() and any(index_dir.iterdir()) and not force:
+ console.print("[yellow]Warning:[/yellow] Index already exists. Use --force to rebuild.")
+ console.print(f"[dim]Index location: {index_dir}[/dim]")
+ raise typer.Exit(0)
+
+ template, lang = get_template_from_kb(path)
+
+ console.print(f"[blue]Template:[/blue] {template}")
+ console.print(f"[blue]Language:[/blue] {lang}")
+ console.print()
+
+ with Progress(SpinnerColumn(), TextColumn("[progress.description]{task.description}"), console=console) as progress:
+ task = progress.add_task("Initializing...", total=None)
+
+ try:
+ kb = Template.create(template, lang)
+
+ progress.update(task, description="Loading knowledge base...")
+ kb.load(path)
+
+ progress.update(task, description="Building index...")
+ kb.build_index()
+
+ progress.update(task, description="Saving index...")
+ kb.dump(path)
+
+ except Exception as e:
+ console.print(f"[red]Error:[/red] {e}")
+ raise typer.Exit(1)
+
+ console.print()
+ console.print(f"[bold green]Success![/bold green] Index built for {kb_path}")
diff --git a/hyperextract/cli/commands/__init__.py b/hyperextract/cli/commands/__init__.py
index e69de29..82766b2 100644
--- a/hyperextract/cli/commands/__init__.py
+++ b/hyperextract/cli/commands/__init__.py
@@ -0,0 +1,6 @@
+"""Command modules for Hyper-Extract CLI."""
+
+from .list import app as list_app
+from .config import app as config_app
+
+__all__ = ["list_app", "config_app"]
diff --git a/hyperextract/cli/commands/build_index.py b/hyperextract/cli/commands/build_index.py
deleted file mode 100644
index 4022895..0000000
--- a/hyperextract/cli/commands/build_index.py
+++ /dev/null
@@ -1,64 +0,0 @@
-"""Build-index command for Hyper-Extract CLI."""
-
-import typer
-from rich.console import Console
-from rich.progress import Progress, SpinnerColumn, TextColumn
-
-from ..utils import (
- validate_config,
- validate_kb_with_data,
- create_template,
- get_template_from_kb,
-)
-
-console = Console()
-
-app = typer.Typer(
- name="build-index",
- help="Build vector index for knowledge base",
-)
-
-
-def main(
- kb_path: str = typer.Argument(..., help="Knowledge base directory"),
- force: bool = typer.Option(False, "--force", "-f", help="Force rebuild"),
-):
- """Build vector index for knowledge base."""
- validate_config()
-
- path = validate_kb_with_data(kb_path)
-
- index_dir = path / "index"
- if index_dir.exists() and any(index_dir.iterdir()) and not force:
- console.print(f"[yellow]Warning:[/yellow] Index already exists. Use --force to rebuild.")
- console.print(f"[dim]Index location: {index_dir}[/dim]")
- raise typer.Exit(0)
-
- template, lang = get_template_from_kb(path)
-
- console.print(f"[blue]Template:[/blue] {template}")
- console.print(f"[blue]Language:[/blue] {lang}")
- console.print()
-
- with Progress(SpinnerColumn(), TextColumn("[progress.description]{task.description}"), console=console) as progress:
- task = progress.add_task("Initializing...", total=None)
-
- try:
- kb = create_template(template, lang)
-
- progress.update(task, description="Loading knowledge base...")
- kb.load(path)
-
- progress.update(task, description="Building index...")
- kb.build_index()
-
- progress.update(task, description="Saving index...")
- kb.dump(path)
-
- except Exception as e:
- console.print(f"[red]Error:[/red] {e}")
- raise typer.Exit(1)
-
- console.print()
- console.print(f"[bold green]Success![/bold green] Index built for {kb_path}")
diff --git a/hyperextract/cli/commands/feed.py b/hyperextract/cli/commands/feed.py
deleted file mode 100644
index f08abc4..0000000
--- a/hyperextract/cli/commands/feed.py
+++ /dev/null
@@ -1,82 +0,0 @@
-"""Feed command for Hyper-Extract CLI."""
-
-from pathlib import Path
-from datetime import datetime
-from typing import Optional
-
-import typer
-from rich.console import Console
-from rich.progress import Progress, SpinnerColumn, TextColumn
-
-from ..utils import (
- read_input,
- validate_config,
- validate_kb_path,
- create_template,
- get_template_from_kb,
-)
-from ..config import load_kb_metadata, save_kb_metadata
-
-console = Console()
-
-app = typer.Typer(
- name="feed",
- help="Append knowledge to an existing directory",
-)
-
-
-def main(
- input: str = typer.Argument(..., help="Input file path or '-' for stdin"),
- output: str = typer.Option(..., "--output", "-o", help="Output directory"),
- template: Optional[str] = typer.Option(None, "--template", "-t", help="Template"),
- lang: Optional[str] = typer.Option(None, "--lang", "-l", help="Language"),
-):
- """Append knowledge to an existing directory."""
- validate_config()
-
- output_path = validate_kb_path(output)
-
- metadata = load_kb_metadata(output_path)
- if not metadata:
- console.print(f"[red]Error:[/red] Not a valid knowledge base directory: {output}")
- raise typer.Exit(1)
-
- if template is None:
- template = metadata.get("template", "knowledge_graph")
- if lang is None:
- lang = metadata.get("lang", "zh")
-
- console.print(f"[blue]Input:[/blue] {input}")
- console.print(f"[blue]Output:[/blue] {output}")
- console.print(f"[blue]Template:[/blue] {template} (from metadata)")
- console.print(f"[blue]Language:[/blue] {lang} (from metadata)")
- console.print()
-
- try:
- kb = create_template(template, lang)
- console.print(f"[green]Template loaded:[/green] {template}")
- except ValueError as e:
- console.print(f"[red]Error:[/red] {e}")
- raise typer.Exit(1)
-
- with Progress(SpinnerColumn(), TextColumn("[progress.description]{task.description}"), console=console) as progress:
- task = progress.add_task("Loading existing knowledge...", total=None)
-
- kb.load(output_path)
-
- progress.update(task, description="Reading input...")
- text = read_input(input)
- console.print(f"[dim]Input text: {len(text)} characters[/dim]")
-
- progress.update(task, description="Appending knowledge...")
- kb.feed_text(text)
-
- progress.update(task, description="Saving data...")
- kb.dump(output_path)
-
- metadata["updated_at"] = datetime.now().isoformat()
- save_kb_metadata(output_path, metadata)
-
- console.print()
- console.print(f"[bold green]Success![/bold green] Knowledge appended to {output_path}")
diff --git a/hyperextract/cli/commands/list.py b/hyperextract/cli/commands/list.py
index 34567bd..67dc6bd 100644
--- a/hyperextract/cli/commands/list.py
+++ b/hyperextract/cli/commands/list.py
@@ -5,13 +5,7 @@ from rich.console import Console
from rich.table import Table
import typer
-from hyperextract.utils.template_engine import Gallery, Template
-
-from ..utils import (
- get_autotype_description,
- get_domain_description,
- AUTOTYPE_MAP,
-)
+from hyperextract.utils.template_engine import Gallery
console = Console()
@@ -20,11 +14,11 @@ app = typer.Typer(
help="List available resources",
)
-
METHODS = [
("kg_gen", "Knowledge Graph Generation"),
("atom", "Atomic Information Extraction"),
- ("graphiti", "Graphiti - Temporal Knowledge Graph"),
+ ("itext2kg", "iText2KG - Iterative Knowledge Graph Construction"),
+ ("itext2kg_star", "iText2KG-Star - Star-shaped Knowledge Graph"),
("light_rag", "LightRAG - Lightweight Graph RAG"),
("hyper_rag", "HyperRAG - Hypergraph RAG"),
("cog_rag", "CogRAG - Cognitive RAG"),
@@ -32,112 +26,70 @@ METHODS = [
]
[email protected](name="autotype")
-def autotype(
- search: Optional[str] = typer.Option(None, "--search", "-s", help="Search autotypes"),
-):
- """List all available AutoTypes."""
- autotypes = Gallery.list_autotypes()
-
- if search:
- search_lower = search.lower()
- autotypes = [a for a in autotypes if search_lower in a.lower()]
-
- table = Table(title="Available AutoTypes", show_header=True, header_style="bold magenta")
- table.add_column("AutoType", style="cyan", width=25)
- table.add_column("Description", style="green")
-
- for name in autotypes:
- display_name = AUTOTYPE_MAP.get(name, name)
- desc = get_autotype_description(display_name)
- table.add_row(display_name, desc)
-
- console.print(table)
- console.print(f"\n[dim]Total: {len(autotypes)} AutoTypes[/dim]")
-
-
@app.command(name="template")
def template(
- search: Optional[str] = typer.Option(None, "--search", "-s", help="Search templates by name"),
+ query: Optional[str] = typer.Option(None, "--query", "-q", help="Query to search templates"),
autotype: Optional[str] = typer.Option(None, "--autotype", "-a", help="Filter by autotype"),
language: Optional[str] = typer.Option(None, "--lang", "-l", help="Filter by language"),
):
"""List all available templates."""
- results = Gallery.search(query=search, autotype=autotype, language=language)
-
+ results = Gallery.list(
+ filter_by_query=query,
+ filter_by_type=autotype,
+ filter_by_language=language,
+ )
+
templates = []
- for cfg in results:
+ for path, cfg in results.items():
full_name = cfg.name
languages = cfg.language if cfg.language else ["zh"]
if isinstance(languages, str):
languages = [languages]
-
+
for lang in languages:
- templates.append((full_name, full_name, lang))
-
+ templates.append((path, full_name, lang))
+
+ if not templates:
+ console.print("[yellow]No templates found.[/yellow]")
+ return
+
table = Table(title="Available Templates", show_header=True, header_style="bold magenta")
table.add_column("Template ID", style="cyan", width=30)
table.add_column("Name", style="green", width=30)
table.add_column("Language", style="yellow", width=10)
-
+
for tid, name, lang in templates:
table.add_row(tid, name, lang)
-
+
console.print(table)
console.print(f"\n[dim]Total: {len(templates)} templates[/dim]")
console.print("\n[dim]Tip: Use -t/--template to specify a template.[/dim]")
[email protected](name="domain")
-def domain(
- search: Optional[str] = typer.Option(None, "--search", "-s", help="Search domains"),
-):
- """List all available domains."""
- domains = Template.domains()
-
- items = []
- for name, count in domains:
- desc = get_domain_description(name)
- items.append((name, f"{desc} ({count} templates)"))
-
- if search:
- search_lower = search.lower()
- items = [
- item for item in items
- if search_lower in item[0].lower() or search_lower in item[1].lower()
- ]
-
- table = Table(title="Available Domains", show_header=True, header_style="bold magenta")
- table.add_column("Domain", style="cyan", width=20)
- table.add_column("Description", style="green")
-
- for name, desc in items:
- table.add_row(name, desc)
-
- console.print(table)
- console.print(f"\n[dim]Total: {len(items)} domains[/dim]")
-
-
@app.command(name="method")
def method(
- search: Optional[str] = typer.Option(None, "--search", "-s", help="Search methods"),
+ query: Optional[str] = typer.Option(None, "--query", "-q", help="Query to search methods"),
):
"""List all available extraction methods."""
items = METHODS
-
- if search:
- search_lower = search.lower()
+
+ if query:
+ query_lower = query.lower()
items = [
item for item in items
- if search_lower in item[0].lower() or search_lower in item[1].lower()
+ if query_lower in item[0].lower() or query_lower in item[1].lower()
]
-
+
+ if not items:
+ console.print("[yellow]No methods found.[/yellow]")
+ return
+
table = Table(title="Available Methods", show_header=True, header_style="bold magenta")
table.add_column("Method", style="cyan", width=20)
table.add_column("Description", style="green")
-
+
for name, desc in items:
table.add_row(name, desc)
-
+
console.print(table)
console.print(f"\n[dim]Total: {len(items)} methods[/dim]")
diff --git a/hyperextract/cli/commands/parse.py b/hyperextract/cli/commands/parse.py
deleted file mode 100644
index 0bfba93..0000000
--- a/hyperextract/cli/commands/parse.py
+++ /dev/null
@@ -1,89 +0,0 @@
-"""Parse command for Hyper-Extract CLI."""
-
-from pathlib import Path
-from datetime import datetime
-
-import typer
-from rich.console import Console
-from rich.progress import Progress, SpinnerColumn, TextColumn
-
-from ..utils import (
- read_input,
- validate_config,
- create_template,
- get_template_config,
- get_auto_type_from_config,
-)
-from ..config import save_kb_metadata
-
-console = Console()
-
-app = typer.Typer(
- name="parse",
- help="Extract knowledge from text to a new directory",
-)
-
-
-def main(
- input: str = typer.Argument(..., help="Input file path or '-' for stdin"),
- output: str = typer.Option(..., "--output", "-o", help="Output directory"),
- template: str = typer.Option("knowledge_graph", "--template", "-t", help="Template"),
- lang: str = typer.Option("zh", "--lang", "-l", help="Language (zh/en)"),
- force: bool = typer.Option(False, "--force", "-f", help="Force overwrite"),
-):
- """Extract knowledge from text to a new directory."""
- validate_config()
-
- output_path = Path(output)
-
- if output_path.exists() and not force:
- if any(output_path.iterdir()):
- console.print(f"[red]Error:[/red] Output directory already exists and is not empty. Use --force to overwrite.")
- raise typer.Exit(1)
-
- output_path.mkdir(parents=True, exist_ok=True)
-
- console.print(f"[blue]Input:[/blue] {input}")
- console.print(f"[blue]Output:[/blue] {output}")
- console.print(f"[blue]Template:[/blue] {template}")
- console.print(f"[blue]Language:[/blue] {lang}")
- console.print()
-
- try:
- template_config = get_template_config(template, lang)
- if template_config is None:
- raise ValueError(f"Template '{template}' not found")
- console.print(f"[green]Template resolved:[/green] {template_config.name}")
- except ValueError as e:
- console.print(f"[red]Error:[/red] {e}")
- raise typer.Exit(1)
-
- with Progress(SpinnerColumn(), TextColumn("[progress.description]{task.description}"), console=console) as progress:
- task = progress.add_task("Creating template instance...", total=None)
-
- kb = create_template(template, lang)
-
- progress.update(task, description="Reading input...")
- text = read_input(input)
- console.print(f"[dim]Input text: {len(text)} characters[/dim]")
-
- progress.update(task, description="Extracting knowledge...")
- kb.feed_text(text)
-
- progress.update(task, description="Saving data...")
- kb.dump(output_path)
-
- metadata = {
- "template": template,
- "lang": lang,
- "auto_type": get_auto_type_from_config(template_config),
- "created_at": datetime.now().isoformat(),
- "updated_at": datetime.now().isoformat(),
- }
- save_kb_metadata(output_path, metadata)
-
- console.print()
- console.print(f"[bold green]Success![/bold green] Knowledge extracted to {output_path}")
- console.print(f"[dim]Use 'he show {output}' to visualize[/dim]")
- console.print(f"[dim]Use 'he build-index {output}' to build search index[/dim]")
diff --git a/hyperextract/cli/commands/search.py b/hyperextract/cli/commands/search.py
deleted file mode 100644
index b0c10be..0000000
--- a/hyperextract/cli/commands/search.py
+++ /dev/null
@@ -1,72 +0,0 @@
-"""Search command for Hyper-Extract CLI."""
-
-import json
-
-import typer
-from rich.console import Console
-from rich.progress import Progress, SpinnerColumn, TextColumn
-
-from ..utils import (
- validate_config,
- validate_kb_with_index,
- create_template,
- get_template_from_kb,
-)
-
-console = Console()
-
-app = typer.Typer(
- name="search",
- help="Semantic search in knowledge base",
-)
-
-
-def main(
- query: str = typer.Argument(..., help="Search query"),
- kb_path: str = typer.Option(..., "--kb", "-k", help="Knowledge base directory"),
- top_k: int = typer.Option(3, "--top-k", "-n", help="Number of results"),
-):
- """Semantic search in knowledge base."""
- validate_config()
-
- path = validate_kb_with_index(kb_path)
- template, lang = get_template_from_kb(path)
-
- console.print(f"[blue]Query:[/blue] {query}")
- console.print(f"[blue]Knowledge base:[/blue] {kb_path}")
- console.print(f"[blue]Top K:[/blue] {top_k}")
- console.print()
-
- with Progress(SpinnerColumn(), TextColumn("[progress.description]{task.description}"), console=console) as progress:
- task = progress.add_task("Searching...", total=None)
-
- try:
- kb = create_template(template, lang)
-
- progress.update(task, description="Loading knowledge base...")
- kb.load(path)
-
- progress.update(task, description="Searching...")
- results = kb.search(query, top_k=top_k)
-
- except Exception as e:
- console.print(f"[red]Error:[/red] {e}")
- raise typer.Exit(1)
-
- console.print()
- if not results:
- console.print("[yellow]No results found.[/yellow]")
- else:
- console.print(f"[bold green]Found {len(results)} result(s):[/bold green]")
- console.print()
-
- for i, result in enumerate(results, 1):
- console.print(f"[bold cyan]Result {i}:[/bold cyan]")
- if hasattr(result, "model_dump"):
- console.print_json(json.dumps(result.model_dump(), indent=2, ensure_ascii=False))
- elif hasattr(result, "dict"):
- console.print_json(json.dumps(result.dict(), indent=2, ensure_ascii=False))
- else:
- console.print(str(result))
- console.print()
diff --git a/hyperextract/cli/commands/show.py b/hyperextract/cli/commands/show.py
deleted file mode 100644
index 2f0a509..0000000
--- a/hyperextract/cli/commands/show.py
+++ /dev/null
@@ -1,71 +0,0 @@
-"""Show command for Hyper-Extract CLI."""
-
-import json
-
-import typer
-from rich.console import Console
-
-from ..utils import (
- validate_config,
- validate_kb_with_data,
- create_template,
- get_template_from_kb,
-)
-
-console = Console()
-
-app = typer.Typer(
- name="show",
- help="Show knowledge base visualization",
-)
-
-
-def main(
- kb_path: str = typer.Argument(..., help="Knowledge base directory"),
- build_index: bool = typer.Option(False, "--build-index", "-b", help="Build index"),
-):
- """Show knowledge base visualization."""
- path = validate_kb_with_data(kb_path)
-
- template, lang = get_template_from_kb(path)
-
- console.print(f"[blue]Template:[/blue] {template}")
- console.print(f"[blue]Language:[/blue] {lang}")
- console.print()
-
- if build_index:
- validate_config()
-
- with console.status("[bold blue]Loading knowledge base..."):
- try:
- kb = create_template(template, lang)
- kb.load(path)
-
- if build_index:
- console.print("[blue]Building index...[/blue]")
- kb.build_index()
- kb.dump(path)
-
- except Exception as e:
- console.print(f"[red]Error loading knowledge base:[/red] {e}")
- raise typer.Exit(1)
-
- data_file = path / "data.json"
- with open(data_file, "r", encoding="utf-8") as f:
- data = json.load(f)
-
- console.print(f"[green]Loaded {len(str(data))} chars of data[/green]")
- console.print()
-
- console.print("[bold blue]Visualizing...[/bold blue]")
- try:
- kb.show(
- node_label_extractor=lambda n: getattr(n, "name", str(n)),
- edge_label_extractor=lambda e: getattr(e, "relation", str(e)),
- )
- except Exception as e:
- console.print(f"[yellow]Warning: Could not show graph:[/yellow] {e}")
- console.print()
- console.print("[bold]Data preview:[/bold]")
- console.print_json(json.dumps(data, indent=2, ensure_ascii=False))
diff --git a/hyperextract/cli/commands/talk.py b/hyperextract/cli/commands/talk.py
deleted file mode 100644
index 2a71f3f..0000000
--- a/hyperextract/cli/commands/talk.py
+++ /dev/null
@@ -1,102 +0,0 @@
-"""Talk command for Hyper-Extract CLI."""
-
-from typing import Optional
-
-import typer
-from rich.console import Console
-from rich.progress import Progress, SpinnerColumn, TextColumn
-
-from ..utils import (
- validate_config,
- validate_kb_with_index,
- create_template,
- get_template_from_kb,
-)
-
-console = Console()
-
-app = typer.Typer(
- name="talk",
- help="Chat with knowledge base",
-)
-
-
-def chat_loop(kb):
- """Interactive chat loop."""
- console.print("\n[bold green]Entering interactive mode. Type 'exit' or 'quit' to stop.[/bold green]\n")
- while True:
- try:
- query = console.input("[bold cyan]>[/bold cyan] ")
- if query.lower() in ["exit", "quit", "q"]:
- console.print("[dim]Goodbye![/dim]")
- break
- if not query.strip():
- continue
- response = kb.chat(query)
- console.print()
- console.print(response.content)
- console.print()
- except KeyboardInterrupt:
- console.print("\n[dim]Goodbye![/dim]")
- break
- except Exception as e:
- console.print(f"[red]Error:[/red] {e}")
-
-
-def main(
- query: Optional[str] = typer.Argument(None, help="Question to ask"),
- kb_path: str = typer.Option(..., "--kb", "-k", help="Knowledge base directory"),
- top_k: int = typer.Option(3, "--top-k", "-n", help="Number of context items"),
- interactive: bool = typer.Option(False, "--interactive", "-i", help="Interactive mode"),
-):
- """Chat with knowledge base."""
- validate_config()
-
- path = validate_kb_with_index(kb_path)
- template, lang = get_template_from_kb(path)
-
- if interactive:
- console.print(f"[blue]Knowledge base:[/blue] {kb_path}")
- console.print(f"[blue]Template:[/blue] {template}")
- console.print(f"[blue]Top K:[/blue] {top_k}")
- console.print()
- elif query is None:
- console.print(f"[red]Error:[/red] Please provide a query or use --interactive mode")
- raise typer.Exit(1)
- else:
- console.print(f"[blue]Query:[/blue] {query}")
- console.print(f"[blue]Knowledge base:[/blue] {kb_path}")
- console.print(f"[blue]Top K:[/blue] {top_k}")
- console.print()
-
- with Progress(SpinnerColumn(), TextColumn("[progress.description]{task.description}"), console=console) as progress:
- task = progress.add_task("Loading...", total=None)
-
- try:
- kb = create_template(template, lang)
-
- progress.update(task, description="Loading knowledge base...")
- kb.load(path)
-
- except Exception as e:
- console.print(f"[red]Error:[/red] {e}")
- raise typer.Exit(1)
-
- if interactive:
- chat_loop(kb)
- else:
- with console.status("[bold blue]Thinking..."):
- try:
- response = kb.chat(query, top_k=top_k)
- console.print(response.content)
-
- if response.additional_kwargs.get("retrieved_items"):
- console.print()
- console.print("[dim]Retrieved context:[/dim]")
- items = response.additional_kwargs["retrieved_items"]
- for i, item in enumerate(items, 1):
- console.print(f"[dim]{i}. {str(item)[:100]}...[/dim]")
- except Exception as e:
- console.print(f"[red]Error:[/red] {e}")
- raise typer.Exit(1)
diff --git a/hyperextract/cli/config.py b/hyperextract/cli/config.py
index 81355fd..f2f7d52 100644
--- a/hyperextract/cli/config.py
+++ b/hyperextract/cli/config.py
@@ -196,10 +196,3 @@ def load_kb_metadata(kb_path: Path) -> Optional[Dict[str, Any]]:
with open(metadata_path, "r", encoding="utf-8") as f:
return json.load(f)
-
-
-def save_kb_metadata(kb_path: Path, metadata: Dict[str, Any]) -> None:
- """Save knowledge base metadata to directory."""
- metadata_path = kb_path / "metadata.json"
- with open(metadata_path, "w", encoding="utf-8") as f:
- json.dump(metadata, f, indent=2, ensure_ascii=False, default=str)
diff --git a/hyperextract/cli/utils.py b/hyperextract/cli/utils.py
index 76c80e8..a6fe924 100644
--- a/hyperextract/cli/utils.py
+++ b/hyperextract/cli/utils.py
@@ -3,53 +3,14 @@
import sys
import typer
from pathlib import Path
+from typing import Tuple
from rich.console import Console
-from typing import Optional, Tuple
-from hyperextract.utils.template_engine import Template, TemplateCfg
from .config import ConfigManager
console = Console()
-AUTOTYPE_MAP = {
- "model": "AutoModel",
- "list": "AutoList",
- "set": "AutoSet",
- "graph": "AutoGraph",
- "hypergraph": "AutoHypergraph",
- "temporal_graph": "AutoTemporalGraph",
- "spatial_graph": "AutoSpatialGraph",
- "spatio_temporal_graph": "AutoSpatioTemporalGraph",
-}
-
-AUTOTYPE_DESCRIPTIONS = {
- "AutoModel": "Extract into a complete data model",
- "AutoList": "Extract as a list (keywords, items)",
- "AutoSet": "Extract and deduplicate (entity registry)",
- "AutoGraph": "Extract as a knowledge graph (relations)",
- "AutoTemporalGraph": "Extract as timeline (events over time)",
- "AutoSpatialGraph": "Extract as spatial graph (locations)",
- "AutoSpatioTemporalGraph": "Extract as spatiotemporal graph (time + space)",
- "AutoHypergraph": "Extract as hypergraph (multi-party relations)",
-}
-
-DOMAIN_DESCRIPTIONS = {
- "finance": "Financial documents",
- "medicine": "Medical documents",
- "legal": "Legal documents",
- "history": "Historical documents",
- "literature": "Literature works",
- "tcm": "Traditional Chinese Medicine",
- "news": "News articles",
- "biology": "Biological documents",
- "industry": "Industrial documents",
- "agriculture": "Agricultural documents",
- "food": "Food and culinary",
- "general": "General purpose",
-}
-
-
def read_input(input_path: str) -> str:
"""Read input from file or stdin."""
if input_path == "-":
@@ -147,14 +108,8 @@ def get_template_from_kb(kb_path: Path) -> Tuple[str, str]:
metadata = load_kb_metadata(kb_path)
- if not metadata:
- console.print(
- "[yellow]Warning:[/yellow] No metadata found, assuming knowledge_graph"
- )
- return "knowledge_graph", "zh"
-
- template = metadata.get("template", "knowledge_graph")
- lang = metadata.get("lang", "zh")
+ template = metadata.get("template")
+ lang = metadata.get("lang")
return template, lang
@@ -177,66 +132,3 @@ def validate_config() -> "ConfigManager":
raise typer.Exit(1)
return config
-
-
-def create_template(template: str, lang: str = "zh"):
- """Create template instance.
-
- Args:
- template: Template name
- lang: Language (zh/en)
-
- Returns:
- TemplateWrapper instance
- """
- return Template.create(template, lang)
-
-
-def get_template_config(template: str, lang: str = "zh") -> Optional[TemplateCfg]:
- """Get template config.
-
- Args:
- template: Template name
- lang: Language (zh/en)
-
- Returns:
- TemplateCfg if found
- """
- return Template.get(template, lang)
-
-
-def get_auto_type_from_config(config: TemplateCfg) -> str:
- """Infer AutoType from template config.
-
- Args:
- config: TemplateCfg instance
-
- Returns:
- AutoType str (e.g., "AutoGraph")
- """
- autotype = config.autotype
- return AUTOTYPE_MAP.get(autotype, "AutoGraph")
-
-
-def get_autotype_description(autotype: str) -> str:
- """Get description for AutoType.
-
- Args:
- autotype: AutoType name (e.g., "AutoGraph")
-
- Returns:
- Description str
- """
- return AUTOTYPE_DESCRIPTIONS.get(autotype, "")
-
-
-def get_domain_description(domain: str) -> str:
- """Get description for domain.
-
- Args:
- domain: Domain name (e.g., "finance")
-
- Returns:
- Description str
- """
- return DOMAIN_DESCRIPTIONS.get(domain, f"{domain} documents")
diff --git a/hyperextract/methods/typical/__init__.py b/hyperextract/methods/typical/__init__.py
index cc288f7..d6336b6 100644
--- a/hyperextract/methods/typical/__init__.py
+++ b/hyperextract/methods/typical/__init__.py
@@ -1,7 +1,7 @@
"""Typical/Canonical Pipelines.
This module contains typical and canonical methods for constructing knowledge graphs
-from text, including Graphiti, iText2KG, and other established graph-building techniques.
+from text, including iText2KG, and other established graph-building techniques.
"""
from .itext2kg import iText2KG
diff --git a/hyperextract/templates/presets/general/graph.yaml b/hyperextract/templates/presets/general/graph.yaml
index c2c712e..6f808b0 100644
--- a/hyperextract/templates/presets/general/graph.yaml
+++ b/hyperextract/templates/presets/general/graph.yaml
@@ -28,6 +28,9 @@ output:
- name: target
type: str
description: '目标实体'
+ - name: type
+ type: str
+ description: '关系类型'
- name: description
type: str
description: '关系描述'
@@ -59,4 +62,4 @@ options:
display:
entity_label: '{name} ({type})'
- relation_label: '{description}'
+ relation_label: '{type}'
diff --git a/hyperextract/utils/__init__.py b/hyperextract/utils/__init__.py
index cd61787..c8c2e5b 100644
--- a/hyperextract/utils/__init__.py
+++ b/hyperextract/utils/__init__.py
@@ -2,11 +2,9 @@
from .logging import logger, setup_logger
from .client import get_client
-from .template_engine import Template
__all__ = [
"logger",
"setup_logger",
"get_client",
- "Template",
]
diff --git a/hyperextract/utils/template_engine/factory.py b/hyperextract/utils/template_engine/factory.py
index de3da5d..d988894 100644
--- a/hyperextract/utils/template_engine/factory.py
+++ b/hyperextract/utils/template_engine/factory.py
@@ -327,7 +327,7 @@ class TemplateFactory:
@classmethod
def create(
cls,
- source: Union[str, Path, TemplateCfg],
+ source: Union[str, TemplateCfg],
language: str,
llm_client: BaseChatModel,
embedder: Embeddings,
@@ -338,7 +338,6 @@ class TemplateFactory:
Args:
source: Template source - can be:
- str: Template name (e.g., "knowledge_graph") or file path
- - Path: YAML file path
- TemplateCfg: Template configuration instance
language: Language code for localization (e.g., 'zh', 'en') - required
llm_client: LLM client
@@ -387,42 +386,43 @@ class TemplateFactory:
match source:
case str() as s if s.endswith(".yaml") or Path(s).exists():
- config = load_template(s)
+ template_cfg = load_template(s)
case str() as s:
- config = Gallery.get(s)
- case Path() as p if p.exists():
- config = load_template(p)
+ template_cfg = Gallery.get(s)
case _:
raise ValueError("Invalid source: must be a template path or file path")
- if config is None:
+ if template_cfg is None:
raise ValueError(f"Template not found: {source}")
- template = localize_template(config, language)
+ template_cfg = localize_template(template_cfg, language)
- match template.type:
+ match template_cfg.type:
case "model":
- template = cls.create_model(template, llm_client, embedder, **kwargs)
+ template = cls.create_model(template_cfg, llm_client, embedder, **kwargs)
case "list":
- template = cls.create_list(template, llm_client, embedder, **kwargs)
+ template = cls.create_list(template_cfg, llm_client, embedder, **kwargs)
case "set":
- template = cls.create_set(template, llm_client, embedder, **kwargs)
+ template = cls.create_set(template_cfg, llm_client, embedder, **kwargs)
case "graph":
- template = cls.create_graph(template, llm_client, embedder, **kwargs)
+ template = cls.create_graph(template_cfg, llm_client, embedder, **kwargs)
case "hypergraph":
template = cls.create_hypergraph(
- template, llm_client, embedder, **kwargs
+ template_cfg, llm_client, embedder, **kwargs
)
case "temporal_graph":
template = cls.create_temporal_graph(
- template, llm_client, embedder, **kwargs
+ template_cfg, llm_client, embedder, **kwargs
)
case "spatial_graph":
template = cls.create_spatial_graph(
- template, llm_client, embedder, **kwargs
+ template_cfg, llm_client, embedder, **kwargs
)
case "spatio_temporal_graph":
template = cls.create_spatio_temporal_graph(
- template, llm_client, embedder, **kwargs
+ template_cfg, llm_client, embedder, **kwargs
)
+ template.metadata["template"] = source
+ template.metadata["lang"] = language
+ template.metadata["type"] = template_cfg.type
return template
diff --git a/hyperextract/utils/template_engine/template.py b/hyperextract/utils/template_engine/template.py
index 317228e..c832cf7 100644
--- a/hyperextract/utils/template_engine/template.py
+++ b/hyperextract/utils/template_engine/template.py
@@ -4,7 +4,6 @@ Provides a clean API for searching and creating knowledge extraction templates.
"""
from typing import Dict, Optional
-from pathlib import Path
from langchain_core.language_models import BaseChatModel
from langchain_core.embeddings import Embeddings
@@ -36,7 +35,7 @@ class Template:
@staticmethod
def create(
- source: str | Path,
+ source: str,
language: str,
llm_client: Optional[BaseChatModel] = None,
embedder: Optional[Embeddings] = None,
diff --git a/tests/kg.md b/tests/kg.md
new file mode 100644
index 0000000..d037ba8
--- /dev/null
+++ b/tests/kg.md
@@ -0,0 +1,2 @@
+
+谷歌公司由拉里·佩奇和谢尔盖·布林于1998年在美国加利福尼亚州创立。谷歌的总部位于加州山景城。谷歌开发了安卓操作系统,该系统被三星、华为、小米等手机厂商广泛使用。谷歌云是谷歌旗下的云计算服务平台,为企业提供云存储和计算服务。Alphabet公司是谷歌的母公司,于2015年成立。谷歌搜索引擎是全球使用最广泛的搜索引擎之一。谷歌还拥有视频平台YouTube,YouTube于2005年创立,2006年被谷歌收购。亚马逊公司是谷歌在云计算领域的主要竞争对手,亚马逊的AWS服务是全球领先的云计算平台。微软公司是另一家重要的科技巨头,其总部位于美国华盛顿州雷德蒙德市。微软的Azure云服务也在快速发展。苹果公司以其iPhone智能手机闻名,总部位于加州库比蒂诺。