From 97801a0fba2f4bb9409bb7a2b2bfec0344c53838 Mon Sep 17 00:00:00 2001 From: Martin Bielik Date: Thu, 13 Apr 2023 21:31:17 +0200 Subject: [PATCH] implemented request_timeout --- py/chat.py | 5 +++++ py/complete.py | 5 +++++ py/utils.py | 11 +++++++++-- 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/py/chat.py b/py/chat.py index 955832b..ce7cd95 100644 --- a/py/chat.py +++ b/py/chat.py @@ -87,3 +87,8 @@ def map_chunk(resp): vim.command("redraw") except KeyboardInterrupt: vim.command("normal! a Ctrl-C...") +except URLError as error: + if isinstance(error.reason, socket.timeout): + vim.command("normal! aRequest timeout...") + else: + raise error diff --git a/py/complete.py b/py/complete.py index 8a8bf7d..29eb760 100644 --- a/py/complete.py +++ b/py/complete.py @@ -50,3 +50,8 @@ def map_chunk(resp): render_text_chunks(text_chunks) except KeyboardInterrupt: vim.command("normal! a Ctrl-C...") +except URLError as error: + if isinstance(error.reason, socket.timeout): + vim.command("normal! aRequest timeout...") + else: + raise error diff --git a/py/utils.py b/py/utils.py index 6fd161a..fb0f48f 100644 --- a/py/utils.py +++ b/py/utils.py @@ -4,6 +4,8 @@ import json import urllib.error import urllib.request +import socket +from urllib.error import URLError def load_api_key(): config_file_path = os.path.join(os.path.expanduser("~"), ".config/openai.token") @@ -25,6 +27,7 @@ def make_request_options(options): request_options['model'] = options['model'] request_options['max_tokens'] = int(options['max_tokens']) request_options['temperature'] = float(options['temperature']) + request_options['request_timeout'] = float(options['request_timeout']) return request_options def render_text_chunks(chunks): @@ -78,13 +81,17 @@ def openai_request(url, data): "Content-Type": "application/json", "Authorization": f"Bearer {OPENAI_API_KEY}" } + # request_timeout is a leftover from the time when openai-python was used, + # it needs to be handled in a special way now + request_timeout=data['request_timeout'] + del data['request_timeout'] req = urllib.request.Request( url, - data=json.dumps(data).encode("utf-8"), + data=json.dumps({ **data }).encode("utf-8"), headers=headers, method="POST", ) - with urllib.request.urlopen(req) as response: + with urllib.request.urlopen(req, timeout=request_timeout) as response: for line_bytes in response: line = line_bytes.decode("utf-8", errors="replace") if line.startswith(OPENAI_RESP_DATA_PREFIX):