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_engine): 重构提示构建逻辑并优化模板配置

yifanfeng9770425e270811
diff --git a/hyperextract/templates/presets/general/life_event_timeline.yaml b/hyperextract/templates/presets/general/life_event_timeline.yaml
index ae2f88d..587ca89 100644
--- a/hyperextract/templates/presets/general/life_event_timeline.yaml
+++ b/hyperextract/templates/presets/general/life_event_timeline.yaml
@@ -55,16 +55,14 @@ output:
guideline:
target:
zh: '你是一位专业的传记年表专家,请从文本中提取人物、地点、组织等实体以及生活事件。'
- rules:
+ rules_for_entities:
zh:
- '提取所有相关实体'
- '为每个实体指定类型'
- - '仅从提取的实体中创建事件边'
- rules_for_entities:
- zh:
- '人物、地点、组织、物品、概念等实体'
rules_for_relations:
zh:
+ - '仅从提取的实体中创建事件边'
- '事件类型:出生、逝世、教育、工作、成就、交往、旅行、其他'
- '时间信息为可选,仅在文本中明确提及时才提取'
rules_for_time:
diff --git a/hyperextract/templates/presets/legal/adjudication_logic.yaml b/hyperextract/templates/presets/legal/adjudication_logic.yaml
index 2a281ac..d71949e 100644
--- a/hyperextract/templates/presets/legal/adjudication_logic.yaml
+++ b/hyperextract/templates/presets/legal/adjudication_logic.yaml
@@ -69,15 +69,6 @@ output:
guideline:
target:
zh: '你是一位专业的法律工作者,请从裁判文书中提取裁判要素和裁判逻辑。'
- rules:
- zh:
- - '每个节点只能对应一个独立的要素,禁止将多个要素合并为一个节点'
- - '要素名称与原文保持一致'
- - '仅从已提取节点中提取边,不要创建新名称'
- - '超边必须包含 logic_type, participants, reasoning'
- - 'participants 应包含涉及的事实认定、法律依据、裁判结论等要素'
- - '要素类型:事实认定(法院认定的案件事实)、法律依据(适用的法律条文、司法解释等)、裁判结论(判决结果或裁定结果)、主旨(裁判的核心观点或要旨)、证据(用于证明事实的证据材料)、其他(不属于上述类别的要素)'
- - '推理类型:事实认定(从证据推导出事实)、法律适用(将法律适用于事实)、因果认定(认定行为与结果之间的因果关系)、责任认定(认定责任主体和责任大小)、金额计算(计算赔偿金额或标的金额)、其他(不属于上述类别的推理)'
rules_for_entities:
zh:
- '每个节点只能对应一个独立的要素,禁止将多个要素合并为一个节点'
@@ -87,10 +78,12 @@ guideline:
- '为每个要素添加简要描述(1-2句话)'
rules_for_relations:
zh:
- - 'participants 必须严格使用已知裁判要素列表中的名称,不要创建新名称'
+ - '仅从已提取节点中提取边,不要创建新名称'
- '超边必须包含 logic_type, participants, reasoning'
- 'participants 应包含涉及的事实认定、法律依据、裁判结论等要素'
- 'reasoning 描述从事实到结论的逻辑推导过程'
+ - '要素类型:事实认定(法院认定的案件事实)、法律依据(适用的法律条文、司法解释等)、裁判结论(判决结果或裁定结果)、主旨(裁判的核心观点或要旨)、证据(用于证明事实的证据材料)、其他(不属于上述类别的要素)'
+ - '推理类型:事实认定(从证据推导出事实)、法律适用(将法律适用于事实)、因果认定(认定行为与结果之间的因果关系)、责任认定(认定责任主体和责任大小)、金额计算(计算赔偿金额或标的金额)、其他(不属于上述类别的推理)'
identifiers:
entity_id: name
diff --git a/hyperextract/utils/template_engine/__init__.py b/hyperextract/utils/template_engine/__init__.py
index 9c8f7e8..505a9fd 100644
--- a/hyperextract/utils/template_engine/__init__.py
+++ b/hyperextract/utils/template_engine/__init__.py
@@ -7,7 +7,7 @@ Main Components:
- TemplateConfig: Configuration model
- ConfigLoader: Loads YAML configuration
- SchemaBuilder: Dynamically generates Pydantic Schema
-- PromptBuilder: Builds extraction prompts
+- PromptParser: Parses prompts based on template type
- IdentifierResolver: Resolves identifier extractors
"""
@@ -18,7 +18,7 @@ from .builder import (
TemplateConfig,
ConfigLoader,
SchemaBuilder,
- PromptBuilder,
+ PromptParser,
IdentifierResolver,
OptionsBuilder,
)
@@ -34,7 +34,7 @@ __all__ = [
"TemplateConfig",
"ConfigLoader",
"SchemaBuilder",
- "PromptBuilder",
+ "PromptParser",
"IdentifierResolver",
"OptionsBuilder",
"Guideline",
diff --git a/hyperextract/utils/template_engine/builder/__init__.py b/hyperextract/utils/template_engine/builder/__init__.py
index 47b44ca..84861a6 100644
--- a/hyperextract/utils/template_engine/builder/__init__.py
+++ b/hyperextract/utils/template_engine/builder/__init__.py
@@ -1,7 +1,7 @@
"""Builder module - Configuration models, loaders and builders."""
from .schema import SchemaBuilder
-from .prompt import PromptBuilder
+from .prompt import PromptParser
from .identifiers import IdentifierResolver
from .loader import TemplateConfig, ConfigLoader
from .options import (
@@ -15,7 +15,7 @@ __all__ = [
"ConfigLoader",
"Options",
"SchemaBuilder",
- "PromptBuilder",
+ "PromptParser",
"IdentifierResolver",
"OptionsBuilder",
]
diff --git a/hyperextract/utils/template_engine/builder/options.py b/hyperextract/utils/template_engine/builder/options.py
index 9cbbea1..f5f802b 100644
--- a/hyperextract/utils/template_engine/builder/options.py
+++ b/hyperextract/utils/template_engine/builder/options.py
@@ -360,90 +360,6 @@ class OptionsBuilder:
relation_fields_for_search=options.relation_fields_for_search,
)
- @classmethod
- def build_prompts(cls, config, prompt_builder, extraction_mode: str = None):
- """Build prompts based on extraction mode and type."""
- if extraction_mode is None:
- extraction_mode = cls.get_extraction_mode(config)
-
- guideline = config.guideline
- autotype = config.type
-
- if autotype in ("model", "list", "set"):
- return prompt_builder.build_model_prompt(guideline), "", ""
-
- if autotype == "graph":
- if extraction_mode == "one_stage":
- return (
- prompt_builder.build_graph_main_prompt(guideline),
- "",
- "",
- )
- else:
- return (
- "",
- prompt_builder.build_graph_entity_prompt(guideline),
- prompt_builder.build_graph_relation_prompt(guideline),
- )
-
- if autotype in ("hypergraph",):
- if extraction_mode == "one_stage":
- return (
- prompt_builder.build_graph_main_prompt(guideline),
- "",
- "",
- )
- else:
- return (
- "",
- prompt_builder.build_graph_entity_prompt(guideline),
- prompt_builder.build_graph_relation_prompt(guideline),
- )
-
- if autotype == "temporal_graph":
- if extraction_mode == "one_stage":
- return (
- prompt_builder.build_graph_main_prompt(guideline),
- "",
- "",
- )
- else:
- return (
- "",
- prompt_builder.build_temporal_graph_entity_prompt(guideline),
- prompt_builder.build_graph_relation_prompt(guideline),
- )
-
- if autotype == "spatial_graph":
- if extraction_mode == "one_stage":
- return (
- prompt_builder.build_graph_main_prompt(guideline),
- "",
- "",
- )
- else:
- return (
- "",
- prompt_builder.build_spatial_graph_entity_prompt(guideline),
- prompt_builder.build_graph_relation_prompt(guideline),
- )
-
- if autotype == "spatio_temporal_graph":
- if extraction_mode == "one_stage":
- return (
- prompt_builder.build_graph_main_prompt(guideline),
- "",
- "",
- )
- else:
- return (
- "",
- prompt_builder.build_spatio_temporal_graph_entity_prompt(guideline),
- prompt_builder.build_graph_relation_prompt(guideline),
- )
-
- return "", "", ""
-
@classmethod
def validate_model_config(cls, config):
"""Validate AutoModel configuration."""
diff --git a/hyperextract/utils/template_engine/builder/prompt.py b/hyperextract/utils/template_engine/builder/prompt.py
index 4311f08..7d92fcc 100644
--- a/hyperextract/utils/template_engine/builder/prompt.py
+++ b/hyperextract/utils/template_engine/builder/prompt.py
@@ -1,304 +1,107 @@
"""Extraction models and prompt builder."""
-from typing import Dict, List, Optional, Union
-from pydantic import BaseModel
+from typing import Tuple
+from .tools import get_text, TemplateSchema, GuidelineSchema
-class GuidelineTarget(BaseModel):
- """Extraction target with role definition and content description."""
-
- zh: Optional[str] = None
- en: Optional[str] = None
-
- @classmethod
- def from_value(cls, value: Union[str, Dict[str, str], None]) -> Optional["GuidelineTarget"]:
- """Create instance from string or dict."""
- if value is None:
- return None
- if isinstance(value, str):
- return cls(zh=value)
- if isinstance(value, dict):
- return cls(**value)
- return None
-
- def get(self, language: str = "zh") -> str:
- """Get content for specified language."""
- return getattr(self, language) or self.zh or ""
-
-
-class Guideline(BaseModel):
- """Extraction guideline configuration."""
-
- target: Optional[Union[str, List[str], Dict[str, Union[str, List[str]]]]] = None
- rules: Optional[Union[str, List[str], Dict[str, Union[str, List[str]]]]] = None
- rules_for_entities: Optional[Union[str, List[str], Dict[str, Union[str, List[str]]]]] = None
- rules_for_relations: Optional[Union[str, List[str], Dict[str, Union[str, List[str]]]]] = None
- rules_for_time: Optional[Union[str, List[str], Dict[str, Union[str, List[str]]]]] = None
- rules_for_location: Optional[Union[str, List[str], Dict[str, Union[str, List[str]]]]] = None
-
- def get_field(self, field_name: str, language: str = "zh") -> Optional[str]:
- """Get specified field content for specified language."""
- value = getattr(self, field_name, None)
- if value is None:
- return None
- if isinstance(value, str):
- return value
- if isinstance(value, list):
- return "\n".join(f"{i+1}. {item}" for i, item in enumerate(value))
- if isinstance(value, dict):
- dict_value = value.get(language) or value.get("zh")
- if dict_value is None:
- return None
- if isinstance(dict_value, str):
- return dict_value
- if isinstance(dict_value, list):
- return "\n".join(f"{i+1}. {item}" for i, item in enumerate(dict_value))
- return None
+__all__ = [
+ "PromptParser",
+]
LABEL_MAPPING = {
"zh": {
+ "role_and_task": "角色与任务",
"source_text": "源文本",
"known_entities": "已知实体",
"extraction_rules": "提取规则",
- "entity_extraction_rules": "实体提取规则",
- "relation_extraction_rules": "关系提取规则",
+ "entity_rules": "实体提取规则",
+ "relation_rules": "关系提取规则",
"time_rules": "时间规则",
"location_rules": "位置规则",
- "role_and_task": "角色与任务",
},
"en": {
+ "role_and_task": "Role and Task",
"source_text": "Source Text",
"known_entities": "Known Entities",
"extraction_rules": "Extraction Rules",
- "entity_extraction_rules": "Entity Extraction Rules",
- "relation_extraction_rules": "Relation Extraction Rules",
+ "entity_rules": "Entity Extraction Rules",
+ "relation_rules": "Relation Extraction Rules",
"time_rules": "Time Rules",
"location_rules": "Location Rules",
- "role_and_task": "Role and Task",
},
}
-class PromptBuilder:
- """Prompt Builder - Builds extraction prompts for various AutoType."""
-
- def __init__(self, language: str = "zh"):
- """Initialize Prompt Builder."""
- self.language = language
- self.labels = LABEL_MAPPING.get(language, LABEL_MAPPING["zh"])
-
- @staticmethod
- def _get_text(value: Optional[Union[str, List[str], Dict[str, Union[str, List[str]]]]], language: str = "zh") -> Optional[str]:
- """Get multilingual text value, supports list format."""
- if value is None:
- return None
- if isinstance(value, str):
- return value
- if isinstance(value, list):
- return "\n".join(f"{i+1}. {item}" for i, item in enumerate(value))
- if isinstance(value, dict):
- dict_value = value.get(language) or value.get("zh")
- if dict_value is None:
- return None
- if isinstance(dict_value, str):
- return dict_value
- if isinstance(dict_value, list):
- return "\n".join(f"{i+1}. {item}" for i, item in enumerate(dict_value))
- return None
-
- def _add_title(self, text: str, title_key: str) -> str:
- """Add title to text."""
- title = self.labels.get(title_key, title_key)
- return f"### {title}\n{text}"
-
- def build_target(self, guideline: Guideline) -> Optional[str]:
- """Build target field (role definition + extraction content description)."""
- target = guideline.target
- if target is None:
- return None
- return self._get_text(target, self.language)
-
- def build_rules(self, guideline: Guideline) -> Optional[str]:
- """Build rules field (with title)."""
- rules = self._get_text(guideline.rules, self.language)
- if rules:
- return self._add_title(rules, "extraction_rules")
- return None
-
- def build_rules_for_entities(self, guideline: Guideline) -> Optional[str]:
- """Build rules_for_entities field (with title)."""
- rules = self._get_text(guideline.rules_for_entities, self.language)
- if rules:
- return self._add_title(rules, "entity_extraction_rules")
- return None
-
- def build_rules_for_relations(self, guideline: Guideline) -> Optional[str]:
- """Build rules_for_relations field (with title)."""
- rules = self._get_text(guideline.rules_for_relations, self.language)
- if rules:
- return self._add_title(rules, "relation_extraction_rules")
- return None
-
- def build_rules_for_time(self, guideline: Guideline) -> Optional[str]:
- """Build rules_for_time field (with title)."""
- rules = self._get_text(guideline.rules_for_time, self.language)
- if rules:
- return self._add_title(rules, "time_rules")
- return None
-
- def build_rules_for_location(self, guideline: Guideline) -> Optional[str]:
- """Build rules_for_location field (with title)."""
- rules = self._get_text(guideline.rules_for_location, self.language)
- if rules:
- return self._add_title(rules, "location_rules")
- return None
-
- def build_model_prompt(self, guideline: Guideline) -> str:
- """Build Model/List/Set type Prompt."""
- parts = []
-
- target = self.build_target(guideline)
- if target:
- parts.append(target)
-
- rules = self.build_rules(guideline)
- if rules:
- parts.append(rules)
-
- source_label = self.labels.get("source_text", "源文本")
- parts.append(f"## {source_label}:\n{{source_text}}")
-
- return "\n\n".join(parts)
-
- def build_graph_main_prompt(self, guideline: Guideline) -> str:
- """Build Graph type main Prompt (single-stage mode)."""
- parts = []
-
- target = self.build_target(guideline)
- if target:
- parts.append(target)
-
- rules_for_entities = self.build_rules_for_entities(guideline)
- if rules_for_entities:
- parts.append(rules_for_entities)
-
- rules_for_relations = self.build_rules_for_relations(guideline)
- if rules_for_relations:
- parts.append(rules_for_relations)
-
- source_label = self.labels.get("source_text", "源文本")
- parts.append(f"## {source_label}:\n{{source_text}}")
-
+def PromptParser(
+ template: TemplateSchema, language: str = "en"
+) -> Tuple[str, str, str]:
+ """Parse template and return prompts based on autotype.
+
+ Args:
+ template: Template Configuration instance
+ language: Language code (default: "en")
+
+ Returns:
+ tuple: main_prompt or (main_prompt, node_prompt, edge_prompt)
+ - For non-graph types: returns main_prompt
+ - For graph types in two_stage mode: returns (main_prompt, node_prompt, edge_prompt)
+ """
+ labels = LABEL_MAPPING.get(language, LABEL_MAPPING["zh"])
+ autotype = template.type
+ guideline: GuidelineSchema = template.guideline
+
+ prefix_prompt = (
+ f"# {labels.get('role_and_task')}:\n{get_text(guideline.target, language)}"
+ )
+
+ if autotype in ("model", "list", "set"):
+ parts = [prefix_prompt]
+ parts.append(
+ f"## {labels.get('extraction_rules')}:\n{get_text(guideline.rules, language)}"
+ )
+ parts.append(f"## {labels.get('source_text')}:\n{{source_text}}")
return "\n\n".join(parts)
-
- def build_graph_entity_prompt(self, guideline: Guideline) -> str:
- """Build Graph type entity extraction Prompt (two-stage mode)."""
- parts = []
-
- target = self.build_target(guideline)
- if target:
- parts.append(target)
-
- rules_for_entities = self.build_rules_for_entities(guideline)
- if rules_for_entities:
- parts.append(rules_for_entities)
-
- source_label = self.labels.get("source_text", "源文本")
- parts.append(f"## {source_label}:\n{{source_text}}")
-
- return "\n\n".join(parts)
-
- def build_graph_relation_prompt(self, guideline: Guideline) -> str:
- """Build Graph type relation extraction Prompt (two-stage mode)."""
- parts = []
-
- target = self.build_target(guideline)
- if target:
- parts.append(target)
-
- rules_for_relations = self.build_rules_for_relations(guideline)
- if rules_for_relations:
- parts.append(rules_for_relations)
-
- known_label = self.labels.get("known_entities", "已知实体")
- parts.append(f"## {known_label}:\n{{known_nodes}}")
-
- source_label = self.labels.get("source_text", "源文本")
- parts.append(f"## {source_label}:\n{{source_text}}")
-
- return "\n\n".join(parts)
-
- def build_temporal_graph_entity_prompt(self, guideline: Guideline) -> str:
- """Build TemporalGraph entity extraction Prompt."""
- parts = []
-
- target = self.build_target(guideline)
- if target:
- parts.append(target)
-
- rules_for_entities = self.build_rules_for_entities(guideline)
- if rules_for_entities:
- parts.append(rules_for_entities)
-
- rules_for_time = self.build_rules_for_time(guideline)
- if rules_for_time:
- parts.append(rules_for_time)
-
- source_label = self.labels.get("source_text", "源文本")
- parts.append(f"## {source_label}:\n{{source_text}}")
-
- return "\n\n".join(parts)
-
- def build_spatial_graph_entity_prompt(self, guideline: Guideline) -> str:
- """Build SpatialGraph entity extraction Prompt."""
- parts = []
-
- target = self.build_target(guideline)
- if target:
- parts.append(target)
-
- rules_for_entities = self.build_rules_for_entities(guideline)
- if rules_for_entities:
- parts.append(rules_for_entities)
-
- rules_for_location = self.build_rules_for_location(guideline)
- if rules_for_location:
- parts.append(rules_for_location)
-
- source_label = self.labels.get("source_text", "源文本")
- parts.append(f"## {source_label}:\n{{source_text}}")
-
- return "\n\n".join(parts)
-
- def build_spatio_temporal_graph_entity_prompt(self, guideline: Guideline) -> str:
- """Build SpatioTemporalGraph entity extraction Prompt."""
- parts = []
-
- target = self.build_target(guideline)
- if target:
- parts.append(target)
-
- rules_for_entities = self.build_rules_for_entities(guideline)
- if rules_for_entities:
- parts.append(rules_for_entities)
-
- rules_for_time = self.build_rules_for_time(guideline)
- if rules_for_time:
- parts.append(rules_for_time)
-
- rules_for_location = self.build_rules_for_location(guideline)
- if rules_for_location:
- parts.append(rules_for_location)
-
- source_label = self.labels.get("source_text", "源文本")
- parts.append(f"## {source_label}:\n{{source_text}}")
-
- return "\n\n".join(parts)
-
-
-__all__ = [
- "GuidelineTarget",
- "Guideline",
- "PromptBuilder",
-]
+ else:
+ # main prompt
+ main_parts = [prefix_prompt]
+ main_parts.append(
+ f"## {labels.get('entity_rules')}:\n{get_text(guideline.rules, language)}"
+ )
+ main_parts.append(
+ f"## {labels.get('relation_rules')}:\n{get_text(guideline.rules_for_relations, language)}"
+ )
+ if autotype in ("temporal_graph", "spatio_temporal_graph"):
+ main_parts.append(
+ f"## {labels.get('time_rules')}:\n{get_text(guideline.rules_for_time, language)}"
+ )
+ if autotype in ("spatial_graph", "spatio_temporal_graph"):
+ main_parts.append(
+ f"## {labels.get('location_rules')}:\n{get_text(guideline.rules_for_location, language)}"
+ )
+ main_parts.append(f"## {labels.get('source_text')}:\n{{source_text}}")
+
+ # node prompt
+ node_parts = [prefix_prompt]
+ node_parts.append(
+ f"## {labels.get('entity_rules')}:\n{get_text(guideline.rules, language)}"
+ )
+ node_parts.append(f"## {labels.get('source_text')}:\n{{source_text}}")
+
+ # edge prompt
+ edge_parts = [prefix_prompt]
+ edge_parts.append(
+ f"## {labels.get('relation_rules')}:\n{get_text(guideline.rules_for_relations, language)}"
+ )
+ if autotype in ("temporal_graph", "spatio_temporal_graph"):
+ edge_parts.append(
+ f"## {labels.get('time_rules')}:\n{get_text(guideline.rules_for_time, language)}"
+ )
+ if autotype in ("spatial_graph", "spatio_temporal_graph"):
+ edge_parts.append(
+ f"## {labels.get('location_rules')}:\n{get_text(guideline.rules_for_location, language)}"
+ )
+ edge_parts.append(f"## {labels.get('known_entities')}:\n{{known_nodes}}")
+ edge_parts.append(f"## {labels.get('source_text')}:\n{{source_text}}")
+
+ return "\n\n".join(main_parts), "\n\n".join(node_parts), "\n\n".join(edge_parts)
diff --git a/hyperextract/utils/template_engine/builder/tools.py b/hyperextract/utils/template_engine/builder/tools.py
new file mode 100644
index 0000000..37a225a
--- /dev/null
+++ b/hyperextract/utils/template_engine/builder/tools.py
@@ -0,0 +1,172 @@
+"""Data model summary - convenient for code hints and intuitive viewing"""
+
+from typing import List, Optional, Union, Dict, Any
+from pydantic import BaseModel
+
+
+class GuidelineSchema(BaseModel):
+ """Extraction guideline configuration"""
+
+ # [all] AutoModel, AutoList, AutoSet, AutoGraph, AutoHypergraph, AutoTemporalGraph, AutoSpatialGraph, AutoSpatioTemporalGraph
+ target: Optional[Union[str, List[str], Dict[str, Union[str, List[str]]]]] = None
+
+ # [non-graph] AutoModel, AutoList, AutoSet
+ rules: Optional[Union[str, List[str], Dict[str, Union[str, List[str]]]]] = None
+
+ # [graph] AutoGraph, AutoHypergraph, AutoTemporalGraph, AutoSpatialGraph, AutoSpatioTemporalGraph
+ rules_for_entities: Optional[
+ Union[str, List[str], Dict[str, Union[str, List[str]]]]
+ ] = None
+
+ # [graph] AutoGraph, AutoHypergraph, AutoTemporalGraph, AutoSpatialGraph, AutoSpatioTemporalGraph
+ rules_for_relations: Optional[
+ Union[str, List[str], Dict[str, Union[str, List[str]]]]
+ ] = None
+
+ # [temporal] AutoTemporalGraph, AutoSpatioTemporalGraph
+ rules_for_time: Optional[
+ Union[str, List[str], Dict[str, Union[str, List[str]]]]
+ ] = None
+
+ # [spatial] AutoSpatialGraph, AutoSpatioTemporalGraph
+ rules_for_location: Optional[
+ Union[str, List[str], Dict[str, Union[str, List[str]]]]
+ ] = None
+
+
+class FieldSchema(BaseModel):
+ """Field definition"""
+
+ # [all]
+ name: str
+ type: str
+ description: Optional[str] = None
+ required: bool = False
+ default: Optional[Any] = None
+
+
+class OutputSchema(BaseModel):
+ """Output definition"""
+
+ description: Optional[str] = None
+
+ # [non-graph] AutoModel, AutoList, AutoSet
+ fields: Optional[List[FieldSchema]] = None
+
+ # [graph] AutoGraph, AutoHypergraph, AutoTemporalGraph, AutoSpatialGraph, AutoSpatioTemporalGraph
+ entities: Optional[List[FieldSchema]] = None
+
+ # [graph] AutoGraph, AutoHypergraph, AutoTemporalGraph, AutoSpatialGraph, AutoSpatioTemporalGraph
+ relations: Optional[List[FieldSchema]] = None
+
+
+class OptionsSchema(BaseModel):
+ """Runtime options"""
+
+ # [non-graph] AutoModel, AutoSet
+ merge_strategy: Optional[str] = None
+
+ # [graph] AutoGraph, AutoHypergraph, AutoTemporalGraph, AutoSpatialGraph, AutoSpatioTemporalGraph
+ entity_merge_strategy: Optional[str] = None
+
+ # [graph] AutoGraph, AutoHypergraph, AutoTemporalGraph, AutoSpatialGraph, AutoSpatioTemporalGraph
+ relation_merge_strategy: Optional[str] = None
+
+ extraction_mode: Optional[str] = None
+
+ # [temporal] AutoTemporalGraph, AutoSpatioTemporalGraph
+ observation_time: Optional[str] = None
+
+ # [spatial] AutoSpatialGraph, AutoSpatioTemporalGraph
+ observation_location: Optional[str] = None
+
+ # [all]
+ chunk_size: int = 2048
+ chunk_overlap: int = 256
+ max_workers: int = 10
+ verbose: bool = False
+ fields_for_search: Optional[List[str]] = None
+ entity_fields_for_search: Optional[List[str]] = None
+ relation_fields_for_search: Optional[List[str]] = None
+
+
+class DisplaySchema(BaseModel):
+ """Display configuration"""
+
+ # [non-graph] AutoModel, AutoList, AutoSet
+ label: Optional[str] = None
+
+ # [graph] AutoGraph, AutoHypergraph, AutoTemporalGraph, AutoSpatialGraph, AutoSpatioTemporalGraph
+ entity_label: Optional[str] = None
+
+ # [graph] AutoGraph, AutoHypergraph, AutoTemporalGraph, AutoSpatialGraph, AutoSpatioTemporalGraph
+ relation_label: Optional[str] = None
+
+
+class IdentifiersSchema(BaseModel):
+ """Identifier configuration"""
+
+ # [non-graph] AutoSet
+ item_id: Optional[str] = None
+
+ # [graph] AutoGraph, AutoHypergraph, AutoTemporalGraph, AutoSpatialGraph, AutoSpatioTemporalGraph
+ entity_id: Optional[str] = None
+
+ # [graph] AutoGraph, AutoHypergraph, AutoTemporalGraph, AutoSpatialGraph, AutoSpatioTemporalGraph
+ relation_id: Optional[str] = None
+
+ # [graph] AutoGraph, AutoHypergraph, AutoTemporalGraph, AutoSpatialGraph, AutoSpatioTemporalGraph
+ relation_members: Optional[Dict[str, str]] = None
+
+ # [temporal] AutoTemporalGraph, AutoSpatioTemporalGraph
+ time_field: Optional[str] = None
+
+ # [spatial] AutoSpatialGraph, AutoSpatioTemporalGraph
+ location_field: Optional[str] = None
+
+
+class TemplateSchema(BaseModel):
+ """Template configuration - corresponds to YAML configuration file"""
+
+ language: str | List[str] = "en"
+ name: str
+ type: str
+ tags: List[str]
+ description: Optional[str] = None
+ output: Optional[OutputSchema] = None
+ guideline: Optional[GuidelineSchema] = None
+ identifiers: Optional[IdentifiersSchema] = None
+ options: Optional[OptionsSchema] = None
+ display: Optional[DisplaySchema] = None
+
+
+def get_text(
+ value: Optional[Union[str, List[str], Dict[str, Union[str, List[str]]]]],
+ language: Optional[str] = None,
+) -> Optional[str]:
+ """Get multilingual text value, supports list format."""
+ if value is None:
+ return None
+ elif isinstance(value, str):
+ return value
+ elif isinstance(value, list):
+ return "\n".join(f"{i + 1}. {item}" for i, item in enumerate(value))
+ else:
+ # multi-language dict
+ dict_value = value.get(language)
+ if isinstance(dict_value, str):
+ return dict_value
+ if isinstance(dict_value, list):
+ return "\n".join(f"{i + 1}. {item}" for i, item in enumerate(dict_value))
+
+
+__all__ = [
+ "get_text",
+ "GuidelineSchema",
+ "FieldSchema",
+ "OutputSchema",
+ "OptionsSchema",
+ "DisplaySchema",
+ "IdentifiersSchema",
+ "TemplateSchema",
+]
diff --git a/hyperextract/utils/template_engine/factory.py b/hyperextract/utils/template_engine/factory.py
index ba86fa4..6dba7e9 100644
--- a/hyperextract/utils/template_engine/factory.py
+++ b/hyperextract/utils/template_engine/factory.py
@@ -10,10 +10,10 @@ from langchain_core.embeddings import Embeddings
from .builder import (
TemplateConfig,
- PromptBuilder,
OptionsBuilder,
Options,
)
+from .builder.prompt import PromptParser
if TYPE_CHECKING:
@@ -86,8 +86,7 @@ class TemplateFactory:
schema_dict = OptionsBuilder.validate_model_config(config)
schema_class = schema_dict.get("schema")
- prompt_builder = PromptBuilder(language)
- prompt = prompt_builder.build_model_prompt(config.guideline)
+ prompt, _, _ = PromptParser(config, language)
options = OptionsBuilder.build_model_options(config, language)
@@ -120,8 +119,7 @@ class TemplateFactory:
schema_dict = OptionsBuilder.validate_list_config(config)
item_schema_class = schema_dict.get("item_schema")
- prompt_builder = PromptBuilder(language)
- prompt = prompt_builder.build_model_prompt(config.guideline)
+ prompt, _, _ = PromptParser(config, language)
options = OptionsBuilder.build_list_options(config)
@@ -149,8 +147,7 @@ class TemplateFactory:
item_schema_class = schema_dict.get("item_schema")
item_id_extractor = identifiers.get("item_id_extractor")
- prompt_builder = PromptBuilder(language)
- prompt = prompt_builder.build_model_prompt(config.guideline)
+ prompt, _, _ = PromptParser(config, language)
options = OptionsBuilder.build_set_options(config, language)
@@ -188,14 +185,9 @@ class TemplateFactory:
relation_key_extractor = identifiers.get("relation_key_extractor")
entities_in_relation_extractor = identifiers.get("entities_in_relation_extractor")
- prompt_builder = PromptBuilder(language)
options = OptionsBuilder.build_graph_options(config, language)
- prompt, prompt_for_entity_extraction, prompt_for_relation_extraction = (
- OptionsBuilder.build_prompts(
- config, prompt_builder, options.extraction_mode
- )
- )
+ prompt, prompt_for_entity_extraction, prompt_for_relation_extraction = PromptParser(config, language)
return AutoGraph(
entity_schema=entity_schema_class,
@@ -253,14 +245,9 @@ class TemplateFactory:
entities_in_relation_extractor = identifiers.get("entities_in_relation_extractor")
time_in_relation_extractor = identifiers.get("time_in_relation_extractor")
- prompt_builder = PromptBuilder(language)
options = OptionsBuilder.build_temporal_graph_options(config, language)
- prompt, prompt_for_entity_extraction, prompt_for_relation_extraction = (
- OptionsBuilder.build_prompts(
- config, prompt_builder, options.extraction_mode
- )
- )
+ prompt, prompt_for_entity_extraction, prompt_for_relation_extraction = PromptParser(config, language)
return AutoTemporalGraph(
node_schema=entity_schema_class,
@@ -314,14 +301,9 @@ class TemplateFactory:
entities_in_relation_extractor = identifiers.get("entities_in_relation_extractor")
location_in_relation_extractor = identifiers.get("location_in_relation_extractor")
- prompt_builder = PromptBuilder(language)
options = OptionsBuilder.build_spatial_graph_options(config, language)
- prompt, prompt_for_entity_extraction, prompt_for_relation_extraction = (
- OptionsBuilder.build_prompts(
- config, prompt_builder, options.extraction_mode
- )
- )
+ prompt, prompt_for_entity_extraction, prompt_for_relation_extraction = PromptParser(config, language)
return AutoSpatialGraph(
entity_schema=entity_schema_class,
@@ -376,16 +358,11 @@ class TemplateFactory:
time_in_relation_extractor = identifiers.get("time_in_relation_extractor")
location_in_relation_extractor = identifiers.get("location_in_relation_extractor")
- prompt_builder = PromptBuilder(language)
options = OptionsBuilder.build_spatio_temporal_graph_options(
config, language
)
- prompt, prompt_for_entity_extraction, prompt_for_relation_extraction = (
- OptionsBuilder.build_prompts(
- config, prompt_builder, options.extraction_mode
- )
- )
+ prompt, prompt_for_entity_extraction, prompt_for_relation_extraction = PromptParser(config, language)
return AutoSpatioTemporalGraph(
entity_schema=entity_schema_class,