Skip to content

Commit c3328bb

Browse files
committed
feat: safety settings and model parameter snippets
1 parent 5018f42 commit c3328bb

File tree

4 files changed

+598
-9
lines changed

4 files changed

+598
-9
lines changed

firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/java/GoogleAISnippets.java

Lines changed: 159 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,29 @@
11
package com.google.firebase.example.ailogic.java;
22

3+
import androidx.annotation.OptIn;
34
import androidx.lifecycle.ViewModel;
45
import com.google.firebase.ai.FirebaseAI;
56
import com.google.firebase.ai.java.GenerativeModelFutures;
67
import com.google.firebase.ai.type.GenerativeBackend;
8+
import com.google.firebase.ai.type.PublicPreviewAPI;
79
import com.google.firebase.ai.type.Tool;
810
import com.google.firebase.ai.type.FunctionDeclaration;
911
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;
1027

1128
public class GoogleAISnippets extends ViewModel {
1229
void functionCalling(FunctionDeclaration fetchWeatherTool) {
@@ -17,11 +34,152 @@ void functionCalling(FunctionDeclaration fetchWeatherTool) {
1734
GenerativeModelFutures.from(
1835
FirebaseAI.getInstance(GenerativeBackend.googleAI())
1936
.generativeModel(
20-
"gemini-2.5-flash",
37+
"gemini-1.5-flash",
2138
null,
2239
null,
2340
// Provide the function declaration to the model.
2441
List.of(Tool.functionDeclarations(List.of(fetchWeatherTool)))));
2542
// [END function_calling_specify_declaration_during_init]
2643
}
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+
}
27185
}

firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/java/VertexAISnippets.java

Lines changed: 159 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,29 @@
11
package com.google.firebase.example.ailogic.java;
22

3+
import androidx.annotation.OptIn;
34
import androidx.lifecycle.ViewModel;
45

5-
import com.google.firebase.ai.type.GenerativeBackend;
66
import com.google.firebase.ai.FirebaseAI;
77
import com.google.firebase.ai.java.GenerativeModelFutures;
8-
import com.google.firebase.ai.type.Tool;
8+
import com.google.firebase.ai.java.ImagenModelFutures;
9+
import com.google.firebase.ai.java.LiveModelFutures;
910
import com.google.firebase.ai.type.FunctionDeclaration;
11+
import com.google.firebase.ai.type.GenerationConfig;
12+
import com.google.firebase.ai.type.GenerativeBackend;
13+
import com.google.firebase.ai.type.HarmBlockThreshold;
14+
import com.google.firebase.ai.type.HarmCategory;
15+
import com.google.firebase.ai.type.ImagenAspectRatio;
16+
import com.google.firebase.ai.type.ImagenGenerationConfig;
17+
import com.google.firebase.ai.type.ImagenImageFormat;
18+
import com.google.firebase.ai.type.LiveGenerationConfig;
19+
import com.google.firebase.ai.type.PublicPreviewAPI;
20+
import com.google.firebase.ai.type.ResponseModality;
21+
import com.google.firebase.ai.type.SafetySetting;
22+
import com.google.firebase.ai.type.SpeechConfig;
23+
import com.google.firebase.ai.type.Tool;
24+
import com.google.firebase.ai.type.Voice;
1025

26+
import java.util.Collections;
1127
import java.util.List;
1228

1329
public class VertexAISnippets extends ViewModel {
@@ -27,4 +43,145 @@ void functionCalling(FunctionDeclaration fetchWeatherTool) {
2743
List.of(Tool.functionDeclarations(List.of(fetchWeatherTool)))));
2844
// [END function_calling_specify_declaration_during_init]
2945
}
46+
47+
public void modelConfiguration_model_parameters_general() {
48+
// [START model_parameters_general]
49+
// ...
50+
51+
// Set parameter values in a `GenerationConfig` (example values shown here)
52+
GenerationConfig.Builder configBuilder = new GenerationConfig.Builder();
53+
configBuilder.maxOutputTokens = 200;
54+
configBuilder.stopSequences = List.of("red");
55+
configBuilder.temperature = 0.9f;
56+
configBuilder.topK = 16;
57+
configBuilder.topP = 0.1f;
58+
59+
GenerationConfig config = configBuilder.build();
60+
61+
// Specify the config as part of creating the `GenerativeModel` instance
62+
GenerativeModelFutures model = GenerativeModelFutures.from(
63+
FirebaseAI.getInstance(GenerativeBackend.vertexAI())
64+
.generativeModel(
65+
"gemini-1.5-flash",
66+
config
67+
)
68+
);
69+
70+
// ...
71+
// [END model_parameters_general]
72+
}
73+
74+
@OptIn(markerClass = PublicPreviewAPI.class)
75+
public void modelConfiguration_model_parameters_imagen() {
76+
// [START model_parameters_imagen]
77+
// ...
78+
79+
// Set parameter values in a `ImagenGenerationConfig` (example values shown here)
80+
ImagenGenerationConfig config = new ImagenGenerationConfig.Builder()
81+
.setNegativePrompt("frogs")
82+
.setNumberOfImages(2)
83+
.setAspectRatio(ImagenAspectRatio.LANDSCAPE_16x9)
84+
.setImageFormat(ImagenImageFormat.jpeg(100))
85+
.setAddWatermark(false)
86+
.build();
87+
88+
// Specify the config as part of creating the `ImagenModel` instance
89+
ImagenModelFutures model = ImagenModelFutures.from(
90+
FirebaseAI.getInstance(GenerativeBackend.vertexAI())
91+
.imagenModel(
92+
"imagen-3",
93+
config
94+
)
95+
);
96+
97+
// ...
98+
// [END model_parameters_imagen]
99+
}
100+
101+
@OptIn(markerClass = PublicPreviewAPI.class)
102+
public void modelConfiguration_model_parameters_live() {
103+
// [START model_parameters_live]
104+
// ...
105+
106+
// Set parameter values in a `LiveGenerationConfig` (example values shown here)
107+
LiveGenerationConfig.Builder configBuilder = new LiveGenerationConfig.Builder();
108+
configBuilder.setMaxOutputTokens(200);
109+
configBuilder.setResponseModality(ResponseModality.AUDIO);
110+
111+
configBuilder.setSpeechConfig(new SpeechConfig(new Voice("FENRIR")));
112+
configBuilder.setTemperature(0.9f);
113+
configBuilder.topK = 16;
114+
configBuilder.topP = 0.1f;
115+
116+
LiveGenerationConfig config = configBuilder.build();
117+
118+
// Initialize the Vertex AI Gemini API backend service
119+
// Specify the config as part of creating the `LiveModel` instance
120+
LiveModelFutures model = LiveModelFutures.from(
121+
FirebaseAI.getInstance(GenerativeBackend.vertexAI())
122+
.liveModel(
123+
"gemini-1.5-flash",
124+
config
125+
)
126+
);
127+
128+
// ...
129+
// [END model_parameters_live]
130+
}
131+
132+
@OptIn(markerClass = PublicPreviewAPI.class)
133+
public void modelConfiguration_safety_settings_imagen() {
134+
// [START safety_settings_imagen]
135+
// Specify the safety settings as part of creating the `ImagenModel` instance
136+
ImagenModelFutures model = ImagenModelFutures.from(
137+
FirebaseAI.getInstance(GenerativeBackend.vertexAI())
138+
.imagenModel(
139+
/* modelName */ "imagen-3",
140+
/* imageGenerationConfig */ null)
141+
);
142+
143+
// ...
144+
// [END safety_settings_imagen]
145+
}
146+
147+
public void modelConfiguration_safety_settings_multiple() {
148+
// [START safety_settings_multiple]
149+
SafetySetting harassmentSafety = new SafetySetting(HarmCategory.HARASSMENT,
150+
HarmBlockThreshold.ONLY_HIGH, null);
151+
152+
SafetySetting hateSpeechSafety = new SafetySetting(HarmCategory.HATE_SPEECH,
153+
HarmBlockThreshold.MEDIUM_AND_ABOVE, null);
154+
155+
// Specify the safety settings as part of creating the `GenerativeModel` instance
156+
GenerativeModelFutures model = GenerativeModelFutures.from(
157+
FirebaseAI.getInstance(GenerativeBackend.vertexAI())
158+
.generativeModel(
159+
/* modelName */ "gemini-1.5-flash",
160+
/* generationConfig is optional */ null,
161+
List.of(harassmentSafety, hateSpeechSafety)
162+
)
163+
);
164+
165+
// ...
166+
// [END safety_settings_multiple]
167+
}
168+
169+
public void modelConfiguration_safety_settings_single() {
170+
// [START safety_settings_single]
171+
SafetySetting harassmentSafety = new SafetySetting(HarmCategory.HARASSMENT,
172+
HarmBlockThreshold.ONLY_HIGH, null);
173+
174+
// Specify the safety settings as part of creating the `GenerativeModel` instance
175+
GenerativeModelFutures model = GenerativeModelFutures.from(
176+
FirebaseAI.getInstance(GenerativeBackend.vertexAI())
177+
.generativeModel(
178+
/* modelName */ "gemini-1.5-flash",
179+
/* generationConfig is optional */ null,
180+
Collections.singletonList(harassmentSafety)
181+
)
182+
);
183+
184+
// ...
185+
// [END safety_settings_single]
186+
}
30187
}

0 commit comments

Comments
 (0)