outputparser

package
v0.0.0-...-7ea8281 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2024 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package outputparser provides a set of output parsers to process structured or unstructured data from language models (LLMs).

The outputparser package includes the following parsers:

  • BooleanParser: a parser that returns a boolean value based on string values assigned to true and false.
  • Simple: a basic parser that returns the raw text as-is without any processing.
  • Structured: a parser that expects a JSON-formatted response and returns it as a map[string]string while validating against a provided schema.
  • Combining: a parser that combines the output of multiple parsers into a single parser.
  • CommaSeparatedList: a parser that takes a string with comma-separated values and returns them as a string slice.
  • Defined: a parser that takes a struct with fields (optionally tagged with the 'describe:' key). It returns a struct of the same type it accepted, however this time with the field values.
  • RegexParser: a parser that takes a string, compiles it into a regular expression, and returns map[string]string of the regex groups.
  • RegexDict: a parser that searches a string for values in a dictionary format, and returns a map[string]string of the keys and their associated value.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BooleanParser

type BooleanParser struct {
	TrueStrings  []string
	FalseStrings []string
}

BooleanParser is an output parser used to parse the output of an LLM as a boolean.

func NewBooleanParser

func NewBooleanParser() BooleanParser

NewBooleanParser returns a new BooleanParser.

func (BooleanParser) GetFormatInstructions

func (p BooleanParser) GetFormatInstructions() string

GetFormatInstructions returns instructions on the expected output format.

func (BooleanParser) Parse

func (p BooleanParser) Parse(text string) (any, error)

Parse parses the output of an LLM into a map of strings.

func (BooleanParser) ParseWithPrompt

func (p BooleanParser) ParseWithPrompt(text string, _ llms.PromptValue) (any, error)

ParseWithPrompt does the same as Parse.

func (BooleanParser) Type

func (p BooleanParser) Type() string

Type returns the type of the parser.

type Combining

type Combining struct {
	Parsers []schema.OutputParser[any]
}

Combining is a parser that combines multiple parsers into one.

func NewCombining

func NewCombining(parsers []schema.OutputParser[any]) Combining

NewCombining creates a new combining parser.

func (Combining) GetFormatInstructions

func (p Combining) GetFormatInstructions() string

GetFormatInstructions returns the format instructions.

func (Combining) Parse

func (p Combining) Parse(text string) (any, error)

Parse parses text delimited by two successive newline (`\n\n`) characters to the respective output parsers.

func (Combining) ParseWithPrompt

func (p Combining) ParseWithPrompt(text string, _ llms.PromptValue) (any, error)

ParseWithPrompt with prompts does the same as Parse.

func (Combining) Type

func (p Combining) Type() string

Type returns the type of the parser.

type CommaSeparatedList

type CommaSeparatedList struct{}

CommaSeparatedList is an output parser used to parse the output of an LLM as a string slice. Splits in the output from the llm are done every comma.

func NewCommaSeparatedList

func NewCommaSeparatedList() CommaSeparatedList

NewCommaSeparatedList creates a new CommaSeparatedList.

func (CommaSeparatedList) GetFormatInstructions

func (p CommaSeparatedList) GetFormatInstructions() string

GetFormatInstructions returns the format instruction.

func (CommaSeparatedList) Parse

func (p CommaSeparatedList) Parse(text string) ([]string, error)

Parse parses the output of an LLM into a string slice.

func (CommaSeparatedList) ParseWithPrompt

func (p CommaSeparatedList) ParseWithPrompt(text string, _ llms.PromptValue) ([]string, error)

ParseWithPrompt with prompts does the same as Parse.

func (CommaSeparatedList) Type

func (p CommaSeparatedList) Type() string

type Defined

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

Defined parses JSON 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 NewDefined

func NewDefined[T any](source T) (Defined[T], error)

NewDefined 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 (Defined[T]) GetFormatInstructions

func (p Defined[T]) GetFormatInstructions() string

GetFormatInstructions returns a string describing the format of the output.

func (Defined[T]) Parse

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

Parse parses the output of an LLM call.

func (Defined[T]) ParseWithPrompt

func (p Defined[T]) ParseWithPrompt(text string, _ llms.PromptValue) (T, error)

ParseWithPrompt is equivalent to Parse.

func (Defined[T]) Type

func (p Defined[T]) Type() string

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

type ParseError

type ParseError struct {
	Text   string
	Reason string
}

ParseError is the error type returned by output parsers.

func (ParseError) Error

func (e ParseError) Error() string

type RegexDict

type RegexDict struct {
	// OutputKeyToFormat is a map which has a key that represents an identifier for the regex,
	// and a value to search for as a key in the parsed text.
	OutputKeyToFormat map[string]string

	// NoUpdateValue is a string that prevents assignment to parsed outputs map when matched.
	NoUpdateValue string
}

RegexDict is an output parser used to parse the output of an LLM as a map.

func NewRegexDict

func NewRegexDict(outputKeyToFormat map[string]string, noUpdateValue string) RegexDict

NewRegexDict returns a new RegexDict.

func (RegexDict) GetFormatInstructions

func (p RegexDict) GetFormatInstructions() string

GetFormatInstructions returns instructions on the expected output format.

func (RegexDict) Parse

func (p RegexDict) Parse(text string) (any, error)

Parse parses the output of an LLM into a map of strings.

func (RegexDict) ParseWithPrompt

func (p RegexDict) ParseWithPrompt(text string, _ llms.PromptValue) (any, error)

ParseWithPrompt does the same as Parse.

func (RegexDict) Type

func (p RegexDict) Type() string

Type returns the type of the parser.

type RegexParser

type RegexParser struct {
	Expression *regexp.Regexp
	OutputKeys []string
}

RegexParser is an output parser used to parse the output of an LLM as a map.

func NewRegexParser

func NewRegexParser(expressionStr string) RegexParser

NewRegexParser returns a new RegexParser.

func (RegexParser) GetFormatInstructions

func (p RegexParser) GetFormatInstructions() string

GetFormatInstructions returns instructions on the expected output format.

func (RegexParser) Parse

func (p RegexParser) Parse(text string) (any, error)

Parse parses the output of an LLM into a map of strings.

func (RegexParser) ParseWithPrompt

func (p RegexParser) ParseWithPrompt(text string, _ llms.PromptValue) (any, error)

ParseWithPrompt does the same as Parse.

func (RegexParser) Type

func (p RegexParser) Type() string

Type returns the type of the parser.

type ResponseSchema

type ResponseSchema struct {
	Name        string
	Description string
}

ResponseSchema is struct used in the structured output parser to describe how the llm should format its response. Name is a key in the parsed output map. Description is a description of what the value should contain.

type Simple

type Simple struct{}

Simple is an output parser that does nothing.

func NewSimple

func NewSimple() Simple

func (Simple) GetFormatInstructions

func (p Simple) GetFormatInstructions() string

func (Simple) Parse

func (p Simple) Parse(text string) (any, error)

func (Simple) ParseWithPrompt

func (p Simple) ParseWithPrompt(text string, _ llms.PromptValue) (any, error)

func (Simple) Type

func (p Simple) Type() string

type Structured

type Structured struct {
	ResponseSchemas []ResponseSchema
}

Structured is an output parser that parses the output of an LLM into key value pairs. The name and description of what values the output of the llm should contain is stored in a list of response schema.

func NewStructured

func NewStructured(schema []ResponseSchema) Structured

NewStructured is a function that creates a new structured output parser from a list of response schemas.

func (Structured) GetFormatInstructions

func (p Structured) GetFormatInstructions() string

GetFormatInstructions returns a string explaining how the llm should format its response.

func (Structured) Parse

func (p Structured) Parse(text string) (any, error)

func (Structured) ParseWithPrompt

func (p Structured) ParseWithPrompt(text string, _ llms.PromptValue) (any, error)

ParseWithPrompt does the same as Parse.

func (Structured) Type

func (p Structured) Type() string

Type returns the type of the output parser.

Jump to

Keyboard shortcuts

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