parser

package
v0.5.13 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 13, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package parser 提供 LLM 输出解析和验证功能

本包实现了多种输出解析器,支持:

  • JSON 解析器:解析 JSON 格式输出

  • Regex 解析器:使用正则表达式提取内容

  • Enum 解析器:验证枚举值

  • List 解析器:解析列表输出

  • 结构化解析器:解析到指定结构体

  • 组合解析器:链式组合多个解析器

  • Semantic Kernel: OutputFilter

使用示例:

parser := parser.NewJSONParser[User]()
user, err := parser.Parse(ctx, llmOutput)

Index

Constants

This section is empty.

Variables

View Source
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

func ParseJSON

func ParseJSON[T any](output string) (T, error)

ParseJSON 解析 JSON 到指定类型

func ParseList

func ParseList(output string) ([]string, error)

ParseList 解析列表

func ParseNumber

func ParseNumber(output string) (float64, error)

ParseNumber 解析数字

func ParseYesNo

func ParseYesNo(output string) (bool, error)

ParseYesNo 解析 Yes/No 回答

Types

type BoolParser

type BoolParser struct {
	// TrueValues 真值列表
	TrueValues []string

	// FalseValues 假值列表
	FalseValues []string
}

BoolParser 布尔解析器

func NewBoolParser

func NewBoolParser() *BoolParser

NewBoolParser 创建布尔解析器

func (*BoolParser) GetFormatInstructions

func (p *BoolParser) GetFormatInstructions() string

GetFormatInstructions 获取格式说明

func (*BoolParser) GetTypeName

func (p *BoolParser) GetTypeName() string

GetTypeName 获取类型名称

func (*BoolParser) Parse

func (p *BoolParser) Parse(ctx context.Context, output string) (bool, error)

Parse 解析布尔值

type EnumParser

type EnumParser struct {
	// Values 有效值列表
	Values []string

	// CaseSensitive 是否大小写敏感
	CaseSensitive bool

	// AllowPartialMatch 是否允许部分匹配
	AllowPartialMatch bool
}

EnumParser 枚举解析器

func NewEnumParser

func NewEnumParser(values ...string) *EnumParser

NewEnumParser 创建枚举解析器

func (*EnumParser) GetFormatInstructions

func (p *EnumParser) GetFormatInstructions() string

GetFormatInstructions 获取格式说明

func (*EnumParser) GetTypeName

func (p *EnumParser) GetTypeName() string

GetTypeName 获取类型名称

func (*EnumParser) Parse

func (p *EnumParser) Parse(ctx context.Context, output string) (string, error)

Parse 解析枚举值

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 NewJSONParser

func NewJSONParser[T any]() *JSONParser[T]

NewJSONParser 创建 JSON 解析器

func (*JSONParser[T]) GetFormatInstructions

func (p *JSONParser[T]) GetFormatInstructions() string

GetFormatInstructions 获取格式说明

func (*JSONParser[T]) GetTypeName

func (p *JSONParser[T]) GetTypeName() string

GetTypeName 获取类型名称

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 NewListParser

func NewListParser() *ListParser

NewListParser 创建列表解析器

func (*ListParser) GetFormatInstructions

func (p *ListParser) GetFormatInstructions() string

GetFormatInstructions 获取格式说明

func (*ListParser) GetTypeName

func (p *ListParser) GetTypeName() string

GetTypeName 获取类型名称

func (*ListParser) Parse

func (p *ListParser) Parse(ctx context.Context, output string) ([]string, error)

Parse 解析列表

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 NewNumberParser

func NewNumberParser() *NumberParser

NewNumberParser 创建数字解析器

func (*NumberParser) GetFormatInstructions

func (p *NumberParser) GetFormatInstructions() string

GetFormatInstructions 获取格式说明

func (*NumberParser) GetTypeName

func (p *NumberParser) GetTypeName() string

GetTypeName 获取类型名称

func (*NumberParser) Parse

func (p *NumberParser) Parse(ctx context.Context, output string) (float64, error)

Parse 解析数字

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

type ParserWithValidation[T any] interface {
	Parser[T]
	Validator[T]
}

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 获取类型名称

func (*PipelineParser[T]) Parse

func (p *PipelineParser[T]) Parse(ctx context.Context, output string) (T, error)

Parse 执行管道解析

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) GetTypeName

func (p *RegexParser) GetTypeName() string

GetTypeName 获取类型名称

func (*RegexParser) Parse

func (p *RegexParser) Parse(ctx context.Context, output string) (map[string]string, error)

Parse 解析输出

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 设置修复函数

type Validator

type Validator[T any] interface {
	// Validate 验证解析结果
	Validate(ctx context.Context, result T) error
}

Validator 验证器接口

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL