返回提交历史
Added
g4f/Provider/Vercel.py
+369
-0
XFEstudio/gpt4free
~
af9fc199
代码差异
1 个文件
+369
-0
@@ -0,0 +1,369 @@
1
from __future__ import annotations
2
3
import json, base64, requests, execjs, random, uuid
4
5
from ..typing import Any, TypedDict, CreateResult
6
from .base_provider import BaseProvider
7
from abc import abstractmethod
8
9
10
class Vercel(BaseProvider):
11
url = 'https://sdk.vercel.ai'
12
working = True
13
supports_gpt_35_turbo = True
14
supports_stream = True
15
16
@staticmethod
17
@abstractmethod
18
def create_completion(
19
model: str,
20
messages: list[dict[str, str]],
21
stream: bool, **kwargs ) -> CreateResult:
22
23
headers = {
24
'authority' : 'sdk.vercel.ai',
25
'accept' : '*/*',
26
'accept-language' : 'en,fr-FR;q=0.9,fr;q=0.8,es-ES;q=0.7,es;q=0.6,en-US;q=0.5,am;q=0.4,de;q=0.3',
27
'cache-control' : 'no-cache',
28
'content-type' : 'application/json',
29
'custom-encoding' : AntiBotToken(),
30
'origin' : 'https://sdk.vercel.ai',
31
'pragma' : 'no-cache',
32
'referer' : 'https://sdk.vercel.ai/',
33
'sec-ch-ua' : '"Google Chrome";v="117", "Not;A=Brand";v="8", "Chromium";v="117"',
34
'sec-ch-ua-mobile' : '?0',
35
'sec-ch-ua-platform': '"macOS"',
36
'sec-fetch-dest' : 'empty',
37
'sec-fetch-mode' : 'cors',
38
'sec-fetch-site' : 'same-origin',
39
'user-agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.%s.%s Safari/537.36' % (
40
random.randint(99, 999),
41
random.randint(99, 999)
42
)
43
}
44
45
json_data = {
46
'model' : model_info[model]['id'],
47
'messages' : messages,
48
'playgroundId': str(uuid.uuid4()),
49
'chatIndex' : 0} | model_info[model]['default_params']
50
51
52
server_error = True
53
while server_error:
54
response = requests.post('https://sdk.vercel.ai/api/generate',
55
headers=headers, json=json_data, stream=True)
56
57
for token in response.iter_content(chunk_size=2046):
58
if token != b'Internal Server Error':
59
server_error = False
60
yield (token.decode())
61
62
def AntiBotToken() -> str:
63
headers = {
64
'authority' : 'sdk.vercel.ai',
65
'accept' : '*/*',
66
'accept-language' : 'en,fr-FR;q=0.9,fr;q=0.8,es-ES;q=0.7,es;q=0.6,en-US;q=0.5,am;q=0.4,de;q=0.3',
67
'cache-control' : 'no-cache',
68
'pragma' : 'no-cache',
69
'referer' : 'https://sdk.vercel.ai/',
70
'sec-ch-ua' : '"Google Chrome";v="117", "Not;A=Brand";v="8", "Chromium";v="117"',
71
'sec-ch-ua-mobile' : '?0',
72
'sec-ch-ua-platform': '"macOS"',
73
'sec-fetch-dest' : 'empty',
74
'sec-fetch-mode' : 'cors',
75
'sec-fetch-site' : 'same-origin',
76
'user-agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.%s.%s Safari/537.36' % (
77
random.randint(99, 999),
78
random.randint(99, 999)
79
)
80
}
81
82
response = requests.get('https://sdk.vercel.ai/openai.jpeg',
83
headers=headers).text
84
85
raw_data = json.loads(base64.b64decode(response,
86
validate=True))
87
88
js_script = '''const globalThis={marker:"mark"};String.prototype.fontcolor=function(){return `<font>${this}</font>`};
89
return (%s)(%s)''' % (raw_data['c'], raw_data['a'])
90
91
raw_token = json.dumps({'r': execjs.compile(js_script).call(''), 't': raw_data['t']},
92
separators = (",", ":"))
93
94
return base64.b64encode(raw_token.encode('utf-16le')).decode()
95
96
class ModelInfo(TypedDict):
97
id: str
98
default_params: dict[str, Any]
99
100
model_info: dict[str, ModelInfo] = {
101
'claude-instant-v1': {
102
'id': 'anthropic:claude-instant-v1',
103
'default_params': {
104
'temperature': 1,
105
'maximumLength': 1024,
106
'topP': 1,
107
'topK': 1,
108
'presencePenalty': 1,
109
'frequencyPenalty': 1,
110
'stopSequences': ['\n\nHuman:'],
111
},
112
},
113
'claude-v1': {
114
'id': 'anthropic:claude-v1',
115
'default_params': {
116
'temperature': 1,
117
'maximumLength': 1024,
118
'topP': 1,
119
'topK': 1,
120
'presencePenalty': 1,
121
'frequencyPenalty': 1,
122
'stopSequences': ['\n\nHuman:'],
123
},
124
},
125
'claude-v2': {
126
'id': 'anthropic:claude-v2',
127
'default_params': {
128
'temperature': 1,
129
'maximumLength': 1024,
130
'topP': 1,
131
'topK': 1,
132
'presencePenalty': 1,
133
'frequencyPenalty': 1,
134
'stopSequences': ['\n\nHuman:'],
135
},
136
},
137
'a16z-infra/llama7b-v2-chat': {
138
'id': 'replicate:a16z-infra/llama7b-v2-chat',
139
'default_params': {
140
'temperature': 0.75,
141
'maximumLength': 3000,
142
'topP': 1,
143
'repetitionPenalty': 1,
144
},
145
},
146
'a16z-infra/llama13b-v2-chat': {
147
'id': 'replicate:a16z-infra/llama13b-v2-chat',
148
'default_params': {
149
'temperature': 0.75,
150
'maximumLength': 3000,
151
'topP': 1,
152
'repetitionPenalty': 1,
153
},
154
},
155
'replicate/llama-2-70b-chat': {
156
'id': 'replicate:replicate/llama-2-70b-chat',
157
'default_params': {
158
'temperature': 0.75,
159
'maximumLength': 3000,
160
'topP': 1,
161
'repetitionPenalty': 1,
162
},
163
},
164
'bigscience/bloom': {
165
'id': 'huggingface:bigscience/bloom',
166
'default_params': {
167
'temperature': 0.5,
168
'maximumLength': 1024,
169
'topP': 0.95,
170
'topK': 4,
171
'repetitionPenalty': 1.03,
172
},
173
},
174
'google/flan-t5-xxl': {
175
'id': 'huggingface:google/flan-t5-xxl',
176
'default_params': {
177
'temperature': 0.5,
178
'maximumLength': 1024,
179
'topP': 0.95,
180
'topK': 4,
181
'repetitionPenalty': 1.03,
182
},
183
},
184
'EleutherAI/gpt-neox-20b': {
185
'id': 'huggingface:EleutherAI/gpt-neox-20b',
186
'default_params': {
187
'temperature': 0.5,
188
'maximumLength': 1024,
189
'topP': 0.95,
190
'topK': 4,
191
'repetitionPenalty': 1.03,
192
'stopSequences': [],
193
},
194
},
195
'OpenAssistant/oasst-sft-4-pythia-12b-epoch-3.5': {
196
'id': 'huggingface:OpenAssistant/oasst-sft-4-pythia-12b-epoch-3.5',
197
'default_params': {
198
'maximumLength': 1024,
199
'typicalP': 0.2,
200
'repetitionPenalty': 1,
201
},
202
},
203
'OpenAssistant/oasst-sft-1-pythia-12b': {
204
'id': 'huggingface:OpenAssistant/oasst-sft-1-pythia-12b',
205
'default_params': {
206
'maximumLength': 1024,
207
'typicalP': 0.2,
208
'repetitionPenalty': 1,
209
},
210
},
211
'bigcode/santacoder': {
212
'id': 'huggingface:bigcode/santacoder',
213
'default_params': {
214
'temperature': 0.5,
215
'maximumLength': 1024,
216
'topP': 0.95,
217
'topK': 4,
218
'repetitionPenalty': 1.03,
219
},
220
},
221
'command-light-nightly': {
222
'id': 'cohere:command-light-nightly',
223
'default_params': {
224
'temperature': 0.9,
225
'maximumLength': 1024,
226
'topP': 1,
227
'topK': 0,
228
'presencePenalty': 0,
229
'frequencyPenalty': 0,
230
'stopSequences': [],
231
},
232
},
233
'command-nightly': {
234
'id': 'cohere:command-nightly',
235
'default_params': {
236
'temperature': 0.9,
237
'maximumLength': 1024,
238
'topP': 1,
239
'topK': 0,
240
'presencePenalty': 0,
241
'frequencyPenalty': 0,
242
'stopSequences': [],
243
},
244
},
245
'gpt-4': {
246
'id': 'openai:gpt-4',
247
'default_params': {
248
'temperature': 0.7,
249
'maximumLength': 8192,
250
'topP': 1,
251
'presencePenalty': 0,
252
'frequencyPenalty': 0,
253
'stopSequences': [],
254
},
255
},
256
'gpt-4-0613': {
257
'id': 'openai:gpt-4-0613',
258
'default_params': {
259
'temperature': 0.7,
260
'maximumLength': 8192,
261
'topP': 1,
262
'presencePenalty': 0,
263
'frequencyPenalty': 0,
264
'stopSequences': [],
265
},
266
},
267
'code-davinci-002': {
268
'id': 'openai:code-davinci-002',
269
'default_params': {
270
'temperature': 0.5,
271
'maximumLength': 1024,
272
'topP': 1,
273
'presencePenalty': 0,
274
'frequencyPenalty': 0,
275
'stopSequences': [],
276
},
277
},
278
'gpt-3.5-turbo': {
279
'id': 'openai:gpt-3.5-turbo',
280
'default_params': {
281
'temperature': 0.7,
282
'maximumLength': 4096,
283
'topP': 1,
284
'topK': 1,
285
'presencePenalty': 1,
286
'frequencyPenalty': 1,
287
'stopSequences': [],
288
},
289
},
290
'gpt-3.5-turbo-16k': {
291
'id': 'openai:gpt-3.5-turbo-16k',
292
'default_params': {
293
'temperature': 0.7,
294
'maximumLength': 16280,
295
'topP': 1,
296
'topK': 1,
297
'presencePenalty': 1,
298
'frequencyPenalty': 1,
299
'stopSequences': [],
300
},
301
},
302
'gpt-3.5-turbo-16k-0613': {
303
'id': 'openai:gpt-3.5-turbo-16k-0613',
304
'default_params': {
305
'temperature': 0.7,
306
'maximumLength': 16280,
307
'topP': 1,
308
'topK': 1,
309
'presencePenalty': 1,
310
'frequencyPenalty': 1,
311
'stopSequences': [],
312
},
313
},
314
'text-ada-001': {
315
'id': 'openai:text-ada-001',
316
'default_params': {
317
'temperature': 0.5,
318
'maximumLength': 1024,
319
'topP': 1,
320
'presencePenalty': 0,
321
'frequencyPenalty': 0,
322
'stopSequences': [],
323
},
324
},
325
'text-babbage-001': {
326
'id': 'openai:text-babbage-001',
327
'default_params': {
328
'temperature': 0.5,
329
'maximumLength': 1024,
330
'topP': 1,
331
'presencePenalty': 0,
332
'frequencyPenalty': 0,
333
'stopSequences': [],
334
},
335
},
336
'text-curie-001': {
337
'id': 'openai:text-curie-001',
338
'default_params': {
339
'temperature': 0.5,
340
'maximumLength': 1024,
341
'topP': 1,
342
'presencePenalty': 0,
343
'frequencyPenalty': 0,
344
'stopSequences': [],
345
},
346
},
347
'text-davinci-002': {
348
'id': 'openai:text-davinci-002',
349
'default_params': {
350
'temperature': 0.5,
351
'maximumLength': 1024,
352
'topP': 1,
353
'presencePenalty': 0,
354
'frequencyPenalty': 0,
355
'stopSequences': [],
356
},
357
},
358
'text-davinci-003': {
359
'id': 'openai:text-davinci-003',
360
'default_params': {
361
'temperature': 0.5,
362
'maximumLength': 4097,
363
'topP': 1,
364
'presencePenalty': 0,
365
'frequencyPenalty': 0,
366
'stopSequences': [],
367
},
368
},
369
}