架构¶
深入了解 Hyper-Extract 的系统设计和数据流。
系统概览¶
graph LR
A[Input Text] --> B[Text Processor]
B --> C[LLM Extractor]
C --> D[Merger]
D --> E[Auto-Type Instance]
E --> F[Operations]
subgraph "Text Processor"
B1[Chunking] --> B2[Parallel Processing]
end
subgraph "LLM Extractor"
C1[Prompt] --> C2[LLM Call]
C2 --> C3[Structured Output]
end
subgraph "Operations"
F1[Search]
F2[Chat]
F3[Visualize]
F4[Save/Load]
end
数据流¶
1. 输入处理¶
# 输入:原始文本
text = "苏轼是北宋著名文学家..."
# 如果 text > chunk_size,拆分为块
chunks = [
"苏轼是北宋著名文学家...",
"他创作了《赤壁赋》...",
# ...
]
分块策略: - 默认大小:2048 字符 - 重叠:256 字符 - 分隔符:段落、句子、单词
2. 提取¶
流程: 1. 使用块格式化提示 2. 使用结构化输出调用 LLM 3. 将响应解析为 Pydantic 模型
3. 合并¶
合并操作: - 实体去重 - 组合关系 - 解决冲突
4. 结果¶
组件详情¶
模板引擎¶
组件: - Gallery — 模板发现和列表 - Parser — YAML 解析和验证 - Factory — 实例创建
Auto-Type 基类¶
class BaseAutoType:
# 核心功能
def parse(text) -> AutoType
def feed_text(text) -> AutoType
# 查询
def build_index()
def search(query) -> List[Item]
def chat(query) -> Response
# 持久化
def dump(path)
def load(path)
# 可视化
def show()
方法注册表¶
# 方法在导入时注册
_METHOD_REGISTRY = {
"light_rag": {
"class": Light_RAG,
"type": "graph",
"description": "..."
},
# ...
}
扩展点¶
自定义模板¶
创建特定领域模板:
# my_template.yaml
name: custom_extraction
type: graph
output:
entities:
fields:
- name: name
type: str
# ...
自定义自动类型¶
扩展基类:
from hyperextract.types import AutoGraph
class MyCustomGraph(AutoGraph):
def _default_prompt(self):
return "Custom prompt..."
自定义方法¶
实现提取算法:
from hyperextract.methods import register_method
class MyMethod:
def extract(self, text):
# 自定义提取逻辑
pass
register_method("my_method", MyMethod, "graph", "Description")
性能考虑¶
分块¶
| 文档大小 | 块数 | 处理时间 |
|---|---|---|
| < 2KB | 1 | 快 |
| 2-10KB | 2-5 | 中等 |
| 10-50KB | 5-25 | 慢 |
| > 50KB | 25+ | 非常慢 |
并行化¶
内存使用¶
设计原则¶
- 类型安全 — 全程使用 Pydantic schema
- 可扩展性 — 方法的插件架构
- 可用性 — 常见任务的模板
- 性能 — 分块和并行化
- 互操作性 — 标准格式(JSON、YAML)