Skip to content

Commit 648de84

Browse files
authored
Add X-Firebase-AppId header to VertexAI requests (#8809)
1 parent edb4001 commit 648de84

File tree

15 files changed

+137
-8
lines changed

15 files changed

+137
-8
lines changed

.changeset/red-hornets-peel.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@firebase/vertexai': patch
3+
---
4+
5+
Throw an error when initializing models if `appId` is not defined in the given `VertexAI` instance.

common/api-review/vertexai.api.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -802,6 +802,7 @@ export const enum VertexAIErrorCode {
802802
INVALID_CONTENT = "invalid-content",
803803
INVALID_SCHEMA = "invalid-schema",
804804
NO_API_KEY = "no-api-key",
805+
NO_APP_ID = "no-app-id",
805806
NO_MODEL = "no-model",
806807
NO_PROJECT_ID = "no-project-id",
807808
PARSE_FAILED = "parse-failed",

docs-devsite/vertexai.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,7 @@ export declare const enum VertexAIErrorCode
551551
| INVALID\_CONTENT | <code>&quot;invalid-content&quot;</code> | An error associated with a Content object. |
552552
| INVALID\_SCHEMA | <code>&quot;invalid-schema&quot;</code> | An error due to invalid Schema input. |
553553
| NO\_API\_KEY | <code>&quot;no-api-key&quot;</code> | An error occurred due to a missing Firebase API key. |
554+
| NO\_APP\_ID | <code>&quot;no-app-id&quot;</code> | An error occured due to a missing Firebase app ID. |
554555
| NO\_MODEL | <code>&quot;no-model&quot;</code> | An error occurred due to a model name not being specified during initialization. |
555556
| NO\_PROJECT\_ID | <code>&quot;no-project-id&quot;</code> | An error occurred due to a missing project ID. |
556557
| PARSE\_FAILED | <code>&quot;parse-failed&quot;</code> | An error occurred while parsing. |

packages/vertexai/src/api.test.ts

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ const fakeVertexAI: VertexAI = {
2727
automaticDataCollectionEnabled: true,
2828
options: {
2929
apiKey: 'key',
30-
projectId: 'my-project'
30+
projectId: 'my-project',
31+
appId: 'my-appid'
3132
}
3233
},
3334
location: 'us-central1'
@@ -48,7 +49,7 @@ describe('Top level API', () => {
4849
it('getGenerativeModel throws if no apiKey is provided', () => {
4950
const fakeVertexNoApiKey = {
5051
...fakeVertexAI,
51-
app: { options: { projectId: 'my-project' } }
52+
app: { options: { projectId: 'my-project', appId: 'my-appid' } }
5253
} as VertexAI;
5354
try {
5455
getGenerativeModel(fakeVertexNoApiKey, { model: 'my-model' });
@@ -64,7 +65,7 @@ describe('Top level API', () => {
6465
it('getGenerativeModel throws if no projectId is provided', () => {
6566
const fakeVertexNoProject = {
6667
...fakeVertexAI,
67-
app: { options: { apiKey: 'my-key' } }
68+
app: { options: { apiKey: 'my-key', appId: 'my-appid' } }
6869
} as VertexAI;
6970
try {
7071
getGenerativeModel(fakeVertexNoProject, { model: 'my-model' });
@@ -79,6 +80,22 @@ describe('Top level API', () => {
7980
);
8081
}
8182
});
83+
it('getGenerativeModel throws if no appId is provided', () => {
84+
const fakeVertexNoProject = {
85+
...fakeVertexAI,
86+
app: { options: { apiKey: 'my-key', projectId: 'my-projectid' } }
87+
} as VertexAI;
88+
try {
89+
getGenerativeModel(fakeVertexNoProject, { model: 'my-model' });
90+
} catch (e) {
91+
expect((e as VertexAIError).code).includes(VertexAIErrorCode.NO_APP_ID);
92+
expect((e as VertexAIError).message).equals(
93+
`VertexAI: The "appId" field is empty in the local` +
94+
` Firebase config. Firebase VertexAI requires this field ` +
95+
`to contain a valid app ID. (vertexAI/${VertexAIErrorCode.NO_APP_ID})`
96+
);
97+
}
98+
});
8299
it('getGenerativeModel gets a GenerativeModel', () => {
83100
const genModel = getGenerativeModel(fakeVertexAI, { model: 'my-model' });
84101
expect(genModel).to.be.an.instanceOf(GenerativeModel);
@@ -98,7 +115,7 @@ describe('Top level API', () => {
98115
it('getImagenModel throws if no apiKey is provided', () => {
99116
const fakeVertexNoApiKey = {
100117
...fakeVertexAI,
101-
app: { options: { projectId: 'my-project' } }
118+
app: { options: { projectId: 'my-project', appId: 'my-appid' } }
102119
} as VertexAI;
103120
try {
104121
getImagenModel(fakeVertexNoApiKey, { model: 'my-model' });
@@ -114,7 +131,7 @@ describe('Top level API', () => {
114131
it('getImagenModel throws if no projectId is provided', () => {
115132
const fakeVertexNoProject = {
116133
...fakeVertexAI,
117-
app: { options: { apiKey: 'my-key' } }
134+
app: { options: { apiKey: 'my-key', appId: 'my-appid' } }
118135
} as VertexAI;
119136
try {
120137
getImagenModel(fakeVertexNoProject, { model: 'my-model' });
@@ -129,6 +146,22 @@ describe('Top level API', () => {
129146
);
130147
}
131148
});
149+
it('getImagenModel throws if no appId is provided', () => {
150+
const fakeVertexNoProject = {
151+
...fakeVertexAI,
152+
app: { options: { apiKey: 'my-key', projectId: 'my-project' } }
153+
} as VertexAI;
154+
try {
155+
getImagenModel(fakeVertexNoProject, { model: 'my-model' });
156+
} catch (e) {
157+
expect((e as VertexAIError).code).includes(VertexAIErrorCode.NO_APP_ID);
158+
expect((e as VertexAIError).message).equals(
159+
`VertexAI: The "appId" field is empty in the local` +
160+
` Firebase config. Firebase VertexAI requires this field ` +
161+
`to contain a valid app ID. (vertexAI/${VertexAIErrorCode.NO_APP_ID})`
162+
);
163+
}
164+
});
132165
it('getImagenModel gets an ImagenModel', () => {
133166
const genModel = getImagenModel(fakeVertexAI, { model: 'my-model' });
134167
expect(genModel).to.be.an.instanceOf(ImagenModel);

packages/vertexai/src/methods/chat-session.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ use(chaiAsPromised);
3030
const fakeApiSettings: ApiSettings = {
3131
apiKey: 'key',
3232
project: 'my-project',
33+
appId: 'my-appid',
3334
location: 'us-central1'
3435
};
3536

packages/vertexai/src/methods/count-tokens.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ use(chaiAsPromised);
3232
const fakeApiSettings: ApiSettings = {
3333
apiKey: 'key',
3434
project: 'my-project',
35+
appId: 'my-appid',
3536
location: 'us-central1'
3637
};
3738

packages/vertexai/src/methods/generate-content.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ use(chaiAsPromised);
3737
const fakeApiSettings: ApiSettings = {
3838
apiKey: 'key',
3939
project: 'my-project',
40+
appId: 'my-appid',
4041
location: 'us-central1'
4142
};
4243

packages/vertexai/src/models/generative-model.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ const fakeVertexAI: VertexAI = {
3030
automaticDataCollectionEnabled: true,
3131
options: {
3232
apiKey: 'key',
33-
projectId: 'my-project'
33+
projectId: 'my-project',
34+
appId: 'my-appid'
3435
}
3536
},
3637
location: 'us-central1'

packages/vertexai/src/models/imagen-model.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ const fakeVertexAI: VertexAI = {
3737
automaticDataCollectionEnabled: true,
3838
options: {
3939
apiKey: 'key',
40-
projectId: 'my-project'
40+
projectId: 'my-project',
41+
appId: 'my-appid'
4142
}
4243
},
4344
location: 'us-central1'

packages/vertexai/src/models/vertexai-model.test.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ const fakeVertexAI: VertexAI = {
3838
automaticDataCollectionEnabled: true,
3939
options: {
4040
apiKey: 'key',
41-
projectId: 'my-project'
41+
projectId: 'my-project',
42+
appId: 'my-appid'
4243
}
4344
},
4445
location: 'us-central1'
@@ -100,4 +101,22 @@ describe('VertexAIModel', () => {
100101
);
101102
}
102103
});
104+
it('throws if not passed an app ID', () => {
105+
const fakeVertexAI: VertexAI = {
106+
app: {
107+
name: 'DEFAULT',
108+
automaticDataCollectionEnabled: true,
109+
options: {
110+
apiKey: 'key',
111+
projectId: 'my-project'
112+
}
113+
},
114+
location: 'us-central1'
115+
};
116+
try {
117+
new TestModel(fakeVertexAI, 'my-model');
118+
} catch (e) {
119+
expect((e as VertexAIError).code).to.equal(VertexAIErrorCode.NO_APP_ID);
120+
}
121+
});
103122
});

0 commit comments

Comments
 (0)