From cefa3c1fa9ed73829b57f66829dd3138c315e0fa Mon Sep 17 00:00:00 2001 From: Martin Bielik Date: Tue, 8 Oct 2024 23:10:06 +0200 Subject: support non streaming api --- py/utils.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) (limited to 'py/utils.py') diff --git a/py/utils.py b/py/utils.py index 3401f4b..381ba75 100644 --- a/py/utils.py +++ b/py/utils.py @@ -49,13 +49,13 @@ def normalize_config(config): normalized['options']['initial_prompt'] = normalized['options']['initial_prompt'].split('\n') return normalized - def make_openai_options(options): max_tokens = int(options['max_tokens']) return { 'model': options['model'], 'max_tokens': max_tokens if max_tokens > 0 else None, 'temperature': float(options['temperature']), + 'stream': int(options['stream']) == 1, } def make_http_options(options): @@ -91,8 +91,10 @@ def render_text_chunks(chunks, is_selection): full_text = '' insert_before_cursor = need_insert_before_cursor(is_selection) for text in chunks: - if not text.strip() and not generating_text: - continue # trim newlines from the beginning + if not generating_text: + text = text.lstrip() # trim newlines from the beginning + if not text: + continue generating_text = True if insert_before_cursor: vim.command("normal! i" + text) @@ -103,7 +105,7 @@ def render_text_chunks(chunks, is_selection): vim.command("redraw") full_text += text if not full_text.strip(): - print_info_message('Empty response received. Tip: You can try modifying the prompt and retry.') + raise KnownError('Empty response received. Tip: You can try modifying the prompt and retry.') def parse_chat_messages(chat_content): @@ -218,7 +220,11 @@ def openai_request(url, data, options): headers=headers, method="POST", ) + with urllib.request.urlopen(req, timeout=request_timeout) as response: + if not data['stream']: + yield json.loads(response.read().decode()) + return for line_bytes in response: line = line_bytes.decode("utf-8", errors="replace") if line.startswith(OPENAI_RESP_DATA_PREFIX): -- cgit v1.2.3