Skip to content

Add support for token-based usage metrics. #8757

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

Merged
merged 22 commits into from
Feb 24, 2025
Merged
Show file tree
Hide file tree
Changes from 5 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
30 changes: 30 additions & 0 deletions common/api-review/vertexai.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ export interface CountTokensRequest {

// @public
export interface CountTokensResponse {
// (undocumented)
promptTokensDetails?: ModalityTokenCount[];
totalBillableCharacters?: number;
totalTokens: number;
}
Expand Down Expand Up @@ -447,6 +449,30 @@ export class IntegerSchema extends Schema {
constructor(schemaParams?: SchemaParams);
}

// @public
export enum Modality {
// (undocumented)
AUDIO = "AUDIO",
// (undocumented)
DOCUMENT = "DOCUMENT",
// (undocumented)
IMAGE = "IMAGE",
// (undocumented)
MODALITY_UNSPECIFIED = "MODALITY_UNSPECIFIED",
// (undocumented)
TEXT = "TEXT",
// (undocumented)
VIDEO = "VIDEO"
}

// @public
export interface ModalityTokenCount {
// (undocumented)
modality: Modality;
// (undocumented)
tokenCount: number;
}

// @public
export interface ModelParams extends BaseParams {
// (undocumented)
Expand Down Expand Up @@ -682,8 +708,12 @@ export interface UsageMetadata {
// (undocumented)
candidatesTokenCount: number;
// (undocumented)
candidatesTokensDetails?: ModalityTokenCount[];
// (undocumented)
promptTokenCount: number;
// (undocumented)
promptTokensDetails?: ModalityTokenCount[];
// (undocumented)
totalTokenCount: number;
}

Expand Down
25 changes: 25 additions & 0 deletions packages/vertexai/src/methods/count-tokens.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,31 @@ describe('countTokens()', () => {
undefined
);
});
it('total tokens with modality details', async () => {
const mockResponse = getMockResponse('unary-success-detailed-token-response.json');
const makeRequestStub = stub(request, 'makeRequest').resolves(
mockResponse as Response
);
const result = await countTokens(
fakeApiSettings,
'model',
fakeRequestParams
);
expect(result.totalTokens).to.equal(1837);
expect(result.totalBillableCharacters).to.equal(117);
expect(result.promptTokensDetails?.[0].modality).to.equal('IMAGE');
expect(result.promptTokensDetails?.[0].tokenCount).to.equal(1806);
expect(makeRequestStub).to.be.calledWith(
'model',
Task.COUNT_TOKENS,
fakeApiSettings,
false,
match((value: string) => {
return value.includes('contents');
}),
undefined
);
});
it('total tokens no billable characters', async () => {
const mockResponse = getMockResponse(
'unary-success-no-billable-characters.json'
Expand Down
24 changes: 24 additions & 0 deletions packages/vertexai/src/methods/generate-content.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,30 @@ describe('generateContent()', () => {
match.any
);
});
it('long response with token details', async () => {
const mockResponse = getMockResponse('unary-success-basic-response-long-usage-metadata.json');
const makeRequestStub = stub(request, 'makeRequest').resolves(
mockResponse as Response
);
const result = await generateContent(
fakeApiSettings,
'model',
fakeRequestParams
);
expect(result.response.usageMetadata?.totalTokenCount).to.equal(1913);
expect(result.response.usageMetadata?.candidatesTokenCount).to.equal(76);
expect(result.response.usageMetadata?.promptTokensDetails?.[0].modality).to.equal('IMAGE');
expect(result.response.usageMetadata?.promptTokensDetails?.[0].tokenCount).to.equal(1806);
expect(result.response.usageMetadata?.candidatesTokensDetails?.[0].modality).to.equal('TEXT');
expect(result.response.usageMetadata?.candidatesTokensDetails?.[0].tokenCount).to.equal(76);
expect(makeRequestStub).to.be.calledWith(
'model',
Task.GENERATE_CONTENT,
fakeApiSettings,
false,
match.any
);
});
it('citations', async () => {
const mockResponse = getMockResponse('unary-success-citations.json');
const makeRequestStub = stub(request, 'makeRequest').resolves(
Expand Down
13 changes: 13 additions & 0 deletions packages/vertexai/src/types/enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,16 @@ export enum FunctionCallingMode {
// not passing any function declarations.
NONE = 'NONE'
}

/**
* Type of content.
* @public
*/
export enum Modality {
MODALITY_UNSPECIFIED = 'MODALITY_UNSPECIFIED',
TEXT = 'TEXT',
IMAGE = 'IMAGE',
VIDEO = 'VIDEO',
AUDIO = 'AUDIO',
DOCUMENT = 'DOCUMENT'
}
16 changes: 15 additions & 1 deletion packages/vertexai/src/types/responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ import {
FinishReason,
HarmCategory,
HarmProbability,
HarmSeverity
HarmSeverity,
Modality
} from './enums';

/**
Expand Down Expand Up @@ -83,6 +84,17 @@ export interface UsageMetadata {
promptTokenCount: number;
candidatesTokenCount: number;
totalTokenCount: number;
promptTokensDetails?: ModalityTokenCount[];
candidatesTokensDetails?: ModalityTokenCount[];
}

/**
* The number of tokens used by a given content type.
* @public
*/
export interface ModalityTokenCount {
modality: Modality;
tokenCount: number;
}

/**
Expand Down Expand Up @@ -213,4 +225,6 @@ export interface CountTokensResponse {
* from the request.
*/
totalBillableCharacters?: number;

promptTokensDetails?: ModalityTokenCount[];
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we want docs on this property? If so, add it in the same comment format as the two other properties above.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added, thanks!

BTW, why are enums shown as (undocumented) in the generated .md file?

}
2 changes: 1 addition & 1 deletion scripts/update_vertexai_responses.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# This script replaces mock response files for Vertex AI unit tests with a fresh
# clone of the shared repository of Vertex AI test data.

RESPONSES_VERSION='v5.*' # The major version of mock responses to use
RESPONSES_VERSION='v6.*' # The major version of mock responses to use
REPO_NAME="vertexai-sdk-test-data"
REPO_LINK="https://github.com/FirebaseExtended/$REPO_NAME.git"

Expand Down