From c182cbbf9998e4d20ca3bf6bdb77de9903b7dc62 Mon Sep 17 00:00:00 2001 From: Kuriko Moe Date: Fri, 26 Jan 2024 21:32:27 +0800 Subject: [PATCH] fix(timeout): Add api connection timeout for SakuraLLM (#501) Add winhttp timeout parameters Add api connection timeout settings for SakuraLLM --- .../LunaTranslator/network/requests_common.py | 4 +++- .../network/winhttp/requests.py | 21 +++++++++++++++---- .../LunaTranslator/network/winhttp/winhttp.py | 4 ++++ .../LunaTranslator/translator/sakura.py | 12 ++++++++++- .../defaultconfig/translatorsetting.json | 7 +++++++ LunaTranslator/files/lang/zh.json | 4 +++- 6 files changed, 45 insertions(+), 7 deletions(-) diff --git a/LunaTranslator/LunaTranslator/network/requests_common.py b/LunaTranslator/LunaTranslator/network/requests_common.py index 79c62cd1..198afb3c 100644 --- a/LunaTranslator/LunaTranslator/network/requests_common.py +++ b/LunaTranslator/LunaTranslator/network/requests_common.py @@ -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'] diff --git a/LunaTranslator/LunaTranslator/network/winhttp/requests.py b/LunaTranslator/LunaTranslator/network/winhttp/requests.py index f2d77434..7e474d5d 100644 --- a/LunaTranslator/LunaTranslator/network/winhttp/requests.py +++ b/LunaTranslator/LunaTranslator/network/winhttp/requests.py @@ -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) diff --git a/LunaTranslator/LunaTranslator/network/winhttp/winhttp.py b/LunaTranslator/LunaTranslator/network/winhttp/winhttp.py index 8e7374ed..f7714b96 100644 --- a/LunaTranslator/LunaTranslator/network/winhttp/winhttp.py +++ b/LunaTranslator/LunaTranslator/network/winhttp/winhttp.py @@ -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 diff --git a/LunaTranslator/LunaTranslator/translator/sakura.py b/LunaTranslator/LunaTranslator/translator/sakura.py index da955d73..b07f4dbf 100644 --- a/LunaTranslator/LunaTranslator/translator/sakura.py +++ b/LunaTranslator/LunaTranslator/translator/sakura.py @@ -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']) diff --git a/LunaTranslator/files/defaultconfig/translatorsetting.json b/LunaTranslator/files/defaultconfig/translatorsetting.json index 1a297b84..ea040f42 100644 --- a/LunaTranslator/files/defaultconfig/translatorsetting.json +++ b/LunaTranslator/files/defaultconfig/translatorsetting.json @@ -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" }, diff --git a/LunaTranslator/files/lang/zh.json b/LunaTranslator/files/lang/zh.json index e3a2c74b..34238b1f 100644 --- a/LunaTranslator/files/lang/zh.json +++ b/LunaTranslator/files/lang/zh.json @@ -701,5 +701,7 @@ "添加关联页面": "", "页面类型": "", "页面链接": "", - "缓存": "" + "缓存": "", + "附带上下文个数(必须打开利用上文翻译)": "", + "API超时(秒)": "" } \ No newline at end of file