0
# Model Tuning
1
2
Create and manage tuning jobs to fine-tune models with custom training data.
3
4
## Core Imports
5
6
```java
7
import com.google.genai.Tunings;
8
import com.google.genai.AsyncTunings;
9
import com.google.genai.Pager;
10
import com.google.genai.types.TuningOperation;
11
import com.google.genai.types.TuningJob;
12
import com.google.genai.types.TunedModel;
13
import com.google.genai.types.CreateTuningJobConfig;
14
import com.google.genai.types.Hyperparameters;
15
```
16
17
## Tunings Service
18
19
```java { .api }
20
package com.google.genai;
21
22
public final class Tunings {
23
public TuningOperation create(CreateTuningJobConfig config);
24
public TuningOperation get(String name, GetTuningJobConfig config);
25
public void cancel(String name, CancelTuningJobConfig config);
26
public Pager<TuningOperation> list(ListTuningJobsConfig config);
27
}
28
```
29
30
## Create Tuning Job
31
32
```java
33
import com.google.genai.types.TuningDataSource;
34
35
CreateTuningJobConfig config = CreateTuningJobConfig.builder()
36
.baseModel("gemini-1.5-flash-002")
37
.tuningDataSource(TuningDataSource.builder()
38
.gcsUri("gs://my-bucket/training-data.jsonl")
39
.build())
40
.hyperparameters(Hyperparameters.builder()
41
.epochCount(5)
42
.batchSize(8)
43
.learningRate(0.001)
44
.build())
45
.displayName("Custom Model v1")
46
.build();
47
48
TuningOperation operation = client.tunings.create(config);
49
System.out.println("Tuning job: " + operation.name().orElse("N/A"));
50
```
51
52
## Monitor Tuning Job
53
54
```java
55
TuningOperation operation = client.tunings.get(jobName, null);
56
57
while (!operation.done().orElse(false)) {
58
Thread.sleep(30000);
59
operation = client.tunings.get(operation.name().get(), null);
60
61
operation.metadata().ifPresent(metadata -> {
62
// Check progress
63
System.out.println("Status: " + metadata.state().orElse("N/A"));
64
});
65
}
66
67
if (operation.error().isPresent()) {
68
System.err.println("Tuning failed: " + operation.error().get());
69
} else {
70
operation.response().ifPresent(model -> {
71
System.out.println("Model ready: " + model.name().orElse("N/A"));
72
});
73
}
74
```
75
76
## Use Tuned Model
77
78
```java
79
// After tuning completes, use the tuned model
80
String tunedModelName = operation.response()
81
.flatMap(TunedModel::name)
82
.orElse(null);
83
84
GenerateContentResponse response = client.models.generateContent(
85
tunedModelName,
86
"Test the tuned model",
87
null
88
);
89
```
90
91
## List Tuning Jobs
92
93
```java
94
Pager<TuningOperation> pager = client.tunings.list(null);
95
96
for (TuningOperation op : pager) {
97
op.metadata().ifPresent(job -> {
98
System.out.println("Job: " + job.displayName().orElse("N/A"));
99
System.out.println(" State: " + job.state().orElse("N/A"));
100
});
101
}
102
```
103
104
## Cancel Tuning Job
105
106
```java
107
client.tunings.cancel(jobName, null);
108
System.out.println("Tuning job cancelled");
109
```
110
111
## Model Management
112
113
```java { .api }
114
public Model get(String model, GetModelConfig config);
115
public Pager<Model> list(ListModelsConfig config);
116
public Model update(String model, UpdateModelConfig config);
117
public DeleteModelResponse delete(String model, DeleteModelConfig config);
118
```
119
120
## List Models
121
122
```java
123
Pager<Model> pager = client.models.list(null);
124
125
for (Model model : pager) {
126
System.out.println("Model: " + model.name().orElse("N/A"));
127
System.out.println(" Display: " + model.displayName().orElse("N/A"));
128
System.out.println(" Description: " + model.description().orElse("N/A"));
129
}
130
```
131
132
## Get Model Info
133
134
```java
135
Model model = client.models.get("gemini-2.0-flash", null);
136
137
System.out.println("Name: " + model.name().orElse("N/A"));
138
System.out.println("Input token limit: " + model.inputTokenLimit().orElse(0));
139
System.out.println("Output token limit: " + model.outputTokenLimit().orElse(0));
140
141
model.supportedGenerationMethods().ifPresent(methods -> {
142
System.out.println("Supported methods: " + methods);
143
});
144
```
145
146
## Update Tuned Model
147
148
```java
149
UpdateModelConfig config = UpdateModelConfig.builder()
150
.displayName("Updated Model Name")
151
.description("Updated description")
152
.build();
153
154
Model updated = client.models.update(tunedModelName, config);
155
```
156
157
## Delete Tuned Model
158
159
```java
160
DeleteModelResponse response = client.models.delete(tunedModelName, null);
161
System.out.println("Model deleted");
162
```
163