outputparser

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 13, 2023 License: MIT Imports: 6 Imported by: 7

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.
  • 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 {
	TrueStr  string
	FalseStr 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, _ schema.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, _ schema.PromptValue) (any, error)

Parse 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, _ schema.PromptValue) ([]string, error)

Parse with prompts does the same as Parse.

func (CommaSeparatedList) Type

func (p CommaSeparatedList) Type() string

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, _ schema.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, _ schema.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, _ schema.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, _ schema.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