From 4fa6e9c0f597c07d2acf10732fe2aeda270c6ca6 Mon Sep 17 00:00:00 2001 From: Heiner Lohaus Date: Sat, 7 Oct 2023 09:02:48 +0200 Subject: Add GptGod Provider Remove timeout from aiohttp providers Disable Opchatgpts and ChatgptLogin provider --- etc/testing/test_async.py | 1 + g4f/Provider/AiAsk.py | 5 +-- g4f/Provider/Aichat.py | 5 +-- g4f/Provider/ChatgptAi.py | 5 +-- g4f/Provider/ChatgptDemo.py | 5 +-- g4f/Provider/ChatgptLogin.py | 74 --------------------------------- g4f/Provider/GptGo.py | 4 +- g4f/Provider/GptGod.py | 51 +++++++++++++++++++++++ g4f/Provider/Liaobots.py | 5 +-- g4f/Provider/Vitalentum.py | 5 +-- g4f/Provider/Yqcloud.py | 3 +- g4f/Provider/__init__.py | 3 +- g4f/Provider/deprecated/ChatgptLogin.py | 74 +++++++++++++++++++++++++++++++++ g4f/Provider/deprecated/Opchatgpts.py | 5 +-- g4f/Provider/deprecated/__init__.py | 3 +- g4f/__init__.py | 1 - 16 files changed, 147 insertions(+), 102 deletions(-) delete mode 100644 g4f/Provider/ChatgptLogin.py create mode 100644 g4f/Provider/GptGod.py create mode 100644 g4f/Provider/deprecated/ChatgptLogin.py diff --git a/etc/testing/test_async.py b/etc/testing/test_async.py index 76b109b1..2c15f6b0 100644 --- a/etc/testing/test_async.py +++ b/etc/testing/test_async.py @@ -3,6 +3,7 @@ from pathlib import Path import asyncio sys.path.append(str(Path(__file__).parent.parent)) +sys.path.append(str(Path(__file__).parent.parent.parent)) import g4f from testing.test_providers import get_providers diff --git a/g4f/Provider/AiAsk.py b/g4f/Provider/AiAsk.py index 870dd1ab..27d3bf15 100644 --- a/g4f/Provider/AiAsk.py +++ b/g4f/Provider/AiAsk.py @@ -1,6 +1,6 @@ from __future__ import annotations -from aiohttp import ClientSession, ClientTimeout +from aiohttp import ClientSession from ..typing import AsyncGenerator from .base_provider import AsyncGeneratorProvider @@ -14,7 +14,6 @@ class AiAsk(AsyncGeneratorProvider): cls, model: str, messages: list[dict[str, str]], - timeout: int = 30, **kwargs ) -> AsyncGenerator: headers = { @@ -22,7 +21,7 @@ class AiAsk(AsyncGeneratorProvider): "origin": cls.url, "referer": f"{cls.url}/chat", } - async with ClientSession(headers=headers, timeout=ClientTimeout(timeout)) as session: + async with ClientSession(headers=headers) as session: data = { "continuous": True, "id": "fRMSQtuHl91A4De9cCvKD", diff --git a/g4f/Provider/Aichat.py b/g4f/Provider/Aichat.py index 2e141b53..8edd17e2 100644 --- a/g4f/Provider/Aichat.py +++ b/g4f/Provider/Aichat.py @@ -1,6 +1,6 @@ from __future__ import annotations -from aiohttp import ClientSession, ClientTimeout +from aiohttp import ClientSession from .base_provider import AsyncProvider, format_prompt @@ -15,7 +15,6 @@ class Aichat(AsyncProvider): model: str, messages: list[dict[str, str]], proxy: str = None, - timeout: int = 30, **kwargs ) -> str: headers = { @@ -34,7 +33,7 @@ class Aichat(AsyncProvider): "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36", } async with ClientSession( - headers=headers, timeout=ClientTimeout(timeout) + headers=headers ) as session: json_data = { "message": format_prompt(messages), diff --git a/g4f/Provider/ChatgptAi.py b/g4f/Provider/ChatgptAi.py index 553a9860..996f99a5 100644 --- a/g4f/Provider/ChatgptAi.py +++ b/g4f/Provider/ChatgptAi.py @@ -1,7 +1,7 @@ from __future__ import annotations import re -from aiohttp import ClientSession, ClientTimeout +from aiohttp import ClientSession from .base_provider import AsyncProvider, format_prompt @@ -20,7 +20,6 @@ class ChatgptAi(AsyncProvider): model: str, messages: list[dict[str, str]], proxy: str = None, - timeout: int = 30, **kwargs ) -> str: headers = { @@ -40,7 +39,7 @@ class ChatgptAi(AsyncProvider): "user-agent" : "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36", } async with ClientSession( - headers=headers, timeout=ClientTimeout(timeout) + headers=headers ) as session: if not cls._nonce: async with session.get(cls.url, proxy=proxy) as response: diff --git a/g4f/Provider/ChatgptDemo.py b/g4f/Provider/ChatgptDemo.py index c9d2894a..95cb9ecf 100644 --- a/g4f/Provider/ChatgptDemo.py +++ b/g4f/Provider/ChatgptDemo.py @@ -1,7 +1,7 @@ from __future__ import annotations import time, json, re -from aiohttp import ClientSession, ClientTimeout +from aiohttp import ClientSession from typing import AsyncGenerator from .base_provider import AsyncGeneratorProvider @@ -18,7 +18,6 @@ class ChatgptDemo(AsyncGeneratorProvider): model: str, messages: list[dict[str, str]], proxy: str = None, - timeout: int = 30, **kwargs ) -> AsyncGenerator: headers = { @@ -34,7 +33,7 @@ class ChatgptDemo(AsyncGeneratorProvider): "sec-fetch-site": "same-origin", "user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36" } - async with ClientSession(headers=headers, timeout=ClientTimeout(timeout)) as session: + async with ClientSession(headers=headers) as session: async with session.get(f"{cls.url}/", proxy=proxy) as response: response.raise_for_status() response = await response.text() diff --git a/g4f/Provider/ChatgptLogin.py b/g4f/Provider/ChatgptLogin.py deleted file mode 100644 index 3eb55a64..00000000 --- a/g4f/Provider/ChatgptLogin.py +++ /dev/null @@ -1,74 +0,0 @@ -from __future__ import annotations - -import os, re -from aiohttp import ClientSession - -from .base_provider import AsyncProvider, format_prompt - - -class ChatgptLogin(AsyncProvider): - url = "https://opchatgpts.net" - supports_gpt_35_turbo = True - working = True - _nonce = None - - @classmethod - async def create_async( - cls, - model: str, - messages: list[dict[str, str]], - **kwargs - ) -> str: - headers = { - "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", - "Accept" : "*/*", - "Accept-language" : "en,fr-FR;q=0.9,fr;q=0.8,es-ES;q=0.7,es;q=0.6,en-US;q=0.5,am;q=0.4,de;q=0.3", - "Origin" : "https://opchatgpts.net", - "Alt-Used" : "opchatgpts.net", - "Referer" : "https://opchatgpts.net/chatgpt-free-use/", - "Sec-Fetch-Dest" : "empty", - "Sec-Fetch-Mode" : "cors", - "Sec-Fetch-Site" : "same-origin", - } - async with ClientSession( - headers=headers - ) as session: - if not cls._nonce: - async with session.get( - "https://opchatgpts.net/chatgpt-free-use/", - params={"id": os.urandom(6).hex()}, - ) as response: - result = re.search(r'data-nonce="(.*?)"', await response.text()) - if not result: - raise RuntimeError("No nonce value") - cls._nonce = result.group(1) - data = { - "_wpnonce": cls._nonce, - "post_id": 28, - "url": "https://opchatgpts.net/chatgpt-free-use", - "action": "wpaicg_chat_shortcode_message", - "message": format_prompt(messages), - "bot_id": 0 - } - async with session.post("https://opchatgpts.net/wp-admin/admin-ajax.php", data=data) as response: - response.raise_for_status() - data = await response.json() - if "data" in data: - return data["data"] - elif "msg" in data: - raise RuntimeError(data["msg"]) - else: - raise RuntimeError(f"Response: {data}") - - - @classmethod - @property - def params(cls): - params = [ - ("model", "str"), - ("messages", "list[dict[str, str]]"), - ("stream", "bool"), - ("temperature", "float"), - ] - 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/GptGo.py b/g4f/Provider/GptGo.py index be62250c..51764221 100644 --- a/g4f/Provider/GptGo.py +++ b/g4f/Provider/GptGo.py @@ -1,6 +1,6 @@ from __future__ import annotations -from aiohttp import ClientSession, ClientTimeout +from aiohttp import ClientSession import json from ..typing import AsyncGenerator @@ -32,7 +32,7 @@ class GptGo(AsyncGeneratorProvider): "Sec-Fetch-Site" : "same-origin", } async with ClientSession( - headers=headers, timeout=ClientTimeout(timeout) + headers=headers ) as session: async with session.get( "https://gptgo.ai/action_get_token.php", diff --git a/g4f/Provider/GptGod.py b/g4f/Provider/GptGod.py new file mode 100644 index 00000000..662884dd --- /dev/null +++ b/g4f/Provider/GptGod.py @@ -0,0 +1,51 @@ +from __future__ import annotations +import secrets, json +from aiohttp import ClientSession +from typing import AsyncGenerator +from .base_provider import AsyncGeneratorProvider +from .helper import format_prompt + +class GptGod(AsyncGeneratorProvider): + url = "https://gptgod.site" + supports_gpt_35_turbo = True + working = True + + @classmethod + async def create_async_generator( + cls, + model: str, + messages: list[dict[str, str]], + **kwargs + ) -> AsyncGenerator: + headers = { + "User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/118.0", + "Accept": "text/event-stream", + "Accept-Language": "de,en-US;q=0.7,en;q=0.3", + "Accept-Encoding": "gzip, deflate, br", + "Alt-Used": "gptgod.site", + "Connection": "keep-alive", + "Referer": "https://gptgod.site/", + "Sec-Fetch-Dest": "empty", + "Sec-Fetch-Mode": "cors", + "Sec-Fetch-Site": "same-origin", + "Pragma": "no-cache", + "Cache-Control": "no-cache", + } + async with ClientSession(headers=headers) as session: + prompt = format_prompt(messages) + data = { + "content": prompt, + "id": secrets.token_hex(16).zfill(32) + } + async with session.get(f"{cls.url}/api/session/free/gpt3p5", params=data) as response: + response.raise_for_status() + event = None + async for line in response.content: + if line.startswith(b'event: '): + event = line[7:-1] + elif event == b"data" and line.startswith(b"data: "): + data = json.loads(line[6:-1]) + if data: + yield data + elif event == b"done": + break \ No newline at end of file diff --git a/g4f/Provider/Liaobots.py b/g4f/Provider/Liaobots.py index 2e0154d5..2ab96ce3 100644 --- a/g4f/Provider/Liaobots.py +++ b/g4f/Provider/Liaobots.py @@ -2,7 +2,7 @@ from __future__ import annotations import uuid -from aiohttp import ClientSession, ClientTimeout +from aiohttp import ClientSession from ..typing import AsyncGenerator from .base_provider import AsyncGeneratorProvider @@ -42,7 +42,6 @@ class Liaobots(AsyncGeneratorProvider): messages: list[dict[str, str]], auth: str = None, proxy: str = None, - timeout: int = 30, **kwargs ) -> AsyncGenerator: model = model if model in models else "gpt-3.5-turbo" @@ -54,7 +53,7 @@ class Liaobots(AsyncGeneratorProvider): "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36", } async with ClientSession( - headers=headers, timeout=ClientTimeout(timeout) + headers=headers ) as session: cls._auth_code = auth if isinstance(auth, str) else cls._auth_code if not cls._auth_code: diff --git a/g4f/Provider/Vitalentum.py b/g4f/Provider/Vitalentum.py index ccaaeb00..d5265428 100644 --- a/g4f/Provider/Vitalentum.py +++ b/g4f/Provider/Vitalentum.py @@ -1,7 +1,7 @@ from __future__ import annotations import json -from aiohttp import ClientSession, ClientTimeout +from aiohttp import ClientSession from .base_provider import AsyncGeneratorProvider from ..typing import AsyncGenerator @@ -18,7 +18,6 @@ class Vitalentum(AsyncGeneratorProvider): model: str, messages: list[dict[str, str]], proxy: str = None, - timeout: int = 30, **kwargs ) -> AsyncGenerator: headers = { @@ -41,7 +40,7 @@ class Vitalentum(AsyncGeneratorProvider): **kwargs } async with ClientSession( - headers=headers, timeout=ClientTimeout(timeout) + headers=headers ) as session: async with session.post(cls.url + "/api/converse-edge", json=data, proxy=proxy) as response: response.raise_for_status() diff --git a/g4f/Provider/Yqcloud.py b/g4f/Provider/Yqcloud.py index 1b22de9e..ac93315c 100644 --- a/g4f/Provider/Yqcloud.py +++ b/g4f/Provider/Yqcloud.py @@ -16,11 +16,10 @@ class Yqcloud(AsyncGeneratorProvider): model: str, messages: list[dict[str, str]], proxy: str = None, - timeout: int = 30, **kwargs, ) -> AsyncGenerator: async with ClientSession( - headers=_create_header(), timeout=timeout + headers=_create_header() ) as session: payload = _create_payload(messages) async with session.post("https://api.aichatos.cloud/api/generateStream", proxy=proxy, json=payload) as response: diff --git a/g4f/Provider/__init__.py b/g4f/Provider/__init__.py index 6a41f637..697f6185 100644 --- a/g4f/Provider/__init__.py +++ b/g4f/Provider/__init__.py @@ -14,13 +14,13 @@ from .Chatgpt4Online import Chatgpt4Online from .ChatgptAi import ChatgptAi from .ChatgptDemo import ChatgptDemo from .ChatgptDuo import ChatgptDuo -from .ChatgptLogin import ChatgptLogin from .ChatgptX import ChatgptX from .DeepAi import DeepAi from .FreeGpt import FreeGpt from .GPTalk import GPTalk from .GptForLove import GptForLove from .GptGo import GptGo +from .GptGod import GptGod from .H2o import H2o from .Liaobots import Liaobots from .Myshell import Myshell @@ -71,6 +71,7 @@ __all__ = [ 'GptForLove', 'GetGpt', 'GptGo', + 'GptGod', 'H2o', 'HuggingChat', 'Liaobots', diff --git a/g4f/Provider/deprecated/ChatgptLogin.py b/g4f/Provider/deprecated/ChatgptLogin.py new file mode 100644 index 00000000..07f3b914 --- /dev/null +++ b/g4f/Provider/deprecated/ChatgptLogin.py @@ -0,0 +1,74 @@ +from __future__ import annotations + +import os, re +from aiohttp import ClientSession + +from ..base_provider import AsyncProvider, format_prompt + + +class ChatgptLogin(AsyncProvider): + url = "https://opchatgpts.net" + supports_gpt_35_turbo = True + working = True + _nonce = None + + @classmethod + async def create_async( + cls, + model: str, + messages: list[dict[str, str]], + **kwargs + ) -> str: + headers = { + "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", + "Accept" : "*/*", + "Accept-language" : "en,fr-FR;q=0.9,fr;q=0.8,es-ES;q=0.7,es;q=0.6,en-US;q=0.5,am;q=0.4,de;q=0.3", + "Origin" : "https://opchatgpts.net", + "Alt-Used" : "opchatgpts.net", + "Referer" : "https://opchatgpts.net/chatgpt-free-use/", + "Sec-Fetch-Dest" : "empty", + "Sec-Fetch-Mode" : "cors", + "Sec-Fetch-Site" : "same-origin", + } + async with ClientSession( + headers=headers + ) as session: + if not cls._nonce: + async with session.get( + "https://opchatgpts.net/chatgpt-free-use/", + params={"id": os.urandom(6).hex()}, + ) as response: + result = re.search(r'data-nonce="(.*?)"', await response.text()) + if not result: + raise RuntimeError("No nonce value") + cls._nonce = result.group(1) + data = { + "_wpnonce": cls._nonce, + "post_id": 28, + "url": "https://opchatgpts.net/chatgpt-free-use", + "action": "wpaicg_chat_shortcode_message", + "message": format_prompt(messages), + "bot_id": 0 + } + async with session.post("https://opchatgpts.net/wp-admin/admin-ajax.php", data=data) as response: + response.raise_for_status() + data = await response.json() + if "data" in data: + return data["data"] + elif "msg" in data: + raise RuntimeError(data["msg"]) + else: + raise RuntimeError(f"Response: {data}") + + + @classmethod + @property + def params(cls): + params = [ + ("model", "str"), + ("messages", "list[dict[str, str]]"), + ("stream", "bool"), + ("temperature", "float"), + ] + 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/deprecated/Opchatgpts.py b/g4f/Provider/deprecated/Opchatgpts.py index 3bfb96f1..ab0d68c9 100644 --- a/g4f/Provider/deprecated/Opchatgpts.py +++ b/g4f/Provider/deprecated/Opchatgpts.py @@ -1,8 +1,7 @@ from __future__ import annotations -from ..ChatgptLogin import ChatgptLogin +from .ChatgptLogin import ChatgptLogin class Opchatgpts(ChatgptLogin): - url = "https://opchatgpts.net" - working = True \ No newline at end of file + url = "https://opchatgpts.net" \ No newline at end of file diff --git a/g4f/Provider/deprecated/__init__.py b/g4f/Provider/deprecated/__init__.py index e4528d02..5c66c87f 100644 --- a/g4f/Provider/deprecated/__init__.py +++ b/g4f/Provider/deprecated/__init__.py @@ -10,4 +10,5 @@ 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 +from .FastGpt import FastGpt +from .ChatgptLogin import ChatgptLogin \ No newline at end of file diff --git a/g4f/__init__.py b/g4f/__init__.py index a5cee80b..268b8aab 100644 --- a/g4f/__init__.py +++ b/g4f/__init__.py @@ -6,7 +6,6 @@ from .typing import CreateResult, Union from .debug import logging from requests import get -logging = False version = '0.1.5.4' def check_pypi_version(): -- cgit v1.2.3