Hypergraph is more powerful. Transform unstructured text into structured knowledge with LLMs. Graphs, hypergraphs, and spatio-temporal extractions — with one command.
refactor(template_engine): 简化模板加载逻辑并移除自定义模板支持
yifanfeng97d5b7d51eefa8
diff --git a/examples/template_api_demo.py b/examples/template_api_demo.pyindex 4d124e8..7e45475 100644--- a/examples/template_api_demo.py+++ b/examples/template_api_demo.py@@ -21,53 +21,71 @@ from hyperextract.utils.template_engine import Templatefrom langchain_openai import ChatOpenAI, OpenAIEmbeddings-def main():- llm = ChatOpenAI(model="gpt-4o-mini")- embedder = OpenAIEmbeddings(model="text-embedding-3-small")+TEMPLATE_FILE = Path(__file__).parent.parent / "hyperextract" / "templates" / "presets" / "general" / "life_event_timeline.yaml"+INPUT_FILE = Path(__file__).parent.parent / "tests" / "test_data" / "test_samples" / "zh" / "general" / "biography_scientist.md"++def main():print("=" * 60)print("Template API 测试")print("=" * 60)+ if not TEMPLATE_FILE.exists():+ print(f"❌ 找不到模板文件: {TEMPLATE_FILE}")+ return++ if not INPUT_FILE.exists():+ print(f"❌ 找不到输入文件: {INPUT_FILE}")+ return++ print(f"\n📄 输入文件: {INPUT_FILE.name}")+ print(f"🎯 使用模板: {TEMPLATE_FILE.name}")++ llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)+ embedder = OpenAIEmbeddings(model="text-embedding-3-small")+print("\n[1] Template.list() - 列出所有模板")names = Template.list()print(f"共 {len(names)} 个模板")- print(f"前10个: {names[:10]}")print("\n[2] Template.list_domains() - 列出所有领域")domains = Template.list_domains()print(f"共 {len(domains)} 个领域: {domains}")- print("\n[3] Template.get() - 通过路径获取配置")- config = Template.get("general/knowledge_graph")+ print("\n[3] Template.get() - 获取配置")+ config = Template.get("general/life_event_timeline")if config:print(f"找到: {config.name}, type: {config.type}")else:print("未找到")- print("\n[4] Template.search(type='graph') - 按类型搜索")- results = Template.search(type="graph")- print(f"找到 {len(results)} 个 graph 类型模板")- if results:- print(f"前5个: {[r.name for r in results[:5]]}")-- print("\n[5] Template.search(query='知识图谱') - 模糊搜索")- results = Template.search(query="知识图谱")- print(f"找到 {len(results)} 个匹配模板")- if results:- print(f"前5个: {[r.name for r in results[:5]]}")+ print("\n[4] Template.search(type='list') - 按类型搜索")+ results = Template.search(type="list")+ print(f"找到 {len(results)} 个 list 类型模板")- print("\n[6] Template.create() - 创建实例(通过路径)")- template = Template.create("general/knowledge_graph", llm, embedder)+ print("\n[5] Template.create() - 通过文件路径创建实例")+ template = Template.create(+ str(TEMPLATE_FILE),+ llm_client=llm,+ embedder=embedder,+ )print(f"创建成功: {type(template).__name__}")- print("\n[7] 测试 parse")- test_text = "苹果公司成立于1976年,总部位于加利福尼亚州库比蒂诺。"- try:- result = template.parse(test_text)- print(f"解析结果: {result}")- except Exception as e:- print(f"解析出错(正常,API调用): {e}")+ print("\n[6] 读取输入文件")+ with open(INPUT_FILE, "r", encoding="utf-8") as f:+ content = f.read()+ print(f"读取成功,共 {len(content)} 字符")++ print("\n[7] template.feed_text() - 喂入文本")+ template.feed_text(content)+ print("✅ 知识提取完成")++ print("\n[8] template.build_index() - 构建索引")+ template.build_index()+ print("✅ 索引构建完成")++ print("\n[9] template.show() - 展示结果")+ template.show()print("\n" + "=" * 60)print("测试完成!")diff --git a/hyperextract/templates/customs/.gitkeep b/hyperextract/templates/customs/.gitkeepdeleted file mode 100644index e69de29..0000000diff --git a/hyperextract/utils/template_engine/factory.py b/hyperextract/utils/template_engine/factory.pyindex 8d2ef57..fc2562b 100644--- a/hyperextract/utils/template_engine/factory.py+++ b/hyperextract/utils/template_engine/factory.py@@ -390,16 +390,12 @@ class TemplateFactory:config = load_template(s)case str() as s:config = Gallery.get(s)- if config is None:- config = Gallery.get(f"{language}/{s}")if config is None:raise ValueError(f"Template '{s}' not found")case Path() as p if p.exists():config = load_template(p)- case TemplateCfg() as cfg:- config = cfgcase _:- raise ValueError(f"Invalid source type: {type(source)}")+ raise ValueError(f"Invalid source: must be a template path or file path")template = localize_template(config, language)diff --git a/hyperextract/utils/template_engine/gallery.py b/hyperextract/utils/template_engine/gallery.pyindex 5207b97..43658bb 100644--- a/hyperextract/utils/template_engine/gallery.py+++ b/hyperextract/utils/template_engine/gallery.py@@ -1,9 +1,8 @@"""Template Gallery - Manages discovery and loading of knowledge extraction templates.-Supports auto-loading presets and customs directories.+Auto-loads templates from presets directory."""-import osfrom pathlib import Pathfrom typing import Dict, List, Optional@@ -16,7 +15,7 @@ class Gallery:Usage Examples:from hyperextract.utils.template_engine import Gallery- # Get template (auto-loaded)+ # Get template by pathconfig = Gallery.get("general/knowledge_graph")# List all templates@@ -27,20 +26,12 @@ class Gallery:# Search templatesresults = Gallery.search(type="graph")-- # Add custom template directory (auto-loaded)- Gallery.add_path("/path/to/my/templates")-- # Add single YAML file- Gallery.add("/path/to/my_template.yaml")"""_instance: Optional["Gallery"] = Nonedef __init__(self):self._configs: Dict[str, TemplateCfg] = {}- self._configs_by_type: Dict[str, List[str]] = {}- self._configs_by_tag: Dict[str, List[str]] = {}@classmethoddef get(cls, path: str) -> Optional[TemplateCfg]:@@ -63,36 +54,6 @@ class Gallery:"""return list(cls._instance._configs.keys()) if cls._instance else []- @classmethod- def add_path(cls, path: str) -> None:- """Add a directory of template files.-- Args:- path: Directory path containing YAML template files- """- if not cls._instance:- return- dir_path = Path(path)- if not dir_path.exists() or not dir_path.is_dir():- return- for root, _, files in os.walk(dir_path):- for file in files:- if file.endswith((".yaml", ".yml")):- cls._instance._load_config(Path(root) / file)-- @classmethod- def add(cls, file_path: str) -> None:- """Add a single template file.-- Args:- file_path: Path to YAML template file- """- if not cls._instance:- return- path = Path(file_path)- if path.exists() and path.is_file() and path.suffix in (".yaml", ".yml"):- cls._instance._load_config(path)-@classmethoddef search(cls,@@ -116,24 +77,22 @@ class Gallery:return []results = []-for config in cls._instance._configs.values():- if query:- query_lower = query.lower()- name_match = config.name.lower()- desc_match = getattr(config, "description", None)- desc_value = desc_match.zh if hasattr(desc_match, "zh") else desc_match- if query_lower not in name_match and (- desc_value and query_lower not in str(desc_value).lower()+ q = query.lower() if query else None++ if q and q not in config.name.lower():+ desc = config.description+ if isinstance(desc, str) and q not in desc.lower():+ continue+ if isinstance(desc, dict) and not any(+ q in v.lower() for v in desc.values()):continueif type and config.type != type:continue-if tag and (not config.tags or tag not in config.tags):continue-if language:config_lang = config.languageif isinstance(config_lang, list):@@ -141,7 +100,6 @@ class Gallery:continueelif config_lang != language:continue-results.append(config)return results@@ -164,39 +122,23 @@ class Gallery:def _load_config(self, file_path: Path) -> None:try:config = load_template(file_path)- self._register(config)+ self._configs[config.name] = configexcept Exception as e:print(f"Failed to load config {file_path}: {e}")- def _register(self, config: TemplateCfg) -> None:- self._configs[config.name] = config-- if config.type not in self._configs_by_type:- self._configs_by_type[config.type] = []- if config.name not in self._configs_by_type[config.type]:- self._configs_by_type[config.type].append(config.name)-- if config.tags:- for tag in config.tags:- if tag not in self._configs_by_tag:- self._configs_by_tag[tag] = []- if config.name not in self._configs_by_tag[tag]:- self._configs_by_tag[tag].append(config.name)-def _init_gallery() -> Gallery:gallery = Gallery()Gallery._instance = gallery- base_dir = Path(__file__).parent.parent.parent / "templates"- presets_dir = base_dir / "presets"- customs_dir = base_dir / "customs"+ presets_dir = Path(__file__).parent.parent.parent / "templates" / "presets"if presets_dir.exists():- Gallery.add_path(str(presets_dir))-- if customs_dir.exists():- Gallery.add_path(str(customs_dir))+ for root, _, files in presets_dir.rglob("*"):+ if root.is_dir():+ for file in files:+ if file.suffix in (".yaml", ".yml"):+ gallery._load_config(root / file)return gallerydiff --git a/hyperextract/utils/template_engine/template.py b/hyperextract/utils/template_engine/template.pyindex 872e345..6ad692f 100644--- a/hyperextract/utils/template_engine/template.py+++ b/hyperextract/utils/template_engine/template.py@@ -3,7 +3,7 @@Provides a clean API for searching and creating knowledge extraction templates."""-from typing import List, Optional, Union+from typing import List, Optionalfrom pathlib import Pathfrom langchain_core.language_models import BaseChatModel@@ -19,10 +19,9 @@ class Template:Usage Examples:from hyperextract.utils.template_engine import Template- # Create template instance (3 ways)+ # Create template instance (2 ways)template = Template.create("general/knowledge_graph", llm, emb)template = Template.create("/path/to/template.yaml", llm, emb)- template = Template.create(config, llm, emb)# Get template config by pathconfig = Template.get("general/knowledge_graph")@@ -41,19 +40,16 @@ class Template:@staticmethoddef create(- source: Union[str, Path, TemplateCfg],+ source: str | Path,llm_client: Optional[BaseChatModel] = None,embedder: Optional[Embeddings] = None,- language: str = "zh",+ language: str = "en",**kwargs,):"""Create template instance.Args:- source: Template source - can be:- - str: Template path (e.g., "general/knowledge_graph") or file path- - Path: YAML file path- - TemplateCfg: Template configuration instance+ source: Template path (e.g., "general/knowledge_graph") or file pathllm_client: LLM client (reads from global config if not provided)embedder: Embedder client (reads from global config if not provided)language: Language code (e.g., "zh", "en")@@ -69,9 +65,6 @@ class Template:# By file pathtemplate = Template.create("/path/to/template.yaml", llm, emb)- # By config instance- template = Template.create(config, llm, emb)-# For temporal/spatial templatestemplate = Template.create("finance/financial_data_temporal_graph",