diff options
| author | Martin Bielik <martin.bielik@instea.sk> | 2024-10-08 23:10:06 +0200 |
|---|---|---|
| committer | Martin Bielik <martin.bielik@instea.sk> | 2024-10-08 23:10:06 +0200 |
| commit | cefa3c1fa9ed73829b57f66829dd3138c315e0fa (patch) | |
| tree | 3a0879a387258ac4bd51ac3d39b5cb8be49de9c4 /py/utils.py | |
| parent | 758be522e6d765eeb78ce7681f4b39e3b05043b8 (diff) | |
| download | vim-ai-cefa3c1fa9ed73829b57f66829dd3138c315e0fa.tar.gz | |
support non streaming api
Diffstat (limited to 'py/utils.py')
| -rw-r--r-- | py/utils.py | 14 |
1 files changed, 10 insertions, 4 deletions
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): |