跳转至

模板 API

hyperextract.Template

Template API - Unified interface for template operations.

Usage Examples

from hyperextract.utils.template_engine import Template

Create template instance (2 ways)

template = Template.create("general/graph", llm, emb) template = Template.create("/path/to/template.yaml", llm, emb)

Get template config by path

config = Template.get("general/graph")

List all templates (returns Dict[str, TemplateCfg])

all_templates = Template.list()

List templates with filters

graph_templates = Template.list(filter_by_type="graph") zh_templates = Template.list(filter_by_language="zh")

Functions

create(source: str, language: Optional[str] = None, llm_client: Optional[BaseChatModel] = None, embedder: Optional[Embeddings] = None, **kwargs: Any) -> BaseAutoType staticmethod

Create template instance.

Parameters:

Name Type Description Default
source str

Template path (e.g., "general/graph", "method/light_rag") or file path

required
language Optional[str]

Language code (e.g., "zh", "en") - Required for knowledge templates - Ignored for method templates (always uses "en")

None
llm_client Optional[BaseChatModel]

LLM client (reads from global config if not provided)

None
embedder Optional[Embeddings]

Embedder client (reads from global config if not provided)

None
**kwargs Any

Additional parameters

{}

Returns:

Type Description
BaseAutoType

AutoType instance

Examples:

Knowledge template (language required)

template = Template.create("general/graph", "zh", llm, emb)

Method template (language ignored, always "en")

template = Template.create("method/light_rag", llm_client=llm, embedder=embedder)

By file path (language required)

template = Template.create("/path/to/template.yaml", "zh", llm, emb)

For temporal/spatial templates

template = Template.create( "finance/event_timeline", "zh", llm, emb, observation_time="2024-06-15" )

get(path: str) -> Optional[TemplateCfg] staticmethod

Get template configuration by path.

Parameters:

Name Type Description Default
path str

Template path (e.g., "general/graph" or "method/light_rag")

required

Returns:

Type Description
Optional[TemplateCfg]

TemplateCfg or None if not found

list(filter_by_query: Optional[str] = None, filter_by_type: Optional[str] = None, filter_by_tag: Optional[str] = None, filter_by_language: Optional[str] = None, include_methods: bool = True) -> Dict[str, TemplateCfg] staticmethod

List templates with optional filters.

Parameters:

Name Type Description Default
filter_by_query Optional[str]

Search in template name/description

None
filter_by_type Optional[str]

Filter by autotype (e.g., "graph", "list", "model")

None
filter_by_tag Optional[str]

Filter by tag

None
filter_by_language Optional[str]

Filter by language (e.g., "zh", "en")

None
include_methods bool

Whether to include method templates (default: True)

True

Returns:

Type Description
Dict[str, TemplateCfg]

Dict mapping template name to TemplateCfg