chain

package
v0.0.103 Latest Latest
Warning

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

Go to latest
Published: Mar 10, 2024 License: MIT Imports: 20 Imported by: 0

Documentation

Overview

Package chain enables the creation and execution of chains, which are sequences of calls to LLMs or other utilities. It provides a standardized interface for working with chains and allows for seamless integration with various tools and services.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNoInputValues        = errors.New("no input values")
	ErrInvalidInputValues   = errors.New("invalid input values")
	ErrInputValuesWrongType = errors.New("input key is of wrong type")
	ErrNoOutputParser       = errors.New("no output parser")
)
View Source
var DefaultStructuredOutputTemplate = StructuredOutputOptions{
	CallbackOptions: &schema.CallbackOptions{},
	Prompt: prompt.NewChatTemplate([]prompt.MessageTemplate{
		prompt.NewHumanMessageTemplate(defaultStructuredOutputTemplate),
	}),
	OutputKey: "output",
}

Functions

This section is empty.

Types

type API added in v0.0.50

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

API represents a chain that makes API calls based on given API documentation and user questions.

WARNING: The API chain has the potential to be susceptible to Server-Side Request Forgery (SSRF) attacks if not used carefully and securely. SSRF allows an attacker to manipulate the server into making unintended and unauthorized requests to internal or external resources, which can lead to potential security breaches and unauthorized access to sensitive information.

To mitigate the risks associated with SSRF attacks, it is strongly advised to use the VerifyURL hook diligently. The VerifyURL hook should be implemented to validate and ensure that the generated URLs are restricted to authorized and safe resources only. By doing so, unauthorized access to sensitive resources can be prevented, and the application's security can be significantly enhanced.

It is the responsibility of developers and administrators to ensure the secure usage of the API chain. We strongly recommend thorough testing, security reviews, and adherence to secure coding practices to protect against potential security threats, including SSRF and other vulnerabilities.

func NewAPI added in v0.0.50

func NewAPI(model schema.Model, apiDoc string, optFns ...func(o *APIOptions)) (*API, error)

NewAPI creates a new instance of API with the given model, API documentation, and optional functions to set options.

func (*API) Call added in v0.0.50

func (c *API) Call(ctx context.Context, inputs schema.ChainValues, optFns ...func(o *schema.CallOptions)) (schema.ChainValues, error)

Call executes the api chain with the given context and inputs. It returns the outputs of the chain or an error, if any.

func (*API) Callbacks added in v0.0.50

func (c *API) Callbacks() []schema.Callback

Callbacks returns the callbacks associated with the chain.

func (*API) InputKeys added in v0.0.50

func (c *API) InputKeys() []string

InputKeys returns the expected input keys.

func (*API) Memory added in v0.0.50

func (c *API) Memory() schema.Memory

Memory returns the memory associated with the chain.

func (*API) OutputKeys added in v0.0.50

func (c *API) OutputKeys() []string

OutputKeys returns the output keys the chain will return.

func (*API) Type added in v0.0.50

func (c *API) Type() string

Type returns the type of the chain.

func (*API) Verbose added in v0.0.50

func (c *API) Verbose() bool

Verbose returns the verbosity setting of the chain.

type APIOptions added in v0.0.50

type APIOptions struct {
	// CallbackOptions contains options for the chain callbacks.
	*schema.CallbackOptions

	// InputKey is the key to access the input value containing the user question.
	InputKey string

	// OutputKey is the key to access the output value containing the API response summary.
	OutputKey string

	// HTTPClient is the HTTP client used for making API requests.
	HTTPClient HTTPClient

	// Header is a map containing additional headers to be included in the API request.
	Header map[string]string

	// VerifyURL is a function used to verify the validity of the generated API URL before making the request.
	// It returns true if the URL is valid, false otherwise.
	VerifyURL VerifyURL
}

type ChatModel added in v0.0.59

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

ChatModel represents a chain that interacts with a ChatModel and a prompt template.

func NewChatModel added in v0.0.59

func NewChatModel(chatModel schema.ChatModel, prompt prompt.ChatTemplate, optFns ...func(o *ChatModelOptions)) (*ChatModel, error)

NewChatModel creates a new ChatModel chain with the given ChatModel and prompt template.

func NewChatModelWithFunctions added in v0.0.59

func NewChatModelWithFunctions(chatModel schema.ChatModel, prompt prompt.ChatTemplate, functions []schema.FunctionDefinition, optFns ...func(o *ChatModelOptions)) (*ChatModel, error)

NewChatModelWithFunctions creates a new ChatModel chain with the given ChatModel, prompt template, and function definitions.

func (*ChatModel) Call added in v0.0.59

func (c *ChatModel) Call(ctx context.Context, inputs schema.ChainValues, optFns ...func(o *schema.CallOptions)) (schema.ChainValues, error)

Call executes the ChatModel chain with the given context and inputs. It returns the outputs of the chain or an error, if any.

func (*ChatModel) Callbacks added in v0.0.59

func (c *ChatModel) Callbacks() []schema.Callback

Callbacks returns the callbacks associated with the chain.

func (*ChatModel) InputKeys added in v0.0.59

func (c *ChatModel) InputKeys() []string

InputKeys returns the expected input keys.

func (*ChatModel) Memory added in v0.0.59

func (c *ChatModel) Memory() schema.Memory

Memory returns the memory associated with the chain.

func (*ChatModel) OutputKeys added in v0.0.59

func (c *ChatModel) OutputKeys() []string

OutputKeys returns the output keys the chain will return.

func (*ChatModel) Type added in v0.0.59

func (c *ChatModel) Type() string

Type returns the type of the chain.

func (*ChatModel) Verbose added in v0.0.59

func (c *ChatModel) Verbose() bool

Verbose returns the verbosity setting of the chain.

type ChatModelOptions added in v0.0.59

type ChatModelOptions struct {
	// CallbackOptions contains options for the chain callbacks.
	*schema.CallbackOptions

	// ForceFunctionCall forced the model to call the first function
	ForceFunctionCall bool

	// OutputKey is the key to access the output value containing the ChatModel response summary.
	OutputKey string
}

ChatModelOptions contains options for the ChatModel chain.

type Conversation added in v0.0.10

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

func NewConversation added in v0.0.10

func NewConversation(model schema.Model, optFns ...func(o *ConversationOptions)) (*Conversation, error)

func (*Conversation) Call added in v0.0.10

func (c *Conversation) Call(ctx context.Context, inputs schema.ChainValues, optFns ...func(o *schema.CallOptions)) (schema.ChainValues, error)

Call executes the conversation chain with the given context and inputs. It returns the outputs of the chain or an error, if any.

func (*Conversation) Callbacks added in v0.0.13

func (c *Conversation) Callbacks() []schema.Callback

Callbacks returns the callbacks associated with the chain.

func (*Conversation) InputKeys added in v0.0.10

func (c *Conversation) InputKeys() []string

InputKeys returns the expected input keys.

func (*Conversation) Memory added in v0.0.13

func (c *Conversation) Memory() schema.Memory

Memory returns the memory associated with the chain.

func (*Conversation) OutputKeys added in v0.0.10

func (c *Conversation) OutputKeys() []string

OutputKeys returns the output keys the chain will return.

func (*Conversation) Prompt added in v0.0.10

func (c *Conversation) Prompt() schema.PromptTemplate

func (*Conversation) Type added in v0.0.13

func (c *Conversation) Type() string

Type returns the type of the chain.

func (*Conversation) Verbose added in v0.0.13

func (c *Conversation) Verbose() bool

Verbose returns the verbosity setting of the chain.

type ConversationOptions added in v0.0.10

type ConversationOptions struct {
	*schema.CallbackOptions
	Prompt       schema.PromptTemplate
	Memory       schema.Memory
	OutputKey    string
	OutputParser schema.OutputParser[any]
	// ReturnFinalOnly determines whether to return only the final parsed result or include extra generation information.
	// When set to true (default), the field will return only the final parsed result.
	// If set to false, the field will include additional information about the generation along with the final parsed result.
	ReturnFinalOnly bool
}

type HTTPClient added in v0.0.50

type HTTPClient interface {
	Do(req *http.Request) (*http.Response, error)
}

HTTPClient is an interface for making HTTP requests.

type LLM added in v0.0.16

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

LLM is a chain implementation that uses the Language Model (LLM) to generate text based on a given prompt.

func NewLLM added in v0.0.16

func NewLLM(model schema.Model, prompt schema.PromptTemplate, optFns ...func(o *LLMOptions)) (*LLM, error)

NewLLM creates a new instance of the LLM chain.

func (*LLM) Call added in v0.0.16

func (c *LLM) Call(ctx context.Context, inputs schema.ChainValues, optFns ...func(o *schema.CallOptions)) (schema.ChainValues, error)

Call executes the llm chain with the given context and inputs. It returns the outputs of the chain or an error, if any.

func (*LLM) Callbacks added in v0.0.16

func (c *LLM) Callbacks() []schema.Callback

Callbacks returns the callbacks associated with the chain.

func (*LLM) GetNumTokens added in v0.0.39

func (c *LLM) GetNumTokens(ctx context.Context, text string) (uint, error)

GetNumTokens returns the number of tokens in the given text for the associated Language Model (LLM).

func (*LLM) InputKeys added in v0.0.16

func (c *LLM) InputKeys() []string

InputKeys returns the expected input keys.

func (*LLM) Memory added in v0.0.16

func (c *LLM) Memory() schema.Memory

Memory returns the memory associated with the chain.

func (*LLM) OutputKeys added in v0.0.16

func (c *LLM) OutputKeys() []string

OutputKeys returns the output keys the chain will return.

func (*LLM) Prompt added in v0.0.16

func (c *LLM) Prompt() schema.PromptTemplate

Prompt returns the prompt.Template associated with the chain.

func (*LLM) Type added in v0.0.16

func (c *LLM) Type() string

Type returns the type of the chain.

func (*LLM) Verbose added in v0.0.16

func (c *LLM) Verbose() bool

Verbose returns the verbosity setting of the chain.

type LLMOptions added in v0.0.16

type LLMOptions struct {
	// CallbackOptions contains options for the chain callbacks.
	*schema.CallbackOptions

	// Memory is the schema.Memory to be associated with the chain.
	Memory schema.Memory

	// OutputKey is the key to access the output value containing the LLM text generation.
	OutputKey string

	// OutputParser is the schema.OutputParser[any] instance used to parse the LLM text generation result.
	OutputParser schema.OutputParser[any]

	// ReturnFinalOnly determines whether to return only the final parsed result or include extra generation information.
	// When set to true (default), the field will return only the final parsed result.
	// If set to false, the field will include additional information about the generation along with the final parsed result.
	ReturnFinalOnly bool
}

LLMOptions contains options for the LLM chain.

type Math added in v0.0.51

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

Math is a chain implementation that prompts the user to provide a math problem in the form of a single-line mathematical expression that can be executed using a golang expr library. It then translates the expression and evaluates it to provide the result.

func NewMath added in v0.0.51

func NewMath(model schema.Model, optFns ...func(o *MathOptions)) (*Math, error)

NewMath creates a new instance of the Math chain.

func (*Math) Call added in v0.0.51

func (c *Math) Call(ctx context.Context, inputs schema.ChainValues, optFns ...func(o *schema.CallOptions)) (schema.ChainValues, error)

Call executes the math chain with the given context and inputs. It returns the outputs of the chain or an error, if any.

func (*Math) Callbacks added in v0.0.51

func (c *Math) Callbacks() []schema.Callback

Callbacks returns the callbacks associated with the chain.

func (*Math) InputKeys added in v0.0.51

func (c *Math) InputKeys() []string

InputKeys returns the expected input keys.

func (*Math) Memory added in v0.0.51

func (c *Math) Memory() schema.Memory

Memory returns the memory associated with the chain.

func (*Math) OutputKeys added in v0.0.51

func (c *Math) OutputKeys() []string

OutputKeys returns the output keys the chain will return.

func (*Math) Type added in v0.0.51

func (c *Math) Type() string

Type returns the type of the chain.

func (*Math) Verbose added in v0.0.51

func (c *Math) Verbose() bool

Verbose returns the verbosity setting of the chain.

type MathOptions added in v0.0.51

type MathOptions struct {
	// CallbackOptions contains options for the chain callbacks.
	*schema.CallbackOptions

	// InputKey is the key to access the input value containing the user question.
	InputKey string

	// OutputKey is the key to access the output value containing the math expression result.
	OutputKey string
}

MathOptions contains options for the Math chain.

type OutputCandidate added in v0.0.60

type OutputCandidate struct {
	Name        string
	Description string
	Data        any
}

OutputCandidate represents a candidate for structured output containing a name, description, and data of any struct type.

type SQL added in v0.0.49

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

SQL is a chain implementation that prompts the user to provide an SQL query to run on a database. It then verifies and executes the provided SQL query and returns the result.

WARNING: The SQL chain is a powerful tool for executing SQL queries dynamically. However, it should be used with caution to prevent potential SQL injection vulnerabilities. SQL injection is a serious security risk that can lead to unauthorized access, data manipulation, and potentially compromising the entire database.

To mitigate the risks of SQL injection, it is crucial to follow these best practices while using the SQL chain:

  • Least Privilege Principle: Ensure that the database user used in the application has the least privilege necessary to perform its required tasks. Restrict the user's permissions to only the required tables and operations.

  • Table Whitelisting or Blacklisting: Use the Tables or Exclude options to reduce the allowed tables that can be accessed via the SQL chain. This will limit the potential impact of any SQL injection attack by restricting the scope of accessible tables.

  • VerifySQL Hook: Implement the VerifySQL hook diligently to validate and sanitize user input. This hook should be used to check and ensure that the generated SQL queries are safe and adhere to the allowed tables and queries.

It is the responsibility of the application developers and administrators to ensure the secure usage of the SQL chain. Failure to do so can lead to severe security breaches and compromise the integrity of the application and database. We strongly recommend thorough testing, security reviews, and adherence to secure coding practices to protect against SQL injection and other security threats.

func NewSQL added in v0.0.49

func NewSQL(model schema.Model, engine sqldb.Engine, optFns ...func(o *SQLOptions)) (*SQL, error)

NewSQL creates a new instance of the SQL chain.

func (*SQL) Call added in v0.0.49

func (c *SQL) Call(ctx context.Context, inputs schema.ChainValues, optFns ...func(o *schema.CallOptions)) (schema.ChainValues, error)

Call executes the sql chain with the given context and inputs. It returns the outputs of the chain or an error, if any.

func (*SQL) Callbacks added in v0.0.49

func (c *SQL) Callbacks() []schema.Callback

Callbacks returns the callbacks associated with the chain.

func (*SQL) InputKeys added in v0.0.49

func (c *SQL) InputKeys() []string

InputKeys returns the expected input keys.

func (*SQL) Memory added in v0.0.49

func (c *SQL) Memory() schema.Memory

Memory returns the memory associated with the chain.

func (*SQL) OutputKeys added in v0.0.49

func (c *SQL) OutputKeys() []string

OutputKeys returns the output keys the chain will return.

func (*SQL) Type added in v0.0.49

func (c *SQL) Type() string

Type returns the type of the chain.

func (*SQL) Verbose added in v0.0.49

func (c *SQL) Verbose() bool

Verbose returns the verbosity setting of the chain.

type SQLOptions added in v0.0.49

type SQLOptions struct {
	// CallbackOptions contains options for the chain callbacks.
	*schema.CallbackOptions

	// InputKey is the key to access the input value containing the user SQL query.
	InputKey string

	// OutputKey is the key to access the output value containing the SQL query result.
	OutputKey string

	// TopK specifies the maximum number of results to return from the SQL query.
	TopK uint

	// Schema represents the database schema information.
	Schema string

	// Tables is the list of tables to consider when executing the SQL query.
	Tables []string

	// Exclude is the list of tables to exclude when executing the SQL query.
	Exclude []string

	// SampleRowsinTableInfo specifies the number of sample rows to include in the table information.
	SampleRowsinTableInfo uint

	// VerifySQL is a function used to verify the validity of the generated SQL query before execution.
	// It should return true if the SQL query is valid, false otherwise.
	VerifySQL VerifySQL
}

SQLOptions contains options for the SQL chain.

type Sequential added in v0.0.21

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

func NewSequential added in v0.0.21

func NewSequential(chains []schema.Chain, inputKeys []string, optFns ...func(o *SequentialOptions)) (*Sequential, error)

func (*Sequential) Call added in v0.0.21

func (c *Sequential) Call(ctx context.Context, inputs schema.ChainValues, optFns ...func(o *schema.CallOptions)) (schema.ChainValues, error)

Call executes the sequential chain with the given context and inputs. It returns the outputs of the chain or an error, if any.

func (*Sequential) Callbacks added in v0.0.21

func (c *Sequential) Callbacks() []schema.Callback

Callbacks returns the callbacks associated with the chain.

func (*Sequential) InputKeys added in v0.0.21

func (c *Sequential) InputKeys() []string

InputKeys returns the expected input keys.

func (*Sequential) Memory added in v0.0.21

func (c *Sequential) Memory() schema.Memory

Memory returns the memory associated with the chain.

func (*Sequential) OutputKeys added in v0.0.21

func (c *Sequential) OutputKeys() []string

OutputKeys returns the output keys the chain will return.

func (*Sequential) Type added in v0.0.21

func (c *Sequential) Type() string

Type returns the type of the chain.

func (*Sequential) Verbose added in v0.0.21

func (c *Sequential) Verbose() bool

Verbose returns the verbosity setting of the chain.

type SequentialOptions added in v0.0.21

type SequentialOptions struct {
	*schema.CallbackOptions
	Memory     schema.Memory
	OutputKeys []string
	ReturnAll  bool
}

type SimpleSequential added in v0.0.21

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

func NewSimpleSequential added in v0.0.21

func NewSimpleSequential(chains []schema.Chain, optFns ...func(o *SimpleSequentialOptions)) (*SimpleSequential, error)

func (*SimpleSequential) Call added in v0.0.21

func (c *SimpleSequential) Call(ctx context.Context, inputs schema.ChainValues, optFns ...func(o *schema.CallOptions)) (schema.ChainValues, error)

Call executes the SimpleSequential chain with the given context and inputs. It returns the outputs of the chain or an error, if any.

func (*SimpleSequential) Callbacks added in v0.0.21

func (c *SimpleSequential) Callbacks() []schema.Callback

Callbacks returns the callbacks associated with the chain.

func (*SimpleSequential) InputKeys added in v0.0.21

func (c *SimpleSequential) InputKeys() []string

InputKeys returns the expected input keys.

func (*SimpleSequential) Memory added in v0.0.21

func (c *SimpleSequential) Memory() schema.Memory

Memory returns the memory associated with the chain.

func (*SimpleSequential) OutputKeys added in v0.0.21

func (c *SimpleSequential) OutputKeys() []string

OutputKeys returns the output keys the chain will return.

func (*SimpleSequential) Type added in v0.0.21

func (c *SimpleSequential) Type() string

Type returns the type of the chain.

func (*SimpleSequential) Verbose added in v0.0.21

func (c *SimpleSequential) Verbose() bool

Verbose returns the verbosity setting of the chain.

type SimpleSequentialOptions added in v0.0.21

type SimpleSequentialOptions struct {
	*schema.CallbackOptions
	Memory       schema.Memory
	InputKey     string
	OutputKey    string
	StripOutputs bool
}

type StructuredOutput added in v0.0.59

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

StructuredOutput is a chain that generates structured output using a ChatModel chain and candidate values.

func NewStructuredOutput added in v0.0.59

func NewStructuredOutput(chatModel schema.ChatModel, candidates []OutputCandidate, optFns ...func(o *StructuredOutputOptions)) (*StructuredOutput, error)

NewStructuredOutput creates a new StructuredOutput chain with the given ChatModel, prompt, and candidates.

func (*StructuredOutput) Call added in v0.0.59

func (c *StructuredOutput) Call(ctx context.Context, inputs schema.ChainValues, optFns ...func(o *schema.CallOptions)) (schema.ChainValues, error)

Call executes the StructuredOutput chain with the given context and inputs. It returns the outputs of the chain or an error, if any.

func (*StructuredOutput) Callbacks added in v0.0.59

func (c *StructuredOutput) Callbacks() []schema.Callback

Callbacks returns the callbacks associated with the chain.

func (*StructuredOutput) InputKeys added in v0.0.59

func (c *StructuredOutput) InputKeys() []string

InputKeys returns the expected input keys.

func (*StructuredOutput) Memory added in v0.0.59

func (c *StructuredOutput) Memory() schema.Memory

Memory returns the memory associated with the chain.

func (*StructuredOutput) OutputKeys added in v0.0.59

func (c *StructuredOutput) OutputKeys() []string

OutputKeys returns the output keys the chain will return.

func (*StructuredOutput) Type added in v0.0.59

func (c *StructuredOutput) Type() string

Type returns the type of the chain.

func (*StructuredOutput) Verbose added in v0.0.59

func (c *StructuredOutput) Verbose() bool

Verbose returns the verbosity setting of the chain.

type StructuredOutputOptions added in v0.0.59

type StructuredOutputOptions struct {
	*schema.CallbackOptions
	Prompt    prompt.ChatTemplate
	OutputKey string
}

StructuredOutputOptions contains options for configuring the StructuredOutput chain.

type Tagging added in v0.0.60

type Tagging struct {
	// StructuredOutput is the underlying structured output chain used for tagging.
	*StructuredOutput
}

Tagging is a chain that uses structured output to perform tagging on a passage. It extracts the desired information from the given passage using a structured output model.

func NewTagging added in v0.0.60

func NewTagging(chatModel schema.ChatModel, data any, optFns ...func(o *StructuredOutputOptions)) (*Tagging, error)

NewTagging creates a new Tagging chain with the provided chat model, structured output data, and optional options. It returns a Tagging chain or an error if the creation fails.

func (*Tagging) Type added in v0.0.60

func (c *Tagging) Type() string

Type returns the type of the chain.

type Transform added in v0.0.17

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

func NewTransform added in v0.0.17

func NewTransform(inputKeys, outputKeys []string, transform TransformFunc, optFns ...func(o *TransformOptions)) (*Transform, error)

func (*Transform) Call added in v0.0.17

func (c *Transform) Call(ctx context.Context, inputs schema.ChainValues, optFns ...func(o *schema.CallOptions)) (schema.ChainValues, error)

Call executes the transform chain with the given context and inputs. It returns the outputs of the chain or an error, if any.

func (*Transform) Callbacks added in v0.0.17

func (c *Transform) Callbacks() []schema.Callback

Callbacks returns the callbacks associated with the chain.

func (*Transform) InputKeys added in v0.0.17

func (c *Transform) InputKeys() []string

InputKeys returns the expected input keys.

func (*Transform) Memory added in v0.0.17

func (c *Transform) Memory() schema.Memory

Memory returns the memory associated with the chain.

func (*Transform) OutputKeys added in v0.0.17

func (c *Transform) OutputKeys() []string

OutputKeys returns the output keys the chain will return.

func (*Transform) Type added in v0.0.17

func (c *Transform) Type() string

Type returns the type of the chain.

func (*Transform) Verbose added in v0.0.17

func (c *Transform) Verbose() bool

Verbose returns the verbosity setting of the chain.

type TransformFunc added in v0.0.6

type TransformFunc func(ctx context.Context, inputs schema.ChainValues, optFns ...func(o *schema.CallOptions)) (schema.ChainValues, error)

type TransformOptions added in v0.0.17

type TransformOptions struct {
	*schema.CallbackOptions
}

type VerifySQL added in v0.0.53

type VerifySQL func(sqlQuery string) bool

VerifySQL is a function signature used to verify the validity of the generated SQL query before execution.

type VerifyURL added in v0.0.53

type VerifyURL func(url string) bool

VerifyURL is the function signature for verifying the API URL.

Jump to

Keyboard shortcuts

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