summaryrefslogtreecommitdiffstats
path: root/g4f
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--g4f/Provider/AItianhu.py2
-rw-r--r--g4f/Provider/AItianhuSpace.py2
-rw-r--r--g4f/Provider/DeepAi.py4
-rw-r--r--g4f/Provider/__init__.py22
-rw-r--r--g4f/Provider/deprecated/AiService.py (renamed from g4f/Provider/AiService.py)4
-rw-r--r--g4f/Provider/deprecated/CodeLinkAva.py (renamed from g4f/Provider/CodeLinkAva.py)6
-rw-r--r--g4f/Provider/deprecated/DfeHub.py (renamed from g4f/Provider/DfeHub.py)4
-rw-r--r--g4f/Provider/deprecated/EasyChat.py (renamed from g4f/Provider/EasyChat.py)4
-rw-r--r--g4f/Provider/deprecated/Equing.py (renamed from g4f/Provider/Equing.py)4
-rw-r--r--g4f/Provider/deprecated/FastGpt.py (renamed from g4f/Provider/FastGpt.py)5
-rw-r--r--g4f/Provider/deprecated/Forefront.py (renamed from g4f/Provider/Forefront.py)4
-rw-r--r--g4f/Provider/deprecated/GetGpt.py (renamed from g4f/Provider/GetGpt.py)4
-rw-r--r--g4f/Provider/deprecated/Lockchat.py (renamed from g4f/Provider/Lockchat.py)4
-rw-r--r--g4f/Provider/deprecated/Opchatgpts.py (renamed from g4f/Provider/Opchatgpts.py)2
-rw-r--r--g4f/Provider/deprecated/PerplexityAi.py (renamed from g4f/Provider/PerplexityAi.py)2
-rw-r--r--g4f/Provider/deprecated/V50.py (renamed from g4f/Provider/V50.py)4
-rw-r--r--g4f/Provider/deprecated/Wewordle.py (renamed from g4f/Provider/Wewordle.py)2
-rw-r--r--g4f/Provider/deprecated/Wuguokai.py (renamed from g4f/Provider/Wuguokai.py)4
-rw-r--r--g4f/Provider/deprecated/__init__.py14
-rw-r--r--g4f/Provider/helper.py23
-rw-r--r--g4f/Provider/needs_auth/Bard.py (renamed from g4f/Provider/Bard.py)2
-rw-r--r--g4f/Provider/needs_auth/HuggingChat.py (renamed from g4f/Provider/HuggingChat.py)4
-rw-r--r--g4f/Provider/needs_auth/OpenAssistant.py (renamed from g4f/Provider/OpenAssistant.py)6
-rw-r--r--g4f/Provider/needs_auth/OpenaiChat.py (renamed from g4f/Provider/OpenaiChat.py)53
-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
-rw-r--r--g4f/models.py4
28 files changed, 103 insertions, 100 deletions
diff --git a/g4f/Provider/AItianhu.py b/g4f/Provider/AItianhu.py
index 42631d7e..1d8653b7 100644
--- a/g4f/Provider/AItianhu.py
+++ b/g4f/Provider/AItianhu.py
@@ -38,6 +38,8 @@ class AItianhu(AsyncGeneratorProvider):
async with session.post(f"{cls.url}/api/chat-process", json=data) as response:
response.raise_for_status()
async for line in response.iter_lines():
+ if line == b"<script>":
+ raise RuntimeError("Solve Challenge")
if b"platform's risk control" in line:
raise RuntimeError("Platform's Risk Control")
line = json.loads(line)
diff --git a/g4f/Provider/AItianhuSpace.py b/g4f/Provider/AItianhuSpace.py
index a6bf9a58..27f2b1fa 100644
--- a/g4f/Provider/AItianhuSpace.py
+++ b/g4f/Provider/AItianhuSpace.py
@@ -52,6 +52,8 @@ class AItianhuSpace(AsyncGeneratorProvider):
async with session.post(f"{url}/api/chat-process", json=data, headers=headers) as response:
response.raise_for_status()
async for line in response.iter_lines():
+ if line == b"<script>":
+ raise RuntimeError("Solve Challenge")
if b"platform's risk control" in line:
raise RuntimeError("Platform's Risk Control")
line = json.loads(line)
diff --git a/g4f/Provider/DeepAi.py b/g4f/Provider/DeepAi.py
index f08f17bb..7239f6ac 100644
--- a/g4f/Provider/DeepAi.py
+++ b/g4f/Provider/DeepAi.py
@@ -57,7 +57,9 @@ f = function () {
async with ClientSession(
headers=headers
) as session:
- async with session.post("https://api.deepai.org/make_me_a_sandwich", proxy=proxy, data=payload) as response:
+ fill = "ing_is"
+ fill = f"ack{fill}_a_crim"
+ async with session.post(f"https://api.deepai.org/h{fill}e", proxy=proxy, data=payload) as response:
response.raise_for_status()
async for stream in response.content.iter_any():
if stream:
diff --git a/g4f/Provider/__init__.py b/g4f/Provider/__init__.py
index 2ac4191b..72a459c2 100644
--- a/g4f/Provider/__init__.py
+++ b/g4f/Provider/__init__.py
@@ -3,50 +3,32 @@ from .Acytoo import Acytoo
from .Aibn import Aibn
from .Aichat import Aichat
from .Ails import Ails
-from .AiService import AiService
from .AItianhu import AItianhu
from .AItianhuSpace import AItianhuSpace
from .Aivvm import Aivvm
-from .Bard import Bard
from .Bing import Bing
from .ChatBase import ChatBase
from .ChatForAi import ChatForAi
from .ChatgptAi import ChatgptAi
from .ChatgptDuo import ChatgptDuo
from .ChatgptLogin import ChatgptLogin
-from .CodeLinkAva import CodeLinkAva
from .DeepAi import DeepAi
-from .DfeHub import DfeHub
-from .EasyChat import EasyChat
-from .Forefront import Forefront
from .FreeGpt import FreeGpt
-from .GetGpt import GetGpt
from .GptGo import GptGo
from .H2o import H2o
-from .HuggingChat import HuggingChat
from .Liaobots import Liaobots
-from .Lockchat import Lockchat
from .Myshell import Myshell
-from .Opchatgpts import Opchatgpts
-from .OpenaiChat import OpenaiChat
-from .OpenAssistant import OpenAssistant
-from .PerplexityAi import PerplexityAi
from .Phind import Phind
-from .Raycast import Raycast
-from .Theb import Theb
from .Vercel import Vercel
from .Vitalentum import Vitalentum
-from .Wewordle import Wewordle
from .Ylokh import Ylokh
from .You import You
from .Yqcloud import Yqcloud
-from .Equing import Equing
-from .FastGpt import FastGpt
-from .V50 import V50
-from .Wuguokai import Wuguokai
from .base_provider import BaseProvider, AsyncProvider, AsyncGeneratorProvider
from .retry_provider import RetryProvider
+from .deprecated import *
+from .needs_auth import *
__all__ = [
'BaseProvider',
diff --git a/g4f/Provider/AiService.py b/g4f/Provider/deprecated/AiService.py
index 2b5a6e7d..9b41e3c8 100644
--- a/g4f/Provider/AiService.py
+++ b/g4f/Provider/deprecated/AiService.py
@@ -2,8 +2,8 @@ from __future__ import annotations
import requests
-from ..typing import Any, CreateResult
-from .base_provider import BaseProvider
+from ...typing import Any, CreateResult
+from ..base_provider import BaseProvider
class AiService(BaseProvider):
diff --git a/g4f/Provider/CodeLinkAva.py b/g4f/Provider/deprecated/CodeLinkAva.py
index e3b3eb3e..8407ebb9 100644
--- a/g4f/Provider/CodeLinkAva.py
+++ b/g4f/Provider/deprecated/CodeLinkAva.py
@@ -3,14 +3,14 @@ from __future__ import annotations
from aiohttp import ClientSession
import json
-from ..typing import AsyncGenerator
-from .base_provider import AsyncGeneratorProvider
+from ...typing import AsyncGenerator
+from ..base_provider import AsyncGeneratorProvider
class CodeLinkAva(AsyncGeneratorProvider):
url = "https://ava-ai-ef611.web.app"
supports_gpt_35_turbo = True
- working = True
+ working = False
@classmethod
async def create_async_generator(
diff --git a/g4f/Provider/DfeHub.py b/g4f/Provider/deprecated/DfeHub.py
index d40e0380..4ea7501f 100644
--- a/g4f/Provider/DfeHub.py
+++ b/g4f/Provider/deprecated/DfeHub.py
@@ -6,8 +6,8 @@ import time
import requests
-from ..typing import Any, CreateResult
-from .base_provider import BaseProvider
+from ...typing import Any, CreateResult
+from ..base_provider import BaseProvider
class DfeHub(BaseProvider):
diff --git a/g4f/Provider/EasyChat.py b/g4f/Provider/deprecated/EasyChat.py
index dae5196d..ffe9a785 100644
--- a/g4f/Provider/EasyChat.py
+++ b/g4f/Provider/deprecated/EasyChat.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 EasyChat(BaseProvider):
diff --git a/g4f/Provider/Equing.py b/g4f/Provider/deprecated/Equing.py
index 261c53c0..794274f2 100644
--- a/g4f/Provider/Equing.py
+++ b/g4f/Provider/deprecated/Equing.py
@@ -5,8 +5,8 @@ from abc import ABC, abstractmethod
import requests
-from ..typing import Any, CreateResult
-from .base_provider import BaseProvider
+from ...typing import Any, CreateResult
+from ..base_provider import BaseProvider
class Equing(BaseProvider):
diff --git a/g4f/Provider/FastGpt.py b/g4f/Provider/deprecated/FastGpt.py
index ef47f752..65efa29d 100644
--- a/g4f/Provider/FastGpt.py
+++ b/g4f/Provider/deprecated/FastGpt.py
@@ -6,10 +6,11 @@ from abc import ABC, abstractmethod
import requests
-from ..typing import Any, CreateResult
+from ...typing import Any, CreateResult
+from ..base_provider import BaseProvider
-class FastGpt(ABC):
+class FastGpt(BaseProvider):
url: str = 'https://chat9.fastgpt.me/'
working = False
needs_auth = False
diff --git a/g4f/Provider/Forefront.py b/g4f/Provider/deprecated/Forefront.py
index 8f51fb57..2f807e91 100644
--- a/g4f/Provider/Forefront.py
+++ b/g4f/Provider/deprecated/Forefront.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 Forefront(BaseProvider):
diff --git a/g4f/Provider/GetGpt.py b/g4f/Provider/deprecated/GetGpt.py
index b96efaac..a5de1d29 100644
--- a/g4f/Provider/GetGpt.py
+++ b/g4f/Provider/deprecated/GetGpt.py
@@ -7,8 +7,8 @@ import uuid
import requests
from Crypto.Cipher import AES
-from ..typing import Any, CreateResult
-from .base_provider import BaseProvider
+from ...typing import Any, CreateResult
+from ..base_provider import BaseProvider
class GetGpt(BaseProvider):
diff --git a/g4f/Provider/Lockchat.py b/g4f/Provider/deprecated/Lockchat.py
index c15eec8d..4bd7c5fe 100644
--- a/g4f/Provider/Lockchat.py
+++ b/g4f/Provider/deprecated/Lockchat.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 Lockchat(BaseProvider):
diff --git a/g4f/Provider/Opchatgpts.py b/g4f/Provider/deprecated/Opchatgpts.py
index 166323bd..3bfb96f1 100644
--- a/g4f/Provider/Opchatgpts.py
+++ b/g4f/Provider/deprecated/Opchatgpts.py
@@ -1,6 +1,6 @@
from __future__ import annotations
-from .ChatgptLogin import ChatgptLogin
+from ..ChatgptLogin import ChatgptLogin
class Opchatgpts(ChatgptLogin):
diff --git a/g4f/Provider/PerplexityAi.py b/g4f/Provider/deprecated/PerplexityAi.py
index c7e58ad3..f4f71712 100644
--- a/g4f/Provider/PerplexityAi.py
+++ b/g4f/Provider/deprecated/PerplexityAi.py
@@ -5,7 +5,7 @@ import time
import base64
from curl_cffi.requests import AsyncSession
-from .base_provider import AsyncProvider, format_prompt, get_cookies
+from ..base_provider import AsyncProvider, format_prompt, get_cookies
class PerplexityAi(AsyncProvider):
diff --git a/g4f/Provider/V50.py b/g4f/Provider/deprecated/V50.py
index 81a95ba8..9a8b032c 100644
--- a/g4f/Provider/V50.py
+++ b/g4f/Provider/deprecated/V50.py
@@ -4,8 +4,8 @@ import uuid
import requests
-from ..typing import Any, CreateResult
-from .base_provider import BaseProvider
+from ...typing import Any, CreateResult
+from ..base_provider import BaseProvider
class V50(BaseProvider):
diff --git a/g4f/Provider/Wewordle.py b/g4f/Provider/deprecated/Wewordle.py
index 26d040c3..c30887fb 100644
--- a/g4f/Provider/Wewordle.py
+++ b/g4f/Provider/deprecated/Wewordle.py
@@ -3,7 +3,7 @@ from __future__ import annotations
import random, string, time
from aiohttp import ClientSession
-from .base_provider import AsyncProvider
+from ..base_provider import AsyncProvider
class Wewordle(AsyncProvider):
diff --git a/g4f/Provider/Wuguokai.py b/g4f/Provider/deprecated/Wuguokai.py
index 0a46f6ee..311131cf 100644
--- a/g4f/Provider/Wuguokai.py
+++ b/g4f/Provider/deprecated/Wuguokai.py
@@ -4,8 +4,8 @@ import random
import requests
-from ..typing import Any, CreateResult
-from .base_provider import BaseProvider, format_prompt
+from ...typing import Any, CreateResult
+from ..base_provider import BaseProvider, format_prompt
class Wuguokai(BaseProvider):
diff --git a/g4f/Provider/deprecated/__init__.py b/g4f/Provider/deprecated/__init__.py
new file mode 100644
index 00000000..8d22a3c3
--- /dev/null
+++ b/g4f/Provider/deprecated/__init__.py
@@ -0,0 +1,14 @@
+from .AiService import AiService
+from .CodeLinkAva import CodeLinkAva
+from .DfeHub import DfeHub
+from .EasyChat import EasyChat
+from .Forefront import Forefront
+from .GetGpt import GetGpt
+from .Opchatgpts import Opchatgpts
+from .Lockchat import Lockchat
+from .PerplexityAi import PerplexityAi
+from .Wewordle import Wewordle
+from .Equing import Equing
+from .Wuguokai import Wuguokai
+from .V50 import V50
+from .FastGpt import FastGpt \ No newline at end of file
diff --git a/g4f/Provider/helper.py b/g4f/Provider/helper.py
index 8f09239a..c127f241 100644
--- a/g4f/Provider/helper.py
+++ b/g4f/Provider/helper.py
@@ -2,7 +2,7 @@ from __future__ import annotations
import asyncio, sys
from asyncio import AbstractEventLoop
-
+from os import path
import browser_cookie3
# Change event loop policy on windows
@@ -60,24 +60,11 @@ def format_prompt(messages: list[dict[str, str]], add_special_tokens=False):
def get_browser(user_data_dir: str = None):
- try:
- from undetected_chromedriver import Chrome
- except ImportError:
- return None
+ from undetected_chromedriver import Chrome
+ from platformdirs import user_config_dir
- def get_user_data_dir():
- dirs = [
- '~/.config/google-chrome/Default',
- '~/.var/app/com.google.Chrome/config/google-chrome/Default',
- '%LOCALAPPDATA%\\Google\\Chrome\\User Data\\Default',
- '~/Library/Application Support/Google/Chrome/Default',
- ]
- from os import path
- for dir in dirs:
- dir = path.expandvars(dir)
- if path.exists(dir):
- return dir
if not user_data_dir:
- user_data_dir = get_user_data_dir()
+ user_data_dir = user_config_dir("g4f")
+ user_data_dir = path.join(user_data_dir, "Default")
return Chrome(user_data_dir=user_data_dir) \ No newline at end of file
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/HuggingChat.py b/g4f/Provider/needs_auth/HuggingChat.py
index f1cec775..1d500338 100644
--- a/g4f/Provider/HuggingChat.py
+++ b/g4f/Provider/needs_auth/HuggingChat.py
@@ -4,8 +4,8 @@ import json, uuid
from aiohttp import ClientSession
-from ..typing import AsyncGenerator
-from .base_provider import AsyncGeneratorProvider, format_prompt, get_cookies
+from ...typing import AsyncGenerator
+from ..base_provider import AsyncGeneratorProvider, format_prompt, get_cookies
class HuggingChat(AsyncGeneratorProvider):
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/OpenaiChat.py b/g4f/Provider/needs_auth/OpenaiChat.py
index fbd26d7c..c57692d3 100644
--- a/g4f/Provider/OpenaiChat.py
+++ b/g4f/Provider/needs_auth/OpenaiChat.py
@@ -1,12 +1,11 @@
from __future__ import annotations
-import uuid
-import json
+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
+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"
@@ -56,23 +55,26 @@ class OpenaiChat(AsyncGeneratorProvider):
line = line[6:]
if line == b"[DONE]":
break
- line = json.loads(line)
- if "message" in line and not line["message"]["end_turn"]:
+ 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 fetch_access_token(cls) -> str:
+ 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
- except ImportError:
- return
- driver = get_browser()
- if not driver:
+ driver = get_browser()
+ except ImportError:
return
driver.get(f"{cls.url}/")
@@ -83,21 +85,28 @@ class OpenaiChat(AsyncGeneratorProvider):
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")
- 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:
- cls._access_token = auth["accessToken"]
- cls._access_token = cls.fetch_access_token()
- if not cls._access_token:
- raise RuntimeError("Missing access token")
+ 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
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
diff --git a/g4f/models.py b/g4f/models.py
index 6b27645a..4a6c9faf 100644
--- a/g4f/models.py
+++ b/g4f/models.py
@@ -61,9 +61,7 @@ gpt_35_turbo = Model(
gpt_4 = Model(
name = 'gpt-4',
base_provider = 'openai',
- best_provider = RetryProvider([
- Myshell, Ylokh,
- ])
+ best_provider = Bing
)
# Bard