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 ¶
- Variables
- func IsChatModel(model schema.Model) bool
- func IsLLM(model schema.Model) bool
- type API
- func (c *API) Call(ctx context.Context, inputs schema.ChainValues, ...) (schema.ChainValues, error)
- func (c *API) Callbacks() []schema.Callback
- func (c *API) InputKeys() []string
- func (c *API) Memory() schema.Memory
- func (c *API) OutputKeys() []string
- func (c *API) Type() string
- func (c *API) Verbose() bool
- type APIOptions
- type Conditional
- type ConditionalFunc
- type ConditionalPromptSelector
- type Conversation
- func (c *Conversation) Call(ctx context.Context, inputs schema.ChainValues, ...) (schema.ChainValues, error)
- func (c *Conversation) Callbacks() []schema.Callback
- func (c *Conversation) InputKeys() []string
- func (c *Conversation) Memory() schema.Memory
- func (c *Conversation) OutputKeys() []string
- func (c *Conversation) Prompt() *prompt.Template
- func (c *Conversation) Type() string
- func (c *Conversation) Verbose() bool
- type ConversationOptions
- type HTTPClient
- type LLM
- func (c *LLM) Call(ctx context.Context, inputs schema.ChainValues, ...) (schema.ChainValues, error)
- func (c *LLM) Callbacks() []schema.Callback
- func (c *LLM) GetNumTokens(text string) (uint, error)
- func (c *LLM) InputKeys() []string
- func (c *LLM) Memory() schema.Memory
- func (c *LLM) OutputKeys() []string
- func (c *LLM) Prompt() *prompt.Template
- func (c *LLM) Type() string
- func (c *LLM) Verbose() bool
- type LLMOptions
- type Math
- func (c *Math) Call(ctx context.Context, inputs schema.ChainValues, ...) (schema.ChainValues, error)
- func (c *Math) Callbacks() []schema.Callback
- func (c *Math) InputKeys() []string
- func (c *Math) Memory() schema.Memory
- func (c *Math) OutputKeys() []string
- func (c *Math) Type() string
- func (c *Math) Verbose() bool
- type MathOptions
- type OpenAIClient
- type OpenAIModerateFunc
- type OpenAIModeration
- func (c *OpenAIModeration) Call(ctx context.Context, inputs schema.ChainValues, ...) (schema.ChainValues, error)
- func (c *OpenAIModeration) Callbacks() []schema.Callback
- func (c *OpenAIModeration) InputKeys() []string
- func (c *OpenAIModeration) Memory() schema.Memory
- func (c *OpenAIModeration) OutputKeys() []string
- func (c *OpenAIModeration) Type() string
- func (c *OpenAIModeration) Verbose() bool
- type OpenAIModerationOptions
- type PromptSelector
- type SQL
- func (c *SQL) Call(ctx context.Context, inputs schema.ChainValues, ...) (schema.ChainValues, error)
- func (c *SQL) Callbacks() []schema.Callback
- func (c *SQL) InputKeys() []string
- func (c *SQL) Memory() schema.Memory
- func (c *SQL) OutputKeys() []string
- func (c *SQL) Type() string
- func (c *SQL) Verbose() bool
- type SQLOptions
- type Sequential
- func (c *Sequential) Call(ctx context.Context, inputs schema.ChainValues, ...) (schema.ChainValues, error)
- func (c *Sequential) Callbacks() []schema.Callback
- func (c *Sequential) InputKeys() []string
- func (c *Sequential) Memory() schema.Memory
- func (c *Sequential) OutputKeys() []string
- func (c *Sequential) Type() string
- func (c *Sequential) Verbose() bool
- type SequentialOptions
- type SimpleSequential
- func (c *SimpleSequential) Call(ctx context.Context, inputs schema.ChainValues, ...) (schema.ChainValues, error)
- func (c *SimpleSequential) Callbacks() []schema.Callback
- func (c *SimpleSequential) InputKeys() []string
- func (c *SimpleSequential) Memory() schema.Memory
- func (c *SimpleSequential) OutputKeys() []string
- func (c *SimpleSequential) Type() string
- func (c *SimpleSequential) Verbose() bool
- type SimpleSequentialOptions
- type Transform
- func (c *Transform) Call(ctx context.Context, inputs schema.ChainValues, ...) (schema.ChainValues, error)
- func (c *Transform) Callbacks() []schema.Callback
- func (c *Transform) InputKeys() []string
- func (c *Transform) Memory() schema.Memory
- func (c *Transform) OutputKeys() []string
- func (c *Transform) Type() string
- func (c *Transform) Verbose() bool
- type TransformFunc
- type TransformOptions
- type VerifySQL
- type VerifyURL
Constants ¶
This section is empty.
Variables ¶
Functions ¶
func IsChatModel ¶ added in v0.0.14
IsChatModel checks if the given model is of type schema.ChatModel.
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
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) OutputKeys ¶ added in v0.0.50
OutputKeys returns the output keys the chain will return.
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 Conditional ¶ added in v0.0.14
type Conditional struct { Condition ConditionalFunc Prompt *prompt.Template }
Conditional represents a conditional prompt configuration.
type ConditionalFunc ¶ added in v0.0.14
ConditionalFunc represents a function that evaluates a condition based on a model.
type ConditionalPromptSelector ¶ added in v0.0.14
type ConditionalPromptSelector struct { DefaultPrompt *prompt.Template Conditionals []Conditional }
ConditionalPromptSelector is a prompt selector that selects prompts based on conditions.
func (*ConditionalPromptSelector) GetPrompt ¶ added in v0.0.14
func (cps *ConditionalPromptSelector) GetPrompt(model schema.Model) *prompt.Template
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 Conversation ¶ added in v0.0.10
type Conversation struct {
// contains filtered or unexported fields
}
func NewConversation ¶ added in v0.0.10
func NewConversation(llm schema.LLM, 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() *prompt.Template
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 *prompt.Template 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
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 (*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) GetNumTokens ¶ added in v0.0.39
GetNumTokens returns the number of tokens in the given text for the associated Language Model (LLM).
func (*LLM) OutputKeys ¶ added in v0.0.16
OutputKeys returns the output keys the chain will return.
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(llm schema.LLM, 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
Callbacks returns the callbacks associated with the chain.
func (*Math) OutputKeys ¶ added in v0.0.51
OutputKeys returns the output keys the chain will return.
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 OpenAIClient ¶ added in v0.0.31
type OpenAIModerateFunc ¶ added in v0.0.31
type OpenAIModerateFunc func(id, model string, result openai.Result) (schema.ChainValues, error)
type OpenAIModeration ¶ added in v0.0.31
type OpenAIModeration struct {
// contains filtered or unexported fields
}
func NewOpenAIModeration ¶ added in v0.0.31
func NewOpenAIModeration(apiKey string, optFns ...func(o *OpenAIModerationOptions)) (*OpenAIModeration, error)
func NewOpenAIModerationFromClient ¶ added in v0.0.31
func NewOpenAIModerationFromClient(client OpenAIClient, optFns ...func(o *OpenAIModerationOptions)) (*OpenAIModeration, error)
func (*OpenAIModeration) Call ¶ added in v0.0.31
func (c *OpenAIModeration) Call(ctx context.Context, inputs schema.ChainValues, optFns ...func(o *schema.CallOptions)) (schema.ChainValues, error)
Call executes the openai moderation chain with the given context and inputs. It returns the outputs of the chain or an error, if any.
func (*OpenAIModeration) Callbacks ¶ added in v0.0.31
func (c *OpenAIModeration) Callbacks() []schema.Callback
Callbacks returns the callbacks associated with the chain.
func (*OpenAIModeration) InputKeys ¶ added in v0.0.31
func (c *OpenAIModeration) InputKeys() []string
InputKeys returns the expected input keys.
func (*OpenAIModeration) Memory ¶ added in v0.0.31
func (c *OpenAIModeration) Memory() schema.Memory
Memory returns the memory associated with the chain.
func (*OpenAIModeration) OutputKeys ¶ added in v0.0.31
func (c *OpenAIModeration) OutputKeys() []string
OutputKeys returns the output keys the chain will return.
func (*OpenAIModeration) Type ¶ added in v0.0.31
func (c *OpenAIModeration) Type() string
Type returns the type of the chain.
func (*OpenAIModeration) Verbose ¶ added in v0.0.31
func (c *OpenAIModeration) Verbose() bool
Verbose returns the verbosity setting of the chain.
type OpenAIModerationOptions ¶ added in v0.0.31
type OpenAIModerationOptions struct { *schema.CallbackOptions ModelName string InputKey string OutputKey string OpenAIModerateFunc OpenAIModerateFunc }
type PromptSelector ¶ added in v0.0.14
PromptSelector is an interface for selecting prompts based on a model.
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 (*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) OutputKeys ¶ added in v0.0.49
OutputKeys returns the output keys the chain will return.
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 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 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
Callbacks returns the callbacks associated with the chain.
func (*Transform) OutputKeys ¶ added in v0.0.17
OutputKeys returns the output keys the chain will return.
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
}