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