prompts

package
v0.4.7 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2023 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package prompts contains types, prompt templates, loading utilities, output parsers, example selectors, and other utilities for working with LLM prompts..

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNoExample is returned when none of the Examples and ExampleSelector are provided.
	ErrNoExample = errors.New("no example is provided")
	// ErrExamplesAndExampleSelectorProvided is returned when there are no Examples and ExampleSelector.
	ErrExamplesAndExampleSelectorProvided = errors.New("only one of 'Examples' and 'example_selector' should be" +
		" provided")
)
View Source
var (
	// ErrInputVariableReserved is returned when there is a conflict with a reserved variable name.
	ErrInputVariableReserved = errors.New("conflict with reserved variable name")
	// ErrInvalidPartialVariableType is returned when the partial variable is not a string or a function.
	ErrInvalidPartialVariableType = errors.New("invalid partial variable type")
)
View Source
var ErrInvalidTemplateFormat = errors.New("invalid template format")

ErrInvalidTemplateFormat is the error when the template format is invalid and not supported.

Functions

func CheckValidTemplate

func CheckValidTemplate(template string, templateFormat TemplateFormat, inputVariables []string) error

CheckValidTemplate checks if the template is valid through checking whether the given TemplateFormat is available and whether the template can be rendered.

func RenderTemplate

func RenderTemplate(tmpl string, tmplFormat TemplateFormat, values map[string]any) (string, error)

RenderTemplate renders the template with the given values.

Types

type AIMessagePromptTemplate

type AIMessagePromptTemplate struct {
	Prompt PromptTemplate
}

AIMessagePromptTemplate is a message formatter that returns a AI message.

func NewAIMessagePromptTemplate

func NewAIMessagePromptTemplate(template string, inputVariables []string) AIMessagePromptTemplate

NewAIMessagePromptTemplate creates a new AI message prompt template.

func (AIMessagePromptTemplate) FormatMessages

func (p AIMessagePromptTemplate) FormatMessages(values map[string]any) ([]schema.ChatMessage, error)

FormatMessages formats the message with the values given.

func (AIMessagePromptTemplate) GetInputVariables

func (p AIMessagePromptTemplate) GetInputVariables() []string

GetInputVariables returns the input variables the prompt expects.

type ChatPromptTemplate

type ChatPromptTemplate struct {
	// Messages is the list of the messages to be formatted.
	Messages []MessageFormatter

	// PartialVariables represents a map of variable names to values or functions that return values.
	// If the value is a function, it will be called when the prompt template is rendered.
	PartialVariables map[string]any
}

ChatPromptTemplate is a prompt template for chat messages.

func NewChatPromptTemplate

func NewChatPromptTemplate(messages []MessageFormatter) ChatPromptTemplate

NewChatPromptTemplate creates a new chat prompt template from a list of message formatters.

func (ChatPromptTemplate) Format

func (p ChatPromptTemplate) Format(values map[string]any) (string, error)

Format formats the messages with values given and returns the messages as a string.

func (ChatPromptTemplate) FormatMessages

func (p ChatPromptTemplate) FormatMessages(values map[string]any) ([]schema.ChatMessage, error)

FormatMessages formats the messages with the values and returns the formatted messages.

func (ChatPromptTemplate) FormatPrompt

func (p ChatPromptTemplate) FormatPrompt(values map[string]any) (schema.PromptValue, error)

FormatPrompt formats the messages into a chat prompt value.

func (ChatPromptTemplate) GetInputVariables

func (p ChatPromptTemplate) GetInputVariables() []string

GetInputVariables returns the input variables the prompt expect.

type ChatPromptValue

type ChatPromptValue []schema.ChatMessage

ChatPromptValue is a prompt value that is a list of chat messages.

func (ChatPromptValue) Messages

func (v ChatPromptValue) Messages() []schema.ChatMessage

Messages returns the ChatMessage slice.

func (ChatPromptValue) String

func (v ChatPromptValue) String() string

String returns the chat message slice as a buffer string.

type ExampleSelector

type ExampleSelector interface {
	AddExample(example map[string]string) string
	SelectExamples(inputVariables map[string]string) []map[string]string
}

ExampleSelector is an interface for example selectors. It is equivalent to BaseExampleSelector in langchain and langchainjs.

type FewShotPrompt

type FewShotPrompt struct {
	// Examples to format into the prompt. Either this or ExamplePrompt should be provided.
	Examples []map[string]string
	// ExampleSelector to choose the examples to format into the prompt. Either this or Examples should be provided.
	ExampleSelector ExampleSelector
	// ExamplePrompt is used to format an individual example.
	ExamplePrompt PromptTemplate
	// A prompt template string to put before the examples.
	Prefix string
	// A prompt template string to put after the examples.
	Suffix string
	// A list of the names of the variables the prompt template expects.
	InputVariables map[string]any
	// Represents a map of variable names to values or functions that return values. If the value is a function, it will
	// be called when the prompt template is rendered.
	PartialVariables map[string]any
	// String separator used to join the prefix, the examples, and suffix.
	ExampleSeparator string
	// The format of the prompt template. Options are: 'f-string', 'jinja2'.
	TemplateFormat TemplateFormat
	// Whether to try validating the template.
	ValidateTemplate bool
}

FewShotPrompt contains fields for a few-shot prompt.

func NewFewShotPrompt

func NewFewShotPrompt(examplePrompt PromptTemplate, examples []map[string]string, exampleSelector ExampleSelector,
	prefix string, suffix string, input map[string]interface{}, partialInput map[string]interface{},
	exampleSeparator string, templateFormat TemplateFormat, validateTemplate bool,
) (*FewShotPrompt, error)

NewFewShotPrompt creates a new few-shot prompt with the given input. It returns error if there is no example, both examples and exampleSelector are provided, or CheckValidTemplate returns err when ValidateTemplate is true.

func (*FewShotPrompt) Format

func (p *FewShotPrompt) Format(values map[string]interface{}) (string, error)

Format assembles and formats the pieces of the prompt with the given input values and partial values.

type FormatPrompter

type FormatPrompter interface {
	FormatPrompt(values map[string]any) (schema.PromptValue, error)
	GetInputVariables() []string
}

FormatPrompter is an interface for formatting a map of values into a prompt.

type Formatter

type Formatter interface {
	Format(values map[string]any) (string, error)
}

Formatter is an interface for formatting a map of values into a string.

type GenericMessagePromptTemplate

type GenericMessagePromptTemplate struct {
	Prompt PromptTemplate
	Role   string
}

GenericMessagePromptTemplate is a message formatter that returns message with the specified speaker.

func NewGenericMessagePromptTemplate

func NewGenericMessagePromptTemplate(role, template string, inputVariables []string) GenericMessagePromptTemplate

NewGenericMessagePromptTemplate creates a new generic message prompt template.

func (GenericMessagePromptTemplate) FormatMessages

func (p GenericMessagePromptTemplate) FormatMessages(values map[string]any) ([]schema.ChatMessage, error)

FormatMessages formats the message with the values given.

func (GenericMessagePromptTemplate) GetInputVariables

func (p GenericMessagePromptTemplate) GetInputVariables() []string

GetInputVariables returns the input variables the prompt expects.

type HumanMessagePromptTemplate

type HumanMessagePromptTemplate struct {
	Prompt PromptTemplate
}

HumanMessagePromptTemplate is a message formatter that returns a human message.

func NewHumanMessagePromptTemplate

func NewHumanMessagePromptTemplate(template string, inputVariables []string) HumanMessagePromptTemplate

NewHumanMessagePromptTemplate creates a new human message prompt template.

func (HumanMessagePromptTemplate) FormatMessages

func (p HumanMessagePromptTemplate) FormatMessages(values map[string]any) ([]schema.ChatMessage, error)

FormatMessages formats the message with the values given.

func (HumanMessagePromptTemplate) GetInputVariables

func (p HumanMessagePromptTemplate) GetInputVariables() []string

GetInputVariables returns the input variables the prompt expects.

type MessageFormatter

type MessageFormatter interface {
	FormatMessages(values map[string]any) ([]schema.ChatMessage, error)
	GetInputVariables() []string
}

Formatter is an interface for formatting a map of values into a list of messages.

type PromptTemplate

type PromptTemplate struct {
	// Template is the prompt template.
	Template string

	// A list of variable names the prompt template expects.
	InputVariables []string

	// TemplateFormat is the format of the prompt template.
	TemplateFormat TemplateFormat

	// OutputParser is a function that parses the output of the prompt template.
	OutputParser schema.OutputParser[any]

	// PartialVariables represents a map of variable names to values or functions that return values.
	// If the value is a function, it will be called when the prompt template is rendered.
	PartialVariables map[string]any
}

PromptTemplate contains common fields for all prompt templates.

func NewPromptTemplate

func NewPromptTemplate(template string, inputVars []string) PromptTemplate

NewPromptTemplate returns a new prompt template.

func (PromptTemplate) Format

func (p PromptTemplate) Format(values map[string]any) (string, error)

Format formats the prompt template and returns a string value.

func (PromptTemplate) FormatPrompt

func (p PromptTemplate) FormatPrompt(values map[string]any) (schema.PromptValue, error)

FormatPrompt formats the prompt template and returns a string prompt value.

func (PromptTemplate) GetInputVariables

func (p PromptTemplate) GetInputVariables() []string

GetInputVariables returns the input variables the prompt expect.

type StringPromptValue

type StringPromptValue string

StringPromptValue is a prompt value that is a string.

func (StringPromptValue) Messages

func (v StringPromptValue) Messages() []schema.ChatMessage

Messages returns a single-element ChatMessage slice.

func (StringPromptValue) String

func (v StringPromptValue) String() string

type SystemMessagePromptTemplate

type SystemMessagePromptTemplate struct {
	Prompt PromptTemplate
}

SystemMessagePromptTemplate is a message formatter that returns a system message.

func NewSystemMessagePromptTemplate

func NewSystemMessagePromptTemplate(template string, inputVariables []string) SystemMessagePromptTemplate

NewSystemMessagePromptTemplate creates a new system message prompt template.

func (SystemMessagePromptTemplate) FormatMessages

func (p SystemMessagePromptTemplate) FormatMessages(values map[string]any) ([]schema.ChatMessage, error)

FormatMessages formats the message with the values given.

func (SystemMessagePromptTemplate) GetInputVariables

func (p SystemMessagePromptTemplate) GetInputVariables() []string

GetInputVariables returns the input variables the prompt expects.

type TemplateFormat

type TemplateFormat string

TemplateFormat is the format of the template.

const (
	// TemplateFormatGoTemplate is the format for go-template.
	TemplateFormatGoTemplate TemplateFormat = "go-template"
)

Jump to

Keyboard shortcuts

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