-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreflection.go
More file actions
513 lines (433 loc) · 14.6 KB
/
reflection.go
File metadata and controls
513 lines (433 loc) · 14.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
package sdk
import (
"context"
"encoding/json"
"fmt"
"strings"
"github.com/xraph/ai-sdk/llm"
logger "github.com/xraph/go-utils/log"
"github.com/xraph/go-utils/metrics"
)
// ReflectionEngine evaluates the quality of reasoning and plans.
type ReflectionEngine struct {
llmManager LLMManager
provider string
model string
logger logger.Logger
metrics metrics.Metrics
// Configuration
qualityThreshold float64
criteria []ReflectionCriterion
promptTemplate string
}
// ReflectionEngineConfig configures the reflection engine.
type ReflectionEngineConfig struct {
LLMManager LLMManager
Provider string
Model string
QualityThreshold float64 // Minimum acceptable quality score
Criteria []ReflectionCriterion // Custom evaluation criteria
PromptTemplate string // Custom prompt template
}
// NewReflectionEngine creates a new reflection engine.
func NewReflectionEngine(logger logger.Logger, metrics metrics.Metrics, config *ReflectionEngineConfig) *ReflectionEngine {
if config == nil {
config = &ReflectionEngineConfig{}
}
// Set defaults
if config.QualityThreshold == 0 {
config.QualityThreshold = 0.7
}
if len(config.Criteria) == 0 {
config.Criteria = getDefaultCriteria()
}
if config.PromptTemplate == "" {
config.PromptTemplate = getDefaultReflectionPrompt()
}
return &ReflectionEngine{
llmManager: config.LLMManager,
provider: config.Provider,
model: config.Model,
logger: logger,
metrics: metrics,
qualityThreshold: config.QualityThreshold,
criteria: config.Criteria,
promptTemplate: config.PromptTemplate,
}
}
// EvaluateStep evaluates the quality of a single reasoning or execution step.
func (re *ReflectionEngine) EvaluateStep(ctx context.Context, step *AgentStep, history []*AgentStep) (*ReflectionResult, error) {
if re.logger != nil {
re.logger.Debug("Evaluating step",
logger.String("step_id", step.ID),
logger.Int("step_index", step.Index),
)
}
// Build evaluation prompt
prompt := re.buildStepEvaluationPrompt(step, history)
// Call LLM for evaluation
request := llm.ChatRequest{
Provider: re.provider,
Model: re.model,
Messages: []llm.ChatMessage{
{
Role: "system",
Content: "You are an expert at evaluating reasoning quality and identifying issues.",
},
{
Role: "user",
Content: prompt,
},
},
}
temp := 0.3
request.Temperature = &temp // Lower temperature for more consistent evaluation
response, err := re.llmManager.Chat(ctx, request)
if err != nil {
return nil, fmt.Errorf("LLM evaluation call failed: %w", err)
}
if len(response.Choices) == 0 {
return nil, fmt.Errorf("no evaluation response from LLM")
}
// Parse reflection result
result, err := re.parseReflectionResult(response.Choices[0].Message.Content)
if err != nil {
return nil, fmt.Errorf("failed to parse reflection result: %w", err)
}
if re.metrics != nil {
re.metrics.Counter("forge.ai.sdk.reflection.evaluations").Inc()
re.metrics.Histogram("forge.ai.sdk.reflection.quality_score").Observe(result.Score)
}
return result, nil
}
// EvaluateTrace evaluates a reasoning trace.
func (re *ReflectionEngine) EvaluateTrace(ctx context.Context, trace ReasoningTrace) (*ReflectionResult, error) {
if re.logger != nil {
re.logger.Debug("Evaluating reasoning trace",
logger.Int("step", trace.Step),
)
}
// Build evaluation prompt
prompt := re.buildTraceEvaluationPrompt(trace)
// Call LLM
request := llm.ChatRequest{
Provider: re.provider,
Model: re.model,
Messages: []llm.ChatMessage{
{
Role: "system",
Content: "You are an expert at evaluating reasoning quality.",
},
{
Role: "user",
Content: prompt,
},
},
}
tempTrace := 0.3
request.Temperature = &tempTrace
response, err := re.llmManager.Chat(ctx, request)
if err != nil {
return nil, fmt.Errorf("LLM evaluation call failed: %w", err)
}
if len(response.Choices) == 0 {
return nil, fmt.Errorf("no evaluation response")
}
// Parse result
result, err := re.parseReflectionResult(response.Choices[0].Message.Content)
if err != nil {
return nil, fmt.Errorf("failed to parse reflection: %w", err)
}
return result, nil
}
// EvaluatePlan evaluates the quality of an entire plan.
func (re *ReflectionEngine) EvaluatePlan(ctx context.Context, plan *Plan) (*ReflectionResult, error) {
if re.logger != nil {
re.logger.Debug("Evaluating plan",
logger.String("plan_id", plan.ID),
logger.Int("steps", len(plan.Steps)),
)
}
// Build evaluation prompt
prompt := re.buildPlanEvaluationPrompt(plan)
// Call LLM
request := llm.ChatRequest{
Provider: re.provider,
Model: re.model,
Messages: []llm.ChatMessage{
{
Role: "system",
Content: "You are an expert at evaluating plan quality and feasibility.",
},
{
Role: "user",
Content: prompt,
},
},
}
tempPlan := 0.3
request.Temperature = &tempPlan
response, err := re.llmManager.Chat(ctx, request)
if err != nil {
return nil, fmt.Errorf("LLM evaluation call failed: %w", err)
}
if len(response.Choices) == 0 {
return nil, fmt.Errorf("no evaluation response")
}
// Parse result
result, err := re.parseReflectionResult(response.Choices[0].Message.Content)
if err != nil {
return nil, fmt.Errorf("failed to parse reflection: %w", err)
}
// Check if plan should be replanned
result.ShouldReplan = result.Score < re.qualityThreshold || result.Quality == "invalid"
if re.metrics != nil {
re.metrics.Counter("forge.ai.sdk.reflection.plan_evaluations").Inc()
if result.ShouldReplan {
re.metrics.Counter("forge.ai.sdk.reflection.replan_triggered").Inc()
}
}
return result, nil
}
// ShouldReplan determines if replanning is needed based on reflection.
func (re *ReflectionEngine) ShouldReplan(result *ReflectionResult) bool {
if result == nil {
return false
}
// Replan if explicitly flagged
if result.ShouldReplan {
return true
}
// Replan if quality is below threshold
if result.Score < re.qualityThreshold {
return true
}
// Replan if quality is invalid
if result.Quality == "invalid" {
return true
}
return false
}
// Helper methods
func (re *ReflectionEngine) buildStepEvaluationPrompt(step *AgentStep, history []*AgentStep) string {
var sb strings.Builder
sb.WriteString(re.promptTemplate)
sb.WriteString("\n\n## Current Step to Evaluate\n\n")
sb.WriteString(fmt.Sprintf("**Input:** %s\n", step.Input))
sb.WriteString(fmt.Sprintf("**Output:** %s\n", step.Output))
if step.Reasoning != "" {
sb.WriteString(fmt.Sprintf("**Reasoning:** %s\n", step.Reasoning))
}
if len(step.ToolCalls) > 0 {
sb.WriteString("\n**Tool Calls:**\n")
for _, tc := range step.ToolCalls {
sb.WriteString(fmt.Sprintf("- %s: %v\n", tc.Name, tc.Arguments))
}
}
if len(step.ToolResults) > 0 {
sb.WriteString("\n**Tool Results:**\n")
for _, tr := range step.ToolResults {
sb.WriteString(fmt.Sprintf("- %s: %v\n", tr.Name, tr.Result))
if tr.Error != "" {
sb.WriteString(fmt.Sprintf(" Error: %s\n", tr.Error))
}
}
}
if step.Error != "" {
sb.WriteString(fmt.Sprintf("\n**Error:** %s\n", step.Error))
}
// Add recent history for context
if len(history) > 0 {
sb.WriteString("\n## Recent History (for context)\n\n")
start := len(history) - 3
if start < 0 {
start = 0
}
for i := start; i < len(history); i++ {
h := history[i]
sb.WriteString(fmt.Sprintf("Step %d: %s → %s\n", h.Index, h.Input, h.Output))
}
}
sb.WriteString("\n## Evaluation Criteria\n\n")
for _, criterion := range re.criteria {
sb.WriteString(fmt.Sprintf("- **%s** (weight: %.2f): %s\n", criterion.Name, criterion.Weight, criterion.Description))
}
sb.WriteString("\n## Required Output Format\n\n")
sb.WriteString("Provide your evaluation in JSON format:\n")
sb.WriteString("```json\n")
sb.WriteString("{\n")
sb.WriteString(" \"quality\": \"good\" | \"needs_improvement\" | \"invalid\",\n")
sb.WriteString(" \"score\": 0.0-1.0,\n")
sb.WriteString(" \"issues\": [\"issue1\", \"issue2\"],\n")
sb.WriteString(" \"suggestions\": [\"suggestion1\", \"suggestion2\"],\n")
sb.WriteString(" \"reasoning\": \"explanation of the evaluation\"\n")
sb.WriteString("}\n")
sb.WriteString("```\n")
return sb.String()
}
func (re *ReflectionEngine) buildTraceEvaluationPrompt(trace ReasoningTrace) string {
var sb strings.Builder
sb.WriteString("Evaluate the quality of this reasoning trace:\n\n")
sb.WriteString(fmt.Sprintf("**Thought:** %s\n", trace.Thought))
sb.WriteString(fmt.Sprintf("**Action:** %s\n", trace.Action))
sb.WriteString(fmt.Sprintf("**Observation:** %s\n", trace.Observation))
if trace.Reflection != "" {
sb.WriteString(fmt.Sprintf("**Self-Reflection:** %s\n", trace.Reflection))
}
sb.WriteString(fmt.Sprintf("\n**Confidence:** %.2f\n", trace.Confidence))
sb.WriteString("\nEvaluate:\n")
sb.WriteString("1. Is the thought process logical and clear?\n")
sb.WriteString("2. Is the chosen action appropriate for the thought?\n")
sb.WriteString("3. Does the observation provide useful information?\n")
sb.WriteString("4. Is the confidence level reasonable?\n")
sb.WriteString("\nProvide evaluation in JSON format with quality, score, issues, and suggestions.\n")
return sb.String()
}
func (re *ReflectionEngine) buildPlanEvaluationPrompt(plan *Plan) string {
var sb strings.Builder
sb.WriteString("Evaluate the quality of this plan:\n\n")
sb.WriteString(fmt.Sprintf("**Goal:** %s\n\n", plan.Goal))
sb.WriteString(fmt.Sprintf("**Status:** %s\n", plan.Status))
sb.WriteString(fmt.Sprintf("**Steps:** %d\n\n", len(plan.Steps)))
sb.WriteString("## Plan Steps:\n\n")
for i, step := range plan.Steps {
sb.WriteString(fmt.Sprintf("%d. %s\n", i+1, step.Description))
if len(step.ToolsNeeded) > 0 {
sb.WriteString(fmt.Sprintf(" Tools: %v\n", step.ToolsNeeded))
}
if len(step.Dependencies) > 0 {
sb.WriteString(fmt.Sprintf(" Dependencies: %v\n", step.Dependencies))
}
sb.WriteString(fmt.Sprintf(" Status: %s\n", step.Status))
if step.Result != nil {
sb.WriteString(fmt.Sprintf(" Result: %v\n", step.Result))
}
if step.Error != "" {
sb.WriteString(fmt.Sprintf(" Error: %s\n", step.Error))
}
sb.WriteString("\n")
}
sb.WriteString("## Evaluation Criteria:\n\n")
sb.WriteString("1. **Completeness:** Does the plan cover all necessary steps to achieve the goal?\n")
sb.WriteString("2. **Feasibility:** Are the steps realistic and achievable?\n")
sb.WriteString("3. **Dependencies:** Are dependencies correctly identified and ordered?\n")
sb.WriteString("4. **Tool Usage:** Are the right tools assigned to each step?\n")
sb.WriteString("5. **Error Handling:** Are there issues that need addressing?\n")
sb.WriteString("\nProvide evaluation in JSON format with quality, score, issues, suggestions, and shouldReplan flag.\n")
return sb.String()
}
func (re *ReflectionEngine) parseReflectionResult(content string) (*ReflectionResult, error) {
// Try to extract JSON from response
jsonStart := strings.Index(content, "{")
jsonEnd := strings.LastIndex(content, "}")
if jsonStart == -1 || jsonEnd == -1 {
// Fallback: parse from text
return re.parseReflectionFromText(content), nil
}
jsonStr := content[jsonStart : jsonEnd+1]
var result ReflectionResult
if err := json.Unmarshal([]byte(jsonStr), &result); err != nil {
// Fallback to text parsing
return re.parseReflectionFromText(content), nil
}
// Calculate score if not provided
if result.Score == 0 {
result.Score = re.calculateScoreFromQuality(result.Quality)
}
return &result, nil
}
func (re *ReflectionEngine) parseReflectionFromText(content string) *ReflectionResult {
result := &ReflectionResult{
Quality: "needs_improvement",
Score: 0.5,
Issues: make([]string, 0),
Suggestions: make([]string, 0),
Reasoning: content,
}
lower := strings.ToLower(content)
// Determine quality from keywords
if strings.Contains(lower, "excellent") || strings.Contains(lower, "perfect") || strings.Contains(lower, "outstanding") {
result.Quality = "good"
result.Score = 0.9
} else if strings.Contains(lower, "good") || strings.Contains(lower, "satisfactory") || strings.Contains(lower, "adequate") {
result.Quality = "good"
result.Score = 0.75
} else if strings.Contains(lower, "invalid") || strings.Contains(lower, "incorrect") || strings.Contains(lower, "wrong") {
result.Quality = "invalid"
result.Score = 0.3
}
// Extract issues
lines := strings.Split(content, "\n")
for _, line := range lines {
line = strings.TrimSpace(line)
if strings.HasPrefix(strings.ToLower(line), "issue:") || strings.HasPrefix(strings.ToLower(line), "- issue:") {
issue := strings.TrimSpace(strings.TrimPrefix(strings.ToLower(line), "issue:"))
issue = strings.TrimSpace(strings.TrimPrefix(issue, "- "))
if issue != "" {
result.Issues = append(result.Issues, issue)
}
}
if strings.HasPrefix(strings.ToLower(line), "suggestion:") || strings.HasPrefix(strings.ToLower(line), "- suggestion:") {
suggestion := strings.TrimSpace(strings.TrimPrefix(strings.ToLower(line), "suggestion:"))
suggestion = strings.TrimSpace(strings.TrimPrefix(suggestion, "- "))
if suggestion != "" {
result.Suggestions = append(result.Suggestions, suggestion)
}
}
}
// Determine if replanning is needed
result.ShouldReplan = result.Quality == "invalid" || len(result.Issues) > 3
return result
}
func (re *ReflectionEngine) calculateScoreFromQuality(quality string) float64 {
switch quality {
case "good":
return 0.8
case "needs_improvement":
return 0.6
case "invalid":
return 0.3
default:
return 0.5
}
}
// Default configuration
func getDefaultCriteria() []ReflectionCriterion {
return []ReflectionCriterion{
{
Name: "Logical Coherence",
Description: "Is the reasoning logically sound and consistent?",
Weight: 0.3,
},
{
Name: "Action Appropriateness",
Description: "Is the chosen action suitable for the situation?",
Weight: 0.25,
},
{
Name: "Completeness",
Description: "Does the step fully address the required task?",
Weight: 0.2,
},
{
Name: "Efficiency",
Description: "Is the approach efficient and not overly complex?",
Weight: 0.15,
},
{
Name: "Error Handling",
Description: "Are errors properly identified and handled?",
Weight: 0.1,
},
}
}
func getDefaultReflectionPrompt() string {
return `You are evaluating the quality of an agent's reasoning or execution step.
Your role is to identify strengths, weaknesses, and provide constructive feedback.
Focus on:
- Logical coherence and consistency
- Appropriateness of actions taken
- Completeness of the solution
- Efficiency and simplicity
- Error handling and recovery`
}