From af9ed889c11f2ca5f1df96c40f072012a68b5713 Mon Sep 17 00:00:00 2001 From: Heiner Lohaus Date: Fri, 6 Oct 2023 18:21:56 +0200 Subject: Fix timeout in create_async --- g4f/Provider/AiAsk.py | 6 ++---- g4f/Provider/Aichat.py | 4 ++-- g4f/Provider/Bing.py | 2 +- g4f/Provider/ChatForAi.py | 14 ++------------ g4f/Provider/ChatgptAi.py | 4 ++-- g4f/Provider/ChatgptDemo.py | 4 ++-- g4f/Provider/GptGo.py | 4 ++-- g4f/Provider/Liaobots.py | 5 ++--- g4f/Provider/Vitalentum.py | 4 ++-- 9 files changed, 17 insertions(+), 30 deletions(-) (limited to 'g4f/Provider') diff --git a/g4f/Provider/AiAsk.py b/g4f/Provider/AiAsk.py index 0a44af3e..870dd1ab 100644 --- a/g4f/Provider/AiAsk.py +++ b/g4f/Provider/AiAsk.py @@ -1,11 +1,9 @@ from __future__ import annotations -from aiohttp import ClientSession - +from aiohttp import ClientSession, ClientTimeout from ..typing import AsyncGenerator from .base_provider import AsyncGeneratorProvider - class AiAsk(AsyncGeneratorProvider): url = "https://e.aiask.me" supports_gpt_35_turbo = True @@ -24,7 +22,7 @@ class AiAsk(AsyncGeneratorProvider): "origin": cls.url, "referer": f"{cls.url}/chat", } - async with ClientSession(headers=headers, timeout=timeout) as session: + async with ClientSession(headers=headers, timeout=ClientTimeout(timeout)) as session: data = { "continuous": True, "id": "fRMSQtuHl91A4De9cCvKD", diff --git a/g4f/Provider/Aichat.py b/g4f/Provider/Aichat.py index 41496765..2e141b53 100644 --- a/g4f/Provider/Aichat.py +++ b/g4f/Provider/Aichat.py @@ -1,6 +1,6 @@ from __future__ import annotations -from aiohttp import ClientSession +from aiohttp import ClientSession, ClientTimeout from .base_provider import AsyncProvider, format_prompt @@ -34,7 +34,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=timeout + headers=headers, timeout=ClientTimeout(timeout) ) as session: json_data = { "message": format_prompt(messages), diff --git a/g4f/Provider/Bing.py b/g4f/Provider/Bing.py index aa2db990..f4275a5f 100644 --- a/g4f/Provider/Bing.py +++ b/g4f/Provider/Bing.py @@ -250,7 +250,7 @@ async def stream_generate( conversation = await create_conversation(session) try: async with session.ws_connect( - f'wss://sydney.bing.com/sydney/ChatHub?sec_access_token={urllib.parse.quote_plus(conversation.conversationSignature)}', + f'wss://sydney.bing.com/sydney/ChatHub', autoping=False, params={'sec_access_token': conversation.conversationSignature} ) as wss: diff --git a/g4f/Provider/ChatForAi.py b/g4f/Provider/ChatForAi.py index 47ac46f0..86b29639 100644 --- a/g4f/Provider/ChatForAi.py +++ b/g4f/Provider/ChatForAi.py @@ -1,7 +1,5 @@ from __future__ import annotations -import time, hashlib - from ..typing import AsyncGenerator from ..requests import StreamSession from .base_provider import AsyncGeneratorProvider @@ -21,11 +19,9 @@ class ChatForAi(AsyncGeneratorProvider): **kwargs ) -> AsyncGenerator: async with StreamSession(impersonate="chrome107", timeout=timeout) as session: - conversation_id = f"id_{int(time.time())}" prompt = messages[-1]["content"] - timestamp = int(time.time()) data = { - "conversationId": conversation_id, + "conversationId": "temp", "conversationType": "chat_continuous", "botId": "chat_continuous", "globalSettings":{ @@ -39,8 +35,6 @@ class ChatForAi(AsyncGeneratorProvider): "botSettings": {}, "prompt": prompt, "messages": messages, - "sign": generate_signature(timestamp, conversation_id, prompt), - "timestamp": timestamp } async with session.post(f"{cls.url}/api/handle/provider-openai", json=data) as response: response.raise_for_status() @@ -56,8 +50,4 @@ class ChatForAi(AsyncGeneratorProvider): ("stream", "bool"), ] param = ", ".join([": ".join(p) for p in params]) - return f"g4f.provider.{cls.__name__} supports: ({param})" - -def generate_signature(timestamp, id, prompt): - data = f"{timestamp}:{id}:{prompt}:6B46K4pt" - return hashlib.sha256(data.encode()).hexdigest() + return f"g4f.provider.{cls.__name__} supports: ({param})" \ No newline at end of file diff --git a/g4f/Provider/ChatgptAi.py b/g4f/Provider/ChatgptAi.py index 0ae0fa38..553a9860 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 +from aiohttp import ClientSession, ClientTimeout from .base_provider import AsyncProvider, format_prompt @@ -40,7 +40,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=timeout + headers=headers, timeout=ClientTimeout(timeout) ) 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 fdd7730b..c9d2894a 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 +from aiohttp import ClientSession, ClientTimeout from typing import AsyncGenerator from .base_provider import AsyncGeneratorProvider @@ -34,7 +34,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=timeout) as session: + async with ClientSession(headers=headers, timeout=ClientTimeout(timeout)) 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/GptGo.py b/g4f/Provider/GptGo.py index 19484291..be62250c 100644 --- a/g4f/Provider/GptGo.py +++ b/g4f/Provider/GptGo.py @@ -1,6 +1,6 @@ from __future__ import annotations -from aiohttp import ClientSession +from aiohttp import ClientSession, ClientTimeout import json from ..typing import AsyncGenerator @@ -32,7 +32,7 @@ class GptGo(AsyncGeneratorProvider): "Sec-Fetch-Site" : "same-origin", } async with ClientSession( - headers=headers, timeout=timeout + headers=headers, timeout=ClientTimeout(timeout) ) as session: async with session.get( "https://gptgo.ai/action_get_token.php", diff --git a/g4f/Provider/Liaobots.py b/g4f/Provider/Liaobots.py index e65c3a0d..2e0154d5 100644 --- a/g4f/Provider/Liaobots.py +++ b/g4f/Provider/Liaobots.py @@ -1,9 +1,8 @@ from __future__ import annotations -import json import uuid -from aiohttp import ClientSession +from aiohttp import ClientSession, ClientTimeout from ..typing import AsyncGenerator from .base_provider import AsyncGeneratorProvider @@ -55,7 +54,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=timeout + headers=headers, timeout=ClientTimeout(timeout) ) 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 9125c285..ccaaeb00 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 +from aiohttp import ClientSession, ClientTimeout from .base_provider import AsyncGeneratorProvider from ..typing import AsyncGenerator @@ -41,7 +41,7 @@ class Vitalentum(AsyncGeneratorProvider): **kwargs } async with ClientSession( - headers=headers, timeout=timeout + headers=headers, timeout=ClientTimeout(timeout) ) as session: async with session.post(cls.url + "/api/converse-edge", json=data, proxy=proxy) as response: response.raise_for_status() -- cgit v1.2.3