Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/platform/openai/node/test/chatTokens.spec.ts
13405 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { Raw } from '@vscode/prompt-tsx';
7
import assert from 'assert';
8
import { suite, test } from 'vitest';
9
import { createPlatformServices } from '../../../../platform/test/node/services';
10
import { TokenizerType } from '../../../../util/common/tokenizer';
11
import { toTextParts } from '../../../chat/common/globalStringUtils';
12
import { NullTelemetryService } from '../../../telemetry/common/nullTelemetryService';
13
import { ITokenizerProvider, TokenizerProvider } from '../../../tokenizer/node/tokenizer';
14
15
suite('Chat tokens', function () {
16
test('counts tokens of messages', async function () {
17
const messages: Raw.ChatMessage[] = [
18
{
19
role: Raw.ChatRole.System,
20
content: toTextParts(
21
'You are a helpful, pattern-following assistant that translates corporate jargon into plain English.'),
22
},
23
{ role: Raw.ChatRole.System, name: 'example_user', content: toTextParts('New synergies will help drive top-line growth.') },
24
{
25
role: Raw.ChatRole.System,
26
name: 'example_assistant',
27
content: toTextParts('Things working well together will increase revenue.'),
28
},
29
{
30
role: Raw.ChatRole.System,
31
name: 'example_user',
32
content: toTextParts(
33
`Let's circle back when we have more bandwidth to touch base on opportunities for increased leverage.`),
34
},
35
{
36
role: Raw.ChatRole.System,
37
name: 'example_assistant',
38
content: toTextParts(`Let's talk later when we're less busy about how to do better.`),
39
},
40
{
41
role: Raw.ChatRole.User,
42
content: toTextParts(`This late pivot means we don't have time to boil the ocean for the client deliverable.`),
43
},
44
];
45
46
const testingServiceCollection = createPlatformServices();
47
testingServiceCollection.define(ITokenizerProvider, new TokenizerProvider(false, new NullTelemetryService()));
48
const accessor = testingServiceCollection.createTestingAccessor();
49
const tokens = await accessor.get(ITokenizerProvider).acquireTokenizer({ tokenizer: TokenizerType.CL100K }).countMessagesTokens(messages);
50
51
assert.deepStrictEqual(tokens, 129);
52
});
53
});
54
55