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