-
Notifications
You must be signed in to change notification settings - Fork 873
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
VertexAI: add test cases for countTokens()
- Loading branch information
Showing
5 changed files
with
133 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import { expect, use } from 'chai'; | ||
import { match, restore, stub } from 'sinon'; | ||
import sinonChai from 'sinon-chai'; | ||
import chaiAsPromised from 'chai-as-promised'; | ||
import { getMockResponse } from '../../test-utils/mock-response'; | ||
import * as request from '../requests/request'; | ||
import { countTokens } from './count-tokens'; | ||
import { CountTokensRequest } from '../types'; | ||
import { ApiSettings } from '../types/internal'; | ||
import { Task } from '../requests/request'; | ||
|
||
use(sinonChai); | ||
use(chaiAsPromised); | ||
|
||
const fakeApiSettings: ApiSettings = { | ||
apiKey: 'key', | ||
project: 'my-project', | ||
location: 'us-central1' | ||
}; | ||
|
||
const fakeRequestParams: CountTokensRequest = { | ||
contents: [{ parts: [{ text: 'hello' }], role: 'user' }] | ||
}; | ||
|
||
describe('countTokens()', () => { | ||
afterEach(() => { | ||
restore(); | ||
}); | ||
it('total tokens', async () => { | ||
const mockResponse = getMockResponse( | ||
'count-tokens-success-total-tokens.json' | ||
); | ||
const makeRequestStub = stub(request, 'makeRequest').resolves( | ||
mockResponse as Response | ||
); | ||
const result = await countTokens( | ||
fakeApiSettings, | ||
'model', | ||
fakeRequestParams | ||
); | ||
expect(result.totalTokens).to.equal(6); | ||
expect(result.totalBillableCharacters).to.equal(16); | ||
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( | ||
'count-tokens-success-no-billable-characters.json' | ||
); | ||
const makeRequestStub = stub(request, 'makeRequest').resolves( | ||
mockResponse as Response | ||
); | ||
const result = await countTokens( | ||
fakeApiSettings, | ||
'model', | ||
fakeRequestParams | ||
); | ||
expect(result.totalTokens).to.equal(258); | ||
expect(result).to.not.have.property('totalBillableCharacters'); | ||
expect(makeRequestStub).to.be.calledWith( | ||
'model', | ||
Task.COUNT_TOKENS, | ||
fakeApiSettings, | ||
false, | ||
match((value: string) => { | ||
return value.includes('contents'); | ||
}), | ||
undefined | ||
); | ||
}); | ||
it('model not found', async () => { | ||
const mockResponse = getMockResponse( | ||
'count-tokens-failure-model-not-found.json' | ||
); | ||
const mockFetch = stub(globalThis, 'fetch').resolves({ | ||
ok: false, | ||
status: 404, | ||
json: mockResponse.json | ||
} as Response); | ||
await expect( | ||
countTokens(fakeApiSettings, 'model', fakeRequestParams) | ||
).to.be.rejectedWith(/404.*not found/); | ||
expect(mockFetch).to.be.called; | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
packages/vertexai/test-utils/mock-responses/count-tokens-failure-model-not-found.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"error": { | ||
"code": 404, | ||
"message": "models/test-model-name is not found for API version v1beta, or is not supported for countTokens. Call ListModels to see the list of available models and their supported methods.", | ||
"status": "NOT_FOUND", | ||
"details": [ | ||
{ | ||
"@type": "type.googleapis.com/google.rpc.DebugInfo", | ||
"detail": "[ORIGINAL ERROR] generic::not_found: models/test-model-name is not found for API version v1beta, or is not supported for countTokens. Call ListModels to see the list of available models and their supported methods. [google.rpc.error_details_ext] { message: \"models/test-model-name is not found for API version v1beta, or is not supported for countTokens. Call ListModels to see the list of available models and their supported methods.\" }" | ||
} | ||
] | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
packages/vertexai/test-utils/mock-responses/count-tokens-success-no-billable-characters.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"totalTokens": 258 | ||
} |
4 changes: 4 additions & 0 deletions
4
packages/vertexai/test-utils/mock-responses/count-tokens-success-total-tokens.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"totalTokens": 6, | ||
"totalBillableCharacters": 16 | ||
} |