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
- 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 ChatModel
- func (c *ChatModel) Call(ctx context.Context, inputs schema.ChainValues, ...) (schema.ChainValues, error)
- func (c *ChatModel) Callbacks() []schema.Callback
- func (c *ChatModel) InputKeys() []string
- func (c *ChatModel) Memory() schema.Memory
- func (c *ChatModel) OutputKeys() []string
- func (c *ChatModel) Type() string
- func (c *ChatModel) Verbose() bool
- type ChatModelOptions
- 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() schema.PromptTemplate
- 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() schema.PromptTemplate
- 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 OutputCandidate
- 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 StructuredOutput
- func (c *StructuredOutput) Call(ctx context.Context, inputs schema.ChainValues, ...) (schema.ChainValues, error)
- func (c *StructuredOutput) Callbacks() []schema.Callback
- func (c *StructuredOutput) InputKeys() []string
- func (c *StructuredOutput) Memory() schema.Memory
- func (c *StructuredOutput) OutputKeys() []string
- func (c *StructuredOutput) Type() string
- func (c *StructuredOutput) Verbose() bool
- type StructuredOutputOptions
- type Tagging
- 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 ¶
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
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 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
Callbacks returns the callbacks associated with the chain.
func (*ChatModel) OutputKeys ¶ added in v0.0.59
OutputKeys returns the output keys the chain will return.
type ChatModelOptions ¶ added in v0.0.59
type ChatModelOptions struct { // CallbackOptions contains options for the chain callbacks. *schema.CallbackOptions // 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
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) 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.
func (*LLM) Prompt ¶ added in v0.0.16
func (c *LLM) Prompt() schema.PromptTemplate
Prompt returns the prompt.Template associated with 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
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 OutputCandidate ¶ added in v0.0.60
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 (*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 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, prompt prompt.ChatTemplate, 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 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.
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
}