diff options
author | H Lohaus <hlohaus@users.noreply.github.com> | 2024-11-20 02:42:15 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-11-20 02:42:15 +0100 |
commit | 419264f9665ed50ac0dcbf86327da8bf221ac3e8 (patch) | |
tree | 027ebe8efcb413b648101ac6fd4fe744803e8a8a /g4f/Provider/needs_auth/Cerebras.py | |
parent | Merge pull request #2387 from hlohaus/info (diff) | |
parent | Fix missing provider_handler in client (diff) | |
download | gpt4free-419264f9665ed50ac0dcbf86327da8bf221ac3e8.tar gpt4free-419264f9665ed50ac0dcbf86327da8bf221ac3e8.tar.gz gpt4free-419264f9665ed50ac0dcbf86327da8bf221ac3e8.tar.bz2 gpt4free-419264f9665ed50ac0dcbf86327da8bf221ac3e8.tar.lz gpt4free-419264f9665ed50ac0dcbf86327da8bf221ac3e8.tar.xz gpt4free-419264f9665ed50ac0dcbf86327da8bf221ac3e8.tar.zst gpt4free-419264f9665ed50ac0dcbf86327da8bf221ac3e8.zip |
Diffstat (limited to 'g4f/Provider/needs_auth/Cerebras.py')
-rw-r--r-- | g4f/Provider/needs_auth/Cerebras.py | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/g4f/Provider/needs_auth/Cerebras.py b/g4f/Provider/needs_auth/Cerebras.py new file mode 100644 index 00000000..0f94c476 --- /dev/null +++ b/g4f/Provider/needs_auth/Cerebras.py @@ -0,0 +1,65 @@ +from __future__ import annotations + +import requests +from aiohttp import ClientSession + +from .OpenaiAPI import OpenaiAPI +from ...typing import AsyncResult, Messages, Cookies +from ...requests.raise_for_status import raise_for_status +from ...cookies import get_cookies + +class Cerebras(OpenaiAPI): + label = "Cerebras Inference" + url = "https://inference.cerebras.ai/" + working = True + default_model = "llama3.1-70b" + fallback_models = [ + "llama3.1-70b", + "llama3.1-8b", + ] + model_aliases = {"llama-3.1-70b": "llama3.1-70b", "llama-3.1-8b": "llama3.1-8b"} + + @classmethod + def get_models(cls, api_key: str = None): + if not cls.models: + try: + headers = {} + if api_key: + headers["authorization"] = f"Bearer ${api_key}" + response = requests.get(f"https://api.cerebras.ai/v1/models", headers=headers) + raise_for_status(response) + data = response.json() + cls.models = [model.get("model") for model in data.get("models")] + except Exception: + cls.models = cls.fallback_models + return cls.models + + @classmethod + async def create_async_generator( + cls, + model: str, + messages: Messages, + api_base: str = "https://api.cerebras.ai/v1", + api_key: str = None, + cookies: Cookies = None, + **kwargs + ) -> AsyncResult: + if api_key is None and cookies is None: + cookies = get_cookies(".cerebras.ai") + async with ClientSession(cookies=cookies) as session: + async with session.get("https://inference.cerebras.ai/api/auth/session") as response: + raise_for_status(response) + data = await response.json() + if data: + api_key = data.get("user", {}).get("demoApiKey") + async for chunk in super().create_async_generator( + model, messages, + api_base=api_base, + impersonate="chrome", + api_key=api_key, + headers={ + "User-Agent": "ex/JS 1.5.0", + }, + **kwargs + ): + yield chunk |