Skip to content

Commit 466884b

Browse files
authored
feat(vertexai): add name constructor for function calling schema (#12898)
* add name constructor for function calling schema * fix for analyzer
1 parent a41c4b9 commit 466884b

File tree

3 files changed

+93
-10
lines changed

3 files changed

+93
-10
lines changed

packages/firebase_vertexai/firebase_vertexai/lib/src/vertex_content.dart

+3
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ final class Content {
5050
static Content functionResponse(
5151
String name, Map<String, Object?>? response) =>
5252
Content('function', [FunctionResponse(name, response)]);
53+
/// Return a [Content] with multiple [FunctionResponse].
54+
static Content functionResponses(Iterable<FunctionResponse> responses) =>
55+
Content('function', responses.toList());
5356

5457
/// Return a [Content] with [TextPart] of system instruction.
5558
static Content system(String instructions) =>

packages/firebase_vertexai/firebase_vertexai/lib/src/vertex_function_calling.dart

+87-1
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,6 @@ enum FunctionCallingMode {
184184
/// Represents a select subset of an
185185
/// [OpenAPI 3.0 schema object](https://spec.openapis.org/oas/v3.0.3#schema).
186186
final class Schema {
187-
// TODO: Add named constructors for the types?
188187
/// Constructor
189188
Schema(
190189
this.type, {
@@ -197,6 +196,93 @@ final class Schema {
197196
this.requiredProperties,
198197
});
199198

199+
/// Construct a schema for an object with one or more properties.
200+
Schema.object({
201+
required Map<String, Schema> properties,
202+
List<String>? requiredProperties,
203+
String? description,
204+
bool? nullable,
205+
}) : this(
206+
SchemaType.object,
207+
properties: properties,
208+
requiredProperties: requiredProperties,
209+
description: description,
210+
nullable: nullable,
211+
);
212+
213+
/// Construct a schema for an array of values with a specified type.
214+
Schema.array({
215+
required Schema items,
216+
String? description,
217+
bool? nullable,
218+
}) : this(
219+
SchemaType.array,
220+
description: description,
221+
nullable: nullable,
222+
items: items,
223+
);
224+
225+
/// Construct a schema for bool value.
226+
Schema.boolean({
227+
String? description,
228+
bool? nullable,
229+
}) : this(
230+
SchemaType.boolean,
231+
description: description,
232+
nullable: nullable,
233+
);
234+
235+
/// Construct a schema for an integer number.
236+
///
237+
/// The [format] may be "int32" or "int64".
238+
Schema.integer({
239+
String? description,
240+
bool? nullable,
241+
String? format,
242+
}) : this(
243+
SchemaType.integer,
244+
description: description,
245+
nullable: nullable,
246+
format: format,
247+
);
248+
249+
/// Construct a schema for a non-integer number.
250+
///
251+
/// The [format] may be "float" or "double".
252+
Schema.number({
253+
String? description,
254+
bool? nullable,
255+
String? format,
256+
}) : this(
257+
SchemaType.number,
258+
description: description,
259+
nullable: nullable,
260+
format: format,
261+
);
262+
263+
/// Construct a schema for String value with enumerated possible values.
264+
Schema.enumString({
265+
required List<String> enumValues,
266+
String? description,
267+
bool? nullable,
268+
}) : this(
269+
SchemaType.string,
270+
enumValues: enumValues,
271+
description: description,
272+
nullable: nullable,
273+
format: 'enum',
274+
);
275+
276+
/// Construct a schema for a String value.
277+
Schema.string({
278+
String? description,
279+
bool? nullable,
280+
}) : this(
281+
SchemaType.string,
282+
description: description,
283+
nullable: nullable,
284+
);
285+
200286
/// The type of this value.
201287
SchemaType type;
202288

packages/firebase_vertexai/firebase_vertexai/lib/src/vertex_model.dart

+3-9
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,7 @@ final class GenerativeModel {
6969
: [],
7070
generationConfig: generationConfig?.toGoogleAI(),
7171
systemInstruction: systemInstruction?.toGoogleAI(),
72-
tools: tools != null
73-
? tools.map((tool) => tool.toGoogleAI()).toList()
74-
: [],
72+
tools: tools?.map((tool) => tool.toGoogleAI()).toList(),
7573
toolConfig: toolConfig?.toGoogleAI(),
7674
);
7775
final google_ai.GenerativeModel _googleAIModel;
@@ -136,9 +134,7 @@ final class GenerativeModel {
136134
final response = await _googleAIModel.generateContent(googlePrompt,
137135
safetySettings: googleSafetySettings,
138136
generationConfig: generationConfig?.toGoogleAI(),
139-
tools: tools != null
140-
? tools.map((tool) => tool.toGoogleAI()).toList()
141-
: [],
137+
tools: tools?.map((tool) => tool.toGoogleAI()).toList(),
142138
toolConfig: toolConfig?.toGoogleAI());
143139
return response.toVertex();
144140
}
@@ -167,9 +163,7 @@ final class GenerativeModel {
167163
? safetySettings.map((setting) => setting.toGoogleAI()).toList()
168164
: [],
169165
generationConfig: generationConfig?.toGoogleAI(),
170-
tools: tools != null
171-
? tools.map((tool) => tool.toGoogleAI()).toList()
172-
: [],
166+
tools: tools?.map((tool) => tool.toGoogleAI()).toList(),
173167
toolConfig: toolConfig?.toGoogleAI())
174168
.map((r) => r.toVertex());
175169
}

0 commit comments

Comments
 (0)