22
33import androidx .annotation .OptIn ;
44import androidx .lifecycle .ViewModel ;
5+
56import com .google .firebase .ai .FirebaseAI ;
7+ import com .google .firebase .ai .GenerativeModel ;
8+ import com .google .firebase .ai .LiveGenerativeModel ;
69import 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 ;
1013import com .google .firebase .ai .type .FunctionDeclaration ;
11- import java .util .List ;
1214import 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 ;
1418import com .google .firebase .ai .type .ImagenAspectRatio ;
19+ import com .google .firebase .ai .type .ImagenGenerationConfig ;
1520import com .google .firebase .ai .type .ImagenImageFormat ;
1621import com .google .firebase .ai .type .LiveGenerationConfig ;
22+ import com .google .firebase .ai .type .PublicPreviewAPI ;
1723import com .google .firebase .ai .type .ResponseModality ;
24+ import com .google .firebase .ai .type .SafetySetting ;
1825import com .google .firebase .ai .type .SpeechConfig ;
26+ import com .google .firebase .ai .type .Tool ;
1927import 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+
2629import java .util .Collections ;
30+ import java .util .List ;
2731
2832public class GoogleAISnippets extends ViewModel {
2933 void functionCalling (FunctionDeclaration fetchWeatherTool ) {
@@ -57,13 +61,10 @@ public void modelConfiguration_model_parameters_general() {
5761 GenerationConfig config = configBuilder .build ();
5862
5963 // 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 ));
6768
6869 // ...
6970 // [END model_parameters_general]
@@ -75,22 +76,19 @@ public void modelConfiguration_model_parameters_imagen() {
7576 // ...
7677
7778 // 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 ();
8587
8688 // 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 ));
9492
9593 // ...
9694 // [END model_parameters_imagen]
@@ -115,13 +113,10 @@ public void modelConfiguration_model_parameters_live() {
115113
116114 // Initialize the Gemini Developer API backend service
117115 // 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 ));
125120
126121 // ...
127122 // [END model_parameters_live]
@@ -131,55 +126,84 @@ public void modelConfiguration_model_parameters_live() {
131126 public void modelConfiguration_safety_settings_imagen () {
132127 // [START safety_settings_imagen]
133128 // 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 ));
140133
141134 // ...
142135 // [END safety_settings_imagen]
143136 }
144137
145138 public void modelConfiguration_safety_settings_multiple () {
146139 // [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 );
149142
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 );
152145
153146 // 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 )));
162154
163155 // ...
164156 // [END safety_settings_multiple]
165157 }
166158
167159 public void modelConfiguration_safety_settings_single () {
168160 // [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 );
171163
172164 // 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 )));
181172
182173 // ...
183174 // [END safety_settings_single]
184175 }
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+ }
185209}
0 commit comments