1
1
package com .google .firebase .example .ailogic .java ;
2
2
3
+ import androidx .annotation .OptIn ;
3
4
import androidx .lifecycle .ViewModel ;
4
5
import com .google .firebase .ai .FirebaseAI ;
5
6
import com .google .firebase .ai .java .GenerativeModelFutures ;
6
7
import com .google .firebase .ai .type .GenerativeBackend ;
8
+ import com .google .firebase .ai .type .PublicPreviewAPI ;
7
9
import com .google .firebase .ai .type .Tool ;
8
10
import com .google .firebase .ai .type .FunctionDeclaration ;
9
11
import java .util .List ;
12
+ import com .google .firebase .ai .type .GenerationConfig ;
13
+ import com .google .firebase .ai .type .ImagenGenerationConfig ;
14
+ import com .google .firebase .ai .type .ImagenAspectRatio ;
15
+ import com .google .firebase .ai .type .ImagenImageFormat ;
16
+ import com .google .firebase .ai .type .LiveGenerationConfig ;
17
+ import com .google .firebase .ai .type .ResponseModality ;
18
+ import com .google .firebase .ai .type .SpeechConfig ;
19
+ 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 ;
26
+ import java .util .Collections ;
10
27
11
28
public class GoogleAISnippets extends ViewModel {
12
29
void functionCalling (FunctionDeclaration fetchWeatherTool ) {
@@ -17,11 +34,152 @@ void functionCalling(FunctionDeclaration fetchWeatherTool) {
17
34
GenerativeModelFutures .from (
18
35
FirebaseAI .getInstance (GenerativeBackend .googleAI ())
19
36
.generativeModel (
20
- "gemini-2 .5-flash" ,
37
+ "gemini-1 .5-flash" ,
21
38
null ,
22
39
null ,
23
40
// Provide the function declaration to the model.
24
41
List .of (Tool .functionDeclarations (List .of (fetchWeatherTool )))));
25
42
// [END function_calling_specify_declaration_during_init]
26
43
}
44
+
45
+ public void modelConfiguration_model_parameters_general () {
46
+ // [START model_parameters_general]
47
+ // ...
48
+
49
+ // Set parameter values in a `GenerationConfig` (example values shown here)
50
+ GenerationConfig .Builder configBuilder = new GenerationConfig .Builder ();
51
+ configBuilder .maxOutputTokens = 200 ;
52
+ configBuilder .stopSequences = List .of ("red" );
53
+ configBuilder .temperature = 0.9f ;
54
+ configBuilder .topK = 16 ;
55
+ configBuilder .topP = 0.1f ;
56
+
57
+ GenerationConfig config = configBuilder .build ();
58
+
59
+ // 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
+ );
67
+
68
+ // ...
69
+ // [END model_parameters_general]
70
+ }
71
+
72
+ @ OptIn (markerClass = PublicPreviewAPI .class )
73
+ public void modelConfiguration_model_parameters_imagen () {
74
+ // [START model_parameters_imagen]
75
+ // ...
76
+
77
+ // 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 ();
85
+
86
+ // 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
+ );
94
+
95
+ // ...
96
+ // [END model_parameters_imagen]
97
+ }
98
+
99
+ @ OptIn (markerClass = PublicPreviewAPI .class )
100
+ public void modelConfiguration_model_parameters_live () {
101
+ // [START model_parameters_live]
102
+ // ...
103
+
104
+ // Set parameter values in a `LiveGenerationConfig` (example values shown here)
105
+ LiveGenerationConfig .Builder configBuilder = new LiveGenerationConfig .Builder ();
106
+ configBuilder .setMaxOutputTokens (200 );
107
+ configBuilder .setResponseModality (ResponseModality .AUDIO );
108
+
109
+ configBuilder .setSpeechConfig (new SpeechConfig (new Voice ("FENRIR" )));
110
+ configBuilder .setTemperature (0.9f );
111
+ configBuilder .topK = 16 ;
112
+ configBuilder .topP = 0.1f ;
113
+
114
+ LiveGenerationConfig config = configBuilder .build ();
115
+
116
+ // Initialize the Gemini Developer API backend service
117
+ // 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
+ );
125
+
126
+ // ...
127
+ // [END model_parameters_live]
128
+ }
129
+
130
+ @ OptIn (markerClass = PublicPreviewAPI .class )
131
+ public void modelConfiguration_safety_settings_imagen () {
132
+ // [START safety_settings_imagen]
133
+ // 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
+ );
140
+
141
+ // ...
142
+ // [END safety_settings_imagen]
143
+ }
144
+
145
+ public void modelConfiguration_safety_settings_multiple () {
146
+ // [START safety_settings_multiple]
147
+ SafetySetting harassmentSafety = new SafetySetting (HarmCategory .HARASSMENT ,
148
+ HarmBlockThreshold .ONLY_HIGH , null );
149
+
150
+ SafetySetting hateSpeechSafety = new SafetySetting (HarmCategory .HATE_SPEECH ,
151
+ HarmBlockThreshold .MEDIUM_AND_ABOVE , null );
152
+
153
+ // 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
+ );
162
+
163
+ // ...
164
+ // [END safety_settings_multiple]
165
+ }
166
+
167
+ public void modelConfiguration_safety_settings_single () {
168
+ // [START safety_settings_single]
169
+ SafetySetting harassmentSafety = new SafetySetting (HarmCategory .HARASSMENT ,
170
+ HarmBlockThreshold .ONLY_HIGH , null );
171
+
172
+ // 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
+ );
181
+
182
+ // ...
183
+ // [END safety_settings_single]
184
+ }
27
185
}
0 commit comments