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/Bard.py92
-rw-r--r--g4f/Provider/needs_auth/HuggingChat.py74
-rw-r--r--g4f/Provider/needs_auth/OpenAssistant.py100
-rw-r--r--g4f/Provider/needs_auth/OpenaiChat.py124
-rw-r--r--g4f/Provider/needs_auth/Raycast.py72
-rw-r--r--g4f/Provider/needs_auth/Theb.py97
-rw-r--r--g4f/Provider/needs_auth/__init__.py6
7 files changed, 565 insertions, 0 deletions
diff --git a/g4f/Provider/needs_auth/Bard.py b/g4f/Provider/needs_auth/Bard.py
new file mode 100644
index 00000000..7c42b680
--- /dev/null
+++ b/g4f/Provider/needs_auth/Bard.py
@@ -0,0 +1,92 @@
+from __future__ import annotations
+
+import json
+import random
+import re
+
+from aiohttp import ClientSession
+
+from ..base_provider import AsyncProvider, format_prompt, get_cookies
+
+
+class Bard(AsyncProvider):
+ url = "https://bard.google.com"
+ needs_auth = True
+ working = True
+ _snlm0e = None
+
+ @classmethod
+ async def create_async(
+ cls,
+ model: str,
+ messages: list[dict[str, str]],
+ proxy: str = None,
+ cookies: dict = None,
+ **kwargs
+ ) -> str:
+ prompt = format_prompt(messages)
+ if proxy and "://" not in proxy:
+ proxy = f"http://{proxy}"
+ if not cookies:
+ cookies = get_cookies(".google.com")
+
+ headers = {
+ 'authority': 'bard.google.com',
+ 'origin': 'https://bard.google.com',
+ 'referer': 'https://bard.google.com/',
+ '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',
+ 'x-same-domain': '1',
+ }
+
+ async with ClientSession(
+ cookies=cookies,
+ headers=headers
+ ) as session:
+ if not cls._snlm0e:
+ async with session.get(cls.url, proxy=proxy) as response:
+ text = await response.text()
+
+ match = re.search(r'SNlM0e\":\"(.*?)\"', text)
+ if not match:
+ raise RuntimeError("No snlm0e value.")
+ cls._snlm0e = match.group(1)
+
+ params = {
+ 'bl': 'boq_assistant-bard-web-server_20230326.21_p0',
+ '_reqid': random.randint(1111, 9999),
+ 'rt': 'c'
+ }
+
+ data = {
+ 'at': cls._snlm0e,
+ 'f.req': json.dumps([None, json.dumps([[prompt]])])
+ }
+
+ intents = '.'.join([
+ 'assistant',
+ 'lamda',
+ 'BardFrontendService'
+ ])
+
+ async with session.post(
+ f'{cls.url}/_/BardChatUi/data/{intents}/StreamGenerate',
+ data=data,
+ params=params,
+ proxy=proxy
+ ) as response:
+ response = await response.text()
+ response = json.loads(response.splitlines()[3])[0][2]
+ response = json.loads(response)[4][0][1][0]
+ return response
+
+ @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/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/needs_auth/OpenAssistant.py b/g4f/Provider/needs_auth/OpenAssistant.py
new file mode 100644
index 00000000..3b0e0424
--- /dev/null
+++ b/g4f/Provider/needs_auth/OpenAssistant.py
@@ -0,0 +1,100 @@
+from __future__ import annotations
+
+import json
+
+from aiohttp import ClientSession
+
+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 = False
+ model = "OA_SFT_Llama_30B_6"
+
+ @classmethod
+ async def create_async_generator(
+ cls,
+ model: str,
+ messages: list[dict[str, str]],
+ proxy: str = None,
+ cookies: dict = None,
+ **kwargs: Any
+ ) -> AsyncGenerator:
+ if not cookies:
+ cookies = get_cookies("open-assistant.io")
+
+ 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("https://open-assistant.io/api/chat", proxy=proxy) as response:
+ chat_id = (await response.json())["id"]
+
+ data = {
+ "chat_id": chat_id,
+ "content": f"<s>[INST]\n{format_prompt(messages)}\n[/INST]",
+ "parent_id": None
+ }
+ async with session.post("https://open-assistant.io/api/chat/prompter_message", proxy=proxy, json=data) as response:
+ parent_id = (await response.json())["id"]
+
+ data = {
+ "chat_id": chat_id,
+ "parent_id": parent_id,
+ "model_config_name": model if model else cls.model,
+ "sampling_parameters":{
+ "top_k": 50,
+ "top_p": None,
+ "typical_p": None,
+ "temperature": 0.35,
+ "repetition_penalty": 1.1111111111111112,
+ "max_new_tokens": 1024,
+ **kwargs
+ },
+ "plugins":[]
+ }
+ async with session.post("https://open-assistant.io/api/chat/assistant_message", proxy=proxy, json=data) as response:
+ data = await response.json()
+ if "id" in data:
+ message_id = data["id"]
+ elif "message" in data:
+ raise RuntimeError(data["message"])
+ else:
+ response.raise_for_status()
+
+ params = {
+ 'chat_id': chat_id,
+ 'message_id': message_id,
+ }
+ async with session.post("https://open-assistant.io/api/chat/events", proxy=proxy, params=params) as response:
+ start = "data: "
+ async for line in response.content:
+ line = line.decode("utf-8")
+ if line and line.startswith(start):
+ line = json.loads(line[len(start):])
+ if line["event_type"] == "token":
+ yield line["text"]
+
+ params = {
+ 'chat_id': chat_id,
+ }
+ async with session.delete("https://open-assistant.io/api/chat", proxy=proxy, params=params) 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/needs_auth/OpenaiChat.py b/g4f/Provider/needs_auth/OpenaiChat.py
new file mode 100644
index 00000000..c57692d3
--- /dev/null
+++ b/g4f/Provider/needs_auth/OpenaiChat.py
@@ -0,0 +1,124 @@
+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,
+ **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") 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/needs_auth/Raycast.py b/g4f/Provider/needs_auth/Raycast.py
new file mode 100644
index 00000000..619b217b
--- /dev/null
+++ b/g4f/Provider/needs_auth/Raycast.py
@@ -0,0 +1,72 @@
+from __future__ import annotations
+
+import json
+
+import requests
+
+from ...typing import Any, CreateResult
+from ..base_provider import BaseProvider
+
+
+class Raycast(BaseProvider):
+ url = "https://raycast.com"
+ supports_gpt_35_turbo = True
+ supports_gpt_4 = True
+ supports_stream = True
+ needs_auth = True
+ working = True
+
+ @staticmethod
+ def create_completion(
+ model: str,
+ messages: list[dict[str, str]],
+ stream: bool,
+ **kwargs: Any,
+ ) -> CreateResult:
+ auth = kwargs.get('auth')
+ headers = {
+ 'Accept': 'application/json',
+ 'Accept-Language': 'en-US,en;q=0.9',
+ 'Authorization': f'Bearer {auth}',
+ 'Content-Type': 'application/json',
+ 'User-Agent': 'Raycast/0 CFNetwork/1410.0.3 Darwin/22.6.0',
+ }
+ parsed_messages = []
+ for message in messages:
+ parsed_messages.append({
+ 'author': message['role'],
+ 'content': {'text': message['content']}
+ })
+ data = {
+ "debug": False,
+ "locale": "en-CN",
+ "messages": parsed_messages,
+ "model": model,
+ "provider": "openai",
+ "source": "ai_chat",
+ "system_instruction": "markdown",
+ "temperature": 0.5
+ }
+ response = requests.post("https://backend.raycast.com/api/v1/ai/chat_completions", headers=headers, json=data, stream=True)
+ for token in response.iter_lines():
+ if b'data: ' not in token:
+ continue
+ completion_chunk = json.loads(token.decode().replace('data: ', ''))
+ token = completion_chunk['text']
+ if token != None:
+ yield token
+
+ @classmethod
+ @property
+ def params(cls):
+ params = [
+ ("model", "str"),
+ ("messages", "list[dict[str, str]]"),
+ ("stream", "bool"),
+ ("temperature", "float"),
+ ("top_p", "int"),
+ ("model", "str"),
+ ("auth", "str"),
+ ]
+ param = ", ".join([": ".join(p) for p in params])
+ return f"g4f.provider.{cls.__name__} supports: ({param})"
diff --git a/g4f/Provider/needs_auth/Theb.py b/g4f/Provider/needs_auth/Theb.py
new file mode 100644
index 00000000..c35ea592
--- /dev/null
+++ b/g4f/Provider/needs_auth/Theb.py
@@ -0,0 +1,97 @@
+from __future__ import annotations
+
+import json
+import random
+
+import requests
+
+from ...typing import Any, CreateResult
+from ..base_provider import BaseProvider
+
+
+class Theb(BaseProvider):
+ url = "https://theb.ai"
+ working = True
+ supports_stream = True
+ supports_gpt_35_turbo = True
+ needs_auth = True
+
+ @staticmethod
+ def create_completion(
+ model: str,
+ messages: list[dict[str, str]],
+ stream: bool, **kwargs: Any) -> CreateResult:
+
+ conversation = "\n".join(f"{message['role']}: {message['content']}" for message in messages)
+ conversation += "\nassistant: "
+
+ auth = kwargs.get("auth", {
+ "bearer_token":"free",
+ "org_id":"theb",
+ })
+
+ bearer_token = auth["bearer_token"]
+ org_id = auth["org_id"]
+
+ headers = {
+ 'authority' : 'beta.theb.ai',
+ 'accept' : 'text/event-stream',
+ 'accept-language' : 'id-ID,id;q=0.9,en-US;q=0.8,en;q=0.7',
+ 'authorization' : 'Bearer '+bearer_token,
+ 'content-type' : 'application/json',
+ 'origin' : 'https://beta.theb.ai',
+ 'referer' : 'https://beta.theb.ai/home',
+ 'sec-ch-ua' : '"Chromium";v="116", "Not)A;Brand";v="24", "Google Chrome";v="116"',
+ 'sec-ch-ua-mobile' : '?0',
+ 'sec-ch-ua-platform': '"Windows"',
+ 'sec-fetch-dest' : 'empty',
+ 'sec-fetch-mode' : 'cors',
+ 'sec-fetch-site' : 'same-origin',
+ 'user-agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36',
+ 'x-ai-model' : 'ee8d4f29cb7047f78cbe84313ed6ace8',
+ }
+
+ req_rand = random.randint(100000000, 9999999999)
+
+ json_data: dict[str, Any] = {
+ "text" : conversation,
+ "category" : "04f58f64a4aa4191a957b47290fee864",
+ "model" : "ee8d4f29cb7047f78cbe84313ed6ace8",
+ "model_params": {
+ "system_prompt" : "You are ChatGPT, a large language model trained by OpenAI, based on the GPT-3.5 architecture.\nKnowledge cutoff: 2021-09\nCurrent date: {{YYYY-MM-DD}}",
+ "temperature" : kwargs.get("temperature", 1),
+ "top_p" : kwargs.get("top_p", 1),
+ "frequency_penalty" : kwargs.get("frequency_penalty", 0),
+ "presence_penalty" : kwargs.get("presence_penalty", 0),
+ "long_term_memory" : "auto"
+ }
+ }
+
+ response = requests.post(f"https://beta.theb.ai/api/conversation?org_id={org_id}&req_rand={req_rand}",
+ headers=headers, json=json_data, stream=True)
+
+ response.raise_for_status()
+ content = ""
+ next_content = ""
+ for chunk in response.iter_lines():
+ if b"content" in chunk:
+ next_content = content
+ data = json.loads(chunk.decode().split("data: ")[1])
+ content = data["content"]
+ yield data["content"].replace(next_content, "")
+
+ @classmethod
+ @property
+ def params(cls):
+ params = [
+ ("model", "str"),
+ ("messages", "list[dict[str, str]]"),
+ ("auth", "list[dict[str, str]]"),
+ ("stream", "bool"),
+ ("temperature", "float"),
+ ("presence_penalty", "int"),
+ ("frequency_penalty", "int"),
+ ("top_p", "int")
+ ]
+ 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/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