summaryrefslogtreecommitdiffstats
path: root/g4f/Provider/needs_auth
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--g4f/Provider/needs_auth/Bard.py (renamed from g4f/Provider/Bard.py)2
-rw-r--r--g4f/Provider/needs_auth/HuggingChat.py74
-rw-r--r--g4f/Provider/needs_auth/OpenAssistant.py (renamed from g4f/Provider/OpenAssistant.py)6
-rw-r--r--g4f/Provider/needs_auth/OpenaiChat.py125
-rw-r--r--g4f/Provider/needs_auth/Raycast.py (renamed from g4f/Provider/Raycast.py)4
-rw-r--r--g4f/Provider/needs_auth/Theb.py (renamed from g4f/Provider/Theb.py)4
-rw-r--r--g4f/Provider/needs_auth/__init__.py6
7 files changed, 213 insertions, 8 deletions
diff --git a/g4f/Provider/Bard.py b/g4f/Provider/needs_auth/Bard.py
index 4e076378..7c42b680 100644
--- a/g4f/Provider/Bard.py
+++ b/g4f/Provider/needs_auth/Bard.py
@@ -6,7 +6,7 @@ import re
from aiohttp import ClientSession
-from .base_provider import AsyncProvider, format_prompt, get_cookies
+from ..base_provider import AsyncProvider, format_prompt, get_cookies
class Bard(AsyncProvider):
diff --git a/g4f/Provider/needs_auth/HuggingChat.py b/g4f/Provider/needs_auth/HuggingChat.py
new file mode 100644
index 00000000..1d500338
--- /dev/null
+++ b/g4f/Provider/needs_auth/HuggingChat.py
@@ -0,0 +1,74 @@
+from __future__ import annotations
+
+import json, uuid
+
+from aiohttp import ClientSession
+
+from ...typing import AsyncGenerator
+from ..base_provider import AsyncGeneratorProvider, format_prompt, get_cookies
+
+
+class HuggingChat(AsyncGeneratorProvider):
+ url = "https://huggingface.co/chat"
+ needs_auth = True
+ working = True
+ model = "meta-llama/Llama-2-70b-chat-hf"
+
+ @classmethod
+ async def create_async_generator(
+ cls,
+ model: str,
+ messages: list[dict[str, str]],
+ stream: bool = True,
+ proxy: str = None,
+ cookies: dict = None,
+ **kwargs
+ ) -> AsyncGenerator:
+ model = model if model else cls.model
+ if proxy and "://" not in proxy:
+ proxy = f"http://{proxy}"
+ if not cookies:
+ cookies = get_cookies(".huggingface.co")
+
+ headers = {
+ 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36',
+ }
+ async with ClientSession(
+ cookies=cookies,
+ headers=headers
+ ) as session:
+ async with session.post(f"{cls.url}/conversation", json={"model": model}, proxy=proxy) as response:
+ conversation_id = (await response.json())["conversationId"]
+
+ send = {
+ "id": str(uuid.uuid4()),
+ "inputs": format_prompt(messages),
+ "is_retry": False,
+ "response_id": str(uuid.uuid4()),
+ "web_search": False
+ }
+ async with session.post(f"{cls.url}/conversation/{conversation_id}", json=send, proxy=proxy) as response:
+ async for line in response.content:
+ line = json.loads(line[:-1])
+ if "type" not in line:
+ raise RuntimeError(f"Response: {line}")
+ elif line["type"] == "stream":
+ yield line["token"]
+ elif line["type"] == "finalAnswer":
+ break
+
+ async with session.delete(f"{cls.url}/conversation/{conversation_id}", proxy=proxy) as response:
+ response.raise_for_status()
+
+
+ @classmethod
+ @property
+ def params(cls):
+ params = [
+ ("model", "str"),
+ ("messages", "list[dict[str, str]]"),
+ ("stream", "bool"),
+ ("proxy", "str"),
+ ]
+ param = ", ".join([": ".join(p) for p in params])
+ return f"g4f.provider.{cls.__name__} supports: ({param})"
diff --git a/g4f/Provider/OpenAssistant.py b/g4f/Provider/needs_auth/OpenAssistant.py
index 1e9a0661..3b0e0424 100644
--- a/g4f/Provider/OpenAssistant.py
+++ b/g4f/Provider/needs_auth/OpenAssistant.py
@@ -4,14 +4,14 @@ import json
from aiohttp import ClientSession
-from ..typing import Any, AsyncGenerator
-from .base_provider import AsyncGeneratorProvider, format_prompt, get_cookies
+from ...typing import Any, AsyncGenerator
+from ..base_provider import AsyncGeneratorProvider, format_prompt, get_cookies
class OpenAssistant(AsyncGeneratorProvider):
url = "https://open-assistant.io/chat"
needs_auth = True
- working = True
+ working = False
model = "OA_SFT_Llama_30B_6"
@classmethod
diff --git a/g4f/Provider/needs_auth/OpenaiChat.py b/g4f/Provider/needs_auth/OpenaiChat.py
new file mode 100644
index 00000000..c41909e3
--- /dev/null
+++ b/g4f/Provider/needs_auth/OpenaiChat.py
@@ -0,0 +1,125 @@
+from __future__ import annotations
+
+import uuid, json, time
+
+from ..base_provider import AsyncGeneratorProvider
+from ..helper import get_browser, get_cookies, format_prompt
+from ...typing import AsyncGenerator
+from ...requests import StreamSession
+
+class OpenaiChat(AsyncGeneratorProvider):
+ url = "https://chat.openai.com"
+ needs_auth = True
+ working = True
+ supports_gpt_35_turbo = True
+ _access_token = None
+
+ @classmethod
+ async def create_async_generator(
+ cls,
+ model: str,
+ messages: list[dict[str, str]],
+ proxy: str = None,
+ access_token: str = None,
+ cookies: dict = None,
+ timeout: int = 30,
+ **kwargs: dict
+ ) -> AsyncGenerator:
+ proxies = {"https": proxy}
+ if not access_token:
+ access_token = await cls.get_access_token(cookies, proxies)
+ headers = {
+ "Accept": "text/event-stream",
+ "Authorization": f"Bearer {access_token}",
+ }
+ async with StreamSession(proxies=proxies, headers=headers, impersonate="chrome107", timeout=timeout) as session:
+ messages = [
+ {
+ "id": str(uuid.uuid4()),
+ "author": {"role": "user"},
+ "content": {"content_type": "text", "parts": [format_prompt(messages)]},
+ },
+ ]
+ data = {
+ "action": "next",
+ "messages": messages,
+ "conversation_id": None,
+ "parent_message_id": str(uuid.uuid4()),
+ "model": "text-davinci-002-render-sha",
+ "history_and_training_disabled": True,
+ }
+ async with session.post(f"{cls.url}/backend-api/conversation", json=data) as response:
+ response.raise_for_status()
+ last_message = ""
+ async for line in response.iter_lines():
+ if line.startswith(b"data: "):
+ line = line[6:]
+ if line == b"[DONE]":
+ break
+ try:
+ line = json.loads(line)
+ except:
+ continue
+ if "message" not in line or "message_type" not in line["message"]["metadata"]:
+ continue
+ if line["message"]["metadata"]["message_type"] == "next":
+ new_message = line["message"]["content"]["parts"][0]
+ yield new_message[len(last_message):]
+ last_message = new_message
+
+ @classmethod
+ def browse_access_token(cls) -> str:
+ try:
+ from selenium.webdriver.common.by import By
+ from selenium.webdriver.support.ui import WebDriverWait
+ from selenium.webdriver.support import expected_conditions as EC
+
+ driver = get_browser()
+ except ImportError:
+ return
+
+ driver.get(f"{cls.url}/")
+ try:
+ WebDriverWait(driver, 1200).until(
+ EC.presence_of_element_located((By.ID, "prompt-textarea"))
+ )
+ javascript = "return (await (await fetch('/api/auth/session')).json())['accessToken']"
+ return driver.execute_script(javascript)
+ finally:
+ time.sleep(1)
+ driver.quit()
+
+ @classmethod
+ async def fetch_access_token(cls, cookies: dict, proxies: dict = None) -> str:
+ async with StreamSession(proxies=proxies, cookies=cookies, impersonate="chrome107") as session:
+ async with session.get(f"{cls.url}/api/auth/session") as response:
+ response.raise_for_status()
+ auth = await response.json()
+ if "accessToken" in auth:
+ return auth["accessToken"]
+
+ @classmethod
+ async def get_access_token(cls, cookies: dict = None, proxies: dict = None) -> str:
+ if not cls._access_token:
+ cookies = cookies if cookies else get_cookies("chat.openai.com")
+ if cookies:
+ cls._access_token = await cls.fetch_access_token(cookies, proxies)
+ if not cls._access_token:
+ cls._access_token = cls.browse_access_token()
+ if not cls._access_token:
+ raise RuntimeError("Read access token failed")
+ return cls._access_token
+
+ @classmethod
+ @property
+ def params(cls):
+ params = [
+ ("model", "str"),
+ ("messages", "list[dict[str, str]]"),
+ ("stream", "bool"),
+ ("proxy", "str"),
+ ("access_token", "str"),
+ ("cookies", "dict[str, str]")
+ ]
+ param = ", ".join([": ".join(p) for p in params])
+ return f"g4f.provider.{cls.__name__} supports: ({param})" \ No newline at end of file
diff --git a/g4f/Provider/Raycast.py b/g4f/Provider/needs_auth/Raycast.py
index 7ddc8acd..619b217b 100644
--- a/g4f/Provider/Raycast.py
+++ b/g4f/Provider/needs_auth/Raycast.py
@@ -4,8 +4,8 @@ import json
import requests
-from ..typing import Any, CreateResult
-from .base_provider import BaseProvider
+from ...typing import Any, CreateResult
+from ..base_provider import BaseProvider
class Raycast(BaseProvider):
diff --git a/g4f/Provider/Theb.py b/g4f/Provider/needs_auth/Theb.py
index 72fce3ac..c35ea592 100644
--- a/g4f/Provider/Theb.py
+++ b/g4f/Provider/needs_auth/Theb.py
@@ -5,8 +5,8 @@ import random
import requests
-from ..typing import Any, CreateResult
-from .base_provider import BaseProvider
+from ...typing import Any, CreateResult
+from ..base_provider import BaseProvider
class Theb(BaseProvider):
diff --git a/g4f/Provider/needs_auth/__init__.py b/g4f/Provider/needs_auth/__init__.py
new file mode 100644
index 00000000..815194c4
--- /dev/null
+++ b/g4f/Provider/needs_auth/__init__.py
@@ -0,0 +1,6 @@
+from .Bard import Bard
+from .Raycast import Raycast
+from .Theb import Theb
+from .HuggingChat import HuggingChat
+from .OpenaiChat import OpenaiChat
+from .OpenAssistant import OpenAssistant \ No newline at end of file