-
Notifications
You must be signed in to change notification settings - Fork 155
Expand file tree
/
Copy pathtunings_test.go
More file actions
214 lines (193 loc) · 6.71 KB
/
Copy pathtunings_test.go
File metadata and controls
214 lines (193 loc) · 6.71 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
package genai
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/google/go-cmp/cmp"
)
func TestTuningsTuneUnit(t *testing.T) {
ctx := context.Background()
tests := []struct {
name string
baseModel string
trainingDataset *TuningDataset
config *CreateTuningJobConfig
expectedPath string
expectedBody map[string]any
backend Backend
envVarProvider func() map[string]string
}{
{
name: "VertexAI_BaseModel_DPO",
baseModel: "gemini-2.5-flash",
trainingDataset: &TuningDataset{
GCSURI: "gs://test-bucket/train.jsonl",
},
config: &CreateTuningJobConfig{
TunedModelDisplayName: "Test Tuned Model",
Method: TuningMethodPreferenceTuning,
AdapterSize: AdapterSizeOne,
},
expectedPath: "/tuningJobs",
expectedBody: map[string]any{
"baseModel": "gemini-2.5-flash",
"tunedModelDisplayName": "Test Tuned Model",
"preferenceOptimizationSpec": map[string]any{
"trainingDatasetUri": "gs://test-bucket/train.jsonl",
"hyperParameters": map[string]any{
"adapterSize": "ADAPTER_SIZE_ONE",
},
},
},
backend: BackendVertexAI,
envVarProvider: func() map[string]string {
return map[string]string{
"GOOGLE_API_KEY": "test-api-key",
}
},
},
{
name: "VertexAI_BaseModel",
baseModel: "gemini-1.5-pro-001",
trainingDataset: &TuningDataset{
GCSURI: "gs://test-bucket/train.jsonl",
},
config: &CreateTuningJobConfig{
TunedModelDisplayName: "Test Tuned Model",
},
expectedPath: "/tuningJobs",
expectedBody: map[string]any{
"baseModel": "gemini-1.5-pro-001",
"tunedModelDisplayName": "Test Tuned Model",
"supervisedTuningSpec": map[string]any{
"trainingDatasetUri": "gs://test-bucket/train.jsonl",
},
},
backend: BackendVertexAI,
envVarProvider: func() map[string]string {
return map[string]string{
"GOOGLE_API_KEY": "test-api-key",
}
},
},
{
name: "VertexAI_PreTunedModel",
baseModel: "projects/123/locations/us-central1/models/456",
trainingDataset: &TuningDataset{
GCSURI: "gs://test-bucket/train.jsonl",
},
expectedPath: "/tuningJobs",
expectedBody: map[string]any{
"preTunedModel": map[string]any{
"tunedModelName": "projects/123/locations/us-central1/models/456",
},
"supervisedTuningSpec": map[string]any{
"trainingDatasetUri": "gs://test-bucket/train.jsonl",
},
},
backend: BackendVertexAI,
envVarProvider: func() map[string]string {
return map[string]string{
"GOOGLE_API_KEY": "test-api-key",
}
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
t.Errorf("Expected method %s, got %s", http.MethodPost, r.Method)
}
if !strings.HasSuffix(r.URL.Path, tt.expectedPath) {
t.Errorf("Expected path suffix %s, got %s", tt.expectedPath, r.URL.Path)
}
var body map[string]any
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
t.Fatalf("Failed to decode request body: %v", err)
}
if diff := cmp.Diff(tt.expectedBody, body); diff != "" {
t.Errorf("Request body mismatch (-want +got):\n%s", diff)
}
w.WriteHeader(http.StatusOK)
if err := json.NewEncoder(w).Encode(map[string]any{
"name": "projects/123/locations/us-central1/tuningJobs/789",
"state": "JOB_STATE_SUCCEEDED",
"tunedModel": map[string]any{
"model": "projects/123/locations/us-central1/models/abc",
},
}); err != nil {
t.Fatalf("Failed to encode response: %v", err)
}
}))
defer ts.Close()
client, err := NewClient(ctx, &ClientConfig{Backend: tt.backend, HTTPOptions: HTTPOptions{BaseURL: ts.URL}, envVarProvider: tt.envVarProvider})
if err != nil {
t.Fatalf("Failed to create client: %v", err)
}
_, err = client.Tunings.Tune(ctx, tt.baseModel, tt.trainingDataset, tt.config)
if err != nil {
t.Errorf("Tunings.Tune() failed unexpectedly: %v", err)
}
})
}
}
func TestTuningsTuneAPIMode(t *testing.T) {
if *mode != apiMode {
t.Skip("Skip. This test is only in the API mode")
}
ctx := context.Background()
t.Run("VertexAI", func(t *testing.T) {
if isDisabledTest(t) {
t.Skip("Skip: disabled test")
}
client, err := NewClient(ctx, &ClientConfig{Backend: BackendVertexAI})
if err != nil {
t.Fatal(err)
}
trainingDataset := &TuningDataset{
GCSURI: "gs://cloud-samples-data/ai-platform/generative_ai/gemini-2_0/text/sft_train_data.jsonl",
}
// Test tuning with a base model.
baseModel := "gemini-2.5-flash"
baseJob, err := client.Tunings.Tune(ctx, baseModel, trainingDataset, nil)
if err != nil {
t.Fatalf("Tunings.Tune() with base model failed: %v", err)
}
if baseJob.State != JobStatePending && baseJob.State != JobStateRunning && baseJob.State != JobStateQueued {
t.Errorf("Expected base job state to be PENDING, RUNNING or QUEUED, but got %s", baseJob.State)
}
// Wait for the base tuning job to start running (not waiting for completion)
for baseJob.State != JobStateRunning && baseJob.State != JobStateSucceeded && baseJob.State != JobStateFailed && baseJob.State != JobStateCancelled {
time.Sleep(10 * time.Second) // Poll every 10 seconds
baseJob, err = client.Tunings.Get(ctx, baseJob.Name, nil)
if err != nil {
t.Fatalf("Failed to get status of base tuning job: %v", err)
}
}
if baseJob.State != JobStateRunning && baseJob.State != JobStateSucceeded {
t.Fatalf("Base tuning job did not start running successfully. State: %s", baseJob.State)
}
preTunedModelName := "projects/801452371447/locations/us-central1/models/3399218262595076096"
// Test tuning with a pre-tuned model.
continuousJob, err := client.Tunings.Tune(ctx, preTunedModelName, trainingDataset, nil)
if err != nil {
t.Fatalf("Tunings.Tune() with pre-tuned model failed: %v", err)
}
if continuousJob.State != JobStatePending && continuousJob.State != JobStateRunning && continuousJob.State != JobStateQueued {
t.Errorf("Expected continuous job state to be PENDING, RUNNING or QUEUED, but got %s", continuousJob.State)
}
// Wait for the continuous tuning job to start running (not waiting for completion)
for continuousJob.State != JobStateRunning && continuousJob.State != JobStateSucceeded && continuousJob.State != JobStateFailed && continuousJob.State != JobStateCancelled {
time.Sleep(10 * time.Second) // Poll every 10 seconds
continuousJob, err = client.Tunings.Get(ctx, continuousJob.Name, nil)
if err != nil {
t.Fatalf("Failed to get status of continuous tuning job: %v", err)
}
}
})
}