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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
|
from __future__ import annotations
import asyncio
import json
import uuid
import cloudscraper
from typing import AsyncGenerator
from ..typing import AsyncResult, Messages
from .base_provider import AsyncGeneratorProvider, ProviderModelMixin
from .helper import format_prompt
class Cloudflare(AsyncGeneratorProvider, ProviderModelMixin):
url = "https://playground.ai.cloudflare.com"
api_endpoint = "https://playground.ai.cloudflare.com/api/inference"
working = True
supports_stream = True
supports_system_message = True
supports_message_history = True
default_model = '@cf/meta/llama-3.1-8b-instruct'
models = [
'@cf/deepseek-ai/deepseek-math-7b-instruct', # Specific answer
'@cf/thebloke/discolm-german-7b-v1-awq',
'@cf/tiiuae/falcon-7b-instruct', # Specific answer
'@hf/google/gemma-7b-it',
'@cf/meta/llama-2-7b-chat-fp16',
'@cf/meta/llama-2-7b-chat-int8',
'@cf/meta/llama-3-8b-instruct',
'@cf/meta/llama-3-8b-instruct-awq',
default_model,
'@hf/meta-llama/meta-llama-3-8b-instruct',
'@cf/meta/llama-3.1-8b-instruct-awq',
'@cf/meta/llama-3.1-8b-instruct-fp8',
'@cf/meta/llama-3.2-11b-vision-instruct',
'@cf/meta/llama-3.2-1b-instruct',
'@cf/meta/llama-3.2-3b-instruct',
'@cf/mistral/mistral-7b-instruct-v0.1',
'@hf/mistral/mistral-7b-instruct-v0.2',
'@cf/openchat/openchat-3.5-0106',
'@cf/microsoft/phi-2',
'@cf/qwen/qwen1.5-0.5b-chat',
'@cf/qwen/qwen1.5-1.8b-chat',
'@cf/qwen/qwen1.5-14b-chat-awq',
'@cf/qwen/qwen1.5-7b-chat-awq',
'@cf/defog/sqlcoder-7b-2', # Specific answer
'@cf/tinyllama/tinyllama-1.1b-chat-v1.0',
'@cf/fblgit/una-cybertron-7b-v2-bf16',
]
model_aliases = {
"german-7b-v1": "@cf/thebloke/discolm-german-7b-v1-awq",
"gemma-7b": "@hf/google/gemma-7b-it",
"llama-2-7b": "@cf/meta/llama-2-7b-chat-fp16",
"llama-2-7b": "@cf/meta/llama-2-7b-chat-int8",
"llama-3-8b": "@cf/meta/llama-3-8b-instruct",
"llama-3-8b": "@cf/meta/llama-3-8b-instruct-awq",
"llama-3-8b": "@cf/meta/llama-3.1-8b-instruct",
"llama-3-8b": "@hf/meta-llama/meta-llama-3-8b-instruct",
"llama-3.1-8b": "@cf/meta/llama-3.1-8b-instruct-awq",
"llama-3.1-8b": "@cf/meta/llama-3.1-8b-instruct-fp8",
"llama-3.1-8b": "@cf/meta/llama-3.1-8b-instruct-fp8",
"llama-3.2-11b": "@cf/meta/llama-3.2-11b-vision-instruct",
"llama-3.2-1b": "@cf/meta/llama-3.2-1b-instruct",
"llama-3.2-3b": "@cf/meta/llama-3.2-3b-instruct",
"mistral-7b": "@cf/mistral/mistral-7b-instruct-v0.1",
"mistral-7b": "@hf/mistral/mistral-7b-instruct-v0.2",
"openchat-3.5": "@cf/openchat/openchat-3.5-0106",
"phi-2": "@cf/microsoft/phi-2",
"qwen-1.5-0.5b": "@cf/qwen/qwen1.5-0.5b-chat",
"qwen-1.5-1.8b": "@cf/qwen/qwen1.5-1.8b-chat",
"qwen-1.5-14b": "@cf/qwen/qwen1.5-14b-chat-awq",
"qwen-1.5-7b": "@cf/qwen/qwen1.5-7b-chat-awq",
"tinyllama-1.1b": "@cf/tinyllama/tinyllama-1.1b-chat-v1.0",
"cybertron-7b": "@cf/fblgit/una-cybertron-7b-v2-bf16",
}
@classmethod
def get_model(cls, model: str) -> str:
if model in cls.models:
return model
elif model in cls.model_aliases:
return cls.model_aliases[model]
else:
return cls.default_model
@classmethod
async def create_async_generator(
cls,
model: str,
messages: Messages,
proxy: str = None,
max_tokens: str = 2048,
stream: bool = True,
**kwargs
) -> AsyncResult:
model = cls.get_model(model)
headers = {
'Accept': 'text/event-stream',
'Accept-Language': 'en-US,en;q=0.9',
'Cache-Control': 'no-cache',
'Content-Type': 'application/json',
'Origin': cls.url,
'Pragma': 'no-cache',
'Referer': f'{cls.url}/',
'Sec-Ch-Ua': '"Chromium";v="129", "Not=A?Brand";v="8"',
'Sec-Ch-Ua-Mobile': '?0',
'Sec-Ch-Ua-Platform': '"Linux"',
'Sec-Fetch-Dest': 'empty',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Site': 'same-origin',
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36',
}
cookies = {
'__cf_bm': uuid.uuid4().hex,
}
scraper = cloudscraper.create_scraper()
prompt = format_prompt(messages)
data = {
"messages": [
{"role": "system", "content": "You are a helpful assistant"},
{"role": "user", "content": prompt}
],
"lora": None,
"model": model,
"max_tokens": max_tokens,
"stream": stream
}
max_retries = 3
for attempt in range(max_retries):
try:
response = scraper.post(
cls.api_endpoint,
headers=headers,
cookies=cookies,
json=data,
stream=True,
proxies={'http': proxy, 'https': proxy} if proxy else None
)
if response.status_code == 403:
await asyncio.sleep(2 ** attempt)
continue
response.raise_for_status()
for line in response.iter_lines():
if line.startswith(b'data: '):
if line == b'data: [DONE]':
break
try:
content = json.loads(line[6:].decode('utf-8'))['response']
yield content
except Exception:
continue
break
except Exception as e:
if attempt == max_retries - 1:
raise
@classmethod
async def create_async(
cls,
model: str,
messages: Messages,
proxy: str = None,
**kwargs
) -> str:
full_response = ""
async for response in cls.create_async_generator(model, messages, proxy, **kwargs):
full_response += response
return full_response
|