diff --git a/packages/firebase_vertexai/firebase_vertexai/example/lib/main.dart b/packages/firebase_vertexai/firebase_vertexai/example/lib/main.dart index 669252a77d82..14d7f819356c 100644 --- a/packages/firebase_vertexai/firebase_vertexai/example/lib/main.dart +++ b/packages/firebase_vertexai/firebase_vertexai/example/lib/main.dart @@ -102,12 +102,14 @@ class _ChatWidgetState extends State { FirebaseVertexAI.instanceFor(auth: FirebaseAuth.instance); _model = vertex_instance.generativeModel( model: 'gemini-1.5-flash', + requestOptions: RequestOptions(apiVersion: ApiVersion.v1beta), ); _functionCallModel = vertex_instance.generativeModel( model: 'gemini-1.5-flash', tools: [ Tool.functionDeclarations([fetchWeatherTool]), ], + requestOptions: RequestOptions(apiVersion: ApiVersion.v1beta), ); _chat = _model.startChat(); }); diff --git a/packages/firebase_vertexai/firebase_vertexai/lib/firebase_vertexai.dart b/packages/firebase_vertexai/firebase_vertexai/lib/firebase_vertexai.dart index a170d8deb969..cec449ff9616 100644 --- a/packages/firebase_vertexai/firebase_vertexai/lib/firebase_vertexai.dart +++ b/packages/firebase_vertexai/firebase_vertexai/lib/firebase_vertexai.dart @@ -56,4 +56,5 @@ export 'src/function_calling.dart' Tool, ToolConfig; export 'src/model.dart' show GenerativeModel; +export 'src/request_options.dart' show ApiVersion, RequestOptions; export 'src/schema.dart' show Schema, SchemaType; diff --git a/packages/firebase_vertexai/firebase_vertexai/lib/src/firebase_vertexai.dart b/packages/firebase_vertexai/firebase_vertexai/lib/src/firebase_vertexai.dart index df9c26f9eaaf..64fd17651806 100644 --- a/packages/firebase_vertexai/firebase_vertexai/lib/src/firebase_vertexai.dart +++ b/packages/firebase_vertexai/firebase_vertexai/lib/src/firebase_vertexai.dart @@ -22,6 +22,7 @@ import 'api.dart'; import 'content.dart'; import 'function_calling.dart'; import 'model.dart'; +import 'request_options.dart'; const _defaultLocation = 'us-central1'; @@ -96,6 +97,7 @@ class FirebaseVertexAI extends FirebasePluginPlatform { List? tools, ToolConfig? toolConfig, Content? systemInstruction, + RequestOptions? requestOptions, }) { return createGenerativeModel( model: model, @@ -108,6 +110,7 @@ class FirebaseVertexAI extends FirebasePluginPlatform { tools: tools, toolConfig: toolConfig, systemInstruction: systemInstruction, + requestOptions: requestOptions, ); } } diff --git a/packages/firebase_vertexai/firebase_vertexai/lib/src/model.dart b/packages/firebase_vertexai/firebase_vertexai/lib/src/model.dart index 605adf9b4fa7..3ed3ab9886a8 100644 --- a/packages/firebase_vertexai/firebase_vertexai/lib/src/model.dart +++ b/packages/firebase_vertexai/firebase_vertexai/lib/src/model.dart @@ -26,10 +26,11 @@ import 'api.dart'; import 'client.dart'; import 'content.dart'; import 'function_calling.dart'; +import 'request_options.dart'; import 'vertex_version.dart'; const _baseUrl = 'firebasevertexai.googleapis.com'; -const _apiVersion = 'v1beta'; +const _defaultApiVersion = ApiVersion.v1beta; /// [Task] enum class for [GenerativeModel] to make request. enum Task { @@ -77,9 +78,10 @@ final class GenerativeModel { List? tools, ToolConfig? toolConfig, Content? systemInstruction, + RequestOptions? requestOptions, http.Client? httpClient, }) : _model = _normalizeModelName(model), - _baseUri = _vertexUri(app, location), + _baseUri = _vertexUri(app, location, requestOptions), _safetySettings = safetySettings ?? [], _generationConfig = generationConfig, _tools = tools, @@ -101,9 +103,10 @@ final class GenerativeModel { List? tools, ToolConfig? toolConfig, Content? systemInstruction, + RequestOptions? requestOptions, ApiClient? apiClient, }) : _model = _normalizeModelName(model), - _baseUri = _vertexUri(app, location), + _baseUri = _vertexUri(app, location, requestOptions), _safetySettings = safetySettings ?? [], _generationConfig = generationConfig, _tools = tools, @@ -135,11 +138,13 @@ final class GenerativeModel { return (prefix: parts.first, name: parts.skip(1).join('/')); } - static Uri _vertexUri(FirebaseApp app, String location) { - var projectId = app.options.projectId; + static Uri _vertexUri( + FirebaseApp app, String location, RequestOptions? requestOptions) { + final projectId = app.options.projectId; + final apiVersion = requestOptions?.apiVersion ?? _defaultApiVersion; return Uri.https( _baseUrl, - '/$_apiVersion/projects/$projectId/locations/$location/publishers/google', + '/${apiVersion.versionIdentifier}/projects/$projectId/locations/$location/publishers/google', ); } @@ -295,6 +300,7 @@ GenerativeModel createGenerativeModel({ List? tools, ToolConfig? toolConfig, Content? systemInstruction, + RequestOptions? requestOptions, }) => GenerativeModel._( model: model, @@ -307,6 +313,7 @@ GenerativeModel createGenerativeModel({ tools: tools, toolConfig: toolConfig, systemInstruction: systemInstruction, + requestOptions: requestOptions, ); /// Creates a model with an overridden [ApiClient] for testing. @@ -324,6 +331,7 @@ GenerativeModel createModelWithClient({ List? safetySettings, List? tools, ToolConfig? toolConfig, + RequestOptions? requestOptions, }) => GenerativeModel._constructTestModel( model: model, @@ -336,4 +344,5 @@ GenerativeModel createModelWithClient({ systemInstruction: systemInstruction, tools: tools, toolConfig: toolConfig, + requestOptions: requestOptions, apiClient: client); diff --git a/packages/firebase_vertexai/firebase_vertexai/lib/src/request_options.dart b/packages/firebase_vertexai/firebase_vertexai/lib/src/request_options.dart new file mode 100644 index 000000000000..e0f2bc46a74a --- /dev/null +++ b/packages/firebase_vertexai/firebase_vertexai/lib/src/request_options.dart @@ -0,0 +1,39 @@ +// Copyright 2025 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +/// Configuration parameters for sending requests to the backend. +final class RequestOptions { + /// Constructor + RequestOptions({this.apiVersion}); + + /// The API version to use in requests to the backend. + final ApiVersion? apiVersion; +} + +/// API versions for the Vertex AI in Firebase endpoint. +enum ApiVersion { + /// The stable channel for version 1 of the API. + v1('v1'), + + /// The beta channel for version 1 of the API. + v1beta('v1beta'); + + const ApiVersion(this.versionIdentifier); + + /// The identifier for the API version. + final String versionIdentifier; + + @override + String toString() => name; +}