agents

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Jul 22, 2025 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

Package agents provides the core agent implementations for smolagents.

This includes MultiStepAgent, ReactCodeAgent, and supporting infrastructure for the ReAct framework with code execution. The ReactCodeAgent implements proper Thought/Code/Observation cycles with YAML-based prompts and sandboxed execution.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetVariableNames

func GetVariableNames(template string) []string

GetVariableNames extracts variable names from a template string

func PopulateTemplate

func PopulateTemplate(template string, variables map[string]interface{}) string

PopulateTemplate fills in template variables using the provided data

Types

type AgentConfig

type AgentConfig struct {
	Model            models.Model            `json:"-"`
	Tools            []tools.Tool            `json:"-"`
	SystemPrompt     string                  `json:"system_prompt"`
	MaxSteps         int                     `json:"max_steps"`
	Planning         bool                    `json:"planning"`
	PlanningInterval int                     `json:"planning_interval"`
	PromptTemplates  *PromptTemplates        `json:"prompt_templates"`
	Logger           *monitoring.AgentLogger `json:"-"`
	Monitor          *monitoring.Monitor     `json:"-"`
	Additional       map[string]interface{}  `json:"additional"`
}

AgentConfig represents configuration for creating agents

func DefaultAgentConfig

func DefaultAgentConfig() *AgentConfig

DefaultAgentConfig returns a default agent configuration

type BaseMultiStepAgent

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

BaseMultiStepAgent provides common functionality for all agent implementations

func NewBaseMultiStepAgent

func NewBaseMultiStepAgent(
	model models.Model,
	toolsArg []tools.Tool,
	systemPrompt string,
	options map[string]interface{},
) (*BaseMultiStepAgent, error)

NewBaseMultiStepAgent creates a new base multi-step agent

func (*BaseMultiStepAgent) AddManagedAgent

func (ba *BaseMultiStepAgent) AddManagedAgent(name string, agent MultiStepAgent) error

AddManagedAgent adds a managed agent

func (*BaseMultiStepAgent) AddTool

func (ba *BaseMultiStepAgent) AddTool(tool tools.Tool) error

AddTool adds a tool to the agent

func (*BaseMultiStepAgent) GetLogger

func (ba *BaseMultiStepAgent) GetLogger() *monitoring.AgentLogger

GetLogger returns the agent's logger

func (*BaseMultiStepAgent) GetManagedAgent

func (ba *BaseMultiStepAgent) GetManagedAgent(name string) (MultiStepAgent, bool)

GetManagedAgent retrieves a managed agent by name

func (*BaseMultiStepAgent) GetMaxSteps

func (ba *BaseMultiStepAgent) GetMaxSteps() int

GetMaxSteps implements MultiStepAgent

func (*BaseMultiStepAgent) GetMemory

func (ba *BaseMultiStepAgent) GetMemory() *memory.AgentMemory

GetMemory implements MultiStepAgent

func (*BaseMultiStepAgent) GetModel

func (ba *BaseMultiStepAgent) GetModel() models.Model

GetModel implements MultiStepAgent

func (*BaseMultiStepAgent) GetMonitor

func (ba *BaseMultiStepAgent) GetMonitor() *monitoring.Monitor

GetMonitor returns the agent's monitor

func (*BaseMultiStepAgent) GetPlanning

func (ba *BaseMultiStepAgent) GetPlanning() bool

GetPlanning returns whether planning is enabled

func (*BaseMultiStepAgent) GetPlanningInterval

func (ba *BaseMultiStepAgent) GetPlanningInterval() int

GetPlanningInterval returns the planning interval

func (*BaseMultiStepAgent) GetPromptTemplates

func (ba *BaseMultiStepAgent) GetPromptTemplates() *PromptTemplates

GetPromptTemplates returns the prompt templates

func (*BaseMultiStepAgent) GetStepCount

func (ba *BaseMultiStepAgent) GetStepCount() int

GetStepCount implements MultiStepAgent

func (*BaseMultiStepAgent) GetSystemPrompt

func (ba *BaseMultiStepAgent) GetSystemPrompt() string

GetSystemPrompt implements MultiStepAgent

func (*BaseMultiStepAgent) GetTool

func (ba *BaseMultiStepAgent) GetTool(name string) (tools.Tool, bool)

GetTool retrieves a tool by name

func (*BaseMultiStepAgent) GetTools

func (ba *BaseMultiStepAgent) GetTools() []tools.Tool

GetTools implements MultiStepAgent

func (*BaseMultiStepAgent) Interrupt

func (ba *BaseMultiStepAgent) Interrupt() error

Interrupt implements MultiStepAgent

func (*BaseMultiStepAgent) IsRunning

func (ba *BaseMultiStepAgent) IsRunning() bool

IsRunning implements MultiStepAgent

func (*BaseMultiStepAgent) PushToHub

func (ba *BaseMultiStepAgent) PushToHub(repoID string, commitMessage string, token string, private bool) error

PushToHub implements MultiStepAgent (placeholder implementation)

func (*BaseMultiStepAgent) RemoveTool

func (ba *BaseMultiStepAgent) RemoveTool(name string) bool

RemoveTool removes a tool from the agent

func (*BaseMultiStepAgent) Reset

func (ba *BaseMultiStepAgent) Reset() error

Reset implements MultiStepAgent

func (*BaseMultiStepAgent) Save

func (ba *BaseMultiStepAgent) Save(outputDir string, toolFileName string, makeGradioApp bool) error

Save implements MultiStepAgent (placeholder implementation)

func (*BaseMultiStepAgent) SetLogger

func (ba *BaseMultiStepAgent) SetLogger(logger *monitoring.AgentLogger)

SetLogger sets the logger for the agent

func (*BaseMultiStepAgent) SetMaxSteps

func (ba *BaseMultiStepAgent) SetMaxSteps(maxSteps int)

SetMaxSteps implements MultiStepAgent

func (*BaseMultiStepAgent) SetMemory

func (ba *BaseMultiStepAgent) SetMemory(mem *memory.AgentMemory)

SetMemory implements MultiStepAgent

func (*BaseMultiStepAgent) SetModel

func (ba *BaseMultiStepAgent) SetModel(model models.Model)

SetModel implements MultiStepAgent

func (*BaseMultiStepAgent) SetMonitor

func (ba *BaseMultiStepAgent) SetMonitor(monitor *monitoring.Monitor)

SetMonitor sets the monitor for the agent

func (*BaseMultiStepAgent) SetPlanning

func (ba *BaseMultiStepAgent) SetPlanning(enabled bool)

SetPlanning enables or disables planning

func (*BaseMultiStepAgent) SetPlanningInterval

func (ba *BaseMultiStepAgent) SetPlanningInterval(interval int)

SetPlanningInterval sets the planning interval

func (*BaseMultiStepAgent) SetPromptTemplates

func (ba *BaseMultiStepAgent) SetPromptTemplates(templates *PromptTemplates)

SetPromptTemplates sets the prompt templates

func (*BaseMultiStepAgent) SetSystemPrompt

func (ba *BaseMultiStepAgent) SetSystemPrompt(prompt string)

SetSystemPrompt implements MultiStepAgent

func (*BaseMultiStepAgent) SetTools

func (ba *BaseMultiStepAgent) SetTools(toolsArg []tools.Tool)

SetTools implements MultiStepAgent

func (*BaseMultiStepAgent) ToDict

func (ba *BaseMultiStepAgent) ToDict() map[string]interface{}

ToDict implements MultiStepAgent

type FinalAnswerPromptTemplate

type FinalAnswerPromptTemplate struct {
	PreMessages  string `json:"pre_messages"`
	PostMessages string `json:"post_messages"`
}

FinalAnswerPromptTemplate represents final answer prompt templates

type FinalOutput

type FinalOutput struct {
	Output interface{} `json:"output"`
}

FinalOutput represents the final output from an agent

type ManagedAgentPromptTemplate

type ManagedAgentPromptTemplate struct {
	Task   string `json:"task"`
	Report string `json:"report"`
}

ManagedAgentPromptTemplate represents managed agent prompt templates

type MultiStepAgent

type MultiStepAgent interface {
	// Core execution methods
	Run(options *RunOptions) (*RunResult, error)
	RunStream(options *RunOptions) (<-chan *StreamStepResult, error)

	// Agent management
	Reset() error
	Interrupt() error

	// Configuration
	GetModel() models.Model
	SetModel(model models.Model)
	GetTools() []tools.Tool
	SetTools(tools []tools.Tool)
	GetMemory() *memory.AgentMemory
	SetMemory(mem *memory.AgentMemory)
	GetSystemPrompt() string
	SetSystemPrompt(prompt string)
	GetMaxSteps() int
	SetMaxSteps(maxSteps int)

	// Serialization
	ToDict() map[string]interface{}
	Save(outputDir string, toolFileName string, makeGradioApp bool) error
	PushToHub(repoID string, commitMessage string, token string, private bool) error

	// Execution state
	IsRunning() bool
	GetStepCount() int
}

MultiStepAgent represents the main interface for multi-step agents

func CreateAgent

func CreateAgent(config *AgentConfig, agentType string) (MultiStepAgent, error)

CreateAgent creates an agent based on the provided configuration

type PlanningPromptTemplate

type PlanningPromptTemplate struct {
	InitialPlan       string `json:"initial_plan"`
	UpdatePlanPreMsg  string `json:"update_plan_pre_messages"`
	UpdatePlanPostMsg string `json:"update_plan_post_messages"`
}

PlanningPromptTemplate represents planning prompt templates

type PromptTemplates

type PromptTemplates struct {
	SystemPrompt string                     `json:"system_prompt"`
	Planning     PlanningPromptTemplate     `json:"planning"`
	ManagedAgent ManagedAgentPromptTemplate `json:"managed_agent"`
	FinalAnswer  FinalAnswerPromptTemplate  `json:"final_answer"`
}

PromptTemplates represents the prompt templates for an agent

func EmptyPromptTemplates

func EmptyPromptTemplates() *PromptTemplates

EmptyPromptTemplates returns empty prompt templates

type ReactCodeAgent

type ReactCodeAgent struct {
	*BaseMultiStepAgent
	// contains filtered or unexported fields
}

ReactCodeAgent implements a full ReAct (Reasoning + Acting) agent for code execution

func NewReactCodeAgent

func NewReactCodeAgent(
	model models.Model,
	toolsArg []tools.Tool,
	systemPrompt string,
	options *ReactCodeAgentOptions,
) (*ReactCodeAgent, error)

NewReactCodeAgent creates a new ReAct code execution agent

func (*ReactCodeAgent) Close

func (rca *ReactCodeAgent) Close() error

Close cleans up resources

func (*ReactCodeAgent) Run

func (rca *ReactCodeAgent) Run(options *RunOptions) (*RunResult, error)

func (*ReactCodeAgent) RunStream

func (rca *ReactCodeAgent) RunStream(options *RunOptions) (<-chan *StreamStepResult, error)

RunStream implements streaming for ReactCodeAgent

func (*ReactCodeAgent) ToDict

func (rca *ReactCodeAgent) ToDict() map[string]interface{}

ToDict exports the agent configuration

type ReactCodeAgentOptions

type ReactCodeAgentOptions struct {
	AuthorizedPackages []string
	CodeBlockTags      [2]string
	StreamOutputs      bool
	StructuredOutput   bool
	MaxCodeLength      int
	EnablePlanning     bool
	PlanningInterval   int
	MaxSteps           int
	Verbose            bool
}

ReactCodeAgentOptions configures the ReactCodeAgent

func DefaultReactCodeAgentOptions

func DefaultReactCodeAgentOptions() *ReactCodeAgentOptions

DefaultReactCodeAgentOptions returns default options for ReactCodeAgent

type RunOptions

type RunOptions struct {
	Task           string                 `json:"task"`
	Stream         bool                   `json:"stream,omitempty"`
	Reset          bool                   `json:"reset,omitempty"`
	Images         []interface{}          `json:"images,omitempty"`
	AdditionalArgs map[string]interface{} `json:"additional_args,omitempty"`
	MaxSteps       *int                   `json:"max_steps,omitempty"`
	Context        context.Context        `json:"-"`
	StepCallbacks  []StepCallback         `json:"-"`
}

RunOptions represents options for agent execution

func NewRunOptions

func NewRunOptions(task string) *RunOptions

NewRunOptions creates run options with defaults

type RunResult

type RunResult struct {
	Output     interface{}              `json:"output"`
	State      string                   `json:"state"` // "success", "max_steps_error"
	Messages   []map[string]interface{} `json:"messages"`
	TokenUsage *monitoring.TokenUsage   `json:"token_usage,omitempty"`
	Timing     *monitoring.Timing       `json:"timing"`
	StepCount  int                      `json:"step_count"`
	Metadata   map[string]interface{}   `json:"metadata,omitempty"`
	Error      error                    `json:"error,omitempty"`
}

RunResult represents the result of an agent run

func NewRunResult

func NewRunResult() *RunResult

NewRunResult creates a new run result

func (*RunResult) MarshalJSON

func (rr *RunResult) MarshalJSON() ([]byte, error)

JSON marshaling support

type StepCallback

type StepCallback func(step memory.MemoryStep) error

StepCallback is called after each agent step

type StreamStepResult

type StreamStepResult struct {
	StepNumber int                    `json:"step_number"`
	StepType   string                 `json:"step_type"`
	Output     interface{}            `json:"output,omitempty"`
	Error      error                  `json:"error,omitempty"`
	IsComplete bool                   `json:"is_complete"`
	Metadata   map[string]interface{} `json:"metadata,omitempty"`
}

StreamStepResult represents a single step result in streaming mode

Jump to

Keyboard shortcuts

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