structured

package
v0.35.0 Latest Latest
Warning

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

Go to latest
Published: Dec 29, 2025 License: MIT Imports: 7 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"`
	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 字符串

func (*JSONSchema) ToMap added in v0.31.0

func (s *JSONSchema) ToMap() (map[string]any, error)

ToMap 将 JSONSchema 转换为 map[string]any

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

func (g *SchemaGenerator) MergeSchemas(schemas ...map[string]any) (map[string]any, error)

MergeSchemas 合并多个 Schema(用于复杂场景)

func (*SchemaGenerator) Validate added in v0.31.0

func (g *SchemaGenerator) Validate(schema map[string]any) error

Validate 验证 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 NewTypedParser

func NewTypedParser(schema *JSONSchema) *TypedParser

NewTypedParser 创建类型化解析器

func (*TypedParser) ExtractAndParse

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

ExtractAndParse 提取 JSON 并解析为通用 map

func (*TypedParser) ParseInto

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

ParseInto 解析并绑定到目标 struct

func (*TypedParser) ParseIntoWithValidation

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

ParseIntoWithValidation 解析并进行自定义验证

Jump to

Keyboard shortcuts

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