2
2
3
3
import androidx .annotation .OptIn ;
4
4
import androidx .lifecycle .ViewModel ;
5
+
5
6
import com .google .firebase .ai .FirebaseAI ;
7
+ import com .google .firebase .ai .GenerativeModel ;
8
+ import com .google .firebase .ai .LiveGenerativeModel ;
6
9
import com .google .firebase .ai .java .GenerativeModelFutures ;
7
- import com .google .firebase .ai .type . GenerativeBackend ;
8
- import com .google .firebase .ai .type . PublicPreviewAPI ;
9
- import com .google .firebase .ai .type .Tool ;
10
+ import com .google .firebase .ai .java . ImagenModelFutures ;
11
+ import com .google .firebase .ai .java . LiveModelFutures ;
12
+ import com .google .firebase .ai .type .Content ;
10
13
import com .google .firebase .ai .type .FunctionDeclaration ;
11
- import java .util .List ;
12
14
import com .google .firebase .ai .type .GenerationConfig ;
13
- import com .google .firebase .ai .type .ImagenGenerationConfig ;
15
+ import com .google .firebase .ai .type .GenerativeBackend ;
16
+ import com .google .firebase .ai .type .HarmBlockThreshold ;
17
+ import com .google .firebase .ai .type .HarmCategory ;
14
18
import com .google .firebase .ai .type .ImagenAspectRatio ;
19
+ import com .google .firebase .ai .type .ImagenGenerationConfig ;
15
20
import com .google .firebase .ai .type .ImagenImageFormat ;
16
21
import com .google .firebase .ai .type .LiveGenerationConfig ;
22
+ import com .google .firebase .ai .type .PublicPreviewAPI ;
17
23
import com .google .firebase .ai .type .ResponseModality ;
24
+ import com .google .firebase .ai .type .SafetySetting ;
18
25
import com .google .firebase .ai .type .SpeechConfig ;
26
+ import com .google .firebase .ai .type .Tool ;
19
27
import com .google .firebase .ai .type .Voice ;
20
- import com .google .firebase .ai .type .Voices ;
21
- import com .google .firebase .ai .java .ImagenModelFutures ;
22
- import com .google .firebase .ai .java .LiveModelFutures ;
23
- import com .google .firebase .ai .type .SafetySetting ;
24
- import com .google .firebase .ai .type .HarmCategory ;
25
- import com .google .firebase .ai .type .HarmBlockThreshold ;
28
+
26
29
import java .util .Collections ;
30
+ import java .util .List ;
27
31
28
32
public class GoogleAISnippets extends ViewModel {
29
33
void functionCalling (FunctionDeclaration fetchWeatherTool ) {
@@ -57,13 +61,10 @@ public void modelConfiguration_model_parameters_general() {
57
61
GenerationConfig config = configBuilder .build ();
58
62
59
63
// Specify the config as part of creating the `GenerativeModel` instance
60
- GenerativeModelFutures model = GenerativeModelFutures .from (
61
- FirebaseAI .getInstance (GenerativeBackend .googleAI ())
62
- .generativeModel (
63
- "gemini-1.5-flash" ,
64
- config
65
- )
66
- );
64
+ GenerativeModelFutures model =
65
+ GenerativeModelFutures .from (
66
+ FirebaseAI .getInstance (GenerativeBackend .googleAI ())
67
+ .generativeModel ("gemini-1.5-flash" , config ));
67
68
68
69
// ...
69
70
// [END model_parameters_general]
@@ -75,22 +76,19 @@ public void modelConfiguration_model_parameters_imagen() {
75
76
// ...
76
77
77
78
// Set parameter values in a `ImagenGenerationConfig` (example values shown here)
78
- ImagenGenerationConfig config = new ImagenGenerationConfig .Builder ()
79
- .setNegativePrompt ("frogs" )
80
- .setNumberOfImages (2 )
81
- .setAspectRatio (ImagenAspectRatio .LANDSCAPE_16x9 )
82
- .setImageFormat (ImagenImageFormat .jpeg (100 ))
83
- .setAddWatermark (false )
84
- .build ();
79
+ ImagenGenerationConfig config =
80
+ new ImagenGenerationConfig .Builder ()
81
+ .setNegativePrompt ("frogs" )
82
+ .setNumberOfImages (2 )
83
+ .setAspectRatio (ImagenAspectRatio .LANDSCAPE_16x9 )
84
+ .setImageFormat (ImagenImageFormat .jpeg (100 ))
85
+ .setAddWatermark (false )
86
+ .build ();
85
87
86
88
// Specify the config as part of creating the `ImagenModel` instance
87
- ImagenModelFutures model = ImagenModelFutures .from (
88
- FirebaseAI .getInstance (GenerativeBackend .googleAI ())
89
- .imagenModel (
90
- "imagen-3" ,
91
- config
92
- )
93
- );
89
+ ImagenModelFutures model =
90
+ ImagenModelFutures .from (
91
+ FirebaseAI .getInstance (GenerativeBackend .googleAI ()).imagenModel ("imagen-3" , config ));
94
92
95
93
// ...
96
94
// [END model_parameters_imagen]
@@ -115,13 +113,10 @@ public void modelConfiguration_model_parameters_live() {
115
113
116
114
// Initialize the Gemini Developer API backend service
117
115
// Specify the config as part of creating the `LiveModel` instance
118
- LiveModelFutures model = LiveModelFutures .from (
119
- FirebaseAI .getInstance (GenerativeBackend .googleAI ())
120
- .liveModel (
121
- "gemini-2.5-flash" ,
122
- config
123
- )
124
- );
116
+ LiveModelFutures model =
117
+ LiveModelFutures .from (
118
+ FirebaseAI .getInstance (GenerativeBackend .googleAI ())
119
+ .liveModel ("gemini-1.5-flash" , config ));
125
120
126
121
// ...
127
122
// [END model_parameters_live]
@@ -131,55 +126,84 @@ public void modelConfiguration_model_parameters_live() {
131
126
public void modelConfiguration_safety_settings_imagen () {
132
127
// [START safety_settings_imagen]
133
128
// Specify the safety settings as part of creating the `ImagenModel` instance
134
- ImagenModelFutures model = ImagenModelFutures .from (
135
- FirebaseAI .getInstance (GenerativeBackend .googleAI ())
136
- .imagenModel (
137
- /* modelName */ "imagen-3" ,
138
- /* imageGenerationConfig */ null )
139
- );
129
+ ImagenModelFutures model =
130
+ ImagenModelFutures .from (
131
+ FirebaseAI .getInstance (GenerativeBackend .googleAI ())
132
+ .imagenModel (/* modelName */ "imagen-3" , /* imageGenerationConfig */ null ));
140
133
141
134
// ...
142
135
// [END safety_settings_imagen]
143
136
}
144
137
145
138
public void modelConfiguration_safety_settings_multiple () {
146
139
// [START safety_settings_multiple]
147
- SafetySetting harassmentSafety = new SafetySetting ( HarmCategory . HARASSMENT ,
148
- HarmBlockThreshold .ONLY_HIGH , null );
140
+ SafetySetting harassmentSafety =
141
+ new SafetySetting ( HarmCategory . HARASSMENT , HarmBlockThreshold .ONLY_HIGH , null );
149
142
150
- SafetySetting hateSpeechSafety = new SafetySetting ( HarmCategory . HATE_SPEECH ,
151
- HarmBlockThreshold .MEDIUM_AND_ABOVE , null );
143
+ SafetySetting hateSpeechSafety =
144
+ new SafetySetting ( HarmCategory . HATE_SPEECH , HarmBlockThreshold .MEDIUM_AND_ABOVE , null );
152
145
153
146
// Specify the safety settings as part of creating the `GenerativeModel` instance
154
- GenerativeModelFutures model = GenerativeModelFutures .from (
155
- FirebaseAI .getInstance (GenerativeBackend .googleAI ())
156
- .generativeModel (
157
- /* modelName */ "gemini-2.5-flash" ,
158
- /* generationConfig is optional */ null ,
159
- List .of (harassmentSafety , hateSpeechSafety )
160
- )
161
- );
147
+ GenerativeModelFutures model =
148
+ GenerativeModelFutures .from (
149
+ FirebaseAI .getInstance (GenerativeBackend .googleAI ())
150
+ .generativeModel (
151
+ /* modelName */ "gemini-1.5-flash" ,
152
+ /* generationConfig is optional */ null ,
153
+ List .of (harassmentSafety , hateSpeechSafety )));
162
154
163
155
// ...
164
156
// [END safety_settings_multiple]
165
157
}
166
158
167
159
public void modelConfiguration_safety_settings_single () {
168
160
// [START safety_settings_single]
169
- SafetySetting harassmentSafety = new SafetySetting ( HarmCategory . HARASSMENT ,
170
- HarmBlockThreshold .ONLY_HIGH , null );
161
+ SafetySetting harassmentSafety =
162
+ new SafetySetting ( HarmCategory . HARASSMENT , HarmBlockThreshold .ONLY_HIGH , null );
171
163
172
164
// Specify the safety settings as part of creating the `GenerativeModel` instance
173
- GenerativeModelFutures model = GenerativeModelFutures .from (
174
- FirebaseAI .getInstance (GenerativeBackend .googleAI ())
175
- .generativeModel (
176
- /* modelName */ "gemini-1.5-flash" ,
177
- /* generationConfig is optional */ null ,
178
- Collections .singletonList (harassmentSafety )
179
- )
180
- );
165
+ GenerativeModelFutures model =
166
+ GenerativeModelFutures .from (
167
+ FirebaseAI .getInstance (GenerativeBackend .googleAI ())
168
+ .generativeModel (
169
+ /* modelName */ "gemini-1.5-flash" ,
170
+ /* generationConfig is optional */ null ,
171
+ Collections .singletonList (harassmentSafety )));
181
172
182
173
// ...
183
174
// [END safety_settings_single]
184
175
}
176
+
177
+ public void systemInstructions_general () {
178
+ // [START system_instructions_general]
179
+ // Specify the system instructions as part of creating the `GenerativeModel` instance
180
+ GenerativeModel ai = FirebaseAI .getInstance (GenerativeBackend .googleAI ())
181
+ .generativeModel (
182
+ /* modelName */ "gemini-1.5-flash" ,
183
+ /* generationConfig (optional) */ null ,
184
+ /* safetySettings (optional) */ null ,
185
+ /* tools (optional) */ null ,
186
+ /* toolConfig (optional) */ null ,
187
+ /* systemInstructions (optional) */ new Content .Builder ().addText ("You are a cat. Your name is Neko." ).build ()
188
+ );
189
+
190
+ GenerativeModelFutures model = GenerativeModelFutures .from (ai );
191
+ // [END system_instructions_general]
192
+ }
193
+
194
+ @ OptIn (markerClass = PublicPreviewAPI .class )
195
+ public void systemInstructions_live () {
196
+ // [START system_instructions_live]
197
+ // Specify the system instructions as part of creating the `LiveModel` instance
198
+ LiveGenerativeModel ai = FirebaseAI .getInstance (GenerativeBackend .googleAI ())
199
+ .liveModel (
200
+ /* modelName */ "gemini-2.5-flash" ,
201
+ /* generationConfig (optional) */ null ,
202
+ /* tools (optional) */ null ,
203
+ /* systemInstructions (optional) */ new Content .Builder ().addText ("You are a cat. Your name is Neko." ).build ()
204
+ );
205
+
206
+ LiveModelFutures model = LiveModelFutures .from (ai );
207
+ // [END system_instructions_live]
208
+ }
185
209
}
0 commit comments