From 110f6a2acd01462037d79036a2b99ad0c2af5b09 Mon Sep 17 00:00:00 2001 From: nsde Date: Wed, 23 Aug 2023 23:26:43 +0200 Subject: [PATCH] I think I fixed the errors --- api/streaming.py | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/api/streaming.py b/api/streaming.py index c0a1b81..5f55e24 100644 --- a/api/streaming.py +++ b/api/streaming.py @@ -106,6 +106,21 @@ async def stream( # We haven't done any requests as of right now, everything until now was just preparation # Here, we process the request async with aiohttp.ClientSession(connector=proxies.get_proxy().connector) as session: + try: + async with session.get( + url='https://checkip.amazonaws.com', + timeout=aiohttp.ClientTimeout( + connect=3, + total=float(os.getenv('TRANSFER_TIMEOUT', '5')) + ) + ) as response: + for actual_ip in os.getenv('ACTUAL_IPS', '').split(' '): + if actual_ip in await response.text(): + raise ValueError(f'Proxy {response.text()} is transparent!') + + except Exception as exc: + continue + try: async with session.request( method=target_request.get('method', 'POST'), @@ -120,6 +135,9 @@ async def stream( total=float(os.getenv('TRANSFER_TIMEOUT', '120')) ), ) as response: + if response.status == 429: + continue + if response.content_type == 'application/json': data = await response.json() @@ -144,14 +162,21 @@ async def stream( model=model, target_request=target_request ): + print(f'[STREAM] {chunk}') yield chunk break except ProxyError as exc: - print('[!] Proxy error:', exc) + print('[!] aiohttp came up with a dumb excuse to not work again ("pRoXy ErRor")') continue + except ConnectionResetError as exc: + print('[!] aiohttp came up with a dumb excuse to not work again ("cOnNeCtIoN rEsEt")') + continue + + print(f'[STREAM] {json_response}') + if is_chat and is_stream: yield await chat.create_chat_chunk(chat_id=chat_id, model=model, content=chat.CompletionStop) yield 'data: [DONE]\n\n'