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(template): 统一模板命名规范并优化模板引擎

yifanfeng973beb32d42d29
diff --git a/examples/template_api_demo.py b/examples/template_api_demo.py
index 6f575f6..c705711 100644
--- a/examples/template_api_demo.py
+++ b/examples/template_api_demo.py
@@ -21,7 +21,7 @@ from hyperextract.utils.template_engine import Template
from langchain_openai import ChatOpenAI, OpenAIEmbeddings
-INPUT_FILE = Path(__file__).parent.parent / "tests" / "test_data" / "test_samples" / "zh" / "general" / "biography_scientist.md"
+INPUT_FILE = Path(__file__).parent.parent / "tests" / "test_data" / "templates" / "biography_scientist.md"
def main():
@@ -63,6 +63,7 @@ def main():
'life_event_timeline',
llm_client=llm,
embedder=embedder,
+ language="zh",
)
print(f"创建成功: {type(template).__name__}")
diff --git a/hyperextract/templates/presets/general/life_event_timeline.yaml b/hyperextract/templates/presets/general/life_event_timeline.yaml
index 15eee61..77264a0 100644
--- a/hyperextract/templates/presets/general/life_event_timeline.yaml
+++ b/hyperextract/templates/presets/general/life_event_timeline.yaml
@@ -1,5 +1,5 @@
language: zh
-name: LifeEventTimeline
+name: life_event_timeline
type: temporal_graph
tags: [general]
description:
@@ -10,7 +10,7 @@ output:
zh: '包含实体和事件关系的时间线'
entities:
description:
- zh: '提取的实体列表'
+ zh: '提取的实体'
fields:
- name: name
type: str
@@ -27,7 +27,7 @@ output:
required: false
relations:
description:
- zh: '提取的事件关系列表'
+ zh: '提取的事件关系'
fields:
- name: source
type: str
diff --git a/hyperextract/templates/presets/legal/adjudication_logic.yaml b/hyperextract/templates/presets/legal/adjudication_logic.yaml
index e88e41d..abf010d 100644
--- a/hyperextract/templates/presets/legal/adjudication_logic.yaml
+++ b/hyperextract/templates/presets/legal/adjudication_logic.yaml
@@ -1,8 +1,7 @@
language: zh
-name: AdjudicationLogic
+name: adjudication_logic
type: hypergraph
-tags:
-- legal
+tags: [legal]
description:
zh: '裁判逻辑超图 - 建模法官的推理过程'
@@ -42,7 +41,7 @@ output:
required: true
default: ''
- name: participants
- type: list
+ type: list[str]
description:
zh: '参与要素名称列表,必须使用已提取节点的 name'
required: true
diff --git "a/hyperextract/templates/\347\237\245\350\257\206\346\250\241\346\235\277\350\256\276\350\256\241\346\214\207\345\215\227.md" "b/hyperextract/templates/\347\237\245\350\257\206\346\250\241\346\235\277\350\256\276\350\256\241\346\214\207\345\215\227.md"
index e1f08f8..c492a3a 100644
--- "a/hyperextract/templates/\347\237\245\350\257\206\346\250\241\346\235\277\350\256\276\350\256\241\346\214\207\345\215\227.md"
+++ "b/hyperextract/templates/\347\237\245\350\257\206\346\250\241\346\235\277\350\256\276\350\256\241\346\214\207\345\215\227.md"
@@ -767,7 +767,7 @@ options:
### 6.1 命名规范
-- **name**: 使用 PascalCase,如 `EarningsCallSummary`
+- **name**: 使用 snake\_case,如 `earnings_call_summary`
- **tags**: 使用小写,如 `[finance]`, `[medicine]`
- **字段名**: 使用 snake\_case,如 `company_name`, `fiscal_period`
- **关系类型**: 使用中文,如 `拥有`、`影响`
diff --git a/hyperextract/utils/template_engine/factory.py b/hyperextract/utils/template_engine/factory.py
index aec9434..c0e0f6b 100644
--- a/hyperextract/utils/template_engine/factory.py
+++ b/hyperextract/utils/template_engine/factory.py
@@ -201,7 +201,7 @@ class TemplateFactory:
) -> "AutoTemporalGraph":
from hyperextract.types import AutoTemporalGraph
- entity_schema, relation_schema = parse_output(config.output)
+ entity_schema, relation_schema = parse_output(config.output, config.type)
(
entity_key_extractor,
relation_key_extractor,
@@ -386,7 +386,7 @@ class TemplateFactory:
from .parsers import load_template
match source:
- case str() as s if s.endswith((".yaml", ".yml")) or Path(s).exists():
+ case str() as s if s.endswith(".yaml") or Path(s).exists():
config = load_template(s)
case str() as s:
config = Gallery.get(s)
diff --git a/hyperextract/utils/template_engine/gallery.py b/hyperextract/utils/template_engine/gallery.py
index 3e519ef..e0aa47d 100644
--- a/hyperextract/utils/template_engine/gallery.py
+++ b/hyperextract/utils/template_engine/gallery.py
@@ -126,10 +126,12 @@ class Gallery:
domains.append(item.name)
return sorted(domains)
- def _load_config(self, file_path: Path) -> None:
+ def _load_config(self, file_path: Path, presets_dir: Path) -> None:
try:
config = load_template(file_path)
- self._configs[config.name] = config
+ domain = file_path.parent.relative_to(presets_dir).parts[0]
+ key = f"{domain}/{config.name}"
+ self._configs[key] = config
except Exception as e:
print(f"Failed to load config {file_path}: {e}")
@@ -141,11 +143,8 @@ def _init_gallery() -> Gallery:
presets_dir = Path(__file__).parent.parent.parent / "templates" / "presets"
if presets_dir.exists():
- 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)
+ for file_path in presets_dir.rglob("*.yaml"):
+ gallery._load_config(file_path, presets_dir)
return gallery
diff --git a/hyperextract/utils/template_engine/parsers/loader.py b/hyperextract/utils/template_engine/parsers/loader.py
index 717becd..1b76dd8 100644
--- a/hyperextract/utils/template_engine/parsers/loader.py
+++ b/hyperextract/utils/template_engine/parsers/loader.py
@@ -86,7 +86,7 @@ def _localize_naive_output(
def _localize_output(
output: NaiveOutputSchema | GraphOutputSchema,
language: str,
- autotype: VALID_AUTOTYPES | None = None,
+ autotype: VALID_AUTOTYPES,
) -> NaiveOutputSchema | GraphOutputSchema:
"""Localize output configuration."""
if autotype in ("model", "list", "set"):
@@ -101,7 +101,7 @@ def _localize_output(
def _localize_guideline(
guideline: NaiveGuidelineSchema | GraphGuidelineSchema,
language: str,
- autotype: VALID_AUTOTYPES | None = None,
+ autotype: VALID_AUTOTYPES,
) -> NaiveGuidelineSchema | GraphGuidelineSchema:
"""Localize guideline configuration."""
if autotype in ("model", "list", "set"):
@@ -144,7 +144,7 @@ def localize_template(config: TemplateCfg, language: str) -> TemplateCfg:
type=config.type,
tags=config.tags,
description=_localize_data(config.description, language),
- output=_localize_output(config.output, language),
+ output=_localize_output(config.output, language, config.type),
guideline=_localize_guideline(config.guideline, language, config.type),
identifiers=config.identifiers,
options=config.options,
diff --git a/hyperextract/utils/template_engine/parsers/output.py b/hyperextract/utils/template_engine/parsers/output.py
index 73bcc58..f2c5730 100644
--- a/hyperextract/utils/template_engine/parsers/output.py
+++ b/hyperextract/utils/template_engine/parsers/output.py
@@ -38,25 +38,22 @@ def build_naive_schema(
def build_graph_schema(
- name: str,
entity: NaiveOutputSchema,
relation: NaiveOutputSchema,
description: str,
-) -> BaseModel:
+) -> tuple[BaseModel, BaseModel]:
"""Build Pydantic schema for graph types (graph, hypergraph, temporal_graph, spatial_graph, spatio_temporal_graph)."""
- schema_fields = {
- "entities": build_naive_schema(
- entity.name,
- entity.fields,
- entity.description,
- ),
- "relations": build_naive_schema(
- relation.name,
- relation.fields,
- relation.description,
- ),
- }
- return create_model(name, __doc__=description, **schema_fields)
+ entity_schema = build_naive_schema(
+ "NodeSchema",
+ entity.fields,
+ entity.description,
+ )
+ relation_schema = build_naive_schema(
+ "EdgeSchema",
+ relation.fields,
+ relation.description,
+ )
+ return entity_schema, relation_schema
def parse_output(
@@ -71,13 +68,12 @@ def parse_output(
if autotype in ("model", "list", "set"):
return build_naive_schema(
- output.name,
+ "DataSchema",
output.fields,
output.description,
)
else:
return build_graph_schema(
- output.name,
output.entities,
output.relations,
output.description,