Fix increase timeout

Add Hashnode Provider
Fix Yqcloud Provider
This commit is contained in:
Heiner Lohaus 2023-10-22 14:22:33 +02:00
parent 4225a39a49
commit 63cda8d779
6 changed files with 94 additions and 54 deletions

79
g4f/Provider/Hashnode.py Normal file
View File

@ -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]

View File

@ -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),

View File

@ -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:

View File

@ -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',

View File

@ -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())
return f"{f}-{r}-{d}-{e}-{f}"

View File

@ -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")
raise RuntimeError("RetryProvider: No provider found")