Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/platform/nesFetch/common/completionsFetchService.ts
13401 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 { Result } from '../../../util/common/result';
7
import { createServiceIdentifier } from '../../../util/common/services';
8
import { CancellationToken } from '../../../util/vs/base/common/cancellation';
9
import { IHeaders } from '../../networking/common/fetcherService';
10
import { ResponseStream } from './responseStream';
11
12
export namespace Completions {
13
interface BaseCompletionsParams {
14
prompt: string;
15
stop?: string[];
16
top_p?: number;
17
best_of?: number;
18
max_tokens?: number;
19
temperature?: number;
20
presence_penalty?: number;
21
frequency_penalty?: number;
22
// required to access certain experimental models
23
model?: string;
24
logprobs?: number;
25
n?: number;
26
stream: true;
27
}
28
29
interface CodexV2Params {
30
suffix?: string;
31
extra?: { [key: string]: any };
32
code_annotations?: boolean;
33
}
34
35
export interface ModelParams extends BaseCompletionsParams, CodexV2Params { }
36
37
export class RequestCancelled {
38
readonly kind = 'cancelled' as const;
39
}
40
export class UnsuccessfulResponse {
41
readonly kind = 'not-200-status' as const;
42
constructor(
43
public readonly status: number,
44
public readonly statusText: string,
45
public readonly headers: IHeaders,
46
public readonly text: () => Promise<string>
47
) { }
48
}
49
export class Unexpected {
50
readonly kind = 'unexpected' as const;
51
constructor(
52
public readonly error: Error
53
) { }
54
}
55
export type CompletionsFetchFailure =
56
| Completions.RequestCancelled
57
| Completions.UnsuccessfulResponse
58
| Completions.Unexpected;
59
60
export namespace Internal {
61
export type FetchOptions = {
62
requestId: string;
63
headers: { [name: string]: string };
64
body: string;
65
};
66
}
67
}
68
69
export type CompletionsFetchErrorType = 'stop_content_filter' | 'stop_length' | 'unknown';
70
71
export class CompletionsFetchError extends Error {
72
constructor(
73
readonly type: CompletionsFetchErrorType,
74
readonly requestId: string,
75
message: string
76
) {
77
super(message);
78
}
79
}
80
81
export const ICompletionsFetchService = createServiceIdentifier<ICompletionsFetchService>('ICompletionsFetchService');
82
83
/**
84
* OpenAI has completions and _chat_ completions endpoints. This's (non-chat) completions endpoint fetcher.
85
*/
86
export interface ICompletionsFetchService {
87
readonly _serviceBrand: undefined;
88
89
fetch(
90
url: string,
91
secretKey: string,
92
params: Completions.ModelParams,
93
requestId: string,
94
ct: CancellationToken,
95
headerOverrides?: Record<string, string>
96
): Promise<Result<ResponseStream, Completions.CompletionsFetchFailure>>;
97
98
disconnectAll(): Promise<unknown>;
99
}
100
101