diff options
| author | Martin Bielik <mx.bielik@gmail.com> | 2023-04-13 21:31:17 +0200 |
|---|---|---|
| committer | Martin Bielik <mx.bielik@gmail.com> | 2023-04-13 21:38:43 +0200 |
| commit | 97801a0fba2f4bb9409bb7a2b2bfec0344c53838 (patch) | |
| tree | 7ecdcf5d4b5266695d57b635ab8f8e8faa4c6666 /py/utils.py | |
| parent | 9a872803baf77b9a687b263d11cb2ac297b57a71 (diff) | |
| download | vim-ai-97801a0fba2f4bb9409bb7a2b2bfec0344c53838.tar.gz | |
implemented request_timeout
Diffstat (limited to 'py/utils.py')
| -rw-r--r-- | py/utils.py | 11 |
1 files changed, 9 insertions, 2 deletions
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): |