diff options
| author | Martin Bielik <mx.bielik@gmail.com> | 2024-12-12 09:44:15 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-12-12 09:44:15 +0100 |
| commit | 522656415ea36e3be2c12c5a78b11067731a50eb (patch) | |
| tree | b8e9dfcd44d84b9b40bb8c1b154b6e31f1e476ee /py | |
| parent | 3122b848ee4b32986a5dda739c3ea73b4e696304 (diff) | |
| parent | b4893b9bdd161cc2e421183d4eaf3c4094656cd8 (diff) | |
| download | vim-ai-522656415ea36e3be2c12c5a78b11067731a50eb.tar.gz | |
Merge pull request #139 from jkoelker/improve_resp
fix(utils): improve response mapping
Diffstat (limited to 'py')
| -rw-r--r-- | py/utils.py | 13 |
1 files changed, 11 insertions, 2 deletions
diff --git a/py/utils.py b/py/utils.py index c58529c..f81eeb8 100644 --- a/py/utils.py +++ b/py/utils.py @@ -362,13 +362,22 @@ def make_chat_text_chunks(messages, config_options): url = config_options['endpoint_url'] response = openai_request(url, request, http_options) + def _choices(resp): + choices = resp.get('choices', [{}]) + + # NOTE choices may exist in the response, but be an empty list. + if not choices: + return [{}] + + return choices + def map_chunk_no_stream(resp): printDebug("[engine-chat] response: {}", resp) - return resp['choices'][0]['message'].get('content', '') + return _choices(resp)[0].get('message', {}).get('content', '') def map_chunk_stream(resp): printDebug("[engine-chat] response: {}", resp) - return resp['choices'][0]['delta'].get('content', '') + return _choices(resp)[0].get('delta', {}).get('content', '') map_chunk = map_chunk_stream if openai_options['stream'] else map_chunk_no_stream |