36
36
import java .io .IOException ;
37
37
import java .util .Arrays ;
38
38
import java .util .List ;
39
+ import java .util .Optional ;
39
40
40
41
/** This class holds a generative model that can complete what you provided. */
41
42
public final class GenerativeModel {
@@ -45,6 +46,7 @@ public final class GenerativeModel {
45
46
private final GenerationConfig generationConfig ;
46
47
private final ImmutableList <SafetySetting > safetySettings ;
47
48
private final ImmutableList <Tool > tools ;
49
+ private final Optional <Content > systemInstruction ;
48
50
49
51
/**
50
52
* Constructs a GenerativeModel instance.
@@ -53,7 +55,7 @@ public final class GenerativeModel {
53
55
* "models/gemini-pro", "publishers/google/models/gemini-pro", where "gemini-pro" is the model
54
56
* name. Valid model names can be found at
55
57
* https://cloud.google.com/vertex-ai/docs/generative-ai/learn/models#gemini-models
56
- * @param vertexAI a {@link com.google.cloud.vertexai.VertexAI} that contains the default configs
58
+ * @param vertexAi a {@link com.google.cloud.vertexai.VertexAI} that contains the default configs
57
59
* for the generative model
58
60
*/
59
61
public GenerativeModel (String modelName , VertexAI vertexAi ) {
@@ -62,6 +64,7 @@ public GenerativeModel(String modelName, VertexAI vertexAi) {
62
64
GenerationConfig .getDefaultInstance (),
63
65
ImmutableList .of (),
64
66
ImmutableList .of (),
67
+ Optional .empty (),
65
68
vertexAi );
66
69
}
67
70
@@ -76,14 +79,15 @@ public GenerativeModel(String modelName, VertexAI vertexAi) {
76
79
* that will be used by default for generating response
77
80
* @param tools a list of {@link com.google.cloud.vertexai.api.Tool} instances that can be used by
78
81
* the model as auxiliary tools to generate content.
79
- * @param vertexAI a {@link com.google.cloud.vertexai.VertexAI} that contains the default configs
82
+ * @param vertexAi a {@link com.google.cloud.vertexai.VertexAI} that contains the default configs
80
83
* for the generative model
81
84
*/
82
85
private GenerativeModel (
83
86
String modelName ,
84
87
GenerationConfig generationConfig ,
85
88
ImmutableList <SafetySetting > safetySettings ,
86
89
ImmutableList <Tool > tools ,
90
+ Optional <Content > systemInstruction ,
87
91
VertexAI vertexAi ) {
88
92
checkArgument (
89
93
!Strings .isNullOrEmpty (modelName ),
@@ -105,6 +109,7 @@ private GenerativeModel(
105
109
this .generationConfig = generationConfig ;
106
110
this .safetySettings = safetySettings ;
107
111
this .tools = tools ;
112
+ this .systemInstruction = systemInstruction ;
108
113
}
109
114
110
115
/** Builder class for {@link GenerativeModel}. */
@@ -114,20 +119,22 @@ public static class Builder {
114
119
private GenerationConfig generationConfig = GenerationConfig .getDefaultInstance ();
115
120
private ImmutableList <SafetySetting > safetySettings = ImmutableList .of ();
116
121
private ImmutableList <Tool > tools = ImmutableList .of ();
122
+ private Optional <Content > systemInstruction = Optional .empty ();
117
123
118
124
public GenerativeModel build () {
119
125
checkArgument (
120
126
!Strings .isNullOrEmpty (modelName ),
121
127
"modelName is required. Please call setModelName() before building." );
122
128
checkNotNull (vertexAi , "vertexAi is required. Please call setVertexAi() before building." );
123
- return new GenerativeModel (modelName , generationConfig , safetySettings , tools , vertexAi );
129
+ return new GenerativeModel (
130
+ modelName , generationConfig , safetySettings , tools , systemInstruction , vertexAi );
124
131
}
125
132
126
133
/**
127
134
* Sets the name of the generative model. This is required for building a GenerativeModel
128
135
* instance. Supported format: "gemini-pro", "models/gemini-pro",
129
136
* "publishers/google/models/gemini-pro", where "gemini-pro" is the model name. Valid model
130
- * names can be found at
137
+ * names can be found in the Gemini models documentation
131
138
* https://cloud.google.com/vertex-ai/docs/generative-ai/learn/models#gemini-models
132
139
*/
133
140
@ CanIgnoreReturnValue
@@ -187,6 +194,19 @@ public Builder setTools(List<Tool> tools) {
187
194
this .tools = ImmutableList .copyOf (tools );
188
195
return this ;
189
196
}
197
+
198
+ /**
199
+ * Sets a system instruction that will be used by default to interact with the generative model.
200
+ */
201
+ @ CanIgnoreReturnValue
202
+ public Builder setSystemInstruction (Content systemInstruction ) {
203
+ checkNotNull (
204
+ systemInstruction ,
205
+ "system instruction can't be null. "
206
+ + "Use Optional.empty() if no system instruction should be provided." );
207
+ this .systemInstruction = Optional .of (systemInstruction );
208
+ return this ;
209
+ }
190
210
}
191
211
192
212
/**
@@ -197,7 +217,13 @@ public Builder setTools(List<Tool> tools) {
197
217
* @return a new {@link GenerativeModel} instance with the specified GenerationConfig.
198
218
*/
199
219
public GenerativeModel withGenerationConfig (GenerationConfig generationConfig ) {
200
- return new GenerativeModel (modelName , generationConfig , safetySettings , tools , vertexAi );
220
+ return new GenerativeModel (
221
+ modelName ,
222
+ generationConfig ,
223
+ ImmutableList .copyOf (safetySettings ),
224
+ ImmutableList .copyOf (tools ),
225
+ systemInstruction ,
226
+ vertexAi );
201
227
}
202
228
203
229
/**
@@ -209,19 +235,46 @@ public GenerativeModel withGenerationConfig(GenerationConfig generationConfig) {
209
235
*/
210
236
public GenerativeModel withSafetySettings (List <SafetySetting > safetySettings ) {
211
237
return new GenerativeModel (
212
- modelName , generationConfig , ImmutableList .copyOf (safetySettings ), tools , vertexAi );
238
+ modelName ,
239
+ generationConfig ,
240
+ ImmutableList .copyOf (safetySettings ),
241
+ ImmutableList .copyOf (tools ),
242
+ systemInstruction ,
243
+ vertexAi );
213
244
}
214
245
215
246
/**
216
247
* Creates a copy of the current model with updated tools.
217
248
*
218
- * @param safetySettings a list of {@link com.google.cloud.vertexai.api.Tool} that will be used in
219
- * the new model.
249
+ * @param tools a list of {@link com.google.cloud.vertexai.api.Tool} that will be used in the new
250
+ * model.
220
251
* @return a new {@link GenerativeModel} instance with the specified tools.
221
252
*/
222
253
public GenerativeModel withTools (List <Tool > tools ) {
223
254
return new GenerativeModel (
224
- modelName , generationConfig , safetySettings , ImmutableList .copyOf (tools ), vertexAi );
255
+ modelName ,
256
+ generationConfig ,
257
+ ImmutableList .copyOf (safetySettings ),
258
+ ImmutableList .copyOf (tools ),
259
+ systemInstruction ,
260
+ vertexAi );
261
+ }
262
+
263
+ /**
264
+ * Creates a copy of the current model with updated system instructions.
265
+ *
266
+ * @param systemInstruction a {@link com.google.cloud.vertexai.api.Content} containing system
267
+ * instructions.
268
+ * @return a new {@link GenerativeModel} instance with the specified tools.
269
+ */
270
+ public GenerativeModel withSystemInstruction (Content systemInstruction ) {
271
+ return new GenerativeModel (
272
+ modelName ,
273
+ generationConfig ,
274
+ ImmutableList .copyOf (safetySettings ),
275
+ ImmutableList .copyOf (tools ),
276
+ Optional .of (systemInstruction ),
277
+ vertexAi );
225
278
}
226
279
227
280
/**
@@ -453,13 +506,20 @@ private ApiFuture<GenerateContentResponse> generateContentAsync(GenerateContentR
453
506
*/
454
507
private GenerateContentRequest buildGenerateContentRequest (List <Content > contents ) {
455
508
checkArgument (contents != null && !contents .isEmpty (), "contents can't be null or empty." );
456
- return GenerateContentRequest .newBuilder ()
457
- .setModel (resourceName )
458
- .addAllContents (contents )
459
- .setGenerationConfig (generationConfig )
460
- .addAllSafetySettings (safetySettings )
461
- .addAllTools (tools )
462
- .build ();
509
+
510
+ GenerateContentRequest .Builder requestBuilder =
511
+ GenerateContentRequest .newBuilder ()
512
+ .setModel (resourceName )
513
+ .addAllContents (contents )
514
+ .setGenerationConfig (generationConfig )
515
+ .addAllSafetySettings (safetySettings )
516
+ .addAllTools (tools );
517
+
518
+ if (systemInstruction .isPresent ()) {
519
+ requestBuilder .setSystemInstruction (systemInstruction .get ());
520
+ }
521
+
522
+ return requestBuilder .build ();
463
523
}
464
524
465
525
/** Returns the model name of this generative model. */
@@ -475,8 +535,7 @@ public GenerationConfig getGenerationConfig() {
475
535
}
476
536
477
537
/**
478
- * Returns a list of {@link com.google.cloud.vertexai.api.SafetySettings} of this generative
479
- * model.
538
+ * Returns a list of {@link com.google.cloud.vertexai.api.SafetySetting} of this generative model.
480
539
*/
481
540
public ImmutableList <SafetySetting > getSafetySettings () {
482
541
return safetySettings ;
@@ -487,6 +546,11 @@ public ImmutableList<Tool> getTools() {
487
546
return tools ;
488
547
}
489
548
549
+ /** Returns the optional system instruction of this generative model. */
550
+ public Optional <Content > getSystemInstruction () {
551
+ return systemInstruction ;
552
+ }
553
+
490
554
public ChatSession startChat () {
491
555
return new ChatSession (this );
492
556
}
0 commit comments