Documentation
¶
Index ¶
- type JSONParser
- type JSONSchema
- type OutputSpec
- type ParseResult
- type Parser
- type SchemaGenerator
- type SchemaValidator
- type TypedOutputSpec
- type TypedParseResult
- type TypedParser
- func (tp *TypedParser) ExtractAndParse(ctx context.Context, text string) (map[string]any, error)
- func (tp *TypedParser) ParseInto(ctx context.Context, text string, target any) error
- func (tp *TypedParser) ParseIntoWithValidation(ctx context.Context, text string, target any, validator func(any) error) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type JSONParser ¶
type JSONParser struct{}
JSONParser 尝试从文本中提取 JSON 对象或数组并解析。
func (*JSONParser) Parse ¶
func (p *JSONParser) Parse(ctx context.Context, text string, spec OutputSpec) (*ParseResult, error)
Parse 实现结构化解析。
type JSONSchema ¶
type JSONSchema struct {
Type string `json:"type,omitempty"`
Description string `json:"description,omitempty"`
Properties map[string]*JSONSchema `json:"properties,omitempty"`
Items *JSONSchema `json:"items,omitempty"`
Required []string `json:"required,omitempty"`
Enum []any `json:"enum,omitempty"`
Minimum *float64 `json:"minimum,omitempty"`
Maximum *float64 `json:"maximum,omitempty"`
Pattern string `json:"pattern,omitempty"`
Format string `json:"format,omitempty"`
Default any `json:"default,omitempty"`
}
JSONSchema JSON Schema 定义(结构化版本)
func GenerateSchema ¶
func GenerateSchema(structType any) (*JSONSchema, error)
GenerateSchema 从 Go struct 生成 JSON Schema
func MustGenerateSchema ¶
func MustGenerateSchema(structType any) *JSONSchema
MustGenerateSchema 生成 Schema,失败时 panic
func (*JSONSchema) ToJSON ¶
func (s *JSONSchema) ToJSON() (string, error)
ToJSON 将 JSONSchema 转换为 JSON 字符串
type OutputSpec ¶
type OutputSpec struct {
Enabled bool // 是否启用结构化解析
Schema map[string]any // 可选的 JSON Schema 信息(当前仅透传)
RequiredFields []string // 期望在顶层出现的字段
AllowTextBackup bool // 解析失败时是否允许保留原始文本
}
OutputSpec 描述期望的结构化输出。 Schema 字段目前仅用于文档/日志,不进行严格校验;RequiredFields 用于轻量必填校验。
type ParseResult ¶
type ParseResult struct {
RawText string // 模型原始输出
RawJSON string // 提取出的 JSON 文本
Data any // JSON 解析结果
MissingFields []string // 缺失的必填字段
}
ParseResult 结构化解析结果。
type Parser ¶
type Parser interface {
Parse(ctx context.Context, text string, spec OutputSpec) (*ParseResult, error)
}
Parser 结构化输出解析器接口。
type SchemaGenerator ¶ added in v0.31.0
type SchemaGenerator struct{}
SchemaGenerator JSON Schema 生成器 用于从 Go struct 生成 JSON Schema,支持结构化输出
func NewSchemaGenerator ¶ added in v0.31.0
func NewSchemaGenerator() *SchemaGenerator
NewSchemaGenerator 创建 Schema 生成器
func (*SchemaGenerator) FromStruct ¶ added in v0.31.0
func (g *SchemaGenerator) FromStruct(v any) (map[string]any, error)
FromStruct 从 Go struct 生成 JSON Schema 支持的 struct tags: - json: JSON 字段名 - required: 是否必需 ("true"/"false") - description: 字段描述 - enum: 枚举值(逗号分隔) - minimum: 最小值(数字类型) - maximum: 最大值(数字类型) - pattern: 正则模式(字符串类型)
func (*SchemaGenerator) MergeSchemas ¶ added in v0.31.0
MergeSchemas 合并多个 Schema(用于复杂场景)
type SchemaValidator ¶
type SchemaValidator struct {
// contains filtered or unexported fields
}
SchemaValidator Schema 验证器
func NewSchemaValidator ¶
func NewSchemaValidator(schema *JSONSchema) *SchemaValidator
NewSchemaValidator 创建 Schema 验证器
func (*SchemaValidator) Validate ¶
func (sv *SchemaValidator) Validate(jsonData string) error
Validate 验证 JSON 数据是否符合 Schema 目前是简化实现,仅做基础类型检查
type TypedOutputSpec ¶
type TypedOutputSpec struct {
StructType any // Go struct 类型(用于反射)
Schema *JSONSchema // JSON Schema 验证
RequiredFields []string // 必填字段
Strict bool // 严格模式(验证失败则报错)
AllowTextBackup bool // 解析失败时是否允许保留原始文本
CustomValidation func(any) error // 自定义验证函数
}
TypedOutputSpec 类型化输出规范
type TypedParseResult ¶
type TypedParseResult struct {
RawText string // 原始文本
RawJSON string // 提取的 JSON
Data any // 解析后的数据(绑定到 struct)
MissingFields []string // 缺失的必填字段
ValidationErrors []string // 验证错误
Success bool // 是否成功
}
TypedParseResult 类型化解析结果
func ParseTyped ¶
func ParseTyped(ctx context.Context, text string, spec TypedOutputSpec) (*TypedParseResult, error)
ParseTyped 执行类型化解析
type TypedParser ¶
type TypedParser struct {
// contains filtered or unexported fields
}
TypedParser 类型化解析器,支持直接绑定到 Go struct
func (*TypedParser) ExtractAndParse ¶
ExtractAndParse 提取 JSON 并解析为通用 map
func (*TypedParser) ParseIntoWithValidation ¶
func (tp *TypedParser) ParseIntoWithValidation(ctx context.Context, text string, target any, validator func(any) error) error
ParseIntoWithValidation 解析并进行自定义验证