structured

package
v0.14.0 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2025 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type JSONParser

type JSONParser struct{}

JSONParser 尝试从文本中提取 JSON 对象或数组并解析。

func NewJSONParser

func NewJSONParser() *JSONParser

NewJSONParser 创建默认解析器。

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"`
	Properties  map[string]*JSONSchema `json:"properties,omitempty"`
	Items       *JSONSchema            `json:"items,omitempty"`
	Required    []string               `json:"required,omitempty"`
	Description string                 `json:"description,omitempty"`
	Enum        []interface{}          `json:"enum,omitempty"`
	Minimum     *float64               `json:"minimum,omitempty"`
	Maximum     *float64               `json:"maximum,omitempty"`
	MinLength   *int                   `json:"minLength,omitempty"`
	MaxLength   *int                   `json:"maxLength,omitempty"`
	Pattern     string                 `json:"pattern,omitempty"`
	Format      string                 `json:"format,omitempty"`
}

JSONSchema JSON Schema 定义

func FromJSON

func FromJSON(jsonData string) (*JSONSchema, error)

FromJSON 从 JSON 解析 Schema

func GenerateSchema

func GenerateSchema(structType interface{}) (*JSONSchema, error)

GenerateSchema 从 Go struct 生成 JSON Schema

func MustGenerateSchema

func MustGenerateSchema(structType interface{}) *JSONSchema

MustGenerateSchema 生成 Schema,失败时 panic

func (*JSONSchema) ToJSON

func (s *JSONSchema) ToJSON() (string, error)

ToJSON 将 Schema 转换为 JSON

type OutputSpec

type OutputSpec struct {
	Enabled         bool                   // 是否启用结构化解析
	Schema          map[string]interface{} // 可选的 JSON Schema 信息(当前仅透传)
	RequiredFields  []string               // 期望在顶层出现的字段
	AllowTextBackup bool                   // 解析失败时是否允许保留原始文本
}

OutputSpec 描述期望的结构化输出。 Schema 字段目前仅用于文档/日志,不进行严格校验;RequiredFields 用于轻量必填校验。

type ParseResult

type ParseResult struct {
	RawText       string      // 模型原始输出
	RawJSON       string      // 提取出的 JSON 文本
	Data          interface{} // JSON 解析结果
	MissingFields []string    // 缺失的必填字段
}

ParseResult 结构化解析结果。

type Parser

type Parser interface {
	Parse(ctx context.Context, text string, spec OutputSpec) (*ParseResult, error)
}

Parser 结构化输出解析器接口。

type SchemaValidator

type SchemaValidator struct {
	// contains filtered or unexported fields
}

SchemaValidator JSON Schema 验证器

func NewSchemaValidator

func NewSchemaValidator(schema *JSONSchema) *SchemaValidator

NewSchemaValidator 创建 Schema 验证器

func (*SchemaValidator) Validate

func (sv *SchemaValidator) Validate(jsonData string) error

Validate 验证 JSON 数据

type TypedOutputSpec

type TypedOutputSpec struct {
	StructType       interface{}             // Go struct 类型(用于反射)
	Schema           *JSONSchema             // JSON Schema 验证
	RequiredFields   []string                // 必填字段
	Strict           bool                    // 严格模式(验证失败则报错)
	AllowTextBackup  bool                    // 解析失败时是否允许保留原始文本
	CustomValidation func(interface{}) error // 自定义验证函数
}

TypedOutputSpec 类型化输出规范

type TypedParseResult

type TypedParseResult struct {
	RawText          string      // 原始文本
	RawJSON          string      // 提取的 JSON
	Data             interface{} // 解析后的数据(绑定到 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 NewTypedParser

func NewTypedParser(schema *JSONSchema) *TypedParser

NewTypedParser 创建类型化解析器

func (*TypedParser) ExtractAndParse

func (tp *TypedParser) ExtractAndParse(ctx context.Context, text string) (map[string]interface{}, error)

ExtractAndParse 提取 JSON 并解析为通用 map

func (*TypedParser) ParseInto

func (tp *TypedParser) ParseInto(ctx context.Context, text string, target interface{}) error

ParseInto 解析并绑定到目标 struct

func (*TypedParser) ParseIntoWithValidation

func (tp *TypedParser) ParseIntoWithValidation(ctx context.Context, text string, target interface{}, validator func(interface{}) error) error

ParseIntoWithValidation 解析并进行自定义验证

Jump to

Keyboard shortcuts

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