-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
Copy pathapi.ts
153 lines (132 loc) · 4.23 KB
/
api.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import { ShareGPTSubmitBodyInterface } from '@type/api';
import { ConfigInterface, MessageInterface, ModelOptions } from '@type/chat';
import { isAzureEndpoint } from '@utils/api';
export const getChatCompletion = async (
endpoint: string,
messages: MessageInterface[],
config: ConfigInterface,
apiKey?: string,
customHeaders?: Record<string, string>
) => {
const headers: HeadersInit = {
'Content-Type': 'application/json',
...customHeaders,
};
if (apiKey) headers.Authorization = `Bearer ${apiKey}`;
if (isAzureEndpoint(endpoint) && apiKey) {
headers['api-key'] = apiKey;
const modelmapping: Partial<Record<ModelOptions, string>> = {
'gpt-3.5-turbo': 'gpt-35-turbo',
'gpt-3.5-turbo-16k': 'gpt-35-turbo-16k',
'gpt-3.5-turbo-1106': 'gpt-35-turbo-1106',
'gpt-3.5-turbo-0125': 'gpt-35-turbo-0125',
};
const model = modelmapping[config.model] || config.model;
// set api version to 2023-07-01-preview for gpt-4 and gpt-4-32k, otherwise use 2023-03-15-preview
const apiVersion =
model === 'gpt-4' || model === 'gpt-4-32k'
? '2023-07-01-preview'
: '2023-03-15-preview';
const path = `openai/deployments/${model}/chat/completions?api-version=${apiVersion}`;
if (!endpoint.endsWith(path)) {
if (!endpoint.endsWith('/')) {
endpoint += '/';
}
endpoint += path;
}
}
const response = await fetch(endpoint, {
method: 'POST',
headers,
body: JSON.stringify({
messages,
...config,
max_tokens: undefined,
}),
});
if (!response.ok) throw new Error(await response.text());
const data = await response.json();
return data;
};
export const getChatCompletionStream = async (
endpoint: string,
messages: MessageInterface[],
config: ConfigInterface,
apiKey?: string,
customHeaders?: Record<string, string>
) => {
const headers: HeadersInit = {
'Content-Type': 'application/json',
...customHeaders,
};
if (apiKey) headers.Authorization = `Bearer ${apiKey}`;
if (isAzureEndpoint(endpoint) && apiKey) {
headers['api-key'] = apiKey;
const modelmapping: Partial<Record<ModelOptions, string>> = {
'gpt-3.5-turbo': 'gpt-35-turbo',
'gpt-3.5-turbo-16k': 'gpt-35-turbo-16k',
};
const model = modelmapping[config.model] || config.model;
// set api version to 2023-07-01-preview for gpt-4 and gpt-4-32k, otherwise use 2023-03-15-preview
const apiVersion =
model === 'gpt-4' || model === 'gpt-4-32k'
? '2023-07-01-preview'
: '2023-03-15-preview';
const path = `openai/deployments/${model}/chat/completions?api-version=${apiVersion}`;
if (!endpoint.endsWith(path)) {
if (!endpoint.endsWith('/')) {
endpoint += '/';
}
endpoint += path;
}
}
const response = await fetch(endpoint, {
method: 'POST',
headers,
body: JSON.stringify({
messages,
...config,
max_tokens: undefined,
stream: true,
}),
});
if (response.status === 404 || response.status === 405) {
const text = await response.text();
if (text.includes('model_not_found')) {
throw new Error(
text +
'\nMessage from Better ChatGPT:\nPlease ensure that you have access to the GPT-4 API!'
);
} else {
throw new Error(
'Message from Better ChatGPT:\nInvalid API endpoint! We recommend you to check your free API endpoint.'
);
}
}
if (response.status === 429 || !response.ok) {
const text = await response.text();
let error = text;
if (text.includes('insufficient_quota')) {
error +=
'\nMessage from Better ChatGPT:\nWe recommend changing your API endpoint or API key';
} else if (response.status === 429) {
error += '\nRate limited!';
}
throw new Error(error);
}
const stream = response.body;
return stream;
};
export const submitShareGPT = async (body: ShareGPTSubmitBodyInterface) => {
const request = await fetch('https://sharegpt.com/api/conversations', {
body: JSON.stringify(body),
headers: {
'Content-Type': 'application/json',
},
method: 'POST',
});
const response = await request.json();
const { id } = response;
const url = `https://shareg.pt/${id}`;
window.open(url, '_blank');
};