Skip to content

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

fix(template): 修复模板路径处理逻辑并改进错误提示

yifanfeng97c7432f9aeb6f
diff --git a/examples/template_api_demo.py b/examples/template_api_demo.py
index 7e45475..6f575f6 100644
--- a/examples/template_api_demo.py
+++ b/examples/template_api_demo.py
@@ -21,7 +21,6 @@ from hyperextract.utils.template_engine import Template
from langchain_openai import ChatOpenAI, OpenAIEmbeddings
-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"
@@ -30,16 +29,12 @@ def main():
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}")
+ print(f"🎯 使用模板: general/life_event_timeline")
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)
embedder = OpenAIEmbeddings(model="text-embedding-3-small")
@@ -65,7 +60,7 @@ def main():
print("\n[5] Template.create() - 通过文件路径创建实例")
template = Template.create(
- str(TEMPLATE_FILE),
+ 'life_event_timeline',
llm_client=llm,
embedder=embedder,
)
diff --git a/hyperextract/utils/template_engine/factory.py b/hyperextract/utils/template_engine/factory.py
index fc2562b..aec9434 100644
--- a/hyperextract/utils/template_engine/factory.py
+++ b/hyperextract/utils/template_engine/factory.py
@@ -386,16 +386,17 @@ class TemplateFactory:
from .parsers import load_template
match source:
- case str() as s if Path(s).exists():
+ case str() as s if s.endswith((".yaml", ".yml")) or Path(s).exists():
config = load_template(s)
case str() as s:
config = Gallery.get(s)
- if config is None:
- raise ValueError(f"Template '{s}' not found")
case Path() as p if p.exists():
config = load_template(p)
case _:
- raise ValueError(f"Invalid source: must be a template path or file path")
+ raise ValueError("Invalid source: must be a template path or file path")
+
+ if config is None:
+ raise ValueError(f"Template not found: {source}")
template = localize_template(config, language)
diff --git a/hyperextract/utils/template_engine/gallery.py b/hyperextract/utils/template_engine/gallery.py
index 43658bb..3e519ef 100644
--- a/hyperextract/utils/template_engine/gallery.py
+++ b/hyperextract/utils/template_engine/gallery.py
@@ -38,12 +38,19 @@ class Gallery:
"""Get template configuration by path.
Args:
- path: Template path (e.g., "general/knowledge_graph")
+ path: Template path
+ If no domain is specified, "general/" is assumed.
+ Only templates in the "general/" domain are supported.
+ Other domains are not supported.
+ (e.g., "general/knowledge_graph" or "knowledge_graph")
Returns:
TemplateCfg or None if not found
"""
- return cls._instance._configs.get(path) if cls._instance else None
+ if "/" in path:
+ return cls._instance._configs.get(path)
+
+ return cls._instance._configs.get(f"general/{path}")
@classmethod
def list(cls) -> List[str]: