Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Vertex AI] Add apiVersion parameter to RequestOptions #8715

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions common/api-review/vertexai.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -505,6 +511,7 @@ export interface PromptFeedback {

// @public
export interface RequestOptions {
apiVersion?: ApiVersion;
baseUrl?: string;
timeout?: number;
}
Expand Down
18 changes: 18 additions & 0 deletions docs-devsite/vertexai.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) | |
Expand Down Expand Up @@ -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.

<b>Signature:</b>

```typescript
export declare enum ApiVersion
```

## Enumeration Members

| Member | Value | Description |
| --- | --- | --- |
| V1 | <code>&quot;v1&quot;</code> | The stable channel for version 1 of the API. |
| V1BETA | <code>&quot;v1beta&quot;</code> | The beta channel for version 1 of the API. |

## BlockReason

Reason that a prompt was blocked.
Expand Down
11 changes: 11 additions & 0 deletions docs-devsite/vertexai.requestoptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <code>"v1beta"</code> (<code>[ApiVersion.V1BETA](./vertexai.md#apiversionv1beta_enummember)</code>). |
| [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 <code>"v1beta"</code> (<code>[ApiVersion.V1BETA](./vertexai.md#apiversionv1beta_enummember)</code>).

<b>Signature:</b>

```typescript
apiVersion?: ApiVersion;
```

## RequestOptions.baseUrl

Base url for endpoint. Defaults to https://firebasevertexai.googleapis.com
Expand Down
14 changes: 13 additions & 1 deletion packages/vertexai/src/requests/request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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',
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 @@ -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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think if we are going to go with the enum, we had better go into constants.ts and assign the new enum to DEFAULT_API_VERSION so we don't have 2 sources of truth for the "v1beta" string (is v1beta the default still or is v1?). Or maybe just get rid of the DEFAULT_API_VERSION in constants.ts since it's not a constant anymore and do the DEFAULT_API_VERSION assignment here in this file.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I prefer the plan of getting rid of the DEFAULT_API_VERSION in constants.ts and setting it here.

In this PR the default is still v1beta but the plan is to move to v1 as the default alongside the release of Imagen (requiring devs to switch to v1beta to use it since it'll be Public Preview, not GA).

const baseUrl = this.requestOptions?.baseUrl || DEFAULT_BASE_URL;
let url = `${baseUrl}/${apiVersion}`;
url += `/projects/${this.apiSettings.project}`;
Expand Down
11 changes: 11 additions & 0 deletions packages/vertexai/src/types/enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ export type Role = (typeof POSSIBLE_ROLES)[number];
*/
export const POSSIBLE_ROLES = ['user', 'model', 'function', 'system'] as const;

/**
* 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
Expand Down
6 changes: 6 additions & 0 deletions packages/vertexai/src/types/requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import { TypedSchema } from '../requests/schema-builder';
import { Content, Part } from './content';
import {
ApiVersion,
FunctionCallingMode,
HarmBlockMethod,
HarmBlockThreshold,
Expand Down Expand Up @@ -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 <code>"v1beta"</code>
* (<code>{@link ApiVersion.V1BETA}</code>).
*/
apiVersion?: ApiVersion;
}

/**
Expand Down
Loading