Skip to content

Commit

Permalink
Revert "Remove apiVersion and location options (#8124)"
Browse files Browse the repository at this point in the history
Re-add the apiVersion and location options. Slight modification to the
revert, will not re-add the placeholder test.

This reverts commit bf7d2d8.
  • Loading branch information
ryanwilson committed May 2, 2024
1 parent 627b561 commit 9d7cefe
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 11 deletions.
9 changes: 6 additions & 3 deletions packages/vertexai/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { Provider } from '@firebase/component';
import { getModularInstance } from '@firebase/util';
import { DEFAULT_LOCATION, VERTEX_TYPE } from './constants';
import { VertexAIService } from './service';
import { VertexAI } from './public-types';
import { VertexAI, VertexAIOptions } from './public-types';
import { ERROR_FACTORY, VertexError } from './errors';
import { ModelParams, RequestOptions } from './types';
import { GenerativeModel } from './models/generative-model';
Expand All @@ -42,13 +42,16 @@ declare module '@firebase/component' {
*
* @param app - The {@link @firebase/app#FirebaseApp} to use.
*/
export function getVertexAI(app: FirebaseApp = getApp()): VertexAI {
export function getVertexAI(
app: FirebaseApp = getApp(),
options?: VertexAIOptions
): VertexAI {
app = getModularInstance(app);
// Dependencies
const vertexProvider: Provider<'vertex'> = _getProvider(app, VERTEX_TYPE);

return vertexProvider.getImmediate({
identifier: DEFAULT_LOCATION
identifier: options?.location || DEFAULT_LOCATION
});
}

Expand Down
27 changes: 27 additions & 0 deletions packages/vertexai/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* @license
* Copyright 2024 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.
*/

import { expect } from 'chai';

describe('Simple test', () => {
it('Should skip this test');
it('Should test this async thing', async () => {
// Do some async assertions, you can use `await` syntax if it helps
const val = await Promise.resolve(42);
expect(val).to.equal(42);
});
});
4 changes: 2 additions & 2 deletions packages/vertexai/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ function registerVertex(): void {
_registerComponent(
new Component(
VERTEX_TYPE,
container => {
(container, { instanceIdentifier: location }) => {
// getImmediate for FirebaseApp will always succeed
const app = container.getProvider('app').getImmediate();
const appCheckProvider = container.getProvider('app-check-internal');
return new VertexAIService(app, appCheckProvider);
return new VertexAIService(app, appCheckProvider, { location });
},
ComponentType.PUBLIC
).setMultipleInstances(true)
Expand Down
4 changes: 4 additions & 0 deletions packages/vertexai/src/public-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,7 @@ export interface VertexAI {
app: FirebaseApp;
location: string;
}

export interface VertexAIOptions {
location?: string;
}
12 changes: 12 additions & 0 deletions packages/vertexai/src/requests/request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,18 @@ describe('request methods', () => {
);
expect(url.toString()).to.include(DEFAULT_API_VERSION);
});
it('custom apiVersion', async () => {
const url = new RequestUrl(
'models/model-name',
Task.GENERATE_CONTENT,
fakeApiSettings,
false,
{ apiVersion: 'v100omega' }
);
expect(url.toString()).to.include(
'/v100omega/projects/my-project/locations/us-central1/models/model-name'
);
});
it('custom baseUrl', async () => {
const url = new RequestUrl(
'models/model-name',
Expand Down
3 changes: 1 addition & 2 deletions packages/vertexai/src/requests/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ export class RequestUrl {
public requestOptions?: RequestOptions
) {}
toString(): string {
// TODO: allow user-set option if that feature becomes available
const apiVersion = DEFAULT_API_VERSION;
const apiVersion = this.requestOptions?.apiVersion || DEFAULT_API_VERSION;
const baseUrl = this.requestOptions?.baseUrl || DEFAULT_BASE_URL;
let url = `${baseUrl}/${apiVersion}`;
url += `/projects/${this.apiSettings.project}`;
Expand Down
8 changes: 4 additions & 4 deletions packages/vertexai/src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/

import { FirebaseApp, _FirebaseService } from '@firebase/app';
import { VertexAI } from './public-types';
import { VertexAI, VertexAIOptions } from './public-types';
import {
AppCheckInternalComponentName,
FirebaseAppCheckInternal
Expand All @@ -30,12 +30,12 @@ export class VertexAIService implements VertexAI, _FirebaseService {

constructor(
public app: FirebaseApp,
appCheckProvider?: Provider<AppCheckInternalComponentName>
appCheckProvider?: Provider<AppCheckInternalComponentName>,
public options?: VertexAIOptions
) {
const appCheck = appCheckProvider?.getImmediate({ optional: true });
this.appCheck = appCheck || null;
// TODO: add in user-set location option when that feature is available
this.location = DEFAULT_LOCATION;
this.location = this.options?.location || DEFAULT_LOCATION;
}

_delete(): Promise<void> {
Expand Down
5 changes: 5 additions & 0 deletions packages/vertexai/src/types/requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ export interface RequestOptions {
* Request timeout in milliseconds.
*/
timeout?: number;
/**
* Version of API endpoint to call (e.g. "v1" or "v1beta"). If not specified,
* defaults to latest stable version.
*/
apiVersion?: string;
/**
* Base url for endpoint. Defaults to https://firebaseml.googleapis.com
*/
Expand Down

0 comments on commit 9d7cefe

Please sign in to comment.