From ed0e0d424c815c1d8782e5a155608ff93d9528aa Mon Sep 17 00:00:00 2001 From: Andrew Heard Date: Mon, 20 Jan 2025 12:19:43 -0500 Subject: [PATCH 1/2] [Vertex AI] Add `apiVersion` parameter to `RequestOptions` --- packages/vertexai/src/requests/request.test.ts | 14 +++++++++++++- packages/vertexai/src/requests/request.ts | 3 +-- packages/vertexai/src/types/enums.ts | 11 +++++++++++ packages/vertexai/src/types/requests.ts | 6 ++++++ 4 files changed, 31 insertions(+), 3 deletions(-) diff --git a/packages/vertexai/src/requests/request.test.ts b/packages/vertexai/src/requests/request.test.ts index b6d0ecb9b71..79386758118 100644 --- a/packages/vertexai/src/requests/request.test.ts +++ b/packages/vertexai/src/requests/request.test.ts @@ -22,7 +22,7 @@ import chaiAsPromised from 'chai-as-promised'; import { RequestUrl, Task, getHeaders, makeRequest } from './request'; import { ApiSettings } from '../types/internal'; import { DEFAULT_API_VERSION } from '../constants'; -import { VertexAIErrorCode } from '../types'; +import { ApiVersion, VertexAIErrorCode } from '../types'; import { VertexAIError } from '../errors'; import { getMockResponse } from '../../test-utils/mock-response'; @@ -74,6 +74,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: ApiVersion.V1 } + ); + expect(url.toString()).to.include(ApiVersion.V1); + // TODO: Update test to set ApiVersion.V1BETA when ApiVersion.V1 becomes the default. + expect(ApiVersion.V1).to.not.equal(DEFAULT_API_VERSION); + }); it('custom baseUrl', async () => { const url = new RequestUrl( 'models/model-name', diff --git a/packages/vertexai/src/requests/request.ts b/packages/vertexai/src/requests/request.ts index f81b40635e3..991a82b18df 100644 --- a/packages/vertexai/src/requests/request.ts +++ b/packages/vertexai/src/requests/request.ts @@ -42,8 +42,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}`; diff --git a/packages/vertexai/src/types/enums.ts b/packages/vertexai/src/types/enums.ts index 3e66bacc612..69146017358 100644 --- a/packages/vertexai/src/types/enums.ts +++ b/packages/vertexai/src/types/enums.ts @@ -27,6 +27,17 @@ export type Role = (typeof POSSIBLE_ROLES)[number]; */ export const POSSIBLE_ROLES = ['user', 'model', 'function', 'system'] as const; +/** + * Supported API versions for the Vertex AI in Firebase endpoint. + * @public + */ +export enum ApiVersion { + /** The stable channel for version 1 of the API. */ + V1 = 'v1', + /** The beta channel for version 1 of the API. */ + V1BETA = 'v1beta' +} + /** * Harm categories that would cause prompts or candidates to be blocked. * @public diff --git a/packages/vertexai/src/types/requests.ts b/packages/vertexai/src/types/requests.ts index dc7576f232d..235c9df13c1 100644 --- a/packages/vertexai/src/types/requests.ts +++ b/packages/vertexai/src/types/requests.ts @@ -18,6 +18,7 @@ import { TypedSchema } from '../requests/schema-builder'; import { Content, Part } from './content'; import { + ApiVersion, FunctionCallingMode, HarmBlockMethod, HarmBlockThreshold, @@ -129,6 +130,11 @@ export interface RequestOptions { * Base url for endpoint. Defaults to https://firebasevertexai.googleapis.com */ baseUrl?: string; + /** + * API version for endpoint. Defaults to "v1beta" + * ({@link ApiVersion.V1BETA}). + */ + apiVersion?: ApiVersion; } /** From 7b3d822fab07006bf37876240b458d8d48618c28 Mon Sep 17 00:00:00 2001 From: Andrew Heard Date: Mon, 20 Jan 2025 12:43:39 -0500 Subject: [PATCH 2/2] Add generated reference docs --- common/api-review/vertexai.api.md | 7 +++++++ docs-devsite/vertexai.md | 18 ++++++++++++++++++ docs-devsite/vertexai.requestoptions.md | 11 +++++++++++ packages/vertexai/src/types/enums.ts | 2 +- 4 files changed, 37 insertions(+), 1 deletion(-) diff --git a/common/api-review/vertexai.api.md b/common/api-review/vertexai.api.md index 041bc62451f..10337b453f8 100644 --- a/common/api-review/vertexai.api.md +++ b/common/api-review/vertexai.api.md @@ -9,6 +9,12 @@ import { FirebaseApp } from '@firebase/app'; import { FirebaseAuthTokenData } from '@firebase/auth-interop-types'; import { FirebaseError } from '@firebase/util'; +// @public +export enum ApiVersion { + V1 = "v1", + V1BETA = "v1beta" +} + // @public export class ArraySchema extends Schema { constructor(schemaParams: SchemaParams, items: TypedSchema); @@ -505,6 +511,7 @@ export interface PromptFeedback { // @public export interface RequestOptions { + apiVersion?: ApiVersion; baseUrl?: string; timeout?: number; } diff --git a/docs-devsite/vertexai.md b/docs-devsite/vertexai.md index d9e26eabc5d..c347e312d27 100644 --- a/docs-devsite/vertexai.md +++ b/docs-devsite/vertexai.md @@ -40,6 +40,7 @@ The Vertex AI in Firebase Web SDK. | Enumeration | Description | | --- | --- | +| [ApiVersion](./vertexai.md#apiversion) | API versions for the Vertex AI in Firebase endpoint. | | [BlockReason](./vertexai.md#blockreason) | Reason that a prompt was blocked. | | [FinishReason](./vertexai.md#finishreason) | Reason that a candidate finished. | | [FunctionCallingMode](./vertexai.md#functioncallingmode) | | @@ -217,6 +218,23 @@ A type that includes all specific Schema types. export type TypedSchema = IntegerSchema | NumberSchema | StringSchema | BooleanSchema | ObjectSchema | ArraySchema; ``` +## ApiVersion + +API versions for the Vertex AI in Firebase endpoint. + +Signature: + +```typescript +export declare enum ApiVersion +``` + +## Enumeration Members + +| Member | Value | Description | +| --- | --- | --- | +| V1 | "v1" | The stable channel for version 1 of the API. | +| V1BETA | "v1beta" | The beta channel for version 1 of the API. | + ## BlockReason Reason that a prompt was blocked. diff --git a/docs-devsite/vertexai.requestoptions.md b/docs-devsite/vertexai.requestoptions.md index 6d074775520..d40becc345d 100644 --- a/docs-devsite/vertexai.requestoptions.md +++ b/docs-devsite/vertexai.requestoptions.md @@ -22,9 +22,20 @@ export interface RequestOptions | Property | Type | Description | | --- | --- | --- | +| [apiVersion](./vertexai.requestoptions.md#requestoptionsapiversion) | [ApiVersion](./vertexai.md#apiversion) | API version for endpoint. Defaults to "v1beta" ([ApiVersion.V1BETA](./vertexai.md#apiversionv1beta_enummember)). | | [baseUrl](./vertexai.requestoptions.md#requestoptionsbaseurl) | string | Base url for endpoint. Defaults to https://firebasevertexai.googleapis.com | | [timeout](./vertexai.requestoptions.md#requestoptionstimeout) | number | Request timeout in milliseconds. Defaults to 180 seconds (180000ms). | +## RequestOptions.apiVersion + +API version for endpoint. Defaults to "v1beta" ([ApiVersion.V1BETA](./vertexai.md#apiversionv1beta_enummember)). + +Signature: + +```typescript +apiVersion?: ApiVersion; +``` + ## RequestOptions.baseUrl Base url for endpoint. Defaults to https://firebasevertexai.googleapis.com diff --git a/packages/vertexai/src/types/enums.ts b/packages/vertexai/src/types/enums.ts index 69146017358..db1361b4c0b 100644 --- a/packages/vertexai/src/types/enums.ts +++ b/packages/vertexai/src/types/enums.ts @@ -28,7 +28,7 @@ export type Role = (typeof POSSIBLE_ROLES)[number]; export const POSSIBLE_ROLES = ['user', 'model', 'function', 'system'] as const; /** - * Supported API versions for the Vertex AI in Firebase endpoint. + * API versions for the Vertex AI in Firebase endpoint. * @public */ export enum ApiVersion {