2024-10-11 09:52:30 +03:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
import asyncio
|
|
|
|
import json
|
|
|
|
import uuid
|
2024-11-06 14:56:33 +03:00
|
|
|
|
Fix api streaming, fix AsyncClient (#2357)
* Fix api streaming, fix AsyncClient, Improve Client class, Some providers fixes, Update models list, Fix some tests, Update model list in Airforce provid
er, Add OpenAi image generation url to api, Fix reload and debug in api arguments, Fix websearch in gui
* Fix Cloadflare and Pi and AmigoChat provider
* Fix conversation support in DDG provider, Add cloudflare bypass with nodriver
* Fix unittests without curl_cffi
2024-11-16 15:19:51 +03:00
|
|
|
from ..typing import AsyncResult, Messages, Cookies
|
|
|
|
from .base_provider import AsyncGeneratorProvider, ProviderModelMixin, get_running_loop
|
|
|
|
from ..requests import Session, StreamSession, get_args_from_nodriver, raise_for_status, merge_cookies
|
2024-11-17 13:06:37 +03:00
|
|
|
from ..errors import ResponseStatusError
|
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"
|
Fix api streaming, fix AsyncClient (#2357)
* Fix api streaming, fix AsyncClient, Improve Client class, Some providers fixes, Update models list, Fix some tests, Update model list in Airforce provid
er, Add OpenAi image generation url to api, Fix reload and debug in api arguments, Fix websearch in gui
* Fix Cloadflare and Pi and AmigoChat provider
* Fix conversation support in DDG provider, Add cloudflare bypass with nodriver
* Fix unittests without curl_cffi
2024-11-16 15:19:51 +03:00
|
|
|
models_url = "https://playground.ai.cloudflare.com/api/models"
|
2024-10-11 09:52:30 +03:00
|
|
|
working = True
|
|
|
|
supports_stream = True
|
|
|
|
supports_system_message = True
|
|
|
|
supports_message_history = True
|
Fix api streaming, fix AsyncClient (#2357)
* Fix api streaming, fix AsyncClient, Improve Client class, Some providers fixes, Update models list, Fix some tests, Update model list in Airforce provid
er, Add OpenAi image generation url to api, Fix reload and debug in api arguments, Fix websearch in gui
* Fix Cloadflare and Pi and AmigoChat provider
* Fix conversation support in DDG provider, Add cloudflare bypass with nodriver
* Fix unittests without curl_cffi
2024-11-16 15:19:51 +03:00
|
|
|
default_model = "@cf/meta/llama-3.1-8b-instruct"
|
2024-10-11 09:52:30 +03:00
|
|
|
model_aliases = {
|
|
|
|
"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",
|
|
|
|
"qwen-1.5-7b": "@cf/qwen/qwen1.5-7b-chat-awq",
|
|
|
|
}
|
Fix api streaming, fix AsyncClient (#2357)
* Fix api streaming, fix AsyncClient, Improve Client class, Some providers fixes, Update models list, Fix some tests, Update model list in Airforce provid
er, Add OpenAi image generation url to api, Fix reload and debug in api arguments, Fix websearch in gui
* Fix Cloadflare and Pi and AmigoChat provider
* Fix conversation support in DDG provider, Add cloudflare bypass with nodriver
* Fix unittests without curl_cffi
2024-11-16 15:19:51 +03:00
|
|
|
_args: dict = None
|
2024-10-11 09:52:30 +03:00
|
|
|
|
|
|
|
@classmethod
|
Fix api streaming, fix AsyncClient (#2357)
* Fix api streaming, fix AsyncClient, Improve Client class, Some providers fixes, Update models list, Fix some tests, Update model list in Airforce provid
er, Add OpenAi image generation url to api, Fix reload and debug in api arguments, Fix websearch in gui
* Fix Cloadflare and Pi and AmigoChat provider
* Fix conversation support in DDG provider, Add cloudflare bypass with nodriver
* Fix unittests without curl_cffi
2024-11-16 15:19:51 +03:00
|
|
|
def get_models(cls) -> str:
|
|
|
|
if not cls.models:
|
|
|
|
if cls._args is None:
|
|
|
|
get_running_loop(check_nested=True)
|
|
|
|
args = get_args_from_nodriver(cls.url, cookies={
|
|
|
|
'__cf_bm': uuid.uuid4().hex,
|
|
|
|
})
|
|
|
|
cls._args = asyncio.run(args)
|
|
|
|
with Session(**cls._args) as session:
|
|
|
|
response = session.get(cls.models_url)
|
2024-11-17 13:06:37 +03:00
|
|
|
cls._args["cookies"] = merge_cookies(cls._args["cookies"] , response)
|
|
|
|
try:
|
|
|
|
raise_for_status(response)
|
|
|
|
except ResponseStatusError as e:
|
|
|
|
cls._args = None
|
|
|
|
raise e
|
Fix api streaming, fix AsyncClient (#2357)
* Fix api streaming, fix AsyncClient, Improve Client class, Some providers fixes, Update models list, Fix some tests, Update model list in Airforce provid
er, Add OpenAi image generation url to api, Fix reload and debug in api arguments, Fix websearch in gui
* Fix Cloadflare and Pi and AmigoChat provider
* Fix conversation support in DDG provider, Add cloudflare bypass with nodriver
* Fix unittests without curl_cffi
2024-11-16 15:19:51 +03:00
|
|
|
json_data = response.json()
|
|
|
|
cls.models = [model.get("name") for model in json_data.get("models")]
|
|
|
|
return cls.models
|
2024-10-11 09:52:30 +03:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
async def create_async_generator(
|
|
|
|
cls,
|
|
|
|
model: str,
|
|
|
|
messages: Messages,
|
|
|
|
proxy: str = None,
|
2024-11-11 20:16:02 +03:00
|
|
|
max_tokens: int = 2048,
|
Fix api streaming, fix AsyncClient (#2357)
* Fix api streaming, fix AsyncClient, Improve Client class, Some providers fixes, Update models list, Fix some tests, Update model list in Airforce provid
er, Add OpenAi image generation url to api, Fix reload and debug in api arguments, Fix websearch in gui
* Fix Cloadflare and Pi and AmigoChat provider
* Fix conversation support in DDG provider, Add cloudflare bypass with nodriver
* Fix unittests without curl_cffi
2024-11-16 15:19:51 +03:00
|
|
|
cookies: Cookies = None,
|
|
|
|
timeout: int = 300,
|
2024-10-11 09:52:30 +03:00
|
|
|
**kwargs
|
|
|
|
) -> AsyncResult:
|
|
|
|
model = cls.get_model(model)
|
Fix api streaming, fix AsyncClient (#2357)
* Fix api streaming, fix AsyncClient, Improve Client class, Some providers fixes, Update models list, Fix some tests, Update model list in Airforce provid
er, Add OpenAi image generation url to api, Fix reload and debug in api arguments, Fix websearch in gui
* Fix Cloadflare and Pi and AmigoChat provider
* Fix conversation support in DDG provider, Add cloudflare bypass with nodriver
* Fix unittests without curl_cffi
2024-11-16 15:19:51 +03:00
|
|
|
if cls._args is None:
|
|
|
|
cls._args = await get_args_from_nodriver(cls.url, proxy, timeout, cookies)
|
2024-10-11 09:52:30 +03:00
|
|
|
data = {
|
Fix api streaming, fix AsyncClient (#2357)
* Fix api streaming, fix AsyncClient, Improve Client class, Some providers fixes, Update models list, Fix some tests, Update model list in Airforce provid
er, Add OpenAi image generation url to api, Fix reload and debug in api arguments, Fix websearch in gui
* Fix Cloadflare and Pi and AmigoChat provider
* Fix conversation support in DDG provider, Add cloudflare bypass with nodriver
* Fix unittests without curl_cffi
2024-11-16 15:19:51 +03:00
|
|
|
"messages": messages,
|
2024-10-11 09:52:30 +03:00
|
|
|
"lora": None,
|
|
|
|
"model": model,
|
2024-11-11 20:16:02 +03:00
|
|
|
"max_tokens": max_tokens,
|
2024-11-06 14:56:33 +03:00
|
|
|
"stream": True
|
2024-10-11 09:52:30 +03:00
|
|
|
}
|
Fix api streaming, fix AsyncClient (#2357)
* Fix api streaming, fix AsyncClient, Improve Client class, Some providers fixes, Update models list, Fix some tests, Update model list in Airforce provid
er, Add OpenAi image generation url to api, Fix reload and debug in api arguments, Fix websearch in gui
* Fix Cloadflare and Pi and AmigoChat provider
* Fix conversation support in DDG provider, Add cloudflare bypass with nodriver
* Fix unittests without curl_cffi
2024-11-16 15:19:51 +03:00
|
|
|
async with StreamSession(**cls._args) as session:
|
|
|
|
async with session.post(
|
|
|
|
cls.api_endpoint,
|
|
|
|
json=data,
|
|
|
|
) as response:
|
|
|
|
cls._args["cookies"] = merge_cookies(cls._args["cookies"] , response)
|
2024-11-17 13:06:37 +03:00
|
|
|
try:
|
|
|
|
await raise_for_status(response)
|
|
|
|
except ResponseStatusError as e:
|
|
|
|
cls._args = None
|
|
|
|
raise e
|
Fix api streaming, fix AsyncClient (#2357)
* Fix api streaming, fix AsyncClient, Improve Client class, Some providers fixes, Update models list, Fix some tests, Update model list in Airforce provid
er, Add OpenAi image generation url to api, Fix reload and debug in api arguments, Fix websearch in gui
* Fix Cloadflare and Pi and AmigoChat provider
* Fix conversation support in DDG provider, Add cloudflare bypass with nodriver
* Fix unittests without curl_cffi
2024-11-16 15:19:51 +03:00
|
|
|
async for line in response.iter_lines():
|
2024-10-11 09:52:30 +03:00
|
|
|
if line.startswith(b'data: '):
|
|
|
|
if line == b'data: [DONE]':
|
|
|
|
break
|
|
|
|
try:
|
Fix api streaming, fix AsyncClient (#2357)
* Fix api streaming, fix AsyncClient, Improve Client class, Some providers fixes, Update models list, Fix some tests, Update model list in Airforce provid
er, Add OpenAi image generation url to api, Fix reload and debug in api arguments, Fix websearch in gui
* Fix Cloadflare and Pi and AmigoChat provider
* Fix conversation support in DDG provider, Add cloudflare bypass with nodriver
* Fix unittests without curl_cffi
2024-11-16 15:19:51 +03:00
|
|
|
content = json.loads(line[6:].decode())
|
|
|
|
if content.get("response") and content.get("response") != '</s>':
|
2024-11-11 20:16:02 +03:00
|
|
|
yield content['response']
|
2024-10-11 09:52:30 +03:00
|
|
|
except Exception:
|
Fix api streaming, fix AsyncClient (#2357)
* Fix api streaming, fix AsyncClient, Improve Client class, Some providers fixes, Update models list, Fix some tests, Update model list in Airforce provid
er, Add OpenAi image generation url to api, Fix reload and debug in api arguments, Fix websearch in gui
* Fix Cloadflare and Pi and AmigoChat provider
* Fix conversation support in DDG provider, Add cloudflare bypass with nodriver
* Fix unittests without curl_cffi
2024-11-16 15:19:51 +03:00
|
|
|
continue
|