agents

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package agents provides agent implementations that use language models to determine which actions to take.

Index

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.

func (*AgentExecutor) Invoke

func (e *AgentExecutor) Invoke(ctx context.Context, input map[string]any, opts ...core.Option) (map[string]any, error)

Invoke runs the agent loop to completion.

func (*AgentExecutor) Stream

func (e *AgentExecutor) Stream(ctx context.Context, input map[string]any, opts ...core.Option) (*core.StreamIterator[map[string]any], error)

Stream runs the agent and returns a single-chunk stream with the final output.

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.

Jump to

Keyboard shortcuts

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