2024-10-11 09:52:30 +03:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
import asyncio
|
|
|
|
import json
|
|
|
|
import uuid
|
|
|
|
import cloudscraper
|
|
|
|
from typing import AsyncGenerator
|
2024-11-06 14:56:33 +03:00
|
|
|
|
2024-10-11 09:52:30 +03:00
|
|
|
from ..typing import AsyncResult, Messages
|
|
|
|
from .base_provider import AsyncGeneratorProvider, ProviderModelMixin
|
|
|
|
from .helper import format_prompt
|
|
|
|
|
2024-11-06 14:56:33 +03:00
|
|
|
|
2024-10-11 09:52:30 +03:00
|
|
|
class Cloudflare(AsyncGeneratorProvider, ProviderModelMixin):
|
2024-11-06 14:56:33 +03:00
|
|
|
label = "Cloudflare AI"
|
2024-10-11 09:52:30 +03:00
|
|
|
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
|
|
|
|
|
2024-11-06 14:56:33 +03:00
|
|
|
default_model = '@cf/meta/llama-3.1-8b-instruct-awq'
|
2024-10-11 09:52:30 +03:00
|
|
|
models = [
|
|
|
|
'@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',
|
|
|
|
'@hf/meta-llama/meta-llama-3-8b-instruct',
|
|
|
|
|
2024-11-06 14:56:33 +03:00
|
|
|
default_model,
|
2024-10-11 09:52:30 +03:00
|
|
|
'@cf/meta/llama-3.1-8b-instruct-fp8',
|
2024-11-06 14:56:33 +03:00
|
|
|
|
2024-10-11 09:52:30 +03:00
|
|
|
'@cf/meta/llama-3.2-1b-instruct',
|
|
|
|
|
|
|
|
'@hf/mistral/mistral-7b-instruct-v0.2',
|
|
|
|
|
|
|
|
'@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',
|
2024-11-06 14:56:33 +03:00
|
|
|
'@cf/qwen/qwen1.5-7b-chat-awq',
|
2024-10-11 09:52:30 +03:00
|
|
|
|
2024-11-06 14:56:33 +03:00
|
|
|
'@cf/defog/sqlcoder-7b-2',
|
2024-10-11 09:52:30 +03:00
|
|
|
]
|
|
|
|
|
|
|
|
model_aliases = {
|
2024-11-06 14:56:33 +03:00
|
|
|
#"falcon-7b": "@cf/tiiuae/falcon-7b-instruct",
|
2024-10-11 09:52:30 +03:00
|
|
|
|
|
|
|
"gemma-7b": "@hf/google/gemma-7b-it",
|
2024-11-06 14:56:33 +03:00
|
|
|
|
2024-10-11 09:52:30 +03:00
|
|
|
"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": "@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.2-1b": "@cf/meta/llama-3.2-1b-instruct",
|
|
|
|
|
|
|
|
"phi-2": "@cf/microsoft/phi-2",
|
|
|
|
|
2024-11-06 14:56:33 +03:00
|
|
|
"qwen-1.5-0-5b": "@cf/qwen/qwen1.5-0.5b-chat",
|
|
|
|
"qwen-1.5-1-8b": "@cf/qwen/qwen1.5-1.8b-chat",
|
2024-10-11 09:52:30 +03:00
|
|
|
"qwen-1.5-14b": "@cf/qwen/qwen1.5-14b-chat-awq",
|
|
|
|
"qwen-1.5-7b": "@cf/qwen/qwen1.5-7b-chat-awq",
|
|
|
|
|
2024-11-06 14:56:33 +03:00
|
|
|
#"sqlcoder-7b": "@cf/defog/sqlcoder-7b-2",
|
2024-10-11 09:52:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@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,
|
|
|
|
**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()
|
|
|
|
|
|
|
|
data = {
|
|
|
|
"messages": [
|
2024-11-06 14:56:33 +03:00
|
|
|
{"role": "user", "content": format_prompt(messages)}
|
2024-10-11 09:52:30 +03:00
|
|
|
],
|
|
|
|
"lora": None,
|
|
|
|
"model": model,
|
2024-11-06 14:56:33 +03:00
|
|
|
"max_tokens": 2048,
|
|
|
|
"stream": True
|
2024-10-11 09:52:30 +03:00
|
|
|
}
|
|
|
|
|
2024-11-06 14:56:33 +03:00
|
|
|
max_retries = 5
|
2024-10-11 09:52:30 +03:00
|
|
|
for attempt in range(max_retries):
|
|
|
|
try:
|
|
|
|
response = scraper.post(
|
|
|
|
cls.api_endpoint,
|
|
|
|
headers=headers,
|
|
|
|
cookies=cookies,
|
|
|
|
json=data,
|
2024-11-06 14:56:33 +03:00
|
|
|
stream=True
|
2024-10-11 09:52:30 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
if response.status_code == 403:
|
|
|
|
await asyncio.sleep(2 ** attempt)
|
|
|
|
continue
|
|
|
|
|
|
|
|
response.raise_for_status()
|
|
|
|
|
2024-11-06 14:56:33 +03:00
|
|
|
skip_tokens = ["</s>", "<s>", "[DONE]", "<|endoftext|>", "<|end|>"]
|
|
|
|
filtered_response = ""
|
|
|
|
|
2024-10-11 09:52:30 +03:00
|
|
|
for line in response.iter_lines():
|
|
|
|
if line.startswith(b'data: '):
|
|
|
|
if line == b'data: [DONE]':
|
|
|
|
break
|
|
|
|
try:
|
2024-11-06 14:56:33 +03:00
|
|
|
content = json.loads(line[6:].decode('utf-8'))
|
|
|
|
response_text = content['response']
|
|
|
|
if not any(token in response_text for token in skip_tokens):
|
|
|
|
filtered_response += response_text
|
2024-10-11 09:52:30 +03:00
|
|
|
except Exception:
|
|
|
|
continue
|
2024-11-06 14:56:33 +03:00
|
|
|
|
|
|
|
yield filtered_response.strip()
|
2024-10-11 09:52:30 +03:00
|
|
|
break
|
|
|
|
except Exception as e:
|
|
|
|
if attempt == max_retries - 1:
|
|
|
|
raise
|