This commit is contained in:
ymq1 2025-06-08 05:20:02 +00:00
parent 20b8db7abd
commit 3631a09f72

View File

@ -8,9 +8,10 @@ from pathlib import Path
import certifi
import ssl
import os
from appPublic.log import exception, debug
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("~")
self.socks_urls_file = Path(f'{home}/.socksurls.txt')
self.socks5_url = socks5_url
@ -41,7 +42,7 @@ class StreamHttpClient:
use_socks = url in self.socks_urls
try:
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(
method, url,
headers=headers, params=params, data=data,
@ -50,7 +51,7 @@ class StreamHttpClient:
):
yield chunk
else:
print(f"🌐 Trying direct request: {url}")
debug(f"🌐 Trying direct request: {url}")
async for chunk in self._request_with_connector(
method, url,
headers=headers, params=params, data=data,
@ -60,10 +61,10 @@ class StreamHttpClient:
yield chunk
except Exception as e:
if use_socks:
print(f"❌ SOCKS5 request failed: {e},{format_exc()}")
exception(f"❌ SOCKS5 request failed: {e},{format_exc()}")
return
print(f"❌ Direct request failed: {e}")
print("🧦 Retrying with SOCKS5 proxy...")
debug(f"❌ Direct request failed: {e}")
debug("🧦 Retrying with SOCKS5 proxy...")
try:
async for chunk in self._request_with_connector(
method, url,
@ -74,7 +75,7 @@ class StreamHttpClient:
self._save_socks_url(url)
yield chunk
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,
headers=None, params=None, data=None,
@ -121,11 +122,11 @@ if __name__ == '__main__':
prompt = sys.argv[1]
else:
prompt = 'who are you'
hc = HttpClient()
hc = StreamHttpClient()
url = 'http://devops.opencomputing.ai/v1/chat/completions'
headers={'Content-Type': 'application/json'}
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)
asyncio.new_event_loop().run_until_complete(main())