Hypergraph is more powerful. Transform unstructured text into structured knowledge with LLMs. Graphs, hypergraphs, and spatio-temporal extractions — with one command.
feat(cli): 添加完整的命令行工具实现
yifanfeng97e59a1c89abf8
diff --git a/hyperextract/cli/__init__.py b/hyperextract/cli/__init__.pynew file mode 100644index 0000000..0894a93--- /dev/null+++ b/hyperextract/cli/__init__.py@@ -0,0 +1,8 @@+"""Hyper-Extract CLI - A command-line tool for knowledge extraction."""++__version__ = "0.1.0"+__author__ = "Yifan Feng"++from .cli import app++__all__ = ["app"]diff --git a/hyperextract/cli/__main__.py b/hyperextract/cli/__main__.pynew file mode 100644index 0000000..0190379--- /dev/null+++ b/hyperextract/cli/__main__.py@@ -0,0 +1,6 @@+"""Run the CLI as a module."""++from .cli import app++if __name__ == "__main__":+ app()diff --git a/hyperextract/cli/cli.py b/hyperextract/cli/cli.pynew file mode 100644index 0000000..35e45af--- /dev/null+++ b/hyperextract/cli/cli.py@@ -0,0 +1,83 @@+"""CLI entry point for Hyper-Extract."""++import typer+from rich.console import Console+from rich.panel import Panel+from rich.table import Table+from rich.text import Text++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++console = Console()++app = typer.Typer(+ name="he",+ help="Hyper-Extract CLI - A command-line tool for knowledge extraction",+ add_completion=False,+ 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")+++def main(+ ctx: typer.Context,+ version: bool = typer.Option(+ False,+ "--version",+ "-v",+ help="Show version information",+ is_eager=True,+ ),+):+ """Hyper-Extract CLI - Transform unstructured text into structured knowledge."""+ if version:+ 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_row("he list", "template", "List available templates")+ table.add_row("he show", "<kb_path>", "Visualize knowledge base")+ table.add_row("he search", "<query> -k <kb>", "Semantic search")+ table.add_row("he talk", "-k <kb>", "Chat with knowledge base")++ panel = Panel(+ table,+ title="Quick Start",+ border_style="cyan",+ padding=(1, 2),+ )++ console.print()+ console.print(welcome_text)+ console.print()+ console.print(desc_text)+ console.print()+ console.print(panel)+ console.print()+ console.print("Run [bold]he --help[/bold] for more information.")+ raise typer.Exit()diff --git a/hyperextract/cli/commands/__init__.py b/hyperextract/cli/commands/__init__.pynew file mode 100644index 0000000..e69de29diff --git a/hyperextract/cli/commands/build_index.py b/hyperextract/cli/commands/build_index.pynew file mode 100644index 0000000..1053847--- /dev/null+++ b/hyperextract/cli/commands/build_index.py@@ -0,0 +1,91 @@+"""Build-index command for Hyper-Extract CLI."""++from pathlib import Path++import typer+from rich.console import Console+from rich.progress import Progress, SpinnerColumn, TextColumn++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."""+ from ..config import ConfigManager, load_kb_metadata++ path = Path(kb_path)++ if not path.exists():+ console.print(f"[red]Error:[/red] Knowledge base not found: {kb_path}")+ raise typer.Exit(1)++ if not path.is_dir():+ console.print(f"[red]Error:[/red] Not a directory: {kb_path}")+ raise typer.Exit(1)++ data_file = path / "data.json"+ if not data_file.exists():+ console.print(f"[red]Error:[/red] Not a valid knowledge base: {kb_path} (no data.json)")+ raise typer.Exit(1)++ metadata = load_kb_metadata(path)+ if not metadata:+ console.print(f"[yellow]Warning:[/yellow] No metadata found, assuming knowledge_graph")+ template = "knowledge_graph"+ lang = "zh"+ else:+ template = metadata.get("template", "knowledge_graph")+ lang = metadata.get("lang", "zh")++ config = ConfigManager()+ valid, msg = config.validate()+ if not valid:+ console.print(f"[red]Error:[/red] {msg}")+ raise typer.Exit(1)++ 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)++ 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:+ from ..templates import resolve_template+ from .feed import create_llm_client, create_embedder++ template_class = resolve_template(template, lang)+ llm = create_llm_client(config)+ embedder = create_embedder(config)++ progress.update(task, description="Loading knowledge base...")+ kb = template_class(llm_client=llm, embedder=embedder)+ 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/config.py b/hyperextract/cli/commands/config.pynew file mode 100644index 0000000..97f6566--- /dev/null+++ b/hyperextract/cli/commands/config.py@@ -0,0 +1,211 @@+"""Config command for Hyper-Extract CLI."""++from typing import Optional+from rich.console import Console+from rich.table import Table++from ..config import ConfigManager, LLM_PROVIDERS, EMBEDDER_PROVIDERS+import typer++console = Console()++app = typer.Typer(+ name="config",+ help="Manage LLM and Embedder configuration",+)+++def _show_config():+ """Show current configuration."""+ config = ConfigManager()+ cfg = config.show()++ table = Table(title="Hyper-Extract Configuration")+ table.add_column("Service", style="cyan", width=15)+ table.add_column("Provider", style="green", width=15)+ table.add_column("Model", style="yellow", width=25)+ table.add_column("API Key", style="magenta", width=20)++ llm_cfg = cfg["llm"]+ emb_cfg = cfg["embedder"]++ table.add_row(+ "LLM",+ llm_cfg["provider"],+ llm_cfg["model"],+ llm_cfg["api_key"][:10] + "..." if llm_cfg["api_key"] else "(not set)",+ )+ table.add_row(+ "Embedder",+ emb_cfg["provider"],+ emb_cfg["model"],+ emb_cfg["api_key"][:10] + "..." if emb_cfg["api_key"] else "(not set)",+ )++ console.print(table)++[email protected](name="show")+def show(+ show_all: bool = typer.Option(False, "--show", help="Show all configuration"),+):+ """Show current configuration."""+ _show_config()++[email protected](name="llm")+def llm(+ provider: str = typer.Option(+ None,+ "--provider",+ "-p",+ help=f"LLM provider ({'/'.join(LLM_PROVIDERS)})",+ ),+ api_key: Optional[str] = typer.Option(+ None,+ "--api-key",+ "-k",+ help="LLM API key",+ ),+ model: Optional[str] = typer.Option(+ None,+ "--model",+ "-m",+ help="LLM model name",+ ),+ base_url: Optional[str] = typer.Option(+ None,+ "--base-url",+ "-u",+ help="Custom API base URL",+ ),+ show: bool = typer.Option(False, "--show", help="Show current LLM configuration"),+ unset: bool = typer.Option(False, "--unset", help="Unset LLM configuration"),+):+ """Configure LLM settings."""+ config = ConfigManager()++ if show:+ cfg = config.get_llm_config()+ table = Table(title="LLM Configuration", show_header=False)+ table.add_column("Key", style="cyan")+ table.add_column("Value", style="green")+ table.add_row("Provider", cfg.provider)+ table.add_row("Model", cfg.model)+ table.add_row("API Key", cfg.api_key[:10] + "..." if cfg.api_key else "(not set)")+ table.add_row("Base URL", cfg.base_url or "(default)")+ console.print(table)+ return++ if unset:+ config.unset_llm()+ console.print("[green]LLM configuration cleared[/green]")+ return++ config.set_llm(+ provider=provider,+ model=model,+ api_key=api_key,+ base_url=base_url,+ )+ console.print("[green]LLM configuration updated[/green]")++[email protected](name="embedder")+def embedder(+ provider: str = typer.Option(+ None,+ "--provider",+ "-p",+ help=f"Embedder provider ({'/'.join(EMBEDDER_PROVIDERS)})",+ ),+ api_key: Optional[str] = typer.Option(+ None,+ "--api-key",+ "-k",+ help="Embedder API key",+ ),+ model: Optional[str] = typer.Option(+ None,+ "--model",+ "-m",+ help="Embedder model name",+ ),+ base_url: Optional[str] = typer.Option(+ None,+ "--base-url",+ "-u",+ help="Custom API base URL",+ ),+ show: bool = typer.Option(False, "--show", help="Show current Embedder configuration"),+ unset: bool = typer.Option(False, "--unset", help="Unset Embedder configuration"),+):+ """Configure Embedder settings."""+ config = ConfigManager()++ if show:+ cfg = config.get_embedder_config()+ table = Table(title="Embedder Configuration", show_header=False)+ table.add_column("Key", style="cyan")+ table.add_column("Value", style="green")+ table.add_row("Provider", cfg.provider)+ table.add_row("Model", cfg.model)+ table.add_row("API Key", cfg.api_key[:10] + "..." if cfg.api_key else "(not set)")+ table.add_row("Base URL", cfg.base_url or "(default)")+ console.print(table)+ return++ if unset:+ config.unset_embedder()+ console.print("[green]Embedder configuration cleared[/green]")+ return++ config.set_embedder(+ provider=provider,+ api_key=api_key,+ model=model,+ base_url=base_url,+ )+ console.print("[green]Embedder configuration updated[/green]")++[email protected](name="init")+def init(+ force: bool = typer.Option(False, "--force", "-f", help="Force overwrite existing config"),+):+ """Initialize configuration interactively."""+ config = ConfigManager()++ console.print("[bold blue]Hyper-Extract Configuration Setup[/bold blue]")+ console.print()++ console.print("[bold]Step 1: LLM Configuration[/bold]")+ provider = console.input(" Provider [openai]: ") or "openai"+ model = console.input(" Model [gpt-4o-mini]: ") or "gpt-4o-mini"+ api_key = console.input(" API Key: ", password=True)+ base_url = console.input(" Base URL (optional): ")++ config.set_llm(+ provider=provider,+ model=model,+ api_key=api_key,+ base_url=base_url or None,+ )++ console.print()++ console.print("[bold]Step 2: Embedder Configuration[/bold]")+ emb_provider = console.input(" Provider [openai]: ") or "openai"+ emb_model = console.input(" Model [text-embedding-3-small]: ") or "text-embedding-3-small"+ emb_api_key = console.input(" API Key: ", password=True)+ emb_base_url = console.input(" Base URL (optional): ")++ config.set_embedder(+ provider=emb_provider,+ model=emb_model,+ api_key=emb_api_key,+ base_url=emb_base_url or None,+ )++ console.print()+ console.print("[bold green]Configuration saved successfully![/bold green]")diff --git a/hyperextract/cli/commands/feed.py b/hyperextract/cli/commands/feed.pynew file mode 100644index 0000000..9f88c68--- /dev/null+++ b/hyperextract/cli/commands/feed.py@@ -0,0 +1,130 @@+"""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++console = Console()++app = typer.Typer(+ name="feed",+ help="Append knowledge to an existing directory",+)+++def create_llm_client(config):+ """Create LLM client from config."""+ llm_config = config.get_llm_config()+ if llm_config.provider == "openai":+ from langchain_openai import ChatOpenAI+ return ChatOpenAI(+ model=llm_config.model,+ api_key=llm_config.api_key,+ base_url=llm_config.base_url or None,+ temperature=0,+ )+ else:+ raise ValueError(f"Unsupported LLM provider: {llm_config.provider}")+++def create_embedder(config):+ """Create embedder from config."""+ emb_config = config.get_embedder_config()+ if emb_config.provider == "openai":+ from langchain_openai import OpenAIEmbeddings+ return OpenAIEmbeddings(+ model=emb_config.model,+ api_key=emb_config.api_key,+ base_url=emb_config.base_url or None,+ )+ else:+ raise ValueError(f"Unsupported Embedder provider: {emb_config.provider}")+++def read_input(input_path: str) -> str:+ """Read input from file or stdin."""+ import sys+ if input_path == "-":+ return sys.stdin.read()+ path = Path(input_path)+ if not path.exists():+ raise FileNotFoundError(f"Input file not found: {input_path}")+ with open(path, "r", encoding="utf-8") as f:+ return f.read()+++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."""+ from ..config import ConfigManager, load_kb_metadata+ from ..templates import resolve_template++ config = ConfigManager()+ valid, msg = config.validate()+ if not valid:+ console.print(f"[red]Error:[/red] {msg}")+ raise typer.Exit(1)++ output_path = Path(output)++ if not output_path.exists():+ console.print(f"[red]Error:[/red] Output directory does not exist: {output}")+ raise typer.Exit(1)++ 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:+ template_class = resolve_template(template, lang)+ console.print(f"[green]Template resolved:[/green] {template_class.__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("Initializing LLM client...", total=None)+ llm = create_llm_client(config)+ embedder = create_embedder(config)++ progress.update(task, description="Reading input...")+ text = read_input(input)+ console.print(f"[dim]Input text: {len(text)} characters[/dim]")++ progress.update(task, description="Loading existing knowledge...")+ kb = template_class(llm_client=llm, embedder=embedder, verbose=True)+ kb.load(output_path)++ 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()+ from ..config import save_kb_metadata+ 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.pynew file mode 100644index 0000000..6fff06f--- /dev/null+++ b/hyperextract/cli/commands/list.py@@ -0,0 +1,152 @@+"""List command for Hyper-Extract CLI."""++from typing import Optional+from rich.console import Console+from rich.table import Table+import typer++console = Console()++app = typer.Typer(+ name="list",+ help="List available resources",+)+++AUTOTYPES = [+ ("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)"),+]++DOMAINS = [+ ("finance", "Financial documents (25+ templates)"),+ ("medicine", "Medical documents (20+ templates)"),+ ("legal", "Legal documents (15+ templates)"),+ ("history", "Historical documents (12+ templates)"),+ ("literature", "Literature works (10+ templates)"),+ ("tcm", "Traditional Chinese Medicine (15+ templates)"),+ ("news", "News articles (12+ templates)"),+ ("biology", "Biological documents (10+ templates)"),+ ("industry", "Industrial documents (18+ templates)"),+ ("agriculture", "Agricultural documents (8+ templates)"),+ ("food", "Food and culinary (8+ templates)"),+ ("general", "General purpose (20+ templates)"),+]++METHODS = [+ ("kg_gen", "Knowledge Graph Generation"),+ ("atom", "Atomic Information Extraction"),+ ("graphiti", "Graphiti - Temporal Knowledge Graph"),+ ("light_rag", "LightRAG - Lightweight Graph RAG"),+ ("hyper_rag", "HyperRAG - Hypergraph RAG"),+ ("cog_rag", "CogRAG - Cognitive RAG"),+ ("hypergraph_rag", "Hypergraph RAG"),+]+++def _get_all_templates() -> list:+ """Get all available templates from hyperextract."""+ templates = []+ try:+ from hyperextract.templates import zh, en+ for name in dir(zh):+ if not name.startswith("_"):+ templates.append((f"zh/{name}", name, "Chinese"))+ for name in dir(en):+ if not name.startswith("_"):+ templates.append((f"en/{name}", name, "English"))+ return templates+ except ImportError:+ return []+++def _filter_items(items: list, search: Optional[str]) -> list:+ """Filter items by search keyword."""+ if not search:+ return items+ search_lower = search.lower()+ return [+ item for item in items+ if any(search_lower in str(field).lower() for field in item)+ ]++[email protected](name="autotype")+def autotype(+ search: Optional[str] = typer.Option(None, "--search", "-s", help="Search autotypes"),+):+ """List all available AutoTypes."""+ items = _filter_items(AUTOTYPES, search)++ 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, desc in items:+ table.add_row(name, desc)++ console.print(table)+ console.print(f"\n[dim]Total: {len(items)} AutoTypes[/dim]")++[email protected](name="template")+def template(+ search: Optional[str] = typer.Option(None, "--search", "-s", help="Search templates"),+):+ """List all available templates."""+ templates = _get_all_templates()+ items = _filter_items(templates, search)++ 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 items:+ table.add_row(tid, name, lang)++ console.print(table)+ console.print(f"\n[dim]Total: {len(items)} 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."""+ items = _filter_items(DOMAINS, search)++ 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]")++[email protected](name="method")+def method(+ search: Optional[str] = typer.Option(None, "--search", "-s", help="Search methods"),+):+ """List all available extraction methods."""+ items = _filter_items(METHODS, search)++ 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.pynew file mode 100644index 0000000..c52a436--- /dev/null+++ b/hyperextract/cli/commands/parse.py@@ -0,0 +1,133 @@+"""Parse command for Hyper-Extract CLI."""++import sys+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 ..config import ConfigManager, save_kb_metadata+from ..templates import resolve_template, get_auto_type_from_template++console = Console()++app = typer.Typer(+ name="parse",+ help="Extract knowledge from text to a new directory",+)+++def create_llm_client(config: ConfigManager):+ """Create LLM client from config."""+ llm_config = config.get_llm_config()+ if llm_config.provider == "openai":+ from langchain_openai import ChatOpenAI+ return ChatOpenAI(+ model=llm_config.model,+ api_key=llm_config.api_key,+ base_url=llm_config.base_url or None,+ temperature=0,+ )+ else:+ raise ValueError(f"Unsupported LLM provider: {llm_config.provider}")+++def create_embedder(config: ConfigManager):+ """Create embedder from config."""+ emb_config = config.get_embedder_config()+ if emb_config.provider == "openai":+ from langchain_openai import OpenAIEmbeddings+ return OpenAIEmbeddings(+ model=emb_config.model,+ api_key=emb_config.api_key,+ base_url=emb_config.base_url or None,+ )+ else:+ raise ValueError(f"Unsupported Embedder provider: {emb_config.provider}")+++def read_input(input_path: str) -> str:+ """Read input from file or stdin."""+ if input_path == "-":+ return sys.stdin.read()+ path = Path(input_path)+ if not path.exists():+ raise FileNotFoundError(f"Input file not found: {input_path}")+ with open(path, "r", encoding="utf-8") as f:+ return f.read()+++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."""+ from ..config import ConfigManager, save_kb_metadata+ from ..templates import resolve_template, get_auto_type_from_template++ config = ConfigManager()+ valid, msg = config.validate()+ if not valid:+ console.print(f"[red]Error:[/red] {msg}")+ raise typer.Exit(1)++ 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_class = resolve_template(template, lang)+ console.print(f"[green]Template resolved:[/green] {template_class.__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("Initializing LLM client...", total=None)+ llm = create_llm_client(config)+ embedder = create_embedder(config)++ progress.update(task, description="Reading input...")+ text = read_input(input)+ console.print(f"[dim]Input text: {len(text)} characters[/dim]")++ progress.update(task, description="Initializing template...")+ kb = template_class(llm_client=llm, embedder=embedder, verbose=True)++ 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_template(template_class),+ "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.pynew file mode 100644index 0000000..dd40151--- /dev/null+++ b/hyperextract/cli/commands/search.py@@ -0,0 +1,100 @@+"""Search command for Hyper-Extract CLI."""++from pathlib import Path++import typer+from rich.console import Console+from rich.progress import Progress, SpinnerColumn, TextColumn++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."""+ from ..config import ConfigManager, load_kb_metadata++ path = Path(kb_path)++ if not path.exists():+ console.print(f"[red]Error:[/red] Knowledge base not found: {kb_path}")+ raise typer.Exit(1)++ if not path.is_dir():+ console.print(f"[red]Error:[/red] Not a directory: {kb_path}")+ raise typer.Exit(1)++ index_dir = path / "index"+ if not index_dir.exists() or not any(index_dir.iterdir()):+ console.print(f"[red]Error:[/red] Index not found. Please run 'he build-index {kb_path}' first.")+ raise typer.Exit(1)++ metadata = load_kb_metadata(path)+ if not metadata:+ console.print(f"[yellow]Warning:[/yellow] No metadata found, assuming knowledge_graph")+ template = "knowledge_graph"+ lang = "zh"+ else:+ template = metadata.get("template", "knowledge_graph")+ lang = metadata.get("lang", "zh")++ config = ConfigManager()+ valid, msg = config.validate()+ if not valid:+ console.print(f"[red]Error:[/red] {msg}")+ raise typer.Exit(1)++ 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:+ from ..templates import resolve_template+ from .feed import create_llm_client, create_embedder++ template_class = resolve_template(template, lang)+ llm = create_llm_client(config)+ embedder = create_embedder(config)++ progress.update(task, description="Loading knowledge base...")+ kb = template_class(llm_client=llm, embedder=embedder)+ 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"):+ import json+ console.print_json(json.dumps(result.model_dump(), indent=2, ensure_ascii=False))+ elif hasattr(result, "dict"):+ import json+ 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.pynew file mode 100644index 0000000..5d181fd--- /dev/null+++ b/hyperextract/cli/commands/show.py@@ -0,0 +1,104 @@+"""Show command for Hyper-Extract CLI."""++from pathlib import Path+import json++import typer+from rich.console import Console++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."""+ from ..config import ConfigManager, load_kb_metadata++ path = Path(kb_path)++ if not path.exists():+ console.print(f"[red]Error:[/red] Knowledge base not found: {kb_path}")+ raise typer.Exit(1)++ if not path.is_dir():+ console.print(f"[red]Error:[/red] Not a directory: {kb_path}")+ raise typer.Exit(1)++ data_file = path / "data.json"+ if not data_file.exists():+ console.print(f"[red]Error:[/red] Not a valid knowledge base: {kb_path} (no data.json)")+ raise typer.Exit(1)++ metadata = load_kb_metadata(path)+ if not metadata:+ console.print(f"[yellow]Warning:[/yellow] No metadata found, assuming knowledge_graph")+ template = "knowledge_graph"+ lang = "zh"+ else:+ template = metadata.get("template", "knowledge_graph")+ lang = metadata.get("lang", "zh")++ console.print(f"[blue]Template:[/blue] {template}")+ console.print(f"[blue]Language:[/blue] {lang}")+ console.print()++ config = ConfigManager()+ valid, msg = config.validate()+ if not valid and build_index:+ console.print(f"[red]Error:[/red] {msg}")+ raise typer.Exit(1)++ with console.status("[bold blue]Loading knowledge base..."):+ try:+ from ..templates import resolve_template++ template_class = resolve_template(template, lang)+ llm = None+ embedder = None++ if build_index:+ valid, msg = config.validate()+ if not valid:+ console.print(f"[yellow]Warning:[/yellow] Skipping index build: {msg}")+ else:+ from .feed import create_llm_client, create_embedder+ llm = create_llm_client(config)+ embedder = create_embedder(config)++ kb = template_class(llm_client=llm, embedder=embedder)+ 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)++ 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.pynew file mode 100644index 0000000..d6cc4d4--- /dev/null+++ b/hyperextract/cli/commands/talk.py@@ -0,0 +1,129 @@+"""Talk command for Hyper-Extract CLI."""++from pathlib import Path+from typing import Optional++import typer+from rich.console import Console+from rich.progress import Progress, SpinnerColumn, TextColumn++console = Console()++app = typer.Typer(+ name="talk",+ help="Chat with knowledge base",+)+++def chat_loop(kb, config):+ """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."""+ from ..config import ConfigManager, load_kb_metadata++ path = Path(kb_path)++ if not path.exists():+ console.print(f"[red]Error:[/red] Knowledge base not found: {kb_path}")+ raise typer.Exit(1)++ if not path.is_dir():+ console.print(f"[red]Error:[/red] Not a directory: {kb_path}")+ raise typer.Exit(1)++ index_dir = path / "index"+ if not index_dir.exists() or not any(index_dir.iterdir()):+ console.print(f"[red]Error:[/red] Index not found. Please run 'he build-index {kb_path}' first.")+ raise typer.Exit(1)++ metadata = load_kb_metadata(path)+ if not metadata:+ console.print(f"[yellow]Warning:[/yellow] No metadata found, assuming knowledge_graph")+ template = "knowledge_graph"+ lang = "zh"+ else:+ template = metadata.get("template", "knowledge_graph")+ lang = metadata.get("lang", "zh")++ config = ConfigManager()+ valid, msg = config.validate()+ if not valid:+ console.print(f"[red]Error:[/red] {msg}")+ raise typer.Exit(1)++ 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:+ from ..templates import resolve_template+ from .feed import create_llm_client, create_embedder++ template_class = resolve_template(template, lang)+ llm = create_llm_client(config)+ embedder = create_embedder(config)++ progress.update(task, description="Loading knowledge base...")+ kb = template_class(llm_client=llm, embedder=embedder)+ kb.load(path)++ except Exception as e:+ console.print(f"[red]Error:[/red] {e}")+ raise typer.Exit(1)++ if interactive:+ chat_loop(kb, config)+ 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.pynew file mode 100644index 0000000..97e19db--- /dev/null+++ b/hyperextract/cli/config.py@@ -0,0 +1,211 @@+"""Configuration management for Hyper-Extract CLI."""++import os+import json+from pathlib import Path+from typing import Optional, Dict, Any+from dataclasses import dataclass+++DEFAULT_CONFIG_DIR = Path.home() / ".he"+DEFAULT_CONFIG_FILE = DEFAULT_CONFIG_DIR / "config.toml"+++LLM_PROVIDERS = ["openai", "anthropic", "google"]+EMBEDDER_PROVIDERS = ["openai", "anthropic", "google"]+++@dataclass+class LLMConfig:+ provider: str = "openai"+ model: str = "gpt-4o-mini"+ api_key: str = ""+ base_url: str = ""++ def to_dict(self) -> Dict[str, Any]:+ return {+ "provider": self.provider,+ "model": self.model,+ "api_key": self.api_key,+ "base_url": self.base_url,+ }++ @classmethod+ def from_dict(cls, data: Dict[str, Any]) -> "LLMConfig":+ return cls(+ provider=data.get("provider", "openai"),+ model=data.get("model", "gpt-4o-mini"),+ api_key=data.get("api_key", ""),+ base_url=data.get("base_url", ""),+ )+++@dataclass+class EmbedderConfig:+ provider: str = "openai"+ model: str = "text-embedding-3-small"+ api_key: str = ""+ base_url: str = ""++ def to_dict(self) -> Dict[str, Any]:+ return {+ "provider": self.provider,+ "model": self.model,+ "api_key": self.api_key,+ "base_url": self.base_url,+ }++ @classmethod+ def from_dict(cls, data: Dict[str, Any]) -> "EmbedderConfig":+ return cls(+ provider=data.get("provider", "openai"),+ model=data.get("model", "text-embedding-3-small"),+ api_key=data.get("api_key", ""),+ base_url=data.get("base_url", ""),+ )+++class ConfigManager:+ """Manages Hyper-Extract CLI configuration."""++ def __init__(self, config_path: Optional[Path] = None):+ self.config_path = config_path or DEFAULT_CONFIG_FILE+ self.llm = LLMConfig()+ self.embedder = EmbedderConfig()+ self._load()++ def _load(self) -> None:+ """Load configuration from file."""+ if not self.config_path.exists():+ return++ try:+ import tomli as tomllib+ except ImportError:+ import tomllib++ with open(self.config_path, "rb") as f:+ data = tomllib.load(f)++ if "llm" in data:+ self.llm = LLMConfig.from_dict(data["llm"])+ if "embedder" in data:+ self.embedder = EmbedderConfig.from_dict(data["embedder"])++ def _save(self) -> None:+ """Save configuration to file."""+ DEFAULT_CONFIG_DIR.mkdir(parents=True, exist_ok=True)++ import tomli as tomllib+ import tomli_w++ data = {+ "llm": self.llm.to_dict(),+ "embedder": self.embedder.to_dict(),+ }++ with open(self.config_path, "wb") as f:+ tomli_w.dump(data, f)++ def get_llm_config(self) -> LLMConfig:+ """Get LLM config with environment variable fallback."""+ config = LLMConfig(+ provider=self.llm.provider,+ model=self.llm.model,+ api_key=self.llm.api_key or os.environ.get("OPENAI_API_KEY", ""),+ base_url=self.llm.base_url or os.environ.get("OPENAI_BASE_URL", ""),+ )+ return config++ def get_embedder_config(self) -> EmbedderConfig:+ """Get Embedder config with environment variable fallback."""+ config = EmbedderConfig(+ provider=self.embedder.provider,+ model=self.embedder.model,+ api_key=self.embedder.api_key or os.environ.get("OPENAI_API_KEY", ""),+ base_url=self.embedder.base_url or os.environ.get("OPENAI_BASE_URL", ""),+ )+ return config++ def set_llm(+ self,+ provider: Optional[str] = None,+ model: Optional[str] = None,+ api_key: Optional[str] = None,+ base_url: Optional[str] = None,+ ) -> None:+ """Set LLM configuration."""+ if provider:+ self.llm.provider = provider+ if model:+ self.llm.model = model+ if api_key is not None:+ self.llm.api_key = api_key+ if base_url is not None:+ self.llm.base_url = base_url+ self._save()++ def set_embedder(+ self,+ provider: Optional[str] = None,+ model: Optional[str] = None,+ api_key: Optional[str] = None,+ base_url: Optional[str] = None,+ ) -> None:+ """Set Embedder configuration."""+ if provider:+ self.embedder.provider = provider+ if model:+ self.embedder.model = model+ if api_key is not None:+ self.embedder.api_key = api_key+ if base_url is not None:+ self.embedder.base_url = base_url+ self._save()++ def unset_llm(self) -> None:+ """Unset LLM configuration."""+ self.llm = LLMConfig()+ self._save()++ def unset_embedder(self) -> None:+ """Unset Embedder configuration."""+ self.embedder = EmbedderConfig()+ self._save()++ def show(self) -> Dict[str, Any]:+ """Show current configuration."""+ return {+ "llm": self.get_llm_config().to_dict(),+ "embedder": self.get_embedder_config().to_dict(),+ }++ def validate(self) -> tuple[bool, str]:+ """Validate configuration."""+ llm_config = self.get_llm_config()+ embedder_config = self.get_embedder_config()++ if not llm_config.api_key:+ return False, "LLM API key is not configured. Run 'he config llm --api-key YOUR_KEY'"++ if not embedder_config.api_key:+ return False, "Embedder API key is not configured. Run 'he config embedder --api-key YOUR_KEY'"++ return True, "Configuration is valid"+++def load_kb_metadata(kb_path: Path) -> Optional[Dict[str, Any]]:+ """Load knowledge base metadata from directory."""+ metadata_path = kb_path / "metadata.json"+ if not metadata_path.exists():+ return None++ 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/templates.py b/hyperextract/cli/templates.pynew file mode 100644index 0000000..c5b6327--- /dev/null+++ b/hyperextract/cli/templates.py@@ -0,0 +1,73 @@+"""Template loading utilities for Hyper-Extract CLI."""++from typing import Type, Optional, Any+import importlib+++TEMPLATE_ALIASES = {+ "kg": "knowledge_graph",+ "graph": "knowledge_graph",+ "list": "keyword_list",+ "entity": "entity_registry",+ "timeline": "chronological_event_chain",+ "risk": "risk_assessment",+ "summary": "encyclopedia_item",+}+++def resolve_template(template_str: str, lang: str = "zh") -> Type:+ """+ Resolve a template string to a template class.++ Args:+ template_str: Template name or alias (e.g., "knowledge_graph", "kg", "zh/finance/risk_assessment")+ lang: Language (zh/en)++ Returns:+ Template class++ Raises:+ ValueError: If template not found+ """+ template_str = template_str.lower()++ if template_str in TEMPLATE_ALIASES:+ template_str = TEMPLATE_ALIASES[template_str]++ parts = template_str.split("/")++ if len(parts) == 1:+ template_name = parts[0]+ module_path = f"hyperextract.templates.{lang}.{template_name}"+ else:+ template_lang = parts[0]+ if template_lang not in ["zh", "en"]:+ template_lang = lang+ domain = parts[1]+ template_name = parts[2] if len(parts) > 2 else parts[1]+ module_path = f"hyperextract.templates.{template_lang}.{domain}.{template_name}"++ try:+ module = importlib.import_module(module_path)+ template_class = getattr(module, template_name)+ return template_class+ except (ImportError, AttributeError) as e:+ raise ValueError(f"Template '{template_str}' not found: {e}")+++def get_template_info(template_class: Type) -> dict:+ """Get template information."""+ return {+ "name": template_class.__name__,+ "module": template_class.__module__,+ }+++def get_auto_type_from_template(template_class: Type) -> str:+ """Infer AutoType from template class."""+ module = template_class.__module__++ if ".templates.zh." in module or ".templates.en." in module:+ return "AutoGraph"++ return "AutoGraph"diff --git a/hyperextract/types/__init__.py b/hyperextract/types/__init__.pyindex db9d7af..21f5eb2 100644--- a/hyperextract/types/__init__.py+++ b/hyperextract/types/__init__.py@@ -46,6 +46,10 @@ These types inject "Observation Context" (Current Time/Location) into the LLM pr* **`AutoSpatioTemporalGraph`**: The combination of both above."""+import os+# Suppress Transformers warnings+os.environ['TRANSFORMERS_NO_ADVISORY_WARNINGS'] = '1'+from .base import BaseAutoTypefrom .model import AutoModelfrom .list import AutoListdiff --git a/pyproject.toml b/pyproject.tomlindex 4a569b7..b70225e 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -4,10 +4,17 @@ version = "0.1.0"description = "An intelligent, LLM-powered knowledge extraction and evolution framework with semantic search capabilities"readme = "README.md"requires-python = ">=3.11"-keywords = ["llm", "knowledge-extraction", "langchain", "faiss", "semantic-search", "pydantic", "nlp", "rag"]-authors = [- { name = "Yifan Feng", email = "[email protected]" }+keywords = [+ "llm",+ "knowledge-extraction",+ "langchain",+ "faiss",+ "semantic-search",+ "pydantic",+ "nlp",+ "rag",]+authors = [{ name = "Yifan Feng", email = "[email protected]" }]license = { text = "Apache-2.0" }classifiers = ["Development Status :: 3 - Alpha",@@ -40,3 +47,13 @@ dev = ["mkdocstrings[python]>=1.0.0","pytest>=9.0.2",]++[project.optional-dependencies]+cli = ["typer[all]>=0.13.0", "rich>=13.7.0", "tomli>=2.0.1", "tomli-w>=1.0.0"]++[project.scripts]+he = "hyperextract.cli:app"++[build-system]+requires = ["hatchling"]+build-backend = "hatchling.build"