Skip to content

Commit cf7ad52

Browse files
yyyu-googlecopybara-github
authored andcommitted
feat: introduce enterprise flag and GOOGLE_GENAI_USE_ENTERPRISE env var
PiperOrigin-RevId: 903940734
1 parent 10d15a6 commit cf7ad52

8 files changed

Lines changed: 240 additions & 20 deletions

File tree

README.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ We recommend using our Code Generation instructions [`codegen_instructions.md`](
2929

3030
1. Node.js version 20 or later
3131

32-
### The following are required for Vertex AI users (excluding Vertex AI Studio)
32+
### The following are required for Gemini Enterprise Agent Platform users (excluding Vertex AI Studio)
3333
1. [Select](https://console.cloud.google.com/project) or [create](https://cloud.google.com/resource-manager/docs/creating-managing-projects#creating_a_project) a Google Cloud project.
3434
1. [Enable billing for your project](https://cloud.google.com/billing/docs/how-to/modify-project).
3535
1. [Enable the Vertex AI API](https://console.cloud.google.com/flows/enableapi?apiid=aiplatform.googleapis.com).
@@ -77,7 +77,7 @@ main();
7777

7878
The Google Gen AI SDK provides support for both the
7979
[Google AI Studio](https://ai.google.dev/gemini-api/docs) and
80-
[Vertex AI](https://cloud.google.com/vertex-ai/generative-ai/docs/learn/overview)
80+
[Gemini Enterprise Agent Platform](https://cloud.google.com/vertex-ai/generative-ai/docs/learn/overview)
8181
implementations of the Gemini API.
8282

8383
### Gemini Developer API
@@ -104,15 +104,15 @@ import { GoogleGenAI } from '@google/genai';
104104
const ai = new GoogleGenAI({apiKey: 'GEMINI_API_KEY'});
105105
```
106106
107-
### Vertex AI
107+
### Gemini Enterprise Agent Platform
108108
109-
Sample code for VertexAI initialization:
109+
Sample code for Gemini Enterprise Agent Platform initialization:
110110
111111
```typescript
112112
import { GoogleGenAI } from '@google/genai';
113113
114114
const ai = new GoogleGenAI({
115-
vertexai: true,
115+
enterprise: true,
116116
project: 'your_project',
117117
location: 'your_location',
118118
});
@@ -122,19 +122,19 @@ const ai = new GoogleGenAI({
122122
123123
For NodeJS environments, you can create a client by configuring the necessary
124124
environment variables. Configuration setup instructions depends on whether
125-
you're using the Gemini Developer API or the Gemini API in Vertex AI.
125+
you're using the Gemini Developer API or the Gemini Enterprise Agent Platform.
126126
127127
**Gemini Developer API:** Set `GOOGLE_API_KEY` as shown below:
128128
129129
```bash
130130
export GOOGLE_API_KEY='your-api-key'
131131
```
132132
133-
**Gemini API on Vertex AI:** Set `GOOGLE_GENAI_USE_VERTEXAI`,
133+
**Gemini Enterprise Agent Platform:** Set `GOOGLE_GENAI_USE_ENTERPRISE`,
134134
`GOOGLE_CLOUD_PROJECT` and `GOOGLE_CLOUD_LOCATION`, as shown below:
135135
136136
```bash
137-
export GOOGLE_GENAI_USE_VERTEXAI=true
137+
export GOOGLE_GENAI_USE_ENTERPRISE=true
138138
export GOOGLE_CLOUD_PROJECT='your-project-id'
139139
export GOOGLE_CLOUD_LOCATION='us-central1'
140140
```
@@ -152,11 +152,11 @@ preview features in the APIs. The stable API endpoints can be selected by
152152
setting the API version to `v1`.
153153
154154
To set the API version use `apiVersion`. For example, to set the API version to
155-
`v1` for Vertex AI:
155+
`v1` for Gemini Enterprise Agent Platform:
156156
157157
```typescript
158158
const ai = new GoogleGenAI({
159-
vertexai: true,
159+
enterprise: true,
160160
project: 'your_project',
161161
location: 'your_location',
162162
apiVersion: 'v1'
@@ -600,7 +600,7 @@ for (const output of interaction.outputs!) {
600600
This SDK (`@google/genai`) is Google Deepmind’s "vanilla" SDK for its generative
601601
AI offerings, and is where Google Deepmind adds new AI features.
602602
603-
Models hosted either on the [Vertex AI platform](https://cloud.google.com/vertex-ai/generative-ai/docs/learn/overview) or the [Gemini Developer platform](https://ai.google.dev/gemini-api/docs) are accessible through this SDK.
603+
Models hosted either on the [Gemini Enterprise Agent Platform](https://cloud.google.com/vertex-ai/generative-ai/docs/learn/overview) or the [Gemini Developer platform](https://ai.google.dev/gemini-api/docs) are accessible through this SDK.
604604
605605
Other SDKs may be offering additional AI frameworks on top of this SDK, or may
606606
be targeting specific project environments (like Firebase).

api-report/genai-node.api.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1807,6 +1807,7 @@ export class GoogleGenAI {
18071807
export interface GoogleGenAIOptions {
18081808
apiKey?: string;
18091809
apiVersion?: string;
1810+
enterprise?: boolean;
18101811
googleAuthOptions?: GoogleAuthOptions;
18111812
httpOptions?: HttpOptions;
18121813
location?: string;

api-report/genai-web.api.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1807,6 +1807,7 @@ export class GoogleGenAI {
18071807
export interface GoogleGenAIOptions {
18081808
apiKey?: string;
18091809
apiVersion?: string;
1810+
enterprise?: boolean;
18101811
googleAuthOptions?: GoogleAuthOptions;
18111812
httpOptions?: HttpOptions;
18121813
location?: string;

api-report/genai.api.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1807,6 +1807,7 @@ export class GoogleGenAI {
18071807
export interface GoogleGenAIOptions {
18081808
apiKey?: string;
18091809
apiVersion?: string;
1810+
enterprise?: boolean;
18101811
googleAuthOptions?: GoogleAuthOptions;
18111812
httpOptions?: HttpOptions;
18121813
location?: string;

src/client.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,23 @@ export interface GoogleGenAIOptions {
3939
* Optional. Determines whether to use the Vertex AI or the Gemini API.
4040
*
4141
* @remarks
42+
* When true, the {@link https://cloud.google.com/vertex-ai/docs/reference/rest | Gemini Enterprise Agent Platform API} will used.
43+
* When false, the {@link https://ai.google.dev/api | Gemini API} will be used.
44+
* When both enterprise and vertexai are set, and they have different values,
45+
* an Error will be thrown.
46+
*
47+
* If unset, default SDK behavior is to use the Gemini API service.
48+
*/
49+
enterprise?: boolean;
50+
/**
51+
* Optional. Determines whether to use the Vertex AI (now Gemini Enterprise Agent Platform) or the Gemini API.
52+
* The `enterprise` flag is recommended instead.
53+
*
54+
* @remarks
4255
* When true, the {@link https://cloud.google.com/vertex-ai/docs/reference/rest | Vertex AI API} will used.
4356
* When false, the {@link https://ai.google.dev/api | Gemini API} will be used.
57+
* When both enterprise and vertexai are set, and they have different values,
58+
* an Error will be thrown.
4459
*
4560
* If unset, default SDK behavior is to use the Gemini API service.
4661
*/
@@ -195,7 +210,16 @@ export class GoogleGenAI {
195210
`An API Key must be set when running in an unspecified environment.\n + ${crossError().message}`,
196211
);
197212
}
198-
this.vertexai = options.vertexai ?? false;
213+
if (
214+
options.enterprise !== undefined &&
215+
options.vertexai !== undefined &&
216+
options.enterprise !== options.vertexai
217+
) {
218+
throw new Error(
219+
'enterprise and vertexAI flags have conflicting values, please set enterprise value only.',
220+
);
221+
}
222+
this.vertexai = options.enterprise ?? options.vertexai ?? false;
199223
this.apiKey = options.apiKey;
200224
this.apiVersion = options.apiVersion;
201225
this.httpOptions = options.httpOptions;

src/node/node_client.ts

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,45 @@ import {NodeFiles} from './node_files.js';
3232

3333
const LANGUAGE_LABEL_PREFIX = 'gl-node/';
3434

35+
function resolveCloudFlag(options: GoogleGenAIOptions): boolean {
36+
if (options.enterprise !== undefined || options.vertexai !== undefined) {
37+
if (
38+
options.enterprise !== undefined &&
39+
options.vertexai !== undefined &&
40+
options.enterprise !== options.vertexai
41+
) {
42+
throw new Error(
43+
'enterprise and vertexAI flags have conflicting values, please set enterprise value only.',
44+
);
45+
}
46+
return options.enterprise ?? options.vertexai!;
47+
}
48+
49+
const envEnterpriseStr = getEnv('GOOGLE_GENAI_USE_ENTERPRISE');
50+
const envVertexaiStr = getEnv('GOOGLE_GENAI_USE_VERTEXAI');
51+
const useEnterpriseEnv = stringToBoolean(envEnterpriseStr);
52+
const useVertexaiEnv = stringToBoolean(envVertexaiStr);
53+
54+
if (
55+
envEnterpriseStr !== undefined &&
56+
envVertexaiStr !== undefined &&
57+
useEnterpriseEnv !== useVertexaiEnv
58+
) {
59+
console.warn(
60+
'Warning: Both GOOGLE_GENAI_USE_ENTERPRISE and GOOGLE_GENAI_USE_VERTEXAI are set with conflicting values. The value of GOOGLE_GENAI_USE_ENTERPRISE will be used.',
61+
);
62+
}
63+
64+
if (envEnterpriseStr !== undefined) {
65+
return useEnterpriseEnv;
66+
}
67+
if (envVertexaiStr !== undefined) {
68+
return useVertexaiEnv;
69+
}
70+
71+
return false;
72+
}
73+
3574
/**
3675
* The Google GenAI SDK.
3776
*
@@ -148,8 +187,7 @@ export class GoogleGenAI {
148187
);
149188
}
150189

151-
this.vertexai =
152-
options.vertexai ?? getBooleanEnv('GOOGLE_GENAI_USE_VERTEXAI') ?? false;
190+
this.vertexai = resolveCloudFlag(options);
153191
const envApiKey = getApiKeyFromEnv();
154192
const envProject = getEnv('GOOGLE_CLOUD_PROJECT');
155193
const envLocation = getEnv('GOOGLE_CLOUD_LOCATION');
@@ -163,7 +201,7 @@ export class GoogleGenAI {
163201
}
164202

165203
// Handle when to use Vertex AI in express mode (api key)
166-
if (options.vertexai) {
204+
if (this.vertexai) {
167205
if (options.googleAuthOptions?.credentials) {
168206
// Explicit credentials take precedence over implicit api_key.
169207
console.debug(
@@ -204,7 +242,7 @@ export class GoogleGenAI {
204242

205243
const baseUrl = getBaseUrl(
206244
options.httpOptions,
207-
options.vertexai,
245+
this.vertexai,
208246
getEnv('GOOGLE_VERTEX_BASE_URL'),
209247
getEnv('GOOGLE_GEMINI_BASE_URL'),
210248
);
@@ -251,10 +289,6 @@ function getEnv(env: string): string | undefined {
251289
return process?.env?.[env]?.trim() ?? undefined;
252290
}
253291

254-
function getBooleanEnv(env: string): boolean {
255-
return stringToBoolean(getEnv(env));
256-
}
257-
258292
function stringToBoolean(str?: string): boolean {
259293
if (str === undefined) {
260294
return false;

test/unit/node/base_url_test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ describe('setDefaultBaseUrls', () => {
1111
afterEach(() => {
1212
delete process.env['GOOGLE_GEMINI_BASE_URL'];
1313
delete process.env['GOOGLE_VERTEX_BASE_URL'];
14+
delete process.env['GOOGLE_GENAI_USE_ENTERPRISE'];
15+
delete process.env['GOOGLE_GENAI_USE_VERTEXAI'];
1416

1517
setDefaultBaseUrls({});
1618
});

0 commit comments

Comments
 (0)