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 11 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
6 changes: 6 additions & 0 deletions .changeset/chilled-tips-judge.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@firebase/vertexai': minor
'firebase': minor
---

Added support for modality-based token count.
28 changes: 28 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,28 @@ 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 {
modality: Modality;
tokenCount: number;
}

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

Expand Down
27 changes: 27 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,33 @@ 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
34 changes: 34 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,40 @@ 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
19 changes: 19 additions & 0 deletions packages/vertexai/src/types/enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,22 @@ export enum FunctionCallingMode {
// not passing any function declarations.
NONE = 'NONE'
}

/**
* Content part modality.
* @public
*/
export enum Modality {
// Unspecified modality.
MODALITY_UNSPECIFIED = 'MODALITY_UNSPECIFIED',
// Plain text.
TEXT = 'TEXT',
// Image.
IMAGE = 'IMAGE',
// Video.
VIDEO = 'VIDEO',
// Audio.
AUDIO = 'AUDIO',
// Document, e.g. PDF.
DOCUMENT = 'DOCUMENT'
}
19 changes: 18 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,20 @@ export interface UsageMetadata {
promptTokenCount: number;
candidatesTokenCount: number;
totalTokenCount: number;
promptTokensDetails?: ModalityTokenCount[];
candidatesTokensDetails?: ModalityTokenCount[];
}

/**
* Represents token counting info for a single modality.
*
* @public
*/
export interface ModalityTokenCount {
/** The modality associated with this token count. */
modality: Modality;
/** The number of tokens counted. */
tokenCount: number;
}

/**
Expand Down Expand Up @@ -213,4 +228,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
Loading