Documentation
¶
Overview ¶
Package reflexion implements the Reflexion strategy for gollem.
Reflexion is a framework for language agents that learn through verbal feedback and self-reflection. It enables agents to improve their performance across multiple trials by maintaining episodic memory of past reflections.
Basic usage:
strategy := reflexion.New(llmClient,
reflexion.WithMaxTrials(3),
reflexion.WithMemorySize(3),
)
agent := gollem.New(llmClient, gollem.WithStrategy(strategy))
response, err := agent.Execute(ctx, gollem.Text("Solve this task..."))
The strategy will:
- Execute a trial with the given input
- Evaluate the result using the configured evaluator
- If failed, generate a reflection and try again with the reflection as context
- Repeat until success or max trials reached
Index ¶
Constants ¶
const ( // DefaultMaxTrials is the default maximum number of trials DefaultMaxTrials = 3 // DefaultMemorySize is the default maximum size of episodic memory DefaultMemorySize = 3 )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type EvaluationResult ¶
type EvaluationResult struct {
Success bool // Whether the trial successfully completed the task
Score float64 // Optional score (0.0-1.0), 0 if not used
Feedback string // Optional feedback or explanation
}
EvaluationResult represents the result of evaluating a trajectory. It indicates whether the trial was successful and optionally provides additional feedback and scoring information.
type Evaluator ¶
type Evaluator func(context.Context, *Trajectory) (*EvaluationResult, error)
Evaluator evaluates a trajectory to determine if it successfully completed the task. Users can provide custom evaluation logic as a function.
Example:
evaluator := reflexion.Evaluator(func(ctx context.Context, t *reflexion.Trajectory) (*reflexion.EvaluationResult, error) {
// Custom evaluation logic
if containsExpectedOutput(t.FinalResponse) {
return &reflexion.EvaluationResult{Success: true}, nil
}
return &reflexion.EvaluationResult{
Success: false,
Feedback: "Output does not match expected result",
}, nil
})
func NewLLMEvaluator ¶
NewLLMEvaluator creates a new LLM-based evaluator. It asks the LLM to determine if the task was successfully completed based on the original task, execution history, and final response.
type Hooks ¶
type Hooks interface {
// OnTrialStart is called when a new trial begins.
OnTrialStart(ctx context.Context, trialNum int) error
// OnTrialEnd is called when a trial completes (success or failure).
OnTrialEnd(ctx context.Context, trialNum int, evaluation *EvaluationResult) error
// OnReflectionGenerated is called when a reflection is generated after a failed trial.
OnReflectionGenerated(ctx context.Context, trialNum int, reflection string) error
}
Hooks provides lifecycle hooks for observing the Reflexion strategy's execution. All methods are optional - implement only the hooks you need.
type Option ¶
type Option func(*Strategy)
Option is a function that configures a Strategy.
func WithEvaluator ¶
WithEvaluator sets a custom evaluator. If not set, LLMEvaluator is used by default.
func WithMaxTrials ¶
WithMaxTrials sets the maximum number of trials. Default is 3.
func WithMemorySize ¶
WithMemorySize sets the maximum size of episodic memory. Default is 3.
type ReflectionGeneratedEvent ¶
type ReflectionGeneratedEvent struct {
TrialNumber int `json:"trial_number"`
Reflection string `json:"reflection"`
}
ReflectionGeneratedEvent is recorded when a reflection is generated.
type Strategy ¶
type Strategy struct {
// contains filtered or unexported fields
}
Strategy is the main Reflexion strategy implementation. It manages multiple trials, evaluates their results, generates reflections, and maintains episodic memory to improve performance across trials.
func New ¶
New creates a new Reflexion strategy with the given LLM client and options. By default, it uses LLMEvaluator for evaluation, max 3 trials, and memory size of 3.
func (*Strategy) Handle ¶
func (s *Strategy) Handle(ctx context.Context, state *gollem.StrategyState) ([]gollem.Input, *gollem.ExecuteResponse, error)
Handle determines the next input for the LLM based on the current state. It manages the trial loop: execution, evaluation, reflection, and retry.
type Trajectory ¶
type Trajectory struct {
TrialNum int // Trial number (1-indexed)
UserInputs []gollem.Input // Initial user inputs for the task
History *gollem.History // Complete conversation history
FinalResponse []string // Final response texts from LLM
StartTime time.Time // Trial start time
EndTime time.Time // Trial end time
}
Trajectory captures the execution trace of a trial. It contains the complete execution history including user inputs, LLM responses, tool executions, and the final response.
type TrialEndEvent ¶
type TrialEndEvent struct {
TrialNumber int `json:"trial_number"`
Success bool `json:"success"`
Feedback string `json:"feedback,omitempty"`
}
TrialEndEvent is recorded when a trial ends.
type TrialStartEvent ¶
type TrialStartEvent struct {
TrialNumber int `json:"trial_number"`
}
TrialStartEvent is recorded when a trial begins.