返回提交历史
Added
g4f/Provider/Cloudflare.py
+212
-0
XFEstudio/gpt4free
feat(g4f/Provider/Cloudflare.py): add new Cloudflare provider for async generation
d80e9dd8
代码差异
1 个文件
+212
-0
@@ -0,0 +1,212 @@
1
from __future__ import annotations
2
3
import asyncio
4
import json
5
import uuid
6
import cloudscraper
7
from typing import AsyncGenerator
8
from ..typing import AsyncResult, Messages
9
from .base_provider import AsyncGeneratorProvider, ProviderModelMixin
10
from .helper import format_prompt
11
12
class Cloudflare(AsyncGeneratorProvider, ProviderModelMixin):
13
url = "https://playground.ai.cloudflare.com"
14
api_endpoint = "https://playground.ai.cloudflare.com/api/inference"
15
working = True
16
supports_stream = True
17
supports_system_message = True
18
supports_message_history = True
19
20
default_model = '@cf/meta/llama-3.1-8b-instruct'
21
models = [
22
'@cf/deepseek-ai/deepseek-math-7b-instruct', # Specific answer
23
24
25
'@cf/thebloke/discolm-german-7b-v1-awq',
26
27
28
'@cf/tiiuae/falcon-7b-instruct', # Specific answer
29
30
31
'@hf/google/gemma-7b-it',
32
33
34
'@cf/meta/llama-2-7b-chat-fp16',
35
'@cf/meta/llama-2-7b-chat-int8',
36
37
'@cf/meta/llama-3-8b-instruct',
38
'@cf/meta/llama-3-8b-instruct-awq',
39
default_model,
40
'@hf/meta-llama/meta-llama-3-8b-instruct',
41
42
'@cf/meta/llama-3.1-8b-instruct-awq',
43
'@cf/meta/llama-3.1-8b-instruct-fp8',
44
'@cf/meta/llama-3.2-11b-vision-instruct',
45
'@cf/meta/llama-3.2-1b-instruct',
46
'@cf/meta/llama-3.2-3b-instruct',
47
48
'@cf/mistral/mistral-7b-instruct-v0.1',
49
'@hf/mistral/mistral-7b-instruct-v0.2',
50
51
'@cf/openchat/openchat-3.5-0106',
52
53
'@cf/microsoft/phi-2',
54
55
'@cf/qwen/qwen1.5-0.5b-chat',
56
'@cf/qwen/qwen1.5-1.8b-chat',
57
'@cf/qwen/qwen1.5-14b-chat-awq',
58
'@cf/qwen/qwen1.5-7b-chat-awq',
59
60
'@cf/defog/sqlcoder-7b-2', # Specific answer
61
62
'@cf/tinyllama/tinyllama-1.1b-chat-v1.0',
63
64
'@cf/fblgit/una-cybertron-7b-v2-bf16',
65
]
66
67
model_aliases = {
68
"german-7b-v1": "@cf/thebloke/discolm-german-7b-v1-awq",
69
70
71
"gemma-7b": "@hf/google/gemma-7b-it",
72
73
74
"llama-2-7b": "@cf/meta/llama-2-7b-chat-fp16",
75
"llama-2-7b": "@cf/meta/llama-2-7b-chat-int8",
76
77
"llama-3-8b": "@cf/meta/llama-3-8b-instruct",
78
"llama-3-8b": "@cf/meta/llama-3-8b-instruct-awq",
79
"llama-3-8b": "@cf/meta/llama-3.1-8b-instruct",
80
"llama-3-8b": "@hf/meta-llama/meta-llama-3-8b-instruct",
81
82
"llama-3.1-8b": "@cf/meta/llama-3.1-8b-instruct-awq",
83
"llama-3.1-8b": "@cf/meta/llama-3.1-8b-instruct-fp8",
84
"llama-3.1-8b": "@cf/meta/llama-3.1-8b-instruct-fp8",
85
86
"llama-3.2-11b": "@cf/meta/llama-3.2-11b-vision-instruct",
87
"llama-3.2-1b": "@cf/meta/llama-3.2-1b-instruct",
88
"llama-3.2-3b": "@cf/meta/llama-3.2-3b-instruct",
89
90
91
"mistral-7b": "@cf/mistral/mistral-7b-instruct-v0.1",
92
"mistral-7b": "@hf/mistral/mistral-7b-instruct-v0.2",
93
94
95
"openchat-3.5": "@cf/openchat/openchat-3.5-0106",
96
97
98
"phi-2": "@cf/microsoft/phi-2",
99
100
101
"qwen-1.5-0.5b": "@cf/qwen/qwen1.5-0.5b-chat",
102
"qwen-1.5-1.8b": "@cf/qwen/qwen1.5-1.8b-chat",
103
"qwen-1.5-14b": "@cf/qwen/qwen1.5-14b-chat-awq",
104
"qwen-1.5-7b": "@cf/qwen/qwen1.5-7b-chat-awq",
105
106
107
"tinyllama-1.1b": "@cf/tinyllama/tinyllama-1.1b-chat-v1.0",
108
109
110
"cybertron-7b": "@cf/fblgit/una-cybertron-7b-v2-bf16",
111
}
112
113
@classmethod
114
def get_model(cls, model: str) -> str:
115
if model in cls.models:
116
return model
117
elif model in cls.model_aliases:
118
return cls.model_aliases[model]
119
else:
120
return cls.default_model
121
122
@classmethod
123
async def create_async_generator(
124
cls,
125
model: str,
126
messages: Messages,
127
proxy: str = None,
128
max_tokens: str = 2048,
129
stream: bool = True,
130
**kwargs
131
) -> AsyncResult:
132
model = cls.get_model(model)
133
134
headers = {
135
'Accept': 'text/event-stream',
136
'Accept-Language': 'en-US,en;q=0.9',
137
'Cache-Control': 'no-cache',
138
'Content-Type': 'application/json',
139
'Origin': cls.url,
140
'Pragma': 'no-cache',
141
'Referer': f'{cls.url}/',
142
'Sec-Ch-Ua': '"Chromium";v="129", "Not=A?Brand";v="8"',
143
'Sec-Ch-Ua-Mobile': '?0',
144
'Sec-Ch-Ua-Platform': '"Linux"',
145
'Sec-Fetch-Dest': 'empty',
146
'Sec-Fetch-Mode': 'cors',
147
'Sec-Fetch-Site': 'same-origin',
148
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36',
149
}
150
151
cookies = {
152
'__cf_bm': uuid.uuid4().hex,
153
}
154
155
scraper = cloudscraper.create_scraper()
156
157
prompt = format_prompt(messages)
158
data = {
159
"messages": [
160
{"role": "system", "content": "You are a helpful assistant"},
161
{"role": "user", "content": prompt}
162
],
163
"lora": None,
164
"model": model,
165
"max_tokens": max_tokens,
166
"stream": stream
167
}
168
169
max_retries = 3
170
for attempt in range(max_retries):
171
try:
172
response = scraper.post(
173
cls.api_endpoint,
174
headers=headers,
175
cookies=cookies,
176
json=data,
177
stream=True,
178
proxies={'http': proxy, 'https': proxy} if proxy else None
179
)
180
181
if response.status_code == 403:
182
await asyncio.sleep(2 ** attempt)
183
continue
184
185
response.raise_for_status()
186
187
for line in response.iter_lines():
188
if line.startswith(b'data: '):
189
if line == b'data: [DONE]':
190
break
191
try:
192
content = json.loads(line[6:].decode('utf-8'))['response']
193
yield content
194
except Exception:
195
continue
196
break
197
except Exception as e:
198
if attempt == max_retries - 1:
199
raise
200
201
@classmethod
202
async def create_async(
203
cls,
204
model: str,
205
messages: Messages,
206
proxy: str = None,
207
**kwargs
208
) -> str:
209
full_response = ""
210
async for response in cls.create_async_generator(model, messages, proxy, **kwargs):
211
full_response += response
212
return full_response