Path: blob/main/extensions/copilot/src/platform/endpoint/test/node/azureEndpoint.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 { TokenizerType } from '../../../../util/common/tokenizer';6import { IInstantiationService } from '../../../../util/vs/platform/instantiation/common/instantiation';7import { IAuthenticationService } from '../../../authentication/common/authentication';8import { IChatMLFetcher } from '../../../chat/common/chatMLFetcher';9import { CHAT_MODEL, IConfigurationService } from '../../../configuration/common/configurationService';10import { ILogService } from '../../../log/common/logService';11import { IFetcherService } from '../../../networking/common/fetcherService';12import { IChatEndpoint, IEndpointBody } from '../../../networking/common/networking';13import { RawMessageConversionCallback } from '../../../networking/common/openai';14import { IChatWebSocketManager } from '../../../networking/node/chatWebSocketManager';15import { IExperimentationService } from '../../../telemetry/common/nullExperimentationService';16import { ITelemetryService } from '../../../telemetry/common/telemetry';17import { ITokenizerProvider } from '../../../tokenizer/node/tokenizer';18import { ICAPIClientService } from '../../common/capiClient';19import { IDomainService } from '../../common/domainService';20import { IChatModelInformation } from '../../common/endpointProvider';21import { ChatEndpoint } from '../../node/chatEndpoint';2223export class AzureTestEndpoint extends ChatEndpoint {24private readonly isThinkingModel: boolean;25constructor(26private readonly _azureModel: string,27@IDomainService domainService: IDomainService,28@ICAPIClientService capiClient: ICAPIClientService,29@IFetcherService fetcherService: IFetcherService,30@ITelemetryService telemetryService: ITelemetryService,31@IAuthenticationService authService: IAuthenticationService,32@IChatMLFetcher chatMLFetcher: IChatMLFetcher,33@ITokenizerProvider tokenizerProvider: ITokenizerProvider,34@IInstantiationService private instantiationService: IInstantiationService,35@IConfigurationService configurationService: IConfigurationService,36@IExperimentationService experimentationService: IExperimentationService,37@IChatWebSocketManager chatWebSocketService: IChatWebSocketManager,38@ILogService logService: ILogService39) {40const modelInfo: IChatModelInformation = {41id: _azureModel,42vendor: 'Microsoft Azure',43name: 'Azure Test',44version: '1.0',45model_picker_enabled: false,46is_chat_default: false,47is_chat_fallback: false,48capabilities: {49type: 'chat',50family: 'azure',51tokenizer: TokenizerType.O200K,52supports: { streaming: true, tool_calls: true, vision: false, prediction: false },53limits: {54max_prompt_tokens: 200000,55max_output_tokens: 56000,56},57}58};59super(60modelInfo,61domainService,62chatMLFetcher,63tokenizerProvider,64instantiationService,65configurationService,66experimentationService,67chatWebSocketService,68logService69);70this.isThinkingModel = false; // Set to true if testing a thinking model71}7273override get urlOrRequestMetadata(): string {74switch (this._azureModel) {75case CHAT_MODEL.EXPERIMENTAL:76// Set model params and thinking in constructor77return '<replace with your experimental endpoint URL>';78default:79throw new Error(`Unknown azure model passed ${this._azureModel} passed to test endpoint`);80}81}8283private getSecretKey(): string {84let secretKey: string | undefined = '';85switch (this._azureModel) {86case CHAT_MODEL.EXPERIMENTAL:87secretKey = process.env.EXPERIMENTAL_TOKEN;88break;89default:90throw new Error(`Unknown azure model passed ${this._azureModel} passed to test endpoint`);91}92if (!secretKey) {93throw new Error(`No secret key found for model ${this._azureModel}`);94}95return secretKey;96}9798private getAuthHeader(): string {99return 'Bearer ' + this.getSecretKey();100}101102public override getExtraHeaders(): Record<string, string> {103return {104'Authorization': this.getAuthHeader(),105'ocp-apim-subscription-key': this.getSecretKey(),106'api-key': this.getSecretKey(),107'x-policy-id': 'nil'108};109}110111override interceptBody(body: IEndpointBody | undefined): void {112super.interceptBody(body);113if (body) {114delete body.snippy;115delete body.intent;116117if (body && this.isThinkingModel) {118delete body.temperature;119body['max_completion_tokens'] = body.max_tokens;120delete body.max_tokens;121}122}123}124125override cloneWithTokenOverride(modelMaxPromptTokens: number): IChatEndpoint {126return this.instantiationService.createInstance(AzureTestEndpoint, this._azureModel);127}128129protected override getCompletionsCallback(): RawMessageConversionCallback | undefined {130return (out, data) => {131if (data && data.id) {132out.cot_id = data.id;133out.cot_summary = Array.isArray(data.text) ? data.text.join('') : data.text;134}135};136}137}138139140