Documentation
¶
Overview ¶
Package parser 提供 LLM 输出解析和验证功能
本包实现了多种输出解析器,支持:
JSON 解析器:解析 JSON 格式输出
Regex 解析器:使用正则表达式提取内容
Enum 解析器:验证枚举值
List 解析器:解析列表输出
结构化解析器:解析到指定结构体
组合解析器:链式组合多个解析器
Semantic Kernel: OutputFilter
使用示例:
parser := parser.NewJSONParser[User]() user, err := parser.Parse(ctx, llmOutput)
Index ¶
- Variables
- func ParseJSON[T any](output string) (T, error)
- func ParseList(output string) ([]string, error)
- func ParseNumber(output string) (float64, error)
- func ParseYesNo(output string) (bool, error)
- type BoolParser
- type EnumParser
- func (p *EnumParser) GetFormatInstructions() string
- func (p *EnumParser) GetTypeName() string
- func (p *EnumParser) Parse(ctx context.Context, output string) (string, error)
- func (p *EnumParser) WithCaseSensitive(sensitive bool) *EnumParser
- func (p *EnumParser) WithPartialMatch(allow bool) *EnumParser
- type JSONParser
- func (p *JSONParser[T]) GetFormatInstructions() string
- func (p *JSONParser[T]) GetTypeName() string
- func (p *JSONParser[T]) Parse(ctx context.Context, output string) (T, error)
- func (p *JSONParser[T]) Validate(ctx context.Context, result T) error
- func (p *JSONParser[T]) WithExtractJSON(extract bool) *JSONParser[T]
- func (p *JSONParser[T]) WithStrictMode(strict bool) *JSONParser[T]
- func (p *JSONParser[T]) WithValidator(validator func(T) error) *JSONParser[T]
- type ListParser
- func (p *ListParser) GetFormatInstructions() string
- func (p *ListParser) GetTypeName() string
- func (p *ListParser) Parse(ctx context.Context, output string) ([]string, error)
- func (p *ListParser) WithMaxItems(max int) *ListParser
- func (p *ListParser) WithMinItems(min int) *ListParser
- func (p *ListParser) WithSeparator(sep string) *ListParser
- type NumberParser
- func (p *NumberParser) GetFormatInstructions() string
- func (p *NumberParser) GetTypeName() string
- func (p *NumberParser) Parse(ctx context.Context, output string) (float64, error)
- func (p *NumberParser) WithInteger(integer bool) *NumberParser
- func (p *NumberParser) WithRange(min, max float64) *NumberParser
- type Parser
- type ParserWithValidation
- type PipelineParser
- type RegexParser
- type RetryParser
- type Validator
Constants ¶
This section is empty.
Variables ¶
var ( // ErrParseFailure 解析失败 ErrParseFailure = errors.New("parse failure") // ErrValidationFailure 验证失败 ErrValidationFailure = errors.New("validation failure") // ErrEmptyOutput 空输出 ErrEmptyOutput = errors.New("empty output") // ErrInvalidFormat 无效格式 ErrInvalidFormat = errors.New("invalid format") // ErrTypeMismatch 类型不匹配 ErrTypeMismatch = errors.New("type mismatch") )
Functions ¶
Types ¶
type BoolParser ¶
type BoolParser struct {
// TrueValues 真值列表
TrueValues []string
// FalseValues 假值列表
FalseValues []string
}
BoolParser 布尔解析器
func (*BoolParser) GetFormatInstructions ¶
func (p *BoolParser) GetFormatInstructions() string
GetFormatInstructions 获取格式说明
type EnumParser ¶
type EnumParser struct {
// Values 有效值列表
Values []string
// CaseSensitive 是否大小写敏感
CaseSensitive bool
// AllowPartialMatch 是否允许部分匹配
AllowPartialMatch bool
}
EnumParser 枚举解析器
func (*EnumParser) GetFormatInstructions ¶
func (p *EnumParser) GetFormatInstructions() string
GetFormatInstructions 获取格式说明
func (*EnumParser) WithCaseSensitive ¶
func (p *EnumParser) WithCaseSensitive(sensitive bool) *EnumParser
WithCaseSensitive 设置大小写敏感
func (*EnumParser) WithPartialMatch ¶
func (p *EnumParser) WithPartialMatch(allow bool) *EnumParser
WithPartialMatch 设置部分匹配
type JSONParser ¶
type JSONParser[T any] struct { // StrictMode 严格模式(不允许多余字段) StrictMode bool // ExtractJSON 是否从文本中提取 JSON ExtractJSON bool // Validators 验证器列表 Validators []func(T) error }
JSONParser JSON 解析器
func (*JSONParser[T]) GetFormatInstructions ¶
func (p *JSONParser[T]) GetFormatInstructions() string
GetFormatInstructions 获取格式说明
func (*JSONParser[T]) Parse ¶
func (p *JSONParser[T]) Parse(ctx context.Context, output string) (T, error)
Parse 解析 JSON 输出
func (*JSONParser[T]) Validate ¶
func (p *JSONParser[T]) Validate(ctx context.Context, result T) error
Validate 验证结果
func (*JSONParser[T]) WithExtractJSON ¶
func (p *JSONParser[T]) WithExtractJSON(extract bool) *JSONParser[T]
WithExtractJSON 设置是否提取 JSON
func (*JSONParser[T]) WithStrictMode ¶
func (p *JSONParser[T]) WithStrictMode(strict bool) *JSONParser[T]
WithStrictMode 设置严格模式
func (*JSONParser[T]) WithValidator ¶
func (p *JSONParser[T]) WithValidator(validator func(T) error) *JSONParser[T]
WithValidator 添加验证器
type ListParser ¶
type ListParser struct {
// Separator 分隔符
Separator string
// TrimItems 是否去除空白
TrimItems bool
// FilterEmpty 是否过滤空项
FilterEmpty bool
// MinItems 最少项数
MinItems int
// MaxItems 最大项数
MaxItems int
}
ListParser 列表解析器
func (*ListParser) GetFormatInstructions ¶
func (p *ListParser) GetFormatInstructions() string
GetFormatInstructions 获取格式说明
func (*ListParser) WithMaxItems ¶
func (p *ListParser) WithMaxItems(max int) *ListParser
WithMaxItems 设置最大项数
func (*ListParser) WithMinItems ¶
func (p *ListParser) WithMinItems(min int) *ListParser
WithMinItems 设置最少项数
func (*ListParser) WithSeparator ¶
func (p *ListParser) WithSeparator(sep string) *ListParser
WithSeparator 设置分隔符
type NumberParser ¶
type NumberParser struct {
// Min 最小值
Min *float64
// Max 最大值
Max *float64
// Integer 是否只接受整数
Integer bool
}
NumberParser 数字解析器
func (*NumberParser) GetFormatInstructions ¶
func (p *NumberParser) GetFormatInstructions() string
GetFormatInstructions 获取格式说明
func (*NumberParser) WithInteger ¶
func (p *NumberParser) WithInteger(integer bool) *NumberParser
WithInteger 设置只接受整数
func (*NumberParser) WithRange ¶
func (p *NumberParser) WithRange(min, max float64) *NumberParser
WithRange 设置范围
type Parser ¶
type Parser[T any] interface { // Parse 解析输出 Parse(ctx context.Context, output string) (T, error) // GetFormatInstructions 获取格式说明(用于提示词) GetFormatInstructions() string // GetTypeName 获取类型名称 GetTypeName() string }
Parser 输出解析器接口
type ParserWithValidation ¶
ParserWithValidation 带验证的解析器
type PipelineParser ¶
type PipelineParser[T any] struct { // contains filtered or unexported fields }
PipelineParser 管道解析器 按顺序执行多个解析步骤
func NewPipelineParser ¶
func NewPipelineParser[T any](final Parser[T]) *PipelineParser[T]
NewPipelineParser 创建管道解析器
func (*PipelineParser[T]) AddStep ¶
func (p *PipelineParser[T]) AddStep(step func(context.Context, string) (string, error)) *PipelineParser[T]
AddStep 添加处理步骤
func (*PipelineParser[T]) GetFormatInstructions ¶
func (p *PipelineParser[T]) GetFormatInstructions() string
GetFormatInstructions 获取格式说明
func (*PipelineParser[T]) GetTypeName ¶
func (p *PipelineParser[T]) GetTypeName() string
GetTypeName 获取类型名称
type RegexParser ¶
type RegexParser struct {
// Pattern 正则表达式
Pattern *regexp.Regexp
// GroupNames 分组名称
GroupNames []string
// Required 必需的分组
Required []string
}
RegexParser 正则表达式解析器
func NewRegexParser ¶
func NewRegexParser(pattern string) (*RegexParser, error)
NewRegexParser 创建正则解析器
func (*RegexParser) GetFormatInstructions ¶
func (p *RegexParser) GetFormatInstructions() string
GetFormatInstructions 获取格式说明
func (*RegexParser) WithRequired ¶
func (p *RegexParser) WithRequired(names ...string) *RegexParser
WithRequired 设置必需的分组
type RetryParser ¶
type RetryParser[T any] struct { // contains filtered or unexported fields }
RetryParser 重试解析器 解析失败时自动重试
func NewRetryParser ¶
func NewRetryParser[T any](parser Parser[T], maxRetries int) *RetryParser[T]
NewRetryParser 创建重试解析器
func (*RetryParser[T]) GetFormatInstructions ¶
func (p *RetryParser[T]) GetFormatInstructions() string
GetFormatInstructions 获取格式说明
func (*RetryParser[T]) GetTypeName ¶
func (p *RetryParser[T]) GetTypeName() string
GetTypeName 获取类型名称
func (*RetryParser[T]) Parse ¶
func (p *RetryParser[T]) Parse(ctx context.Context, output string) (T, error)
Parse 解析(带重试)
func (*RetryParser[T]) WithFixFunc ¶
func (p *RetryParser[T]) WithFixFunc(fixFunc func(string, error) string) *RetryParser[T]
WithFixFunc 设置修复函数