mirror of
https://github.com/HIllya51/LunaTranslator.git
synced 2024-12-27 15:44:12 +08:00
fix(timeout): Add api connection timeout for SakuraLLM (#501)
Add winhttp timeout parameters Add api connection timeout settings for SakuraLLM
This commit is contained in:
parent
f82eab694b
commit
c182cbbf99
@ -190,6 +190,8 @@ class Sessionbase:
|
||||
def request(self,
|
||||
method, url, params=None, data=None, headers=None,proxies=None, json=None,cookies=None, files=None,
|
||||
auth=None, timeout=None, allow_redirects=True, hooks=None, stream=None, verify=False, cert=None, ):
|
||||
# 0 means infinity, cite: WinHttpSetTimeouts
|
||||
timeout = timeout or 0
|
||||
_h=self.headers.copy()
|
||||
if headers:
|
||||
_h.update(headers)
|
||||
@ -203,7 +205,7 @@ class Sessionbase:
|
||||
headers,dataptr,datalen=self._parsedata(data,headers,json)
|
||||
proxy= proxies.get(scheme,None) if proxies else None
|
||||
|
||||
_= self.request_impl(method,scheme,server,port,param,url,headers,cookies,dataptr,datalen,proxy,stream,verify)
|
||||
_= self.request_impl(method,scheme,server,port,param,url,headers,cookies,dataptr,datalen,proxy,stream,verify,timeout)
|
||||
|
||||
if _.status_code==301:
|
||||
location=_.headers['Location']
|
||||
|
@ -60,18 +60,31 @@ class Session(Sessionbase):
|
||||
if verify==False:
|
||||
dwFlags=DWORD(SECURITY_FLAG_IGNORE_ALL_CERT_ERRORS)
|
||||
WinHttpSetOption(curl,WINHTTP_OPTION_SECURITY_FLAGS, pointer(dwFlags),sizeof(dwFlags))
|
||||
|
||||
# 30s is the default timeout on Windows
|
||||
def request_impl(self,
|
||||
method,scheme,server,port,param,url,headers,cookies,dataptr,datalen,proxy,stream,verify):
|
||||
method,scheme,server,port,param,url,headers,cookies,dataptr,datalen,proxy,stream,verify,timeout=30):
|
||||
headers=self._parseheader(headers,cookies)
|
||||
flag=WINHTTP_FLAG_SECURE if scheme=='https' else 0
|
||||
#print(server,port,param,dataptr)
|
||||
headers='\r\n'.join(headers)
|
||||
|
||||
headers='\r\n'.join(headers)
|
||||
|
||||
hConnect=AutoWinHttpHandle(WinHttpConnect(self.hSession,server,port,0))
|
||||
if hConnect==0:
|
||||
raise WinhttpException(GetLastError())
|
||||
hRequest=AutoWinHttpHandle(WinHttpOpenRequest( hConnect ,method,param,None,WINHTTP_NO_REFERER,WINHTTP_DEFAULT_ACCEPT_TYPES,flag) )
|
||||
|
||||
timeout = timeout * 1000 # convert to milliseconds
|
||||
'''
|
||||
WINHTTPAPI BOOL WinHttpSetTimeouts(
|
||||
[in] HINTERNET hInternet,
|
||||
[in] int nResolveTimeout,
|
||||
[in] int nConnectTimeout,
|
||||
[in] int nSendTimeout,
|
||||
[in] int nReceiveTimeout
|
||||
);
|
||||
'''
|
||||
WinHttpSetTimeouts(hRequest, timeout, timeout, timeout, timeout)
|
||||
|
||||
if hRequest==0:
|
||||
raise WinhttpException(GetLastError())
|
||||
self._set_verify(hRequest,verify)
|
||||
|
@ -121,6 +121,10 @@ WinHttpOpen.restype=HINTERNET
|
||||
WinHttpCloseHandle=Winhttp.WinHttpCloseHandle
|
||||
WinHttpCloseHandle.argtypes=HINTERNET,
|
||||
|
||||
WinHttpSetTimeouts=Winhttp.WinHttpSetTimeouts
|
||||
WinHttpSetTimeouts.argtypes=HINTERNET,DWORD,DWORD,DWORD,DWORD
|
||||
WinHttpSetTimeouts.restype=BOOL
|
||||
|
||||
WinHttpConnect=Winhttp.WinHttpConnect
|
||||
WinHttpConnect.argtypes=HINTERNET,LPCWSTR,INTERNET_PORT,DWORD
|
||||
WinHttpConnect.restype=HINTERNET
|
||||
|
@ -1,3 +1,4 @@
|
||||
import winhttp
|
||||
from traceback import print_exc
|
||||
from translator.basetranslator import basetrans
|
||||
import requests
|
||||
@ -8,6 +9,7 @@ class TS(basetrans):
|
||||
def langmap(self):
|
||||
return {"zh": "zh-CN"}
|
||||
def __init__(self, typename) :
|
||||
self.timeout = 30
|
||||
self.api_url = ""
|
||||
self.history = {
|
||||
"ja": [],
|
||||
@ -89,13 +91,21 @@ class TS(basetrans):
|
||||
extra_query=extra_query,
|
||||
stream=False,
|
||||
)
|
||||
output = self.session.post(self.api_url + "/chat/completions", json=data).json()
|
||||
output = self.session.post(self.api_url + "/chat/completions", timeout=self.timeout, json=data).json()
|
||||
except winhttp.WinhttpException as e:
|
||||
code = e.errorcode
|
||||
if code == winhttp.WinhttpException.ERROR_WINHTTP_TIMEOUT:
|
||||
raise ValueError(f"连接到Sakura API超时:{self.api_url},当前最大连接时间为: {self.timeout},请尝试修改参数。")
|
||||
else:
|
||||
raise ValueError(f"连接到Sakura API网络错误:{self.api_url},错误代码: {code}")
|
||||
except Exception as e:
|
||||
print(e)
|
||||
raise ValueError(f"无法连接到Sakura API:{self.api_url},请检查你的API链接是否正确填写,以及API后端是否成功启动。")
|
||||
return output
|
||||
|
||||
def translate(self, query):
|
||||
self.checkempty(['API接口地址'])
|
||||
self.timeout = self.config['API超时(秒)']
|
||||
if self.api_url == "":
|
||||
self.get_client(self.config['API接口地址'])
|
||||
frequency_penalty = float(self.config['frequency_penalty'])
|
||||
|
@ -466,6 +466,7 @@
|
||||
"Sakura部署教程": "https://github.com/SakuraLLM/Sakura-13B-Galgame/wiki",
|
||||
"Github仓库": "https://github.com/SakuraLLM/Sakura-13B-Galgame",
|
||||
"API接口地址": "http://127.0.0.1:8080/",
|
||||
"API超时(秒)": 30,
|
||||
"利用上文信息翻译(通常会有一定的效果提升,但会导致变慢)": false,
|
||||
"附带上下文个数(必须打开利用上文翻译)": 3,
|
||||
"temperature": 0.1,
|
||||
@ -490,6 +491,12 @@
|
||||
"type":"label",
|
||||
"islink":false
|
||||
},
|
||||
"API超时(秒)":{
|
||||
"type":"intspin",
|
||||
"min":30,
|
||||
"max":120,
|
||||
"step":1
|
||||
},
|
||||
"利用上文信息翻译(通常会有一定的效果提升,但会导致变慢)": {
|
||||
"type": "switch"
|
||||
},
|
||||
|
@ -701,5 +701,7 @@
|
||||
"添加关联页面": "",
|
||||
"页面类型": "",
|
||||
"页面链接": "",
|
||||
"缓存": ""
|
||||
"缓存": "",
|
||||
"附带上下文个数(必须打开利用上文翻译)": "",
|
||||
"API超时(秒)": ""
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user