bugfix
This commit is contained in:
parent
20b8db7abd
commit
3631a09f72
@ -8,9 +8,10 @@ from pathlib import Path
|
|||||||
import certifi
|
import certifi
|
||||||
import ssl
|
import ssl
|
||||||
import os
|
import os
|
||||||
|
from appPublic.log import exception, debug
|
||||||
|
|
||||||
class StreamHttpClient:
|
class StreamHttpClient:
|
||||||
def __init__(self, socks5_url="socks5://127.0.0.1:1080"):
|
def __init__(self, socks5_url="socks5://127.0.0.1:1086"):
|
||||||
home = os.path.expanduser("~")
|
home = os.path.expanduser("~")
|
||||||
self.socks_urls_file = Path(f'{home}/.socksurls.txt')
|
self.socks_urls_file = Path(f'{home}/.socksurls.txt')
|
||||||
self.socks5_url = socks5_url
|
self.socks5_url = socks5_url
|
||||||
@ -41,7 +42,7 @@ class StreamHttpClient:
|
|||||||
use_socks = url in self.socks_urls
|
use_socks = url in self.socks_urls
|
||||||
try:
|
try:
|
||||||
if use_socks:
|
if use_socks:
|
||||||
print(f"🔁 Using SOCKS5 directly for: {url}")
|
debug(f"🔁 Using SOCKS5 directly for: {url}")
|
||||||
async for chunk in self._request_with_connector(
|
async for chunk in self._request_with_connector(
|
||||||
method, url,
|
method, url,
|
||||||
headers=headers, params=params, data=data,
|
headers=headers, params=params, data=data,
|
||||||
@ -50,7 +51,7 @@ class StreamHttpClient:
|
|||||||
):
|
):
|
||||||
yield chunk
|
yield chunk
|
||||||
else:
|
else:
|
||||||
print(f"🌐 Trying direct request: {url}")
|
debug(f"🌐 Trying direct request: {url}")
|
||||||
async for chunk in self._request_with_connector(
|
async for chunk in self._request_with_connector(
|
||||||
method, url,
|
method, url,
|
||||||
headers=headers, params=params, data=data,
|
headers=headers, params=params, data=data,
|
||||||
@ -60,10 +61,10 @@ class StreamHttpClient:
|
|||||||
yield chunk
|
yield chunk
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
if use_socks:
|
if use_socks:
|
||||||
print(f"❌ SOCKS5 request failed: {e},{format_exc()}")
|
exception(f"❌ SOCKS5 request failed: {e},{format_exc()}")
|
||||||
return
|
return
|
||||||
print(f"❌ Direct request failed: {e}")
|
debug(f"❌ Direct request failed: {e}")
|
||||||
print("🧦 Retrying with SOCKS5 proxy...")
|
debug("🧦 Retrying with SOCKS5 proxy...")
|
||||||
try:
|
try:
|
||||||
async for chunk in self._request_with_connector(
|
async for chunk in self._request_with_connector(
|
||||||
method, url,
|
method, url,
|
||||||
@ -74,7 +75,7 @@ class StreamHttpClient:
|
|||||||
self._save_socks_url(url)
|
self._save_socks_url(url)
|
||||||
yield chunk
|
yield chunk
|
||||||
except Exception as e2:
|
except Exception as e2:
|
||||||
print(f"❌ SOCKS5 request also failed: {e2},{format_exc()}")
|
exception(f"❌ SOCKS5 request also failed: {e2},{format_exc()}")
|
||||||
|
|
||||||
async def _request_with_connector(self, method, url,
|
async def _request_with_connector(self, method, url,
|
||||||
headers=None, params=None, data=None,
|
headers=None, params=None, data=None,
|
||||||
@ -121,11 +122,11 @@ if __name__ == '__main__':
|
|||||||
prompt = sys.argv[1]
|
prompt = sys.argv[1]
|
||||||
else:
|
else:
|
||||||
prompt = 'who are you'
|
prompt = 'who are you'
|
||||||
hc = HttpClient()
|
hc = StreamHttpClient()
|
||||||
url = 'http://devops.opencomputing.ai/v1/chat/completions'
|
url = 'http://devops.opencomputing.ai/v1/chat/completions'
|
||||||
headers={'Content-Type': 'application/json'}
|
headers={'Content-Type': 'application/json'}
|
||||||
data='{ "model": "devstral", "stream":true, "messages":[ { "role":"user", "content":"' + prompt + '" } ] }'
|
data='{ "model": "devstral", "stream":true, "messages":[ { "role":"user", "content":"' + prompt + '" } ] }'
|
||||||
async for chunk in hc.request('POST', url, data=data, headers=headers):
|
async for chunk in hc('POST', url, data=data, headers=headers):
|
||||||
print(chunk)
|
print(chunk)
|
||||||
|
|
||||||
asyncio.new_event_loop().run_until_complete(main())
|
asyncio.new_event_loop().run_until_complete(main())
|
||||||
|
Loading…
Reference in New Issue
Block a user