summaryrefslogtreecommitdiff
path: root/py
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--py/chat.py5
-rw-r--r--py/complete.py5
-rw-r--r--py/utils.py11
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 @@ try:
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 @@ try:
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 os
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):