encoding

package
v0.14.108 Latest Latest
Warning

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

Go to latest
Published: Dec 25, 2025 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package encoding provides a pluggable framework for encoding and decoding data in various formats (JSON, YAML, TOML, etc.) for agentic flows.

Index

Constants

This section is empty.

Variables

View Source
var ModeDefault = ModeJSONSchema

ModeDefault is the default mode for the encoder. Allow to everride in apps

Functions

func NewSimpleOutputParser

func NewSimpleOutputParser() chatmodel.OutputParser[chatmodel.String]

Types

type Mode

type Mode = string
const (
	ModeJSON             Mode = "json"
	ModeJSONSchema       Mode = "json_schema"
	ModeJSONSchemaStrict Mode = "json_schema_strict" // Not all providers support this and all props must be required
	ModeYAML             Mode = "yaml"
	ModeTOML             Mode = "toml"
	ModePlainText        Mode = "plain_text"
	ModeCustom           Mode = "custom"
)

type SchemaEncoder

type SchemaEncoder interface {
	Marshal(req any) ([]byte, error)
	Unmarshal([]byte, any) error
	// GetFormatInstructions returns the wrapped message with message schema for the prompt
	GetFormatInstructions() string
}

func PredefinedSchemaEncoder

func PredefinedSchemaEncoder(mode Mode, req any) (SchemaEncoder, error)

type SchemaStreamEncoder

type SchemaStreamEncoder interface {
	Read(context.Context, <-chan string) <-chan any
	GetFormatInstructions() string
	EnableValidate()
}

type SimpleOutputParser

type SimpleOutputParser struct{}

Simple is an output parser that does nothing.

func (*SimpleOutputParser) GetFormatInstructions

func (p *SimpleOutputParser) GetFormatInstructions() string

func (*SimpleOutputParser) Parse

func (p *SimpleOutputParser) Parse(text string) (*chatmodel.String, error)

func (*SimpleOutputParser) Type

func (p *SimpleOutputParser) Type() string

type TypedOutputParser

type TypedOutputParser[T any] struct {
	// contains filtered or unexported fields
}

TypedOutputParser parses output from an LLM into Go structs. By providing the NewDefined constructor with a struct, one or more TypeScript interfaces are generated to help LLMs format responses with the desired JSON structure.

func NewTypedOutputParser

func NewTypedOutputParser[T any](sourceType T, mode Mode) (*TypedOutputParser[T], error)

NewTypedOutputParser creates an output parser that structures data according to a given schema, as defined by struct field names and types. Tagging the field with "json" will explicitly use that value as the field name. Tagging with "describe" will add a line comment for the LLM to understand how to generate data, helpful when the field's name is insufficient.

func (*TypedOutputParser[T]) GetFormatInstructions

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

GetFormatInstructions returns a string describing the format of the output.

func (*TypedOutputParser[T]) Parse

func (p *TypedOutputParser[T]) Parse(text string) (*T, error)

Parse parses the output of an LLM call.

func (*TypedOutputParser[T]) Type

func (p *TypedOutputParser[T]) Type() string

Type returns the string type key uniquely identifying this class of parser

func (*TypedOutputParser[T]) WithValidation

func (p *TypedOutputParser[T]) WithValidation(validate bool)

type Validator

type Validator interface {
	Validate(any) error
}

Directories

Path Synopsis
Package dummy encoder/decoder
Package dummy encoder/decoder
Package json json encoder/decoder
Package json json encoder/decoder
Package toml encoder/decoder
Package toml encoder/decoder
Package yaml encoder/decoder
Package yaml encoder/decoder

Jump to

Keyboard shortcuts

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