Skip to content

Commit

Permalink
implemented request_timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
madox2 committed Apr 13, 2023
1 parent 9a87280 commit 97801a0
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
5 changes: 5 additions & 0 deletions py/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 5 additions & 0 deletions py/complete.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
11 changes: 9 additions & 2 deletions py/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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):
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 97801a0

Please sign in to comment.