diff options
Diffstat (limited to 'g4f/Provider')
-rw-r--r-- | g4f/Provider/AItianhu.py | 7 | ||||
-rw-r--r-- | g4f/Provider/AItianhuSpace.py | 44 | ||||
-rw-r--r-- | g4f/Provider/Aichat.py | 59 | ||||
-rw-r--r-- | g4f/Provider/ChatForAi.py | 2 | ||||
-rw-r--r-- | g4f/Provider/Chatgpt4Online.py | 3 | ||||
-rw-r--r-- | g4f/Provider/ChatgptDemo.py | 2 | ||||
-rw-r--r-- | g4f/Provider/ChatgptFree.py | 53 | ||||
-rw-r--r-- | g4f/Provider/ChatgptLogin.py | 2 | ||||
-rw-r--r-- | g4f/Provider/FreeGpt.py | 3 | ||||
-rw-r--r-- | g4f/Provider/GptChatly.py | 46 | ||||
-rw-r--r-- | g4f/Provider/GptGod.py | 6 | ||||
-rw-r--r-- | g4f/Provider/Liaobots.py | 2 | ||||
-rw-r--r-- | g4f/Provider/Opchatgpts.py | 9 | ||||
-rw-r--r-- | g4f/Provider/Vercel.py | 2 | ||||
-rw-r--r-- | g4f/Provider/Yqcloud.py | 2 | ||||
-rw-r--r-- | g4f/Provider/helper.py | 56 |
16 files changed, 175 insertions, 123 deletions
diff --git a/g4f/Provider/AItianhu.py b/g4f/Provider/AItianhu.py index 0105c89a..18ab08d8 100644 --- a/g4f/Provider/AItianhu.py +++ b/g4f/Provider/AItianhu.py @@ -10,7 +10,7 @@ from .base_provider import AsyncGeneratorProvider, format_prompt, get_cookies class AItianhu(AsyncGeneratorProvider): url = "https://www.aitianhu.com" - working = False + working = True supports_gpt_35_turbo = True @classmethod @@ -23,9 +23,9 @@ class AItianhu(AsyncGeneratorProvider): timeout: int = 120, **kwargs) -> AsyncResult: if not cookies: - cookies = browser_cookie3.chrome(domain_name='www.aitianhu.com') + cookies = get_cookies(domain_name='www.aitianhu.com') if not cookies: - raise RuntimeError(f"g4f.provider.{cls.__name__} requires cookies") + raise RuntimeError(f"g4f.provider.{cls.__name__} requires cookies [refresh https://www.aitianhu.com on chrome]") data = { "prompt": format_prompt(messages), @@ -68,7 +68,6 @@ class AItianhu(AsyncGeneratorProvider): if b"platform's risk control" in line: raise RuntimeError("Platform's Risk Control") - print(line) line = json.loads(line) if "detail" in line: diff --git a/g4f/Provider/AItianhuSpace.py b/g4f/Provider/AItianhuSpace.py index 7811c9c8..51297bcd 100644 --- a/g4f/Provider/AItianhuSpace.py +++ b/g4f/Provider/AItianhuSpace.py @@ -1,9 +1,9 @@ from __future__ import annotations import random, json - -from ..typing import AsyncResult, Messages -from ..requests import StreamSession +from ..debug import logging +from ..typing import AsyncResult, Messages +from ..requests import StreamSession from .base_provider import AsyncGeneratorProvider, format_prompt, get_cookies domains = { @@ -17,37 +17,37 @@ class AItianhuSpace(AsyncGeneratorProvider): supports_gpt_35_turbo = True @classmethod - async def create_async_generator( - cls, - model: str, - messages: Messages, - proxy: str = None, - domain: str = None, - cookies: dict = None, - timeout: int = 120, - **kwargs - ) -> AsyncResult: + async def create_async_generator(cls, + model: str, + messages: Messages, + proxy: str = None, + domain: str = None, + cookies: dict = None, + timeout: int = 10, **kwargs) -> AsyncResult: + if not model: model = "gpt-3.5-turbo" + elif not model in domains: raise ValueError(f"Model are not supported: {model}") + if not domain: chars = 'abcdefghijklmnopqrstuvwxyz0123456789' rand = ''.join(random.choice(chars) for _ in range(6)) domain = f"{rand}.{domains[model]}" + + if logging: + print(f"AItianhuSpace | using domain: {domain}") + if not cookies: - cookies = get_cookies(domain) + cookies = get_cookies('.aitianhu.space') if not cookies: - raise RuntimeError(f"g4f.provider.{cls.__name__} requires cookies") + raise RuntimeError(f"g4f.provider.{cls.__name__} requires cookies [refresh https://{domain} on chrome]") url = f'https://{domain}' - async with StreamSession( - proxies={"https": proxy}, - cookies=cookies, - timeout=timeout, - impersonate="chrome110", - verify=False - ) as session: + async with StreamSession(proxies={"https": proxy}, + cookies=cookies, timeout=timeout, impersonate="chrome110", verify=False) as session: + data = { "prompt": format_prompt(messages), "options": {}, diff --git a/g4f/Provider/Aichat.py b/g4f/Provider/Aichat.py index ddc9eb1d..d32c99e1 100644 --- a/g4f/Provider/Aichat.py +++ b/g4f/Provider/Aichat.py @@ -4,7 +4,8 @@ from aiohttp import ClientSession from ..typing import Messages from .base_provider import AsyncProvider, format_prompt - +from .helper import get_cookies +from ..requests import StreamSession class Aichat(AsyncProvider): url = "https://chat-gpt.org/chat" @@ -15,27 +16,34 @@ class Aichat(AsyncProvider): async def create_async( model: str, messages: Messages, - proxy: str = None, - **kwargs - ) -> str: + proxy: str = None, **kwargs) -> str: + + cookies = get_cookies('chat-gpt.org') if not kwargs.get('cookies') else kwargs.get('cookies') + if not cookies: + raise RuntimeError(f"g4f.provider.Aichat requires cookies, [refresh https://chat-gpt.org on chrome]") + headers = { - "authority": "chat-gpt.org", - "accept": "*/*", - "cache-control": "no-cache", - "content-type": "application/json", - "origin": "https://chat-gpt.org", - "pragma": "no-cache", - "referer": "https://chat-gpt.org/chat", - "sec-ch-ua-mobile": "?0", - "sec-ch-ua-platform": '"macOS"', - "sec-fetch-dest": "empty", - "sec-fetch-mode": "cors", - "sec-fetch-site": "same-origin", - "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", + 'authority': 'chat-gpt.org', + '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', + 'content-type': 'application/json', + 'origin': 'https://chat-gpt.org', + 'referer': 'https://chat-gpt.org/chat', + 'sec-ch-ua': '"Chromium";v="118", "Google Chrome";v="118", "Not=A?Brand";v="99"', + 'sec-ch-ua-mobile': '?0', + 'sec-ch-ua-platform': '"macOS"', + 'sec-fetch-dest': 'empty', + 'sec-fetch-mode': 'cors', + 'sec-fetch-site': 'same-origin', + 'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36', } - async with ClientSession( - headers=headers - ) as session: + + async with StreamSession(headers=headers, + cookies=cookies, + timeout=6, + proxies={"https": proxy} if proxy else None, + impersonate="chrome110", verify=False) as session: + json_data = { "message": format_prompt(messages), "temperature": kwargs.get('temperature', 0.5), @@ -43,13 +51,14 @@ class Aichat(AsyncProvider): "top_p": kwargs.get('top_p', 1), "frequency_penalty": 0, } - async with session.post( - "https://chat-gpt.org/api/text", - proxy=proxy, - json=json_data - ) as response: + + async with session.post("https://chat-gpt.org/api/text", + json=json_data) as response: + response.raise_for_status() result = await response.json() + if not result['response']: raise Exception(f"Error Response: {result}") + return result["message"] diff --git a/g4f/Provider/ChatForAi.py b/g4f/Provider/ChatForAi.py index 718affeb..0ccc8444 100644 --- a/g4f/Provider/ChatForAi.py +++ b/g4f/Provider/ChatForAi.py @@ -10,7 +10,7 @@ from .base_provider import AsyncGeneratorProvider class ChatForAi(AsyncGeneratorProvider): url = "https://chatforai.store" - working = True + working = False supports_gpt_35_turbo = True @classmethod diff --git a/g4f/Provider/Chatgpt4Online.py b/g4f/Provider/Chatgpt4Online.py index bfcb1dec..25208067 100644 --- a/g4f/Provider/Chatgpt4Online.py +++ b/g4f/Provider/Chatgpt4Online.py @@ -10,7 +10,7 @@ from .base_provider import AsyncGeneratorProvider class Chatgpt4Online(AsyncGeneratorProvider): url = "https://chatgpt4online.org" supports_gpt_35_turbo = True - working = True + working = False @classmethod async def create_async_generator( @@ -31,6 +31,7 @@ class Chatgpt4Online(AsyncGeneratorProvider): "newMessage": messages[-1]["content"], "stream": True } + async with session.post(cls.url + "/wp-json/mwai-ui/v1/chats/submit", json=data, proxy=proxy) as response: response.raise_for_status() async for line in response.content: diff --git a/g4f/Provider/ChatgptDemo.py b/g4f/Provider/ChatgptDemo.py index 875751b9..9efa89ba 100644 --- a/g4f/Provider/ChatgptDemo.py +++ b/g4f/Provider/ChatgptDemo.py @@ -10,7 +10,7 @@ from .helper import format_prompt class ChatgptDemo(AsyncGeneratorProvider): url = "https://chat.chatgptdemo.net" supports_gpt_35_turbo = True - working = True + working = False @classmethod async def create_async_generator( diff --git a/g4f/Provider/ChatgptFree.py b/g4f/Provider/ChatgptFree.py index ecba2878..8b7d04c4 100644 --- a/g4f/Provider/ChatgptFree.py +++ b/g4f/Provider/ChatgptFree.py @@ -5,6 +5,7 @@ from __future__ import annotations import re from aiohttp import ClientSession +from ..requests import StreamSession from ..typing import Messages from .base_provider import AsyncProvider from .helper import format_prompt, get_cookies @@ -13,7 +14,7 @@ from .helper import format_prompt, get_cookies class ChatgptFree(AsyncProvider): url = "https://chatgptfree.ai" supports_gpt_35_turbo = True - working = False + working = True _post_id = None _nonce = None @@ -23,40 +24,50 @@ class ChatgptFree(AsyncProvider): model: str, messages: Messages, proxy: str = None, + cookies: dict = None, **kwargs ) -> str: - cookies = get_cookies('chatgptfree.ai') + if not cookies: + cookies = get_cookies('chatgptfree.ai') + if not cookies: + raise RuntimeError(f"g4f.provider.{cls.__name__} requires cookies [refresh https://chatgptfree.ai on chrome]") + headers = { - "User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/118.0", - "Accept": "*/*", - "Accept-Language": "de,en-US;q=0.7,en;q=0.3", - "Accept-Encoding": "gzip, deflate, br", - "Origin": cls.url, - "Alt-Used": "chatgptfree.ai", - "Connection": "keep-alive", - "Referer": f"{cls.url}/", - "Sec-Fetch-Dest": "empty", - "Sec-Fetch-Mode": "cors", - "Sec-Fetch-Site": "same-origin", - "Pragma": "no-cache", - "Cache-Control": "no-cache", - "TE": "trailers" + 'authority': 'chatgptfree.ai', + '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://chatgptfree.ai', + 'referer': 'https://chatgptfree.ai/chat/', + 'sec-ch-ua': '"Chromium";v="118", "Google Chrome";v="118", "Not=A?Brand";v="99"', + 'sec-ch-ua-mobile': '?0', + 'sec-ch-ua-platform': '"macOS"', + 'sec-fetch-dest': 'empty', + 'sec-fetch-mode': 'cors', + 'sec-fetch-site': 'same-origin', + 'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36', } - async with ClientSession(headers=headers) as session: + + async with StreamSession(headers=headers, + impersonate="chrome107", proxies={"https": proxy}, timeout=10) as session: + if not cls._nonce: - async with session.get(f"{cls.url}/", - proxy=proxy, cookies=cookies) as response: + async with session.get(f"{cls.url}/", cookies=cookies) as response: + response.raise_for_status() response = await response.text() + result = re.search(r'data-post-id="([0-9]+)"', response) if not result: raise RuntimeError("No post id found") cls._post_id = result.group(1) + result = re.search(r'data-nonce="(.*?)"', response) if not result: raise RuntimeError("No nonce found") + cls._nonce = result.group(1) + prompt = format_prompt(messages) data = { "_wpnonce": cls._nonce, @@ -66,6 +77,8 @@ class ChatgptFree(AsyncProvider): "message": prompt, "bot_id": "0" } - async with session.post(cls.url + "/wp-admin/admin-ajax.php", data=data, proxy=proxy) as response: + async with session.post(cls.url + "/wp-admin/admin-ajax.php", + data=data, cookies=cookies) as response: + response.raise_for_status() return (await response.json())["data"]
\ No newline at end of file diff --git a/g4f/Provider/ChatgptLogin.py b/g4f/Provider/ChatgptLogin.py index 1cf0698b..fee9252d 100644 --- a/g4f/Provider/ChatgptLogin.py +++ b/g4f/Provider/ChatgptLogin.py @@ -13,7 +13,7 @@ from .helper import format_prompt class ChatgptLogin(AsyncGeneratorProvider): url = "https://chatgptlogin.ai" supports_gpt_35_turbo = True - working = True + working = False _user_id = None @classmethod diff --git a/g4f/Provider/FreeGpt.py b/g4f/Provider/FreeGpt.py index 8a3ca6cc..6638b67f 100644 --- a/g4f/Provider/FreeGpt.py +++ b/g4f/Provider/FreeGpt.py @@ -7,8 +7,7 @@ from ..requests import StreamSession from .base_provider import AsyncGeneratorProvider domains = [ - 'https://k.aifree.site', - 'https://p.aifree.site' + 'https://r.aifree.site' ] class FreeGpt(AsyncGeneratorProvider): diff --git a/g4f/Provider/GptChatly.py b/g4f/Provider/GptChatly.py index 80fe6349..f4953b78 100644 --- a/g4f/Provider/GptChatly.py +++ b/g4f/Provider/GptChatly.py @@ -2,18 +2,17 @@ from __future__ import annotations -from aiohttp import ClientSession - -from ..typing import Messages +from ..requests import StreamSession +from ..typing import Messages from .base_provider import AsyncProvider -from .helper import get_cookies +from .helper import get_cookies class GptChatly(AsyncProvider): url = "https://gptchatly.com" supports_gpt_35_turbo = True supports_gpt_4 = True - working = False + working = True @classmethod async def create_async( @@ -22,9 +21,9 @@ class GptChatly(AsyncProvider): messages: Messages, proxy: str = None, cookies: dict = None, **kwargs) -> str: + cookies = get_cookies('gptchatly.com') if not cookies else cookies if not cookies: - cookies = get_cookies('gptchatly.com') - + raise RuntimeError(f"g4f.provider.GptChatly requires cookies, [refresh https://gptchatly.com on chrome]") if model.startswith("gpt-4"): chat_url = f"{cls.url}/fetch-gpt4-response" @@ -32,25 +31,26 @@ class GptChatly(AsyncProvider): chat_url = f"{cls.url}/fetch-response" headers = { - "User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/118.0", - "Accept": "*/*", - "Accept-Language": "de,en-US;q=0.7,en;q=0.3", - "Accept-Encoding": "gzip, deflate, br", - "Referer": f"{cls.url}/", - "Content-Type": "application/json", - "Origin": cls.url, - "Connection": "keep-alive", - "Sec-Fetch-Dest": "empty", - "Sec-Fetch-Mode": "cors", - "Sec-Fetch-Site": "same-origin", - "Pragma": "no-cache", - "Cache-Control": "no-cache", - "TE": "trailers", + 'authority': 'gptchatly.com', + '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', + 'content-type': 'application/json', + 'origin': 'https://gptchatly.com', + 'referer': 'https://gptchatly.com/', + 'sec-ch-ua': '"Chromium";v="118", "Google Chrome";v="118", "Not=A?Brand";v="99"', + 'sec-ch-ua-mobile': '?0', + 'sec-ch-ua-platform': '"macOS"', + 'sec-fetch-dest': 'empty', + 'sec-fetch-mode': 'cors', + 'sec-fetch-site': 'same-origin', + 'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36', } - async with ClientSession(headers=headers) as session: + + async with StreamSession(headers=headers, + proxies={"https": proxy}, cookies=cookies, impersonate='chrome110') as session: data = { "past_conversations": messages } - async with session.post(chat_url, json=data, proxy=proxy) as response: + async with session.post(chat_url, json=data) as response: response.raise_for_status() return (await response.json())["chatGPTResponse"]
\ No newline at end of file diff --git a/g4f/Provider/GptGod.py b/g4f/Provider/GptGod.py index 6c975389..060500a9 100644 --- a/g4f/Provider/GptGod.py +++ b/g4f/Provider/GptGod.py @@ -8,7 +8,7 @@ from .helper import format_prompt class GptGod(AsyncGeneratorProvider): url = "https://gptgod.site" supports_gpt_35_turbo = True - working = True + working = False @classmethod async def create_async_generator( @@ -18,6 +18,7 @@ class GptGod(AsyncGeneratorProvider): proxy: str = None, **kwargs ) -> AsyncResult: + headers = { "User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/118.0", "Accept": "text/event-stream", @@ -32,6 +33,7 @@ class GptGod(AsyncGeneratorProvider): "Pragma": "no-cache", "Cache-Control": "no-cache", } + async with ClientSession(headers=headers) as session: prompt = format_prompt(messages) data = { @@ -42,6 +44,8 @@ class GptGod(AsyncGeneratorProvider): response.raise_for_status() event = None async for line in response.content: + print(line) + if line.startswith(b'event: '): event = line[7:-1] elif event == b"data" and line.startswith(b"data: "): diff --git a/g4f/Provider/Liaobots.py b/g4f/Provider/Liaobots.py index 740be856..72731728 100644 --- a/g4f/Provider/Liaobots.py +++ b/g4f/Provider/Liaobots.py @@ -30,7 +30,7 @@ models = { class Liaobots(AsyncGeneratorProvider): url = "https://liaobots.site" - working = True + working = False supports_gpt_35_turbo = True supports_gpt_4 = True _auth_code = None diff --git a/g4f/Provider/Opchatgpts.py b/g4f/Provider/Opchatgpts.py index fb0b1ac3..e56f5b40 100644 --- a/g4f/Provider/Opchatgpts.py +++ b/g4f/Provider/Opchatgpts.py @@ -10,16 +10,15 @@ from .base_provider import AsyncGeneratorProvider class Opchatgpts(AsyncGeneratorProvider): url = "https://opchatgpts.net" supports_gpt_35_turbo = True - working = True + working = False @classmethod async def create_async_generator( cls, model: str, messages: Messages, - proxy: str = None, - **kwargs - ) -> AsyncResult: + proxy: str = None, **kwargs) -> AsyncResult: + 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" : "*/*", @@ -58,7 +57,6 @@ class Opchatgpts(AsyncGeneratorProvider): elif line["type"] == "end": break - @classmethod @property def params(cls): @@ -70,7 +68,6 @@ class Opchatgpts(AsyncGeneratorProvider): ] param = ", ".join([": ".join(p) for p in params]) return f"g4f.provider.{cls.__name__} supports: ({param})" - def random_string(length: int = 10): return ''.join(random.choice(string.ascii_lowercase + string.digits) for _ in range(length))
\ No newline at end of file diff --git a/g4f/Provider/Vercel.py b/g4f/Provider/Vercel.py index 9b1292a0..10130320 100644 --- a/g4f/Provider/Vercel.py +++ b/g4f/Provider/Vercel.py @@ -22,8 +22,6 @@ class Vercel(BaseProvider): stream: bool, proxy: str = None, **kwargs) -> CreateResult: - print(model) - if not model: model = "gpt-3.5-turbo" diff --git a/g4f/Provider/Yqcloud.py b/g4f/Provider/Yqcloud.py index 0e96e20f..d6ce21a9 100644 --- a/g4f/Provider/Yqcloud.py +++ b/g4f/Provider/Yqcloud.py @@ -9,7 +9,7 @@ from .base_provider import AsyncGeneratorProvider, format_prompt class Yqcloud(AsyncGeneratorProvider): url = "https://chat9.yqcloud.top/" - working = True + working = False supports_gpt_35_turbo = True @staticmethod diff --git a/g4f/Provider/helper.py b/g4f/Provider/helper.py index db19adc1..096293b2 100644 --- a/g4f/Provider/helper.py +++ b/g4f/Provider/helper.py @@ -1,11 +1,15 @@ from __future__ import annotations -import asyncio import sys -from asyncio import AbstractEventLoop -from os import path -from ..typing import Dict, List, Messages -import browser_cookie3 +import asyncio +import webbrowser +import http.cookiejar + +from os import path +from asyncio import AbstractEventLoop +from ..typing import Dict, Messages +from browser_cookie3 import chrome, chromium, opera, opera_gx, brave, edge, vivaldi, firefox, BrowserCookieError + # Change event loop policy on windows if sys.platform == 'win32': @@ -39,18 +43,46 @@ def get_event_loop() -> AbstractEventLoop: 'Use "create_async" instead of "create" function in a running event loop. Or install the "nest_asyncio" package.' ) +def init_cookies(): + + urls = [ + 'https://chat-gpt.org', + 'https://www.aitianhu.com', + 'https://chatgptfree.ai', + 'https://gptchatly.com', + 'https://bard.google.com', + 'https://huggingface.co/chat', + 'https://open-assistant.io/chat' + ] + + browsers = ['google-chrome', 'chrome', 'firefox', 'safari'] + + def open_urls_in_browser(browser): + b = webbrowser.get(browser) + for url in urls: + b.open(url, new=0, autoraise=True) + + for browser in browsers: + try: + open_urls_in_browser(browser) + break + except webbrowser.Error: + continue # Load cookies for a domain from all supported browsers. # Cache the results in the "_cookies" variable. -def get_cookies(cookie_domain: str) -> Dict[str, str]: - if cookie_domain not in _cookies: - _cookies[cookie_domain] = {} +def get_cookies(domain_name=''): + cj = http.cookiejar.CookieJar() + for cookie_fn in [chrome, chromium, opera, opera_gx, brave, edge, vivaldi, firefox]: try: - for cookie in browser_cookie3.load(cookie_domain): - _cookies[cookie_domain][cookie.name] = cookie.value - except: + for cookie in cookie_fn(domain_name=domain_name): + cj.set_cookie(cookie) + except BrowserCookieError: pass - return _cookies[cookie_domain] + + _cookies[domain_name] = {cookie.name: cookie.value for cookie in cj} + + return _cookies[domain_name] def format_prompt(messages: Messages, add_special_tokens=False) -> str: |