Documentation
¶
Overview ¶
Package adw 提供智能文档工作流(Agentic Document Workflows)功能
ADW 系统超越传统 RAG,提供端到端的文档自动化能力: - 智能文档解析和理解 - 结构化信息提取 - 表格和表单识别 - 实体识别和关系抽取 - Schema 驱动的数据验证 - 与 Agent 系统深度集成
对标框架: - LlamaIndex: Document Workflows - Semantic Kernel: Document Processing
使用示例:
pipeline := adw.NewPipeline("invoice-processing").
AddStep(adw.NewOCRStep()).
AddStep(adw.NewTableExtractor()).
AddStep(adw.NewEntityExtractor(llmProvider)).
AddStep(adw.NewSchemaValidator(invoiceSchema)).
Build()
result, err := pipeline.Process(ctx, documents)
Package adw 提供智能文档工作流功能 ¶
本文件实现 ADW Pipeline,用于构建和执行文档处理流程。
Index ¶
- Variables
- type BaseStep
- type BoundingBox
- type ConfidenceCalculatorStep
- type Document
- func (d *Document) AddEntity(entity Entity)
- func (d *Document) AddProcessingStep(step ProcessingStep)
- func (d *Document) AddRelation(relation Relation)
- func (d *Document) AddSection(section Section)
- func (d *Document) AddTable(table Table)
- func (d *Document) AddValidationError(err ValidationError)
- func (d *Document) GetStructuredValue(key string) (any, bool)
- func (d *Document) IsValid() bool
- func (d *Document) SetStructuredValue(key string, value any)
- type DocumentType
- type DocumentTypeDetectorStep
- type Entity
- type EntityType
- type ErrorSeverity
- type ExtractionSchema
- func (s *ExtractionSchema) AddDateField(name, description, format string, required bool) *ExtractionSchema
- func (s *ExtractionSchema) AddField(field SchemaField) *ExtractionSchema
- func (s *ExtractionSchema) AddMoneyField(name, description string, required bool) *ExtractionSchema
- func (s *ExtractionSchema) AddNumberField(name, description string, required bool) *ExtractionSchema
- func (s *ExtractionSchema) AddStringField(name, description string, required bool) *ExtractionSchema
- func (s *ExtractionSchema) GetField(name string) *SchemaField
- type Extractor
- type FieldType
- type FormField
- type Image
- type Pipeline
- type PipelineBuilder
- func (b *PipelineBuilder) AddStep(step Step) *PipelineBuilder
- func (b *PipelineBuilder) Build() Pipeline
- func (b *PipelineBuilder) WithDescription(desc string) *PipelineBuilder
- func (b *PipelineBuilder) WithHooks(hooks *PipelineHooks) *PipelineBuilder
- func (b *PipelineBuilder) WithOptions(opts ProcessOptions) *PipelineBuilder
- type PipelineHooks
- type PipelineImpl
- type PipelineInput
- type PipelineMetrics
- type PipelineOutput
- type ProcessOptions
- type ProcessingStep
- type Relation
- type SchemaField
- type Section
- type Step
- type StepMetrics
- type Table
- type TextNormalizerStep
- type ValidationError
- type Validator
Constants ¶
This section is empty.
Variables ¶
var ( // ErrInvalidDocument 无效的文档 ErrInvalidDocument = errors.New("adw: invalid document") // ErrExtractionFailed 提取失败 ErrExtractionFailed = errors.New("adw: extraction failed") // ErrValidationFailed 验证失败 ErrValidationFailed = errors.New("adw: validation failed") // ErrNoExtractor 没有可用的提取器 ErrNoExtractor = errors.New("adw: no extractor available") // ErrSchemaRequired Schema 必需 ErrSchemaRequired = errors.New("adw: schema required for extraction") // ErrProcessingFailed 处理失败 ErrProcessingFailed = errors.New("adw: processing failed") // ErrUnsupportedFormat 不支持的格式 ErrUnsupportedFormat = errors.New("adw: unsupported document format") )
Functions ¶
This section is empty.
Types ¶
type BoundingBox ¶
type BoundingBox struct {
// X 左上角 X 坐标
X float64
// Y 左上角 Y 坐标
Y float64
// Width 宽度
Width float64
// Height 高度
Height float64
}
BoundingBox 边界框
type ConfidenceCalculatorStep ¶
type ConfidenceCalculatorStep struct {
BaseStep
}
ConfidenceCalculatorStep 置信度计算步骤
func NewConfidenceCalculatorStep ¶
func NewConfidenceCalculatorStep() *ConfidenceCalculatorStep
NewConfidenceCalculatorStep 创建置信度计算步骤
type Document ¶
type Document struct {
// 继承基础文档
rag.Document
// Type 文档类型
Type DocumentType
// StructuredData 结构化数据(提取结果)
StructuredData map[string]any
// Tables 表格列表
Tables []Table
// Forms 表单字段列表
Forms []FormField
// Entities 实体列表
Entities []Entity
// Relations 实体关系列表
Relations []Relation
// Sections 文档分节
Sections []Section
// Images 图片列表
Images []Image
// ValidationErrors 验证错误列表
ValidationErrors []ValidationError
// ProcessingHistory 处理历史
ProcessingHistory []ProcessingStep
// Confidence 整体置信度 (0-1)
Confidence float64
// ProcessedAt 处理时间
ProcessedAt time.Time
// ProcessingDuration 处理耗时
ProcessingDuration time.Duration
// ExtraMetadata 额外元数据
ExtraMetadata map[string]any
}
Document ADW 增强文档 扩展 rag.Document,增加结构化信息
func NewDocument ¶
NewDocument 从 rag.Document 创建 ADW Document
func (*Document) AddProcessingStep ¶
func (d *Document) AddProcessingStep(step ProcessingStep)
AddProcessingStep 添加处理步骤
func (*Document) AddValidationError ¶
func (d *Document) AddValidationError(err ValidationError)
AddValidationError 添加验证错误
func (*Document) GetStructuredValue ¶
GetStructuredValue 获取结构化数据值
func (*Document) SetStructuredValue ¶
SetStructuredValue 设置结构化数据值
type DocumentType ¶
type DocumentType string
DocumentType 文档类型
const ( // DocTypeUnknown 未知类型 DocTypeUnknown DocumentType = "unknown" // DocTypePDF PDF 文档 DocTypePDF DocumentType = "pdf" // DocTypeWord Word 文档 DocTypeWord DocumentType = "word" // DocTypeExcel Excel 文档 DocTypeExcel DocumentType = "excel" // DocTypeImage 图片文档 DocTypeImage DocumentType = "image" // DocTypeHTML HTML 文档 DocTypeHTML DocumentType = "html" // DocTypeJSON JSON 文档 DocTypeJSON DocumentType = "json" // DocTypeXML XML 文档 DocTypeXML DocumentType = "xml" // DocTypeText 纯文本文档 DocTypeText DocumentType = "text" // DocTypeEmail 电子邮件 DocTypeEmail DocumentType = "email" // DocTypeInvoice 发票 DocTypeInvoice DocumentType = "invoice" // DocTypeContract 合同 DocTypeContract DocumentType = "contract" // DocTypeReceipt 收据 DocTypeReceipt DocumentType = "receipt" // DocTypeForm 表单 DocTypeForm DocumentType = "form" )
type DocumentTypeDetectorStep ¶
type DocumentTypeDetectorStep struct {
BaseStep
}
DocumentTypeDetectorStep 文档类型检测步骤
func NewDocumentTypeDetectorStep ¶
func NewDocumentTypeDetectorStep() *DocumentTypeDetectorStep
NewDocumentTypeDetectorStep 创建文档类型检测步骤
type Entity ¶
type Entity struct {
// ID 实体 ID
ID string
// Text 实体文本
Text string
// Type 实体类型
Type EntityType
// NormalizedValue 标准化值
NormalizedValue string
// Start 起始位置
Start int
// End 结束位置
End int
// Confidence 置信度
Confidence float64
// Properties 属性
Properties map[string]any
// Metadata 元数据
Metadata map[string]any
}
Entity 实体
type EntityType ¶
type EntityType string
EntityType 实体类型
const ( // EntityPerson 人名 EntityPerson EntityType = "person" // EntityOrganization 组织名 EntityOrganization EntityType = "organization" // EntityLocation 地名 EntityLocation EntityType = "location" // EntityDate 日期 EntityDate EntityType = "date" // EntityTime 时间 EntityTime EntityType = "time" // EntityMoney 金额 EntityMoney EntityType = "money" // EntityPercent 百分比 EntityPercent EntityType = "percent" // EntityEmail 电子邮件 EntityEmail EntityType = "email" // EntityPhone 电话 EntityPhone EntityType = "phone" // EntityURL URL EntityURL EntityType = "url" // EntityProduct 产品 EntityProduct EntityType = "product" // EntityEvent 事件 EntityEvent EntityType = "event" // EntityCustom 自定义 EntityCustom EntityType = "custom" )
type ErrorSeverity ¶
type ErrorSeverity string
ErrorSeverity 错误严重程度
const ( // SeverityError 错误 SeverityError ErrorSeverity = "error" // SeverityWarning 警告 SeverityWarning ErrorSeverity = "warning" // SeverityInfo 信息 SeverityInfo ErrorSeverity = "info" )
type ExtractionSchema ¶
type ExtractionSchema struct {
// Name Schema 名称
Name string
// Description Schema 描述
Description string
// Fields 字段定义
Fields []SchemaField
// Required 必需字段
Required []string
// Metadata 元数据
Metadata map[string]any
}
ExtractionSchema 提取 Schema 定义需要从文档中提取的数据结构
func NewExtractionSchema ¶
func NewExtractionSchema(name string) *ExtractionSchema
NewExtractionSchema 创建提取 Schema
func (*ExtractionSchema) AddDateField ¶
func (s *ExtractionSchema) AddDateField(name, description, format string, required bool) *ExtractionSchema
AddDateField 添加日期字段
func (*ExtractionSchema) AddField ¶
func (s *ExtractionSchema) AddField(field SchemaField) *ExtractionSchema
AddField 添加字段
func (*ExtractionSchema) AddMoneyField ¶
func (s *ExtractionSchema) AddMoneyField(name, description string, required bool) *ExtractionSchema
AddMoneyField 添加金额字段
func (*ExtractionSchema) AddNumberField ¶
func (s *ExtractionSchema) AddNumberField(name, description string, required bool) *ExtractionSchema
AddNumberField 添加数值字段
func (*ExtractionSchema) AddStringField ¶
func (s *ExtractionSchema) AddStringField(name, description string, required bool) *ExtractionSchema
AddStringField 添加字符串字段
func (*ExtractionSchema) GetField ¶
func (s *ExtractionSchema) GetField(name string) *SchemaField
GetField 获取字段定义
type Extractor ¶
type Extractor interface {
// Name 提取器名称
Name() string
// Extract 提取结构化数据
Extract(ctx context.Context, doc *Document, schema *ExtractionSchema) (map[string]any, error)
// ExtractEntities 提取实体
ExtractEntities(ctx context.Context, doc *Document) ([]Entity, error)
// ExtractRelations 提取关系
ExtractRelations(ctx context.Context, doc *Document, entities []Entity) ([]Relation, error)
}
Extractor 提取器接口
type FieldType ¶
type FieldType string
FieldType 字段类型
const ( FieldTypeString FieldType = "string" FieldTypeNumber FieldType = "number" FieldTypeInteger FieldType = "integer" FieldTypeBoolean FieldType = "boolean" FieldTypeArray FieldType = "array" FieldTypeObject FieldType = "object" FieldTypeDate FieldType = "date" FieldTypeTime FieldType = "time" FieldTypeMoney FieldType = "money" FieldTypeEmail FieldType = "email" FieldTypePhone FieldType = "phone" FieldTypeURL FieldType = "url" )
type FormField ¶
type FormField struct {
// ID 字段 ID
ID string
// Name 字段名称
Name string
// Label 字段标签
Label string
// Value 字段值
Value string
// Type 字段类型
Type string
// Required 是否必填
Required bool
// Confidence 置信度
Confidence float64
// BoundingBox 边界框
BoundingBox *BoundingBox
// Metadata 元数据
Metadata map[string]any
}
FormField 表单字段
type Image ¶
type Image struct {
// ID 图片 ID
ID string
// Name 图片名称
Name string
// Data 图片数据(base64)
Data string
// MimeType MIME 类型
MimeType string
// Width 宽度
Width int
// Height 高度
Height int
// PageNumber 所在页码
PageNumber int
// BoundingBox 边界框
BoundingBox *BoundingBox
// Caption 图片说明(如果有)
Caption string
// OCRText OCR 识别文本
OCRText string
// Metadata 元数据
Metadata map[string]any
}
Image 图片
type Pipeline ¶
type Pipeline interface {
// Name 管道名称
Name() string
// Process 处理文档
Process(ctx context.Context, input PipelineInput) (*PipelineOutput, error)
// AddStep 添加处理步骤
AddStep(step Step) Pipeline
// GetSteps 获取所有步骤
GetSteps() []Step
}
Pipeline ADW 管道接口
type PipelineBuilder ¶
type PipelineBuilder struct {
// contains filtered or unexported fields
}
PipelineBuilder 管道构建器
func (*PipelineBuilder) AddStep ¶
func (b *PipelineBuilder) AddStep(step Step) *PipelineBuilder
AddStep 添加步骤
func (*PipelineBuilder) WithDescription ¶
func (b *PipelineBuilder) WithDescription(desc string) *PipelineBuilder
WithDescription 设置描述
func (*PipelineBuilder) WithHooks ¶
func (b *PipelineBuilder) WithHooks(hooks *PipelineHooks) *PipelineBuilder
WithHooks 设置钩子
func (*PipelineBuilder) WithOptions ¶
func (b *PipelineBuilder) WithOptions(opts ProcessOptions) *PipelineBuilder
WithOptions 设置默认选项
type PipelineHooks ¶
type PipelineHooks struct {
// OnStart 管道开始时调用
OnStart func(ctx context.Context, input *PipelineInput)
// OnEnd 管道结束时调用
OnEnd func(ctx context.Context, output *PipelineOutput, err error)
// OnStepStart 步骤开始时调用
OnStepStart func(ctx context.Context, step Step, doc *Document)
// OnStepEnd 步骤结束时调用
OnStepEnd func(ctx context.Context, step Step, doc *Document, err error)
// OnDocumentStart 文档处理开始时调用
OnDocumentStart func(ctx context.Context, doc *Document)
// OnDocumentEnd 文档处理结束时调用
OnDocumentEnd func(ctx context.Context, doc *Document, err error)
// OnError 错误时调用
OnError func(ctx context.Context, step Step, doc *Document, err error)
}
PipelineHooks 管道钩子
type PipelineImpl ¶
type PipelineImpl struct {
// contains filtered or unexported fields
}
PipelineImpl Pipeline 实现
func (*PipelineImpl) GetMetrics ¶
func (p *PipelineImpl) GetMetrics() *PipelineMetrics
GetMetrics 获取指标
func (*PipelineImpl) Process ¶
func (p *PipelineImpl) Process(ctx context.Context, input PipelineInput) (*PipelineOutput, error)
Process 处理文档
type PipelineInput ¶
type PipelineInput struct {
// Documents 待处理文档
Documents []rag.Document
// Schema 提取 Schema
Schema *ExtractionSchema
// Options 处理选项
Options ProcessOptions
// Metadata 元数据
Metadata map[string]any
}
PipelineInput 管道输入
type PipelineMetrics ¶
type PipelineMetrics struct {
// TotalDocuments 总文档数
TotalDocuments int64
// ProcessedDocuments 已处理文档数
ProcessedDocuments int64
// FailedDocuments 失败文档数
FailedDocuments int64
// TotalProcessingTime 总处理时间
TotalProcessingTime time.Duration
// StepMetrics 步骤指标
StepMetrics map[string]*StepMetrics
// contains filtered or unexported fields
}
PipelineMetrics 管道指标
type PipelineOutput ¶
type PipelineOutput struct {
// Documents 处理后的文档
Documents []*Document
// ExtractedData 提取的数据(汇总)
ExtractedData map[string]any
// TotalDocuments 总文档数
TotalDocuments int
// SuccessCount 成功数
SuccessCount int
// FailureCount 失败数
FailureCount int
// ValidationErrors 所有验证错误
ValidationErrors []ValidationError
// ProcessingTime 处理时间
ProcessingTime time.Duration
// Metadata 元数据
Metadata map[string]any
}
PipelineOutput 管道输出
type ProcessOptions ¶
type ProcessOptions struct {
// EnableOCR 启用 OCR
EnableOCR bool
// EnableTableExtraction 启用表格提取
EnableTableExtraction bool
// EnableFormRecognition 启用表单识别
EnableFormRecognition bool
// EnableEntityRecognition 启用实体识别
EnableEntityRecognition bool
// EnableRelationExtraction 启用关系抽取
EnableRelationExtraction bool
// EnableValidation 启用验证
EnableValidation bool
// MaxConcurrency 最大并发数
MaxConcurrency int
// Timeout 处理超时(秒)
Timeout int
// Language 语言
Language string
// Schema 提取 Schema
Schema *ExtractionSchema
// CustomExtractors 自定义提取器
CustomExtractors []string
// Metadata 额外元数据
Metadata map[string]any
}
ProcessOptions 处理选项
func DefaultProcessOptions ¶
func DefaultProcessOptions() ProcessOptions
DefaultProcessOptions 默认处理选项
type ProcessingStep ¶
type ProcessingStep struct {
// StepName 步骤名称
StepName string
// StartTime 开始时间
StartTime time.Time
// EndTime 结束时间
EndTime time.Time
// Duration 耗时
Duration time.Duration
// Success 是否成功
Success bool
// Error 错误信息
Error error
// Output 输出数据
Output map[string]any
// Metadata 元数据
Metadata map[string]any
}
ProcessingStep 处理步骤记录
type Relation ¶
type Relation struct {
// ID 关系 ID
ID string
// Type 关系类型
Type string
// SourceEntityID 源实体 ID
SourceEntityID string
// TargetEntityID 目标实体 ID
TargetEntityID string
// Confidence 置信度
Confidence float64
// Properties 属性
Properties map[string]any
}
Relation 实体关系
type SchemaField ¶
type SchemaField struct {
// Name 字段名
Name string
// Description 字段描述
Description string
// Type 字段类型
Type FieldType
// Format 格式(如 date: "YYYY-MM-DD")
Format string
// Required 是否必需
Required bool
// Default 默认值
Default any
// Enum 枚举值(如果有)
Enum []any
// Pattern 正则表达式(如果有)
Pattern string
// Min 最小值(数值或长度)
Min *float64
// Max 最大值(数值或长度)
Max *float64
// Items 数组元素定义(如果 Type 是 array)
Items *SchemaField
// Properties 对象属性定义(如果 Type 是 object)
Properties []SchemaField
// Examples 示例值
Examples []any
// Metadata 元数据
Metadata map[string]any
}
SchemaField Schema 字段定义
type Section ¶
type Section struct {
// ID 分节 ID
ID string
// Title 标题
Title string
// Content 内容
Content string
// Level 层级 (1-6)
Level int
// StartPage 起始页
StartPage int
// EndPage 结束页
EndPage int
// SubSections 子分节
SubSections []Section
// Metadata 元数据
Metadata map[string]any
}
Section 文档分节
type Step ¶
type Step interface {
// Name 步骤名称
Name() string
// Description 步骤描述
Description() string
// Process 处理文档
Process(ctx context.Context, doc *Document, opts ProcessOptions) error
// CanHandle 检查是否能处理该文档
CanHandle(doc *Document) bool
}
Step 处理步骤接口
func NewConditionalStep ¶
func NewConditionalStep(name string, condition func(doc *Document) bool, fn func(ctx context.Context, doc *Document, opts ProcessOptions) error) Step
NewConditionalStep 创建条件步骤
func NewFuncStep ¶
func NewFuncStep(name string, fn func(ctx context.Context, doc *Document, opts ProcessOptions) error) Step
NewFuncStep 创建函数步骤
type StepMetrics ¶
type StepMetrics struct {
// Name 步骤名称
Name string
// ExecutionCount 执行次数
ExecutionCount int64
// SuccessCount 成功次数
SuccessCount int64
// FailureCount 失败次数
FailureCount int64
// TotalDuration 总耗时
TotalDuration time.Duration
// AverageDuration 平均耗时
AverageDuration time.Duration
}
StepMetrics 步骤指标
type Table ¶
type Table struct {
// ID 表格 ID
ID string
// Name 表格名称
Name string
// Headers 表头
Headers []string
// Rows 数据行
Rows [][]string
// PageNumber 所在页码
PageNumber int
// BoundingBox 边界框
BoundingBox *BoundingBox
// Confidence 置信度
Confidence float64
// Metadata 元数据
Metadata map[string]any
}
Table 表格
type TextNormalizerStep ¶
type TextNormalizerStep struct {
BaseStep
}
TextNormalizerStep 文本规范化步骤
func NewTextNormalizerStep ¶
func NewTextNormalizerStep() *TextNormalizerStep
NewTextNormalizerStep 创建文本规范化步骤
type ValidationError ¶
type ValidationError struct {
// Field 字段名
Field string
// Rule 违反的规则
Rule string
// Message 错误消息
Message string
// Severity 严重程度
Severity ErrorSeverity
// ExpectedValue 期望值
ExpectedValue any
// ActualValue 实际值
ActualValue any
}
ValidationError 验证错误
type Validator ¶
type Validator interface {
// Name 验证器名称
Name() string
// Validate 验证文档
Validate(ctx context.Context, doc *Document, schema *ExtractionSchema) ([]ValidationError, error)
}
Validator 验证器接口