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 GA] Rewrite Schema #8479

Merged
merged 24 commits into from
Sep 24, 2024
Merged
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
176 changes: 138 additions & 38 deletions common/api-review/vertexai.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ import { FirebaseApp } from '@firebase/app';
import { FirebaseAuthTokenData } from '@firebase/auth-interop-types';
import { FirebaseError } from '@firebase/util';

// @public
export class ArraySchema extends Schema {
constructor(schemaParams: SchemaParams, items: TypedSchema);
// (undocumented)
items: TypedSchema;
// @internal (undocumented)
toJSON(): SchemaRequest;
}

// @public
export interface BaseParams {
// (undocumented)
Expand All @@ -27,6 +36,11 @@ export enum BlockReason {
SAFETY = "SAFETY"
}

// @public
export class BooleanSchema extends Schema {
constructor(schemaParams?: SchemaParams);
}

// @public
export class ChatSession {
// Warning: (ae-forgotten-export) The symbol "ApiSettings" needs to be exported by the entry point index.d.ts
Expand Down Expand Up @@ -203,42 +217,7 @@ export interface FunctionCallPart {
export interface FunctionDeclaration {
description?: string;
name: string;
parameters?: FunctionDeclarationSchema;
}

// @public
export interface FunctionDeclarationSchema {
description?: string;
properties: {
[k: string]: FunctionDeclarationSchemaProperty;
};
required?: string[];
type: FunctionDeclarationSchemaType;
}

// @public
export interface FunctionDeclarationSchemaProperty {
description?: string;
enum?: string[];
example?: unknown;
format?: string;
items?: FunctionDeclarationSchema;
nullable?: boolean;
properties?: {
[k: string]: FunctionDeclarationSchema;
};
required?: string[];
type?: FunctionDeclarationSchemaType;
}

// @public
export enum FunctionDeclarationSchemaType {
ARRAY = "ARRAY",
BOOLEAN = "BOOLEAN",
INTEGER = "INTEGER",
NUMBER = "NUMBER",
OBJECT = "OBJECT",
STRING = "STRING"
parameters?: ObjectSchemaInterface;
}

// @public
Expand Down Expand Up @@ -331,6 +310,7 @@ export interface GenerationConfig {
// (undocumented)
presencePenalty?: number;
responseMimeType?: string;
responseSchema?: TypedSchema | SchemaRequest;
// (undocumented)
stopSequences?: string[];
// (undocumented)
Expand Down Expand Up @@ -478,6 +458,11 @@ export interface InlineDataPart {
videoMetadata?: VideoMetadata;
}

// @public
export class IntegerSchema extends Schema {
constructor(schemaParams?: SchemaParams);
}

// @public
export interface ModelParams extends BaseParams {
// (undocumented)
Expand All @@ -490,6 +475,34 @@ export interface ModelParams extends BaseParams {
tools?: Tool[];
}

// @public
export class NumberSchema extends Schema {
constructor(schemaParams?: SchemaParams);
}

// @public
export class ObjectSchema extends Schema {
constructor(schemaParams: SchemaParams, properties: {
[k: string]: TypedSchema;
}, optionalProperties?: string[]);
// (undocumented)
optionalProperties: string[];
// (undocumented)
properties: {
[k: string]: TypedSchema;
};
// @internal (undocumented)
toJSON(): SchemaRequest;
}

// @public
export interface ObjectSchemaInterface extends SchemaInterface {
// (undocumented)
optionalProperties?: string[];
// (undocumented)
type: SchemaType.OBJECT;
}

// @public
export type Part = TextPart | InlineDataPart | FunctionCallPart | FunctionResponsePart | FileDataPart;

Expand Down Expand Up @@ -549,6 +562,82 @@ export interface SafetySetting {
threshold: HarmBlockThreshold;
}

// @public
export abstract class Schema implements SchemaInterface {
constructor(schemaParams: SchemaInterface);
[key: string]: unknown;
// (undocumented)
static array(arrayParams: SchemaParams & {
items: Schema;
}): ArraySchema;
// (undocumented)
static boolean(booleanParams?: SchemaParams): BooleanSchema;
description?: string;
// (undocumented)
static enumString(stringParams: SchemaParams & {
enum: string[];
}): StringSchema;
example?: unknown;
format?: string;
// (undocumented)
static integer(integerParams?: SchemaParams): IntegerSchema;
nullable: boolean;
// (undocumented)
static number(numberParams?: SchemaParams): NumberSchema;
// (undocumented)
static object(objectParams: SchemaParams & {
properties: {
[k: string]: Schema;
};
optionalProperties?: string[];
}): ObjectSchema;
// (undocumented)
static string(stringParams?: SchemaParams): StringSchema;
// @internal
toJSON(): SchemaRequest;
type: SchemaType;
}

// @public
export interface SchemaInterface extends SchemaShared<SchemaInterface> {
type: SchemaType;
}

// @public
export interface SchemaParams extends SchemaShared<SchemaInterface> {
}

// @public
export interface SchemaRequest extends SchemaShared<SchemaRequest> {
required?: string[];
type: SchemaType;
}

// @public
export interface SchemaShared<T> {
// (undocumented)
[key: string]: unknown;
description?: string;
enum?: string[];
example?: unknown;
format?: string;
items?: T;
nullable?: boolean;
properties?: {
[k: string]: T;
};
}

// @public
export enum SchemaType {
ARRAY = "array",
BOOLEAN = "boolean",
INTEGER = "integer",
NUMBER = "number",
OBJECT = "object",
STRING = "string"
}

// @public (undocumented)
export interface Segment {
// (undocumented)
Expand All @@ -571,6 +660,15 @@ export interface StartChatParams extends BaseParams {
tools?: Tool[];
}

// @public
export class StringSchema extends Schema {
constructor(schemaParams?: SchemaParams, enumValues?: string[]);
// (undocumented)
enum?: string[];
// @internal (undocumented)
toJSON(): SchemaRequest;
}

// @public
export interface TextPart {
// (undocumented)
Expand All @@ -592,6 +690,9 @@ export interface ToolConfig {
functionCallingConfig: FunctionCallingConfig;
}

// @public
export type TypedSchema = IntegerSchema | NumberSchema | StringSchema | BooleanSchema | ObjectSchema | ArraySchema;

// @public
export interface UsageMetadata {
// (undocumented)
Expand All @@ -616,15 +717,14 @@ export class VertexAIError extends FirebaseError {
readonly code: VertexAIErrorCode;
// (undocumented)
readonly customErrorData?: CustomErrorData | undefined;
// (undocumented)
readonly message: string;
}

// @public
export const enum VertexAIErrorCode {
ERROR = "error",
FETCH_ERROR = "fetch-error",
INVALID_CONTENT = "invalid-content",
INVALID_SCHEMA = "invalid-schema",
NO_API_KEY = "no-api-key",
NO_MODEL = "no-model",
NO_PROJECT_ID = "no-project-id",
Expand Down
13 changes: 9 additions & 4 deletions packages/vertexai/src/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ describe('Top level API', () => {
getGenerativeModel(fakeVertexAI, {} as ModelParams);
} catch (e) {
expect((e as VertexAIError).code).includes(VertexAIErrorCode.NO_MODEL);
expect((e as VertexAIError).message).equals(
`Must provide a model name. Example: getGenerativeModel({ model: 'my-model-name' })`
expect((e as VertexAIError).message).includes(
`VertexAI: Must provide a model name. Example: ` +
`getGenerativeModel({ model: 'my-model-name' }) (vertexAI/${VertexAIErrorCode.NO_MODEL})`
);
}
});
Expand All @@ -54,7 +55,9 @@ describe('Top level API', () => {
} catch (e) {
expect((e as VertexAIError).code).includes(VertexAIErrorCode.NO_API_KEY);
expect((e as VertexAIError).message).equals(
`The "apiKey" field is empty in the local Firebase config. Firebase VertexAI requires this field to contain a valid API key.`
`VertexAI: The "apiKey" field is empty in the local ` +
`Firebase config. Firebase VertexAI requires this field to` +
` contain a valid API key. (vertexAI/${VertexAIErrorCode.NO_API_KEY})`
);
}
});
Expand All @@ -70,7 +73,9 @@ describe('Top level API', () => {
VertexAIErrorCode.NO_PROJECT_ID
);
expect((e as VertexAIError).message).equals(
`The "projectId" field is empty in the local Firebase config. Firebase VertexAI requires this field to contain a valid project ID.`
`VertexAI: The "projectId" field is empty in the local` +
` Firebase config. Firebase VertexAI requires this field ` +
`to contain a valid project ID. (vertexAI/${VertexAIErrorCode.NO_PROJECT_ID})`
);
}
});
Expand Down
1 change: 1 addition & 0 deletions packages/vertexai/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { VertexAIError } from './errors';
import { GenerativeModel } from './models/generative-model';

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

export { GenerativeModel };

Expand Down
6 changes: 3 additions & 3 deletions packages/vertexai/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ export class VertexAIError extends FirebaseError {
*/
constructor(
readonly code: VertexAIErrorCode,
readonly message: string,
message: string,
Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry, I know I reviewed this already, but is there a reason that this isn't readonly anymore?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a good question, I was going to mention it in the description. If it's readonly it can't be overwritten by super() which was causing some unexpected behavior. super() at the bottom of the constructor sets message to fullMessage (adds the service name and code) but it simply won't do anything if it's readonly. The next question would be, what about code - it's ok for code to not be overwritten with fullCode because adding the prefix just makes it harder to compare (e.g. error.code === VertexAIErrorCode.SOME_CODE). The formatted fullCode. only needs to go into the message.

readonly customErrorData?: CustomErrorData
) {
// Match error format used by FirebaseError from ErrorFactory
const service = VERTEX_TYPE;
const serviceName = 'VertexAI';
const fullCode = `${service}/${code}`;
const fullMessage = `${serviceName}: ${message} (${fullCode}).`;
super(fullCode, fullMessage);
const fullMessage = `${serviceName}: ${message} (${fullCode})`;
super(code, fullMessage);

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