prompt

package
v0.0.110 Latest Latest
Warning

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

Go to latest
Published: Mar 29, 2024 License: MIT Imports: 9 Imported by: 1

Documentation

Overview

Package prompt provides utilities for managing and optimizing prompts.

Index

Constants

This section is empty.

Variables

View Source
var DefaultTemplateOptions = TemplateOptions{
	Language:                "en",
	TransformPythonTemplate: false,
	FormatterOptions: FormatterOptions{
		IgnoreMissingKeys: false,
	},
}
View Source
var (
	ErrInvalidPartialVariableType = errors.New("invalid partial variable type")
)

Functions

func IsChatModel added in v0.0.78

func IsChatModel(model schema.Model) bool

IsChatModel checks if the given model is of type schema.ChatModel.

func IsLLM added in v0.0.78

func IsLLM(model schema.Model) bool

IsLLM checks if the given model is of type schema.LLM.

func ListTemplateFields

func ListTemplateFields(t *template.Template) []string

Types

type AIMessageTemplate added in v0.0.26

type AIMessageTemplate struct {
	// contains filtered or unexported fields
}

AIMessageTemplate represents an AI message template.

func NewAIMessageTemplate added in v0.0.26

func NewAIMessageTemplate(template string, optFns ...func(o *TemplateOptions)) *AIMessageTemplate

NewAIMessageTemplate creates a new AIMessageTemplate with the given template.

func (*AIMessageTemplate) Format added in v0.0.26

func (pt *AIMessageTemplate) Format(values map[string]any) (schema.ChatMessage, error)

Format formats the message using the provided values and returns an AIChatMessage.

func (*AIMessageTemplate) FormatPrompt added in v0.0.42

func (mt *AIMessageTemplate) FormatPrompt(values map[string]any) (schema.PromptValue, error)

func (*AIMessageTemplate) InputVariables added in v0.0.59

func (pt *AIMessageTemplate) InputVariables() []string

InputVariables returns the input variables used in the AI message template.

type ChatPromptValue

type ChatPromptValue struct {
	// contains filtered or unexported fields
}

ChatPromptValue represents a chat prompt value containing chat messages.

func NewChatPromptValue added in v0.0.29

func NewChatPromptValue(messages schema.ChatMessages) *ChatPromptValue

NewChatPromptValue creates a new ChatPromptValue with the given chat messages.

func (ChatPromptValue) Messages added in v0.0.29

func (v ChatPromptValue) Messages() schema.ChatMessages

Messages returns the chat messages contained in the ChatPromptValue.

func (ChatPromptValue) String added in v0.0.29

func (v ChatPromptValue) String() string

String returns a string representation of the ChatPromptValue.

type ChatTemplate

type ChatTemplate interface {
	schema.PromptTemplate
	FormatMessages(values map[string]any) (schema.ChatMessages, error)
}

ChatTemplate represents a chat template.

func NewChatTemplate added in v0.0.29

func NewChatTemplate(messageTemplates []MessageTemplate) ChatTemplate

NewChatTemplate creates a new ChatTemplate with the given message templates.

func NewChatTemplateWrapper added in v0.0.29

func NewChatTemplateWrapper(chatTemplates ...ChatTemplate) ChatTemplate

NewChatTemplateWrapper creates a new ChatTemplate that wraps multiple ChatTemplates.

func NewMessagesPlaceholder added in v0.0.29

func NewMessagesPlaceholder(inputKey string) ChatTemplate

NewMessagesPlaceholder creates a new ChatTemplate placeholder for chat messages.

type Conditional added in v0.0.78

type Conditional struct {
	Condition ConditionalFunc
	Prompt    schema.PromptTemplate
}

Conditional represents a conditional prompt configuration.

type ConditionalFunc added in v0.0.78

type ConditionalFunc func(model schema.Model) bool

ConditionalFunc represents a function that evaluates a condition based on a model.

type ConditionalPromptSelector added in v0.0.78

type ConditionalPromptSelector struct {
	DefaultPrompt schema.PromptTemplate
	Conditionals  []Conditional
}

ConditionalPromptSelector is a prompt selector that selects prompts based on conditions.

func (*ConditionalPromptSelector) GetPrompt added in v0.0.78

GetPrompt selects a prompt template based on the provided model. It evaluates the conditions in order and returns the prompt associated with the first matching condition, or returns the default prompt if no condition is met.

type FewShotTemplate added in v0.0.69

type FewShotTemplate struct {
	// contains filtered or unexported fields
}

FewShotTemplate is a template that combines examples with a main template.

func NewFewShotTemplate added in v0.0.69

func NewFewShotTemplate(template string, examples []map[string]any, exampleTemplate *Template, optFns ...func(o *FewShotTemplateOptions)) *FewShotTemplate

NewFewShotTemplate creates a new FewShotTemplate with the provided template, examples, and options.

func (*FewShotTemplate) Format added in v0.0.69

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

Format applies values to the template and returns the formatted result.

func (*FewShotTemplate) FormatPrompt added in v0.0.69

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

FormatPrompt applies values to the template and returns a PromptValue representation of the formatted result.

func (*FewShotTemplate) InputVariables added in v0.0.69

func (p *FewShotTemplate) InputVariables() []string

InputVariables returns the input variables used in the template.

func (*FewShotTemplate) OutputParser added in v0.0.69

func (p *FewShotTemplate) OutputParser() (schema.OutputParser[any], bool)

OutputParser returns the output parser function and a boolean indicating if an output parser is defined.

func (*FewShotTemplate) Partial added in v0.0.69

func (p *FewShotTemplate) Partial(values map[string]any) schema.PromptTemplate

Partial creates a new FewShotTemplate with partial values.

type FewShotTemplateOptions added in v0.0.69

type FewShotTemplateOptions struct {
	// Prefix to be added before the template.
	Prefix string
	// Separator between examples and the template.
	Separator string
	// OutputParser to parse the response.
	OutputParser schema.OutputParser[any]
	// PartialValues to be used in the template.
	PartialValues map[string]any
	// IgnoreMissingKeys allows ignoring missing keys in the template.
	IgnoreMissingKeys bool
}

FewShotTemplateOptions represents options for configuring a FewShotTemplate.

type Formatter

type Formatter struct {
	// contains filtered or unexported fields
}

func NewFormatter

func NewFormatter(text string, optFns ...func(o *FormatterOptions)) *Formatter

func (*Formatter) Fields

func (pt *Formatter) Fields() []string

func (*Formatter) Render

func (pt *Formatter) Render(values map[string]any) (string, error)

type FormatterOptions added in v0.0.66

type FormatterOptions struct {
	IgnoreMissingKeys bool
	TemplateFuncMap   template.FuncMap
}

type HumanMessageTemplate

type HumanMessageTemplate struct {
	// contains filtered or unexported fields
}

HumanMessageTemplate represents a human message template.

func NewHumanMessageTemplate added in v0.0.26

func NewHumanMessageTemplate(template string, optFns ...func(o *TemplateOptions)) *HumanMessageTemplate

NewHumanMessageTemplate creates a new HumanMessageTemplate with the given template.

func (*HumanMessageTemplate) Format

func (pt *HumanMessageTemplate) Format(values map[string]any) (schema.ChatMessage, error)

Format formats the message using the provided values and returns a HumanChatMessage.

func (*HumanMessageTemplate) FormatPrompt added in v0.0.42

func (mt *HumanMessageTemplate) FormatPrompt(values map[string]any) (schema.PromptValue, error)

func (*HumanMessageTemplate) InputVariables added in v0.0.59

func (pt *HumanMessageTemplate) InputVariables() []string

InputVariables returns the input variables used in the human message template.

type MessageTemplate added in v0.0.29

type MessageTemplate interface {
	Format(values map[string]any) (schema.ChatMessage, error)
	FormatPrompt(values map[string]any) (schema.PromptValue, error)
	InputVariables() []string
}

MessageTemplate represents a chat message template.

type PromptSelector added in v0.0.78

type PromptSelector interface {
	GetPrompt(model schema.Model) schema.PromptTemplate
}

PromptSelector is an interface for selecting prompts based on a model.

type StringPromptValue

type StringPromptValue string

StringPromptValue represents a string value that satisfies the PromptValue interface.

func (StringPromptValue) Messages

func (v StringPromptValue) Messages() schema.ChatMessages

Messages returns a ChatMessages slice containing a single HumanChatMessage with the string value.

func (StringPromptValue) String

func (v StringPromptValue) String() string

String returns the string representation of the StringPromptValue.

type SystemMessageTemplate

type SystemMessageTemplate struct {
	// contains filtered or unexported fields
}

SystemMessageTemplate represents a system message template.

func NewSystemMessageTemplate

func NewSystemMessageTemplate(template string, optFns ...func(o *TemplateOptions)) *SystemMessageTemplate

NewSystemMessageTemplate creates a new SystemMessageTemplate with the given template.

func (*SystemMessageTemplate) Format

func (pt *SystemMessageTemplate) Format(values map[string]any) (schema.ChatMessage, error)

Format formats the message using the provided values and returns a SystemChatMessage.

func (*SystemMessageTemplate) FormatPrompt added in v0.0.42

func (mt *SystemMessageTemplate) FormatPrompt(values map[string]any) (schema.PromptValue, error)

func (*SystemMessageTemplate) InputVariables added in v0.0.59

func (pt *SystemMessageTemplate) InputVariables() []string

InputVariables returns the input variables used in the system message template.

type Template

type Template struct {
	// contains filtered or unexported fields
}

Template represents a template that can be formatted with dynamic values.

func NewTemplate

func NewTemplate(template string, optFns ...func(o *TemplateOptions)) *Template

NewTemplate creates a new Template with the provided template and options.

func (*Template) Format

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

Format applies values to the template and returns the formatted result.

func (*Template) FormatPrompt

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

FormatPrompt applies values to the template and returns a PromptValue representation of the formatted result.

func (*Template) InputVariables added in v0.0.2

func (p *Template) InputVariables() []string

InputVariables returns the input variables used in the template.

func (*Template) OutputParser added in v0.0.3

func (p *Template) OutputParser() (schema.OutputParser[any], bool)

OutputParser returns the output parser function and a boolean indicating if an output parser is defined.

func (*Template) Partial

func (p *Template) Partial(values map[string]any) schema.PromptTemplate

Partial creates a new Template with partial values.

type TemplateOptions

type TemplateOptions struct {
	PartialValues           map[string]any
	Language                string
	OutputParser            schema.OutputParser[any]
	TransformPythonTemplate bool
	FormatterOptions
}

TemplateOptions defines the options for configuring a Template.

Jump to

Keyboard shortcuts

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