From 63cda8d779f9aaccbdac7cea39f496eca44a96ad Mon Sep 17 00:00:00 2001 From: Heiner Lohaus Date: Sun, 22 Oct 2023 14:22:33 +0200 Subject: Fix increase timeout Add Hashnode Provider Fix Yqcloud Provider --- g4f/Provider/Hashnode.py | 79 ++++++++++++++++++++++++++++++++++++++ g4f/Provider/Phind.py | 4 +- g4f/Provider/Yqcloud.py | 5 ++- g4f/Provider/__init__.py | 3 ++ g4f/Provider/deprecated/Myshell.py | 44 +-------------------- g4f/Provider/retry_provider.py | 13 +++---- 6 files changed, 94 insertions(+), 54 deletions(-) create mode 100644 g4f/Provider/Hashnode.py diff --git a/g4f/Provider/Hashnode.py b/g4f/Provider/Hashnode.py new file mode 100644 index 00000000..7f308d7e --- /dev/null +++ b/g4f/Provider/Hashnode.py @@ -0,0 +1,79 @@ +from __future__ import annotations + +import secrets +from aiohttp import ClientSession + +from ..typing import AsyncResult, Messages +from .base_provider import AsyncGeneratorProvider + +class SearchTypes(): + quick = "quick" + code = "code" + websearch = "websearch" + +class Hashnode(AsyncGeneratorProvider): + url = "https://hashnode.com" + supports_gpt_35_turbo = True + working = True + _sources = [] + + @classmethod + async def create_async_generator( + cls, + model: str, + messages: Messages, + search_type: str = SearchTypes.websearch, + 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": "*/*", + "Accept-Language": "de,en-US;q=0.7,en;q=0.3", + "Accept-Encoding": "gzip, deflate, br", + "Referer": f"{cls.url}/rix", + "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", + } + async with ClientSession(headers=headers) as session: + prompt = messages[-1]["content"] + cls._sources = [] + if search_type == "websearch": + async with session.post( + f"{cls.url}/api/ai/rix/search", + json={"prompt": prompt}, + proxy=proxy, + ) as response: + response.raise_for_status() + cls._sources = (await response.json())["result"] + data = { + "chatId": secrets.token_hex(16).zfill(32), + "history": messages, + "prompt": prompt, + "searchType": search_type, + "urlToScan": None, + "searchResults": cls._sources, + } + async with session.post( + f"{cls.url}/api/ai/rix/completion", + json=data, + proxy=proxy, + ) as response: + response.raise_for_status() + async for chunk in response.content.iter_any(): + if chunk: + yield chunk.decode() + + @classmethod + def get_sources(cls) -> list: + return [{ + "title": source["name"], + "url": source["url"] + } for source in cls._sources] \ No newline at end of file diff --git a/g4f/Provider/Phind.py b/g4f/Provider/Phind.py index d7c6f7c7..0e698cba 100644 --- a/g4f/Provider/Phind.py +++ b/g4f/Provider/Phind.py @@ -1,6 +1,6 @@ from __future__ import annotations -import random +import random, string from datetime import datetime from ..typing import AsyncResult, Messages @@ -22,7 +22,7 @@ class Phind(AsyncGeneratorProvider): timeout: int = 120, **kwargs ) -> AsyncResult: - chars = 'abcdefghijklmnopqrstuvwxyz0123456789' + chars = string.ascii_lowercase + string.digits user_id = ''.join(random.choice(chars) for _ in range(24)) data = { "question": format_prompt(messages), diff --git a/g4f/Provider/Yqcloud.py b/g4f/Provider/Yqcloud.py index b567f3a0..2829c5bf 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 = False + working = True supports_gpt_35_turbo = True @staticmethod @@ -17,10 +17,11 @@ class Yqcloud(AsyncGeneratorProvider): model: str, messages: Messages, proxy: str = None, + timeout: int = 120, **kwargs, ) -> AsyncResult: async with StreamSession( - headers=_create_header(), proxies={"https": proxy} + headers=_create_header(), proxies={"https": proxy}, timeout=timeout ) as session: payload = _create_payload(messages, **kwargs) async with session.post("https://api.aichatos.cloud/api/generateStream", json=payload) as response: diff --git a/g4f/Provider/__init__.py b/g4f/Provider/__init__.py index 97351120..653b6026 100644 --- a/g4f/Provider/__init__.py +++ b/g4f/Provider/__init__.py @@ -24,6 +24,7 @@ from .GptChatly import GptChatly from .GptForLove import GptForLove from .GptGo import GptGo from .GptGod import GptGod +from .Hashnode import Hashnode from .Liaobots import Liaobots from .Llama2 import Llama2 from .MyShell import MyShell @@ -82,6 +83,7 @@ class ProviderUtils: 'GptForLove': GptForLove, 'GptGo': GptGo, 'GptGod': GptGod, + 'Hashnode': Hashnode, 'H2o': H2o, 'HuggingChat': HuggingChat, 'Komo': Komo, @@ -154,6 +156,7 @@ __all__ = [ 'GetGpt', 'GptGo', 'GptGod', + 'Hashnode', 'H2o', 'HuggingChat', 'Liaobots', diff --git a/g4f/Provider/deprecated/Myshell.py b/g4f/Provider/deprecated/Myshell.py index b1aa2b2d..85731325 100644 --- a/g4f/Provider/deprecated/Myshell.py +++ b/g4f/Provider/deprecated/Myshell.py @@ -174,46 +174,4 @@ def generate_visitor_id(user_agent: str) -> str: r = hex(int(random.random() * (16**16)))[2:-2] d = xor_hash(user_agent) e = hex(1080 * 1920)[2:] - return f"{f}-{r}-{d}-{e}-{f}" - - - -# update -# from g4f.requests import StreamSession - -# async def main(): -# headers = { -# 'authority': 'api.myshell.ai', -# 'accept': 'application/json', -# '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', -# 'myshell-service-name': 'organics-api', -# 'origin': 'https://app.myshell.ai', -# 'referer': 'https://app.myshell.ai/', -# '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-site', -# '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', -# 'visitor-id': '18ae8fe5d916d3-0213f29594b17f-18525634-157188-18ae8fe5d916d3', -# } - -# json_data = { -# 'conversation_scenario': 3, -# 'botId': '4738', -# 'message': 'hi', -# 'messageType': 1, -# } - -# async with StreamSession(headers=headers, impersonate="chrome110") as session: -# async with session.post(f'https://api.myshell.ai/v1/bot/chat/send_message', -# json=json_data) as response: - -# response.raise_for_status() -# async for chunk in response.iter_content(): -# print(chunk.decode("utf-8")) - -# import asyncio -# asyncio.run(main()) \ No newline at end of file + return f"{f}-{r}-{d}-{e}-{f}" \ No newline at end of file diff --git a/g4f/Provider/retry_provider.py b/g4f/Provider/retry_provider.py index ee342315..39d61c35 100644 --- a/g4f/Provider/retry_provider.py +++ b/g4f/Provider/retry_provider.py @@ -71,11 +71,10 @@ class RetryProvider(AsyncProvider): self.exceptions: Dict[str, Exception] = {} for provider in providers: try: - return await asyncio.wait_for(provider.create_async(model, messages, **kwargs), timeout=60) - except asyncio.TimeoutError as e: - self.exceptions[provider.__name__] = e - if self.logging: - print(f"{provider.__name__}: TimeoutError: {e}") + return await asyncio.wait_for( + provider.create_async(model, messages, **kwargs), + timeout=kwargs.get("timeout", 60) + ) except Exception as e: self.exceptions[provider.__name__] = e if self.logging: @@ -85,8 +84,8 @@ class RetryProvider(AsyncProvider): def raise_exceptions(self) -> None: if self.exceptions: - raise RuntimeError("\n".join(["All providers failed:"] + [ + raise RuntimeError("\n".join(["RetryProvider failed:"] + [ f"{p}: {self.exceptions[p].__class__.__name__}: {self.exceptions[p]}" for p in self.exceptions ])) - raise RuntimeError("No provider found") \ No newline at end of file + raise RuntimeError("RetryProvider: No provider found") \ No newline at end of file -- cgit v1.2.3