summaryrefslogtreecommitdiffstats
path: root/g4f/Provider/needs_auth
diff options
context:
space:
mode:
Diffstat (limited to 'g4f/Provider/needs_auth')
-rw-r--r--g4f/Provider/needs_auth/Cerebras.py65
-rw-r--r--g4f/Provider/needs_auth/CopilotAccount.py7
-rw-r--r--g4f/Provider/needs_auth/HuggingFace2.py28
-rw-r--r--g4f/Provider/needs_auth/OpenaiAPI.py4
-rw-r--r--g4f/Provider/needs_auth/__init__.py2
5 files changed, 103 insertions, 3 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
diff --git a/g4f/Provider/needs_auth/CopilotAccount.py b/g4f/Provider/needs_auth/CopilotAccount.py
index 76e51278..497aab98 100644
--- a/g4f/Provider/needs_auth/CopilotAccount.py
+++ b/g4f/Provider/needs_auth/CopilotAccount.py
@@ -1,9 +1,12 @@
from __future__ import annotations
+from ..base_provider import ProviderModelMixin
from ..Copilot import Copilot
-class CopilotAccount(Copilot):
+class CopilotAccount(Copilot, ProviderModelMixin):
needs_auth = True
parent = "Copilot"
default_model = "Copilot"
- default_vision_model = default_model \ No newline at end of file
+ default_vision_model = default_model
+ models = [default_model]
+ image_models = models \ No newline at end of file
diff --git a/g4f/Provider/needs_auth/HuggingFace2.py b/g4f/Provider/needs_auth/HuggingFace2.py
new file mode 100644
index 00000000..847d459b
--- /dev/null
+++ b/g4f/Provider/needs_auth/HuggingFace2.py
@@ -0,0 +1,28 @@
+from __future__ import annotations
+
+from .OpenaiAPI import OpenaiAPI
+from ..HuggingChat import HuggingChat
+from ...typing import AsyncResult, Messages
+
+class HuggingFace2(OpenaiAPI):
+ label = "HuggingFace (Inference API)"
+ url = "https://huggingface.co"
+ working = True
+ default_model = "meta-llama/Llama-3.2-11B-Vision-Instruct"
+ default_vision_model = default_model
+ models = [
+ *HuggingChat.models
+ ]
+
+ @classmethod
+ def create_async_generator(
+ cls,
+ model: str,
+ messages: Messages,
+ api_base: str = "https://api-inference.huggingface.co/v1",
+ max_tokens: int = 500,
+ **kwargs
+ ) -> AsyncResult:
+ return super().create_async_generator(
+ model, messages, api_base=api_base, max_tokens=max_tokens, **kwargs
+ ) \ No newline at end of file
diff --git a/g4f/Provider/needs_auth/OpenaiAPI.py b/g4f/Provider/needs_auth/OpenaiAPI.py
index 116b5f6f..83268b6d 100644
--- a/g4f/Provider/needs_auth/OpenaiAPI.py
+++ b/g4f/Provider/needs_auth/OpenaiAPI.py
@@ -34,6 +34,7 @@ class OpenaiAPI(AsyncGeneratorProvider, ProviderModelMixin):
stop: Union[str, list[str]] = None,
stream: bool = False,
headers: dict = None,
+ impersonate: str = None,
extra_data: dict = {},
**kwargs
) -> AsyncResult:
@@ -55,7 +56,8 @@ class OpenaiAPI(AsyncGeneratorProvider, ProviderModelMixin):
async with StreamSession(
proxies={"all": proxy},
headers=cls.get_headers(stream, api_key, headers),
- timeout=timeout
+ timeout=timeout,
+ impersonate=impersonate,
) as session:
data = filter_none(
messages=messages,
diff --git a/g4f/Provider/needs_auth/__init__.py b/g4f/Provider/needs_auth/__init__.py
index 0f430ab5..1c7fe7c5 100644
--- a/g4f/Provider/needs_auth/__init__.py
+++ b/g4f/Provider/needs_auth/__init__.py
@@ -1,6 +1,7 @@
from .gigachat import *
from .BingCreateImages import BingCreateImages
+from .Cerebras import Cerebras
from .CopilotAccount import CopilotAccount
from .DeepInfra import DeepInfra
from .DeepInfraImage import DeepInfraImage
@@ -8,6 +9,7 @@ from .Gemini import Gemini
from .GeminiPro import GeminiPro
from .Groq import Groq
from .HuggingFace import HuggingFace
+from .HuggingFace2 import HuggingFace2
from .MetaAI import MetaAI
from .MetaAIAccount import MetaAIAccount
from .OpenaiAPI import OpenaiAPI