agents

package
v0.0.0-...-25d0b29 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2023 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package agent 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, ie. 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 then 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 eturns 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 {
	// 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 it's 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
}

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.LanguageModel, tools []tools.Tool, opts ...CreationOption) *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 CreationOption

type CreationOption func(*CreationOptions)

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

func WithCallbacksHandler

func WithCallbacksHandler(handler callbacks.Handler) CreationOption

func WithMaxIterations

func WithMaxIterations(iterations int) CreationOption

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

func WithMemory

func WithMemory(m schema.Memory) CreationOption

func WithOutputKey

func WithOutputKey(outputKey string) CreationOption

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

func WithPrompt

func WithPrompt(prompt prompts.PromptTemplate) CreationOption

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

func WithPromptFormatInstructions

func WithPromptFormatInstructions(instructions string) CreationOption

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

func WithPromptPrefix

func WithPromptPrefix(prefix string) CreationOption

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

func WithPromptSuffix

func WithPromptSuffix(suffix string) CreationOption

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

func WithReturnIntermediateSteps

func WithReturnIntermediateSteps() CreationOption

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

type CreationOptions

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

type Executor

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

	MaxIterations           int
	ReturnIntermediateSteps bool
}

Executor is the chain responsible for running agents.

func Initialize

func Initialize(
	llm llms.LanguageModel,
	tools []tools.Tool,
	agentType AgentType,
	opts ...CreationOption,
) (Executor, error)

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 ...CreationOption) Executor

NewExecutor creates a new agent executor with a 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 it's 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
}

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.LanguageModel, tools []tools.Tool, opts ...CreationOption) *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.

Jump to

Keyboard shortcuts

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