Documentation
¶
Overview ¶
Package agents provides agent implementations that use language models to determine which actions to take.
Index ¶
- func DefaultReActPrompt() *prompts.ChatPromptTemplate
- type Agent
- type AgentAction
- type AgentExecutor
- func (e *AgentExecutor) Batch(ctx context.Context, inputs []map[string]any, opts ...core.Option) ([]map[string]any, error)
- func (e *AgentExecutor) GetName() string
- func (e *AgentExecutor) Invoke(ctx context.Context, input map[string]any, opts ...core.Option) (map[string]any, error)
- func (e *AgentExecutor) Stream(ctx context.Context, input map[string]any, opts ...core.Option) (*core.StreamIterator[map[string]any], error)
- type AgentFinish
- type AgentOutput
- type AgentStep
- type ExecutorOption
- type ReActAgent
- type ToolCallingAgent
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DefaultReActPrompt ¶
func DefaultReActPrompt() *prompts.ChatPromptTemplate
DefaultReActPrompt returns the default ReAct prompt template.
Types ¶
type Agent ¶
type Agent interface {
// Plan takes the intermediate steps so far and returns the next action(s) or finish.
Plan(ctx context.Context, intermediateSteps []AgentStep, inputs map[string]any) (*AgentOutput, error)
// InputKeys returns the expected input keys.
InputKeys() []string
// OutputKeys returns the output keys.
OutputKeys() []string
}
Agent is the interface for the planning component that decides what to do next.
type AgentAction ¶
type AgentAction struct {
// Tool is the name of the tool to execute.
Tool string `json:"tool"`
// ToolInput is the input to pass to the tool (may be JSON).
ToolInput string `json:"tool_input"`
// ToolCallID is the provider-issued identifier for this tool call.
ToolCallID string `json:"tool_call_id,omitempty"`
// Log is additional information about why this action was taken.
Log string `json:"log"`
// MessageLog contains the messages that led to this action.
MessageLog []core.Message `json:"-"`
}
AgentAction represents a request from the agent to execute a tool.
type AgentExecutor ¶
type AgentExecutor struct {
// contains filtered or unexported fields
}
AgentExecutor runs an agent loop: plan -> execute tool -> plan -> ... -> finish. It implements Runnable[map[string]any, map[string]any].
func NewAgentExecutor ¶
func NewAgentExecutor(agent Agent, agentTools []tools.Tool, options ...ExecutorOption) (*AgentExecutor, error)
NewAgentExecutor creates a new AgentExecutor.
func (*AgentExecutor) Batch ¶
func (e *AgentExecutor) Batch(ctx context.Context, inputs []map[string]any, opts ...core.Option) ([]map[string]any, error)
Batch runs the agent for multiple inputs.
func (*AgentExecutor) GetName ¶
func (e *AgentExecutor) GetName() string
GetName returns the executor name.
type AgentFinish ¶
type AgentFinish struct {
// ReturnValues contains the final output of the agent.
ReturnValues map[string]any `json:"return_values"`
// Log is additional information about the final output.
Log string `json:"log"`
// MessageLog contains the messages that led to this finish.
MessageLog []core.Message `json:"-"`
}
AgentFinish represents the final output of an agent.
type AgentOutput ¶
type AgentOutput struct {
// Actions contains tool calls to execute (may be multiple for parallel tool calling).
Actions []AgentAction
// Finish contains the final output if the agent is done.
Finish *AgentFinish
}
AgentOutput is the union type returned by an agent's planning step. Either Actions or Finish will be populated, not both.
type AgentStep ¶
type AgentStep struct {
// Action is the action that was executed.
Action AgentAction `json:"action"`
// Observation is the result of executing the action.
Observation string `json:"observation"`
}
AgentStep represents one iteration of the agent loop: an action that was taken and the observation (tool result) that followed.
type ExecutorOption ¶
type ExecutorOption func(*AgentExecutor)
ExecutorOption configures the AgentExecutor.
func WithHandleParsingErrors ¶
func WithHandleParsingErrors(v bool) ExecutorOption
WithHandleParsingErrors enables handling of parsing errors.
func WithMaxIterations ¶
func WithMaxIterations(n int) ExecutorOption
WithMaxIterations sets the maximum number of agent loop iterations.
func WithReturnIntermediateSteps ¶
func WithReturnIntermediateSteps(v bool) ExecutorOption
WithReturnIntermediateSteps includes intermediate steps in the output.
type ReActAgent ¶
type ReActAgent struct {
// contains filtered or unexported fields
}
ReActAgent uses the ReAct (Reasoning + Acting) prompting pattern.
func NewReActAgent ¶
func NewReActAgent(llm llms.ChatModel, agentTools []tools.Tool, prompt *prompts.ChatPromptTemplate) *ReActAgent
NewReActAgent creates a new ReAct agent. If prompt is nil, the default ReAct prompt is used.
func (*ReActAgent) InputKeys ¶
func (a *ReActAgent) InputKeys() []string
InputKeys returns the expected input keys.
func (*ReActAgent) OutputKeys ¶
func (a *ReActAgent) OutputKeys() []string
OutputKeys returns the output keys.
func (*ReActAgent) Plan ¶
func (a *ReActAgent) Plan(ctx context.Context, intermediateSteps []AgentStep, inputs map[string]any) (*AgentOutput, error)
Plan decides the next action based on intermediate steps and inputs.
type ToolCallingAgent ¶
type ToolCallingAgent struct {
// contains filtered or unexported fields
}
ToolCallingAgent uses a chat model's native tool calling capability. This is the modern, recommended agent type.
func NewToolCallingAgent ¶
func NewToolCallingAgent(llm llms.ChatModel, agentTools []tools.Tool, prompt *prompts.ChatPromptTemplate) *ToolCallingAgent
NewToolCallingAgent creates a new ToolCallingAgent. The prompt must include a Placeholder("agent_scratchpad") for intermediate steps.
func (*ToolCallingAgent) InputKeys ¶
func (a *ToolCallingAgent) InputKeys() []string
InputKeys returns the expected input keys.
func (*ToolCallingAgent) OutputKeys ¶
func (a *ToolCallingAgent) OutputKeys() []string
OutputKeys returns the output keys.
func (*ToolCallingAgent) Plan ¶
func (a *ToolCallingAgent) Plan(ctx context.Context, intermediateSteps []AgentStep, inputs map[string]any) (*AgentOutput, error)
Plan decides the next action(s) based on intermediate steps and inputs.