Documentation
¶
Overview ¶
Package react implements the ReAct (Reasoning and Acting) strategy for gollem.
ReAct is a framework that combines reasoning and acting in language models. It alternates between thought (reasoning), action (tool use), and observation (results) to solve complex problems step by step.
Basic usage:
strategy := react.New(llmClient,
react.WithMaxIterations(20),
react.WithMaxRepeatedActions(3),
)
agent := gollem.New(llmClient, gollem.WithStrategy(strategy))
response, err := agent.Execute(ctx, gollem.Text("What is the weather in Tokyo?"))
Index ¶
- Constants
- type ActionData
- type ActionEvent
- type ActionType
- type FewShotExample
- type ObservationData
- type ObservationEvent
- type Option
- type Strategy
- func (s *Strategy) ExportTrace() *TraceExport
- func (s *Strategy) ExportTraceJSON() ([]byte, error)
- func (s *Strategy) Handle(ctx context.Context, state *gollem.StrategyState) ([]gollem.Input, *gollem.ExecuteResponse, error)
- func (s *Strategy) Init(ctx context.Context, inputs []gollem.Input) error
- func (s *Strategy) Tools(ctx context.Context) ([]gollem.Tool, error)
- type TAOEntry
- type ThoughtData
- type ThoughtEvent
- type ToolResult
- type TraceExport
- type TraceMetadata
- type TraceSummary
Constants ¶
const ( // DefaultSystemPrompt is the default system prompt for ReAct strategy DefaultSystemPrompt = `` /* 981-byte string literal not displayed */ // DefaultThoughtPrompt is the default prompt to encourage reasoning DefaultThoughtPrompt = `Thought:` // DefaultObservationPromptTemplate is the template for observation prompts // Use with fmt.Sprintf(DefaultObservationPromptTemplate, toolName, result) DefaultObservationPromptTemplate = `Observation: %s returned: %s Thought:` )
Default prompt templates based on ReAct paper
const ( // DefaultMaxIterations is the default maximum number of iterations DefaultMaxIterations = 20 // DefaultMaxRepeatedActions is the default maximum number of repeated actions DefaultMaxRepeatedActions = 3 // MaxConsecutiveErrors is the maximum number of consecutive errors before giving up MaxConsecutiveErrors = 3 )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ActionData ¶
type ActionData struct {
Type ActionType `json:"type"`
ToolCalls []*gollem.FunctionCall `json:"tool_calls,omitempty"`
Response string `json:"response,omitempty"`
}
ActionData represents the action taken by the LLM
type ActionEvent ¶
type ActionEvent struct {
Iteration int `json:"iteration"`
ActionType string `json:"action_type"`
ToolNames []string `json:"tool_names,omitempty"`
}
ActionEvent is recorded when an action is taken.
type ActionType ¶
type ActionType string
ActionType represents the type of action taken
const ( // ActionTypeToolCall indicates the action is calling one or more tools ActionTypeToolCall ActionType = "tool_call" // ActionTypeRespond indicates the action is responding to the user ActionTypeRespond ActionType = "respond" )
type FewShotExample ¶
type FewShotExample struct {
Question string `json:"question"`
Thought string `json:"thought"`
Action string `json:"action"`
Observation string `json:"observation"`
Answer string `json:"answer"`
}
FewShotExample represents a single few-shot learning example
type ObservationData ¶
type ObservationData struct {
ToolResults []ToolResult `json:"tool_results"`
Success bool `json:"success"`
Error string `json:"error,omitempty"`
}
ObservationData represents the results observed from actions
type ObservationEvent ¶
ObservationEvent is recorded when tool results are observed.
type Option ¶
type Option func(*Strategy)
Option is a function that configures the Strategy
func WithFewShotExamples ¶
func WithFewShotExamples(examples []FewShotExample) Option
WithFewShotExamples enables few-shot learning with provided examples
func WithMaxIterations ¶
WithMaxIterations sets the maximum number of iterations Default is 20 if not specified
func WithMaxRepeatedActions ¶
WithMaxRepeatedActions sets the maximum number of times the same action can be repeated This helps detect infinite loops. Default is 3 if not specified
func WithObservationPrompt ¶
WithObservationPrompt sets a custom observation prompt template The template should contain two %s placeholders for tool name and result Example: "Tool %s result: %s\nNext thought:"
func WithSystemPrompt ¶
WithSystemPrompt sets a custom system prompt If not set, DefaultSystemPrompt will be used
func WithThoughtPrompt ¶
WithThoughtPrompt sets a custom thought prompt If not set, DefaultThoughtPrompt will be used
type Strategy ¶
type Strategy struct {
// contains filtered or unexported fields
}
Strategy implements the ReAct (Reasoning and Acting) pattern
func (*Strategy) ExportTrace ¶
func (s *Strategy) ExportTrace() *TraceExport
ExportTrace exports the complete trace data
func (*Strategy) ExportTraceJSON ¶
ExportTraceJSON exports the trace data as JSON
func (*Strategy) Handle ¶
func (s *Strategy) Handle(ctx context.Context, state *gollem.StrategyState) ([]gollem.Input, *gollem.ExecuteResponse, error)
Handle implements the ReAct loop logic
type TAOEntry ¶
type TAOEntry struct {
Iteration int `json:"iteration"`
Thought *ThoughtData `json:"thought,omitempty"`
Action *ActionData `json:"action,omitempty"`
Observation *ObservationData `json:"observation,omitempty"`
Timestamp time.Time `json:"timestamp"`
}
TAOEntry represents a single Thought-Action-Observation cycle entry
type ThoughtData ¶
type ThoughtData struct {
Content string `json:"content"` // The thought content generated by LLM
Reasoning string `json:"reasoning"` // The reasoning process
}
ThoughtData represents the reasoning process of the LLM
type ThoughtEvent ¶
ThoughtEvent is recorded when the LLM generates a thought.
type ToolResult ¶
type ToolResult struct {
ToolName string `json:"tool_name"`
Success bool `json:"success"`
Output string `json:"output"`
Error string `json:"error,omitempty"`
}
ToolResult represents the result of a single tool execution
type TraceExport ¶
type TraceExport struct {
Entries []TAOEntry `json:"entries"`
Summary TraceSummary `json:"summary"`
Metadata TraceMetadata `json:"metadata"`
}
TraceExport represents the exportable trace data
type TraceMetadata ¶
type TraceMetadata struct {
Strategy string `json:"strategy"`
StartTime time.Time `json:"start_time"`
EndTime time.Time `json:"end_time"`
CompletionType string `json:"completion_type"` // "success", "max_iterations", "loop_detected", "error"
}
TraceMetadata contains metadata about the trace execution