agents

package
v0.1.10 Latest Latest
Warning

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

Go to latest
Published: May 9, 2024 License: MIT Imports: 15 Imported by: 15

Documentation

Overview

Package agents contains the standard interface all agents must implement, implementations of this interface, and an agent executor.

An Agent is a wrapper around a model, which takes in user input and returns a response corresponding to an “action” to take and a corresponding “action input”. Alternatively the agent can return a finish with the finished answer to the query. This package contains and standard interface for such agents.

Package agents provides and implementation of the agent interface called OneShotZeroAgent. This agent uses the ReAct Framework (based on the descriptions of tools) to decide what action to take. This agent is optimized to be used with LLMs.

To make agents more powerful we need to make them iterative, i.e. call the model multiple times until they arrive at the final answer. That's the job of the Executor. The Executor is an Agent and set of Tools. The agent executor is responsible for calling the agent, getting back and action and action input, calling the tool that the action references with the corresponding input, getting the output of the tool, and then passing all that information back into the Agent to get the next action it should take.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrExecutorInputNotString is returned if an input to the executor call function is not a string.
	ErrExecutorInputNotString = errors.New("input to executor not string")
	// ErrAgentNoReturn is returned if the agent returns no actions and no finish.
	ErrAgentNoReturn = errors.New("no actions or finish was returned by the agent")
	// ErrNotFinished is returned if the agent does not give a finish before  the number of iterations
	// is larger than max iterations.
	ErrNotFinished = errors.New("agent not finished before max iterations")
	// ErrUnknownAgentType is returned if the type given to the initializer is invalid.
	ErrUnknownAgentType = errors.New("unknown agent type")
	// ErrInvalidOptions is returned if the options given to the initializer is invalid.
	ErrInvalidOptions = errors.New("invalid options")

	// ErrUnableToParseOutput is returned if the output of the llm is unparsable.
	ErrUnableToParseOutput = errors.New("unable to parse agent output")
	// ErrInvalidChainReturnType is returned if the internal chain of the agent returns a value in the
	// "text" filed that is not a string.
	ErrInvalidChainReturnType = errors.New("agent chain did not return a string")
)

Functions

This section is empty.

Types

type Agent

type Agent interface {
	// Plan Given an input and previous steps decide what to do next. Returns
	// either actions or a finish.
	Plan(ctx context.Context, intermediateSteps []schema.AgentStep, inputs map[string]string) ([]schema.AgentAction, *schema.AgentFinish, error) //nolint:lll
	GetInputKeys() []string
	GetOutputKeys() []string
}

Agent is the interface all agents must implement.

type AgentType

type AgentType string

AgentType is a string type representing the type of agent to create.

const (
	// ZeroShotReactDescription is an AgentType constant that represents
	// the "zeroShotReactDescription" agent type.
	ZeroShotReactDescription AgentType = "zeroShotReactDescription"
	// ConversationalReactDescription is an AgentType constant that represents
	// the "conversationalReactDescription" agent type.
	ConversationalReactDescription AgentType = "conversationalReactDescription"
)

type ConversationalAgent

type ConversationalAgent struct {
	// Chain is the chain used to call with the values. The chain should have an
	// input called "agent_scratchpad" for the agent to put its thoughts in.
	Chain chains.Chain
	// Tools is a list of the tools the agent can use.
	Tools []tools.Tool
	// Output key is the key where the final output is placed.
	OutputKey string
	// CallbacksHandler is the handler for callbacks.
	CallbacksHandler callbacks.Handler
}

ConversationalAgent is a struct that represents an agent responsible for deciding what to do or give the final output if the task is finished given a set of inputs and previous steps taken.

Other agents are often optimized for using tools to figure out the best response, which is not ideal in a conversational setting where you may want the agent to be able to chat with the user as well.

func NewConversationalAgent

func NewConversationalAgent(llm llms.Model, tools []tools.Tool, opts ...Option) *ConversationalAgent

func (*ConversationalAgent) GetInputKeys

func (a *ConversationalAgent) GetInputKeys() []string

func (*ConversationalAgent) GetOutputKeys

func (a *ConversationalAgent) GetOutputKeys() []string

func (*ConversationalAgent) Plan

func (a *ConversationalAgent) Plan(
	ctx context.Context,
	intermediateSteps []schema.AgentStep,
	inputs map[string]string,
) ([]schema.AgentAction, *schema.AgentFinish, error)

Plan decides what action to take or returns the final result of the input.

type Executor

type Executor struct {
	Agent            Agent
	Tools            []tools.Tool
	Memory           schema.Memory
	CallbacksHandler callbacks.Handler
	ErrorHandler     *ParserErrorHandler

	MaxIterations           int
	ReturnIntermediateSteps bool
}

Executor is the chain responsible for running agents.

func Initialize deprecated

func Initialize(
	llm llms.Model,
	tools []tools.Tool,
	agentType AgentType,
	opts ...Option,
) (*Executor, error)

Deprecated: This may be removed in the future; please use NewExecutor instead. Initialize is a function that creates a new executor with the specified LLM model, tools, agent type, and options. It returns an Executor or an error if there is any issues during the creation process.

func NewExecutor

func NewExecutor(agent Agent, tools []tools.Tool, opts ...Option) *Executor

NewExecutor creates a new agent executor with an agent and the tools the agent can use.

func (*Executor) Call

func (e *Executor) Call(ctx context.Context, inputValues map[string]any, _ ...chains.ChainCallOption) (map[string]any, error)

func (*Executor) GetCallbackHandler

func (e *Executor) GetCallbackHandler() callbacks.Handler

func (*Executor) GetInputKeys

func (e *Executor) GetInputKeys() []string

GetInputKeys gets the input keys the agent of the executor expects. Often "input".

func (*Executor) GetMemory

func (e *Executor) GetMemory() schema.Memory

func (*Executor) GetOutputKeys

func (e *Executor) GetOutputKeys() []string

GetOutputKeys gets the output keys the agent of the executor returns.

type OneShotZeroAgent

type OneShotZeroAgent struct {
	// Chain is the chain used to call with the values. The chain should have an
	// input called "agent_scratchpad" for the agent to put its thoughts in.
	Chain chains.Chain
	// Tools is a list of the tools the agent can use.
	Tools []tools.Tool
	// Output key is the key where the final output is placed.
	OutputKey string
	// CallbacksHandler is the handler for callbacks.
	CallbacksHandler callbacks.Handler
}

OneShotZeroAgent is a struct that represents an agent responsible for deciding what to do or give the final output if the task is finished given a set of inputs and previous steps taken.

This agent is optimized to be used with LLMs.

func NewOneShotAgent

func NewOneShotAgent(llm llms.Model, tools []tools.Tool, opts ...Option) *OneShotZeroAgent

NewOneShotAgent creates a new OneShotZeroAgent with the given LLM model, tools, and options. It returns a pointer to the created agent. The opts parameter represents the options for the agent.

func (*OneShotZeroAgent) GetInputKeys

func (a *OneShotZeroAgent) GetInputKeys() []string

func (*OneShotZeroAgent) GetOutputKeys

func (a *OneShotZeroAgent) GetOutputKeys() []string

func (*OneShotZeroAgent) Plan

func (a *OneShotZeroAgent) Plan(
	ctx context.Context,
	intermediateSteps []schema.AgentStep,
	inputs map[string]string,
) ([]schema.AgentAction, *schema.AgentFinish, error)

Plan decides what action to take or returns the final result of the input.

type OpenAIFunctionsAgent added in v0.1.4

type OpenAIFunctionsAgent struct {
	// LLM is the llm used to call with the values. The llm should have an
	// input called "agent_scratchpad" for the agent to put its thoughts in.
	LLM    llms.Model
	Prompt prompts.FormatPrompter
	// Chain chains.Chain
	// Tools is a list of the tools the agent can use.
	Tools []tools.Tool
	// Output key is the key where the final output is placed.
	OutputKey string
	// CallbacksHandler is the handler for callbacks.
	CallbacksHandler callbacks.Handler
}

OpenAIFunctionsAgent is an Agent driven by OpenAIs function powered API.

func NewOpenAIFunctionsAgent added in v0.1.4

func NewOpenAIFunctionsAgent(llm llms.Model, tools []tools.Tool, opts ...Option) *OpenAIFunctionsAgent

NewOpenAIFunctionsAgent creates a new OpenAIFunctionsAgent.

func (*OpenAIFunctionsAgent) GetInputKeys added in v0.1.4

func (o *OpenAIFunctionsAgent) GetInputKeys() []string

func (*OpenAIFunctionsAgent) GetOutputKeys added in v0.1.4

func (o *OpenAIFunctionsAgent) GetOutputKeys() []string

func (*OpenAIFunctionsAgent) ParseOutput added in v0.1.4

func (o *OpenAIFunctionsAgent) ParseOutput(contentResp *llms.ContentResponse) (
	[]schema.AgentAction, *schema.AgentFinish, error,
)

func (*OpenAIFunctionsAgent) Plan added in v0.1.4

func (o *OpenAIFunctionsAgent) Plan(
	ctx context.Context,
	intermediateSteps []schema.AgentStep,
	inputs map[string]string,
) ([]schema.AgentAction, *schema.AgentFinish, error)

Plan decides what action to take or returns the final result of the input.

type OpenAIOption added in v0.1.4

type OpenAIOption struct{}

func NewOpenAIOption added in v0.1.4

func NewOpenAIOption() OpenAIOption

func (OpenAIOption) WithExtraMessages added in v0.1.4

func (o OpenAIOption) WithExtraMessages(extraMessages []prompts.MessageFormatter) Option

func (OpenAIOption) WithSystemMessage added in v0.1.4

func (o OpenAIOption) WithSystemMessage(msg string) Option

type Option added in v0.1.8

type Option func(*Options)

Option is a function type that can be used to modify the creation of the agents and executors.

func WithCallbacksHandler

func WithCallbacksHandler(handler callbacks.Handler) Option

WithCallbacksHandler is an option for setting a callback handler to an executor.

func WithMaxIterations

func WithMaxIterations(iterations int) Option

WithMaxIterations is an option for setting the max number of iterations the executor will complete.

func WithMemory

func WithMemory(m schema.Memory) Option

WithMemory is an option for setting the memory of the executor.

func WithOutputKey

func WithOutputKey(outputKey string) Option

WithOutputKey is an option for setting the output key of the agent.

func WithParserErrorHandler

func WithParserErrorHandler(errorHandler *ParserErrorHandler) Option

WithParserErrorHandler is an option for setting a parser error handler to an executor.

func WithPrompt

func WithPrompt(prompt prompts.PromptTemplate) Option

WithPrompt is an option for setting the prompt the agent will use.

func WithPromptFormatInstructions

func WithPromptFormatInstructions(instructions string) Option

WithPromptFormatInstructions is an option for setting the format instructions of the prompt used by the agent.

func WithPromptPrefix

func WithPromptPrefix(prefix string) Option

WithPromptPrefix is an option for setting the prefix of the prompt used by the agent.

func WithPromptSuffix

func WithPromptSuffix(suffix string) Option

WithPromptSuffix is an option for setting the suffix of the prompt used by the agent.

func WithReturnIntermediateSteps

func WithReturnIntermediateSteps() Option

WithReturnIntermediateSteps is an option for making the executor return the intermediate steps taken.

type Options added in v0.1.8

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

type ParserErrorHandler

type ParserErrorHandler struct {
	// The formatter function can be used to format the parsing error. If nil the error will be given
	// as an observation directly.
	Formatter func(err string) string
}

ParserErrorHandler is the struct used to handle parse errors from the agent in the executor. If an executor have a ParserErrorHandler, parsing errors will be formatted using the formatter function and added as an observation. In the next executor step the agent will then have the possibility to fix the error.

func NewParserErrorHandler

func NewParserErrorHandler(formatFunc func(string) string) *ParserErrorHandler

NewParserErrorHandler creates a new parser error handler.

Jump to

Keyboard shortcuts

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