Skip to content

Commit 09ba48f

Browse files
authored
[Vertex GA] Rewrite Schema (#8479)
1 parent 097bd67 commit 09ba48f

File tree

10 files changed

+958
-116
lines changed

10 files changed

+958
-116
lines changed

common/api-review/vertexai.api.md

+138-38
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@ import { FirebaseApp } from '@firebase/app';
99
import { FirebaseAuthTokenData } from '@firebase/auth-interop-types';
1010
import { FirebaseError } from '@firebase/util';
1111

12+
// @public
13+
export class ArraySchema extends Schema {
14+
constructor(schemaParams: SchemaParams, items: TypedSchema);
15+
// (undocumented)
16+
items: TypedSchema;
17+
// @internal (undocumented)
18+
toJSON(): SchemaRequest;
19+
}
20+
1221
// @public
1322
export interface BaseParams {
1423
// (undocumented)
@@ -27,6 +36,11 @@ export enum BlockReason {
2736
SAFETY = "SAFETY"
2837
}
2938

39+
// @public
40+
export class BooleanSchema extends Schema {
41+
constructor(schemaParams?: SchemaParams);
42+
}
43+
3044
// @public
3145
export class ChatSession {
3246
// Warning: (ae-forgotten-export) The symbol "ApiSettings" needs to be exported by the entry point index.d.ts
@@ -203,42 +217,7 @@ export interface FunctionCallPart {
203217
export interface FunctionDeclaration {
204218
description?: string;
205219
name: string;
206-
parameters?: FunctionDeclarationSchema;
207-
}
208-
209-
// @public
210-
export interface FunctionDeclarationSchema {
211-
description?: string;
212-
properties: {
213-
[k: string]: FunctionDeclarationSchemaProperty;
214-
};
215-
required?: string[];
216-
type: FunctionDeclarationSchemaType;
217-
}
218-
219-
// @public
220-
export interface FunctionDeclarationSchemaProperty {
221-
description?: string;
222-
enum?: string[];
223-
example?: unknown;
224-
format?: string;
225-
items?: FunctionDeclarationSchema;
226-
nullable?: boolean;
227-
properties?: {
228-
[k: string]: FunctionDeclarationSchema;
229-
};
230-
required?: string[];
231-
type?: FunctionDeclarationSchemaType;
232-
}
233-
234-
// @public
235-
export enum FunctionDeclarationSchemaType {
236-
ARRAY = "ARRAY",
237-
BOOLEAN = "BOOLEAN",
238-
INTEGER = "INTEGER",
239-
NUMBER = "NUMBER",
240-
OBJECT = "OBJECT",
241-
STRING = "STRING"
220+
parameters?: ObjectSchemaInterface;
242221
}
243222

244223
// @public
@@ -331,6 +310,7 @@ export interface GenerationConfig {
331310
// (undocumented)
332311
presencePenalty?: number;
333312
responseMimeType?: string;
313+
responseSchema?: TypedSchema | SchemaRequest;
334314
// (undocumented)
335315
stopSequences?: string[];
336316
// (undocumented)
@@ -478,6 +458,11 @@ export interface InlineDataPart {
478458
videoMetadata?: VideoMetadata;
479459
}
480460

461+
// @public
462+
export class IntegerSchema extends Schema {
463+
constructor(schemaParams?: SchemaParams);
464+
}
465+
481466
// @public
482467
export interface ModelParams extends BaseParams {
483468
// (undocumented)
@@ -490,6 +475,34 @@ export interface ModelParams extends BaseParams {
490475
tools?: Tool[];
491476
}
492477

478+
// @public
479+
export class NumberSchema extends Schema {
480+
constructor(schemaParams?: SchemaParams);
481+
}
482+
483+
// @public
484+
export class ObjectSchema extends Schema {
485+
constructor(schemaParams: SchemaParams, properties: {
486+
[k: string]: TypedSchema;
487+
}, optionalProperties?: string[]);
488+
// (undocumented)
489+
optionalProperties: string[];
490+
// (undocumented)
491+
properties: {
492+
[k: string]: TypedSchema;
493+
};
494+
// @internal (undocumented)
495+
toJSON(): SchemaRequest;
496+
}
497+
498+
// @public
499+
export interface ObjectSchemaInterface extends SchemaInterface {
500+
// (undocumented)
501+
optionalProperties?: string[];
502+
// (undocumented)
503+
type: SchemaType.OBJECT;
504+
}
505+
493506
// @public
494507
export type Part = TextPart | InlineDataPart | FunctionCallPart | FunctionResponsePart | FileDataPart;
495508

@@ -549,6 +562,82 @@ export interface SafetySetting {
549562
threshold: HarmBlockThreshold;
550563
}
551564

565+
// @public
566+
export abstract class Schema implements SchemaInterface {
567+
constructor(schemaParams: SchemaInterface);
568+
[key: string]: unknown;
569+
// (undocumented)
570+
static array(arrayParams: SchemaParams & {
571+
items: Schema;
572+
}): ArraySchema;
573+
// (undocumented)
574+
static boolean(booleanParams?: SchemaParams): BooleanSchema;
575+
description?: string;
576+
// (undocumented)
577+
static enumString(stringParams: SchemaParams & {
578+
enum: string[];
579+
}): StringSchema;
580+
example?: unknown;
581+
format?: string;
582+
// (undocumented)
583+
static integer(integerParams?: SchemaParams): IntegerSchema;
584+
nullable: boolean;
585+
// (undocumented)
586+
static number(numberParams?: SchemaParams): NumberSchema;
587+
// (undocumented)
588+
static object(objectParams: SchemaParams & {
589+
properties: {
590+
[k: string]: Schema;
591+
};
592+
optionalProperties?: string[];
593+
}): ObjectSchema;
594+
// (undocumented)
595+
static string(stringParams?: SchemaParams): StringSchema;
596+
// @internal
597+
toJSON(): SchemaRequest;
598+
type: SchemaType;
599+
}
600+
601+
// @public
602+
export interface SchemaInterface extends SchemaShared<SchemaInterface> {
603+
type: SchemaType;
604+
}
605+
606+
// @public
607+
export interface SchemaParams extends SchemaShared<SchemaInterface> {
608+
}
609+
610+
// @public
611+
export interface SchemaRequest extends SchemaShared<SchemaRequest> {
612+
required?: string[];
613+
type: SchemaType;
614+
}
615+
616+
// @public
617+
export interface SchemaShared<T> {
618+
// (undocumented)
619+
[key: string]: unknown;
620+
description?: string;
621+
enum?: string[];
622+
example?: unknown;
623+
format?: string;
624+
items?: T;
625+
nullable?: boolean;
626+
properties?: {
627+
[k: string]: T;
628+
};
629+
}
630+
631+
// @public
632+
export enum SchemaType {
633+
ARRAY = "array",
634+
BOOLEAN = "boolean",
635+
INTEGER = "integer",
636+
NUMBER = "number",
637+
OBJECT = "object",
638+
STRING = "string"
639+
}
640+
552641
// @public (undocumented)
553642
export interface Segment {
554643
// (undocumented)
@@ -571,6 +660,15 @@ export interface StartChatParams extends BaseParams {
571660
tools?: Tool[];
572661
}
573662

663+
// @public
664+
export class StringSchema extends Schema {
665+
constructor(schemaParams?: SchemaParams, enumValues?: string[]);
666+
// (undocumented)
667+
enum?: string[];
668+
// @internal (undocumented)
669+
toJSON(): SchemaRequest;
670+
}
671+
574672
// @public
575673
export interface TextPart {
576674
// (undocumented)
@@ -592,6 +690,9 @@ export interface ToolConfig {
592690
functionCallingConfig: FunctionCallingConfig;
593691
}
594692

693+
// @public
694+
export type TypedSchema = IntegerSchema | NumberSchema | StringSchema | BooleanSchema | ObjectSchema | ArraySchema;
695+
595696
// @public
596697
export interface UsageMetadata {
597698
// (undocumented)
@@ -616,15 +717,14 @@ export class VertexAIError extends FirebaseError {
616717
readonly code: VertexAIErrorCode;
617718
// (undocumented)
618719
readonly customErrorData?: CustomErrorData | undefined;
619-
// (undocumented)
620-
readonly message: string;
621720
}
622721

623722
// @public
624723
export const enum VertexAIErrorCode {
625724
ERROR = "error",
626725
FETCH_ERROR = "fetch-error",
627726
INVALID_CONTENT = "invalid-content",
727+
INVALID_SCHEMA = "invalid-schema",
628728
NO_API_KEY = "no-api-key",
629729
NO_MODEL = "no-model",
630730
NO_PROJECT_ID = "no-project-id",

packages/vertexai/src/api.test.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,9 @@ describe('Top level API', () => {
3939
getGenerativeModel(fakeVertexAI, {} as ModelParams);
4040
} catch (e) {
4141
expect((e as VertexAIError).code).includes(VertexAIErrorCode.NO_MODEL);
42-
expect((e as VertexAIError).message).equals(
43-
`Must provide a model name. Example: getGenerativeModel({ model: 'my-model-name' })`
42+
expect((e as VertexAIError).message).includes(
43+
`VertexAI: Must provide a model name. Example: ` +
44+
`getGenerativeModel({ model: 'my-model-name' }) (vertexAI/${VertexAIErrorCode.NO_MODEL})`
4445
);
4546
}
4647
});
@@ -54,7 +55,9 @@ describe('Top level API', () => {
5455
} catch (e) {
5556
expect((e as VertexAIError).code).includes(VertexAIErrorCode.NO_API_KEY);
5657
expect((e as VertexAIError).message).equals(
57-
`The "apiKey" field is empty in the local Firebase config. Firebase VertexAI requires this field to contain a valid API key.`
58+
`VertexAI: The "apiKey" field is empty in the local ` +
59+
`Firebase config. Firebase VertexAI requires this field to` +
60+
` contain a valid API key. (vertexAI/${VertexAIErrorCode.NO_API_KEY})`
5861
);
5962
}
6063
});
@@ -70,7 +73,9 @@ describe('Top level API', () => {
7073
VertexAIErrorCode.NO_PROJECT_ID
7174
);
7275
expect((e as VertexAIError).message).equals(
73-
`The "projectId" field is empty in the local Firebase config. Firebase VertexAI requires this field to contain a valid project ID.`
76+
`VertexAI: The "projectId" field is empty in the local` +
77+
` Firebase config. Firebase VertexAI requires this field ` +
78+
`to contain a valid project ID. (vertexAI/${VertexAIErrorCode.NO_PROJECT_ID})`
7479
);
7580
}
7681
});

packages/vertexai/src/api.ts

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import { VertexAIError } from './errors';
2626
import { GenerativeModel } from './models/generative-model';
2727

2828
export { ChatSession } from './methods/chat-session';
29+
export * from './requests/schema-builder';
2930

3031
export { GenerativeModel };
3132

packages/vertexai/src/errors.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@ export class VertexAIError extends FirebaseError {
3434
*/
3535
constructor(
3636
readonly code: VertexAIErrorCode,
37-
readonly message: string,
37+
message: string,
3838
readonly customErrorData?: CustomErrorData
3939
) {
4040
// Match error format used by FirebaseError from ErrorFactory
4141
const service = VERTEX_TYPE;
4242
const serviceName = 'VertexAI';
4343
const fullCode = `${service}/${code}`;
44-
const fullMessage = `${serviceName}: ${message} (${fullCode}).`;
45-
super(fullCode, fullMessage);
44+
const fullMessage = `${serviceName}: ${message} (${fullCode})`;
45+
super(code, fullMessage);
4646

4747
// FirebaseError initializes a stack trace, but it assumes the error is created from the error
4848
// factory. Since we break this assumption, we set the stack trace to be originating from this

0 commit comments

Comments
 (0)