|
| 1 | +// Copyright 2025 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package main |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "fmt" |
| 20 | + "log" |
| 21 | + "os" |
| 22 | + "time" |
| 23 | + |
| 24 | + "google.golang.org/adk/agent/llmagent" |
| 25 | + "google.golang.org/adk/evaluation" |
| 26 | + "google.golang.org/adk/evaluation/evaluators" |
| 27 | + "google.golang.org/adk/evaluation/storage" |
| 28 | + "google.golang.org/adk/model/gemini" |
| 29 | + "google.golang.org/adk/runner" |
| 30 | + "google.golang.org/adk/session" |
| 31 | + "google.golang.org/genai" |
| 32 | +) |
| 33 | + |
| 34 | +func main() { |
| 35 | + ctx := context.Background() |
| 36 | + |
| 37 | + model, err := gemini.NewModel(ctx, "gemini-2.0-flash-exp", &genai.ClientConfig{ |
| 38 | + APIKey: os.Getenv("GOOGLE_API_KEY"), |
| 39 | + }) |
| 40 | + if err != nil { |
| 41 | + log.Fatalf("Failed to create model: %v", err) |
| 42 | + } |
| 43 | + |
| 44 | + agent, err := llmagent.New(llmagent.Config{ |
| 45 | + Name: "math_assistant", |
| 46 | + Model: model, |
| 47 | + Description: "A helpful math assistant that answers basic math questions.", |
| 48 | + Instruction: "You are a math tutor. Answer math questions clearly and concisely.", |
| 49 | + }) |
| 50 | + if err != nil { |
| 51 | + log.Fatalf("Failed to create agent: %v", err) |
| 52 | + } |
| 53 | + |
| 54 | + sessionService := session.InMemoryService() |
| 55 | + |
| 56 | + agentRunner, err := runner.New(runner.Config{ |
| 57 | + AppName: "math-eval-app", |
| 58 | + Agent: agent, |
| 59 | + SessionService: sessionService, |
| 60 | + }) |
| 61 | + if err != nil { |
| 62 | + log.Fatalf("Failed to create agent runner: %v", err) |
| 63 | + } |
| 64 | + |
| 65 | + if err := evaluation.RegisterDefaultEvaluators(map[evaluation.MetricType]evaluation.EvaluatorFactory{ |
| 66 | + evaluation.MetricResponseMatch: evaluators.NewResponseMatchEvaluator, |
| 67 | + evaluation.MetricSemanticResponseMatch: evaluators.NewSemanticResponseMatchEvaluator, |
| 68 | + }); err != nil { |
| 69 | + log.Fatalf("Failed to register evaluators: %v", err) |
| 70 | + } |
| 71 | + |
| 72 | + judgeLLM, err := gemini.NewModel(ctx, "gemini-2.0-flash-exp", &genai.ClientConfig{ |
| 73 | + APIKey: os.Getenv("GOOGLE_API_KEY"), |
| 74 | + }) |
| 75 | + if err != nil { |
| 76 | + log.Fatalf("Failed to create judge LLM: %v", err) |
| 77 | + } |
| 78 | + |
| 79 | + evalStorage := storage.NewMemoryStorage() |
| 80 | + |
| 81 | + evalRunner := evaluation.NewRunner(evaluation.RunnerConfig{ |
| 82 | + AgentRunner: agentRunner, |
| 83 | + Storage: evalStorage, |
| 84 | + SessionService: sessionService, |
| 85 | + AppName: "math-eval-app", |
| 86 | + RateLimitDelay: 6 * time.Second, |
| 87 | + }) |
| 88 | + |
| 89 | + evalSet := &evaluation.EvalSet{ |
| 90 | + ID: "basic-math-eval", |
| 91 | + Name: "Basic Math Evaluation", |
| 92 | + EvalCases: []evaluation.EvalCase{ |
| 93 | + { |
| 94 | + ID: "addition-simple", |
| 95 | + Conversation: []evaluation.ConversationTurn{ |
| 96 | + {Role: "user", Content: "What is 2 + 2?"}, |
| 97 | + }, |
| 98 | + ExpectedResponse: "2 + 2 = 4", |
| 99 | + }, |
| 100 | + { |
| 101 | + ID: "multiplication-simple", |
| 102 | + Conversation: []evaluation.ConversationTurn{ |
| 103 | + {Role: "user", Content: "What is 5 times 3?"}, |
| 104 | + }, |
| 105 | + ExpectedResponse: "5 times 3 = 15", |
| 106 | + }, |
| 107 | + }, |
| 108 | + } |
| 109 | + |
| 110 | + err = evalStorage.SaveEvalSet(ctx, "math-eval-app", evalSet) |
| 111 | + if err != nil { |
| 112 | + log.Fatalf("Failed to save eval set: %v", err) |
| 113 | + } |
| 114 | + |
| 115 | + config := &evaluation.EvalConfig{ |
| 116 | + JudgeLLM: judgeLLM, |
| 117 | + JudgeModel: "gemini-2.0-flash-exp", |
| 118 | + Criteria: []evaluation.Criterion{ |
| 119 | + &evaluation.Threshold{ |
| 120 | + MinScore: 0.5, |
| 121 | + MetricType: evaluation.MetricResponseMatch, |
| 122 | + }, |
| 123 | + &evaluation.LLMAsJudgeCriterion{ |
| 124 | + Threshold: &evaluation.Threshold{ |
| 125 | + MinScore: 0.8, |
| 126 | + MetricType: evaluation.MetricSemanticResponseMatch, |
| 127 | + }, |
| 128 | + MetricType: evaluation.MetricSemanticResponseMatch, |
| 129 | + JudgeModel: "gemini-2.0-flash-exp", |
| 130 | + }, |
| 131 | + }, |
| 132 | + } |
| 133 | + |
| 134 | + fmt.Println("Running evaluation...") |
| 135 | + fmt.Println("===================") |
| 136 | + |
| 137 | + result, err := evalRunner.RunEvalSet(ctx, evalSet, config) |
| 138 | + if err != nil { |
| 139 | + log.Fatalf("Evaluation failed: %v", err) |
| 140 | + } |
| 141 | + |
| 142 | + fmt.Printf("\nEvaluation Complete!\n") |
| 143 | + fmt.Printf("===================\n") |
| 144 | + fmt.Printf("Overall Status: %s\n", result.Status) |
| 145 | + fmt.Printf("Overall Score: %.2f\n\n", result.OverallScore) |
| 146 | + |
| 147 | + for i, caseResult := range result.EvalCaseResults { |
| 148 | + fmt.Printf("Case %d: %s\n", i+1, caseResult.EvalID) |
| 149 | + fmt.Printf(" Status: %s\n", caseResult.FinalEvalStatus) |
| 150 | + for metricName, metric := range caseResult.OverallMetricResults { |
| 151 | + fmt.Printf(" %s: %.2f (%s)\n", metricName, metric.Score, metric.Status) |
| 152 | + if metric.ErrorMessage != "" { |
| 153 | + fmt.Printf(" Error: %s\n", metric.ErrorMessage) |
| 154 | + } |
| 155 | + } |
| 156 | + fmt.Println() |
| 157 | + } |
| 158 | + |
| 159 | + fmt.Println("Evaluation results saved to storage.") |
| 160 | +} |
0 commit comments