Skip to content

Commit a02a19c

Browse files
author
Arman Schwarz
committed
avoid null choice.content breaking response loop
1 parent 8ed58d2 commit a02a19c

File tree

1 file changed

+26
-30
lines changed

1 file changed

+26
-30
lines changed

plugin/chatgpt.vim

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -223,38 +223,34 @@ def chat_gpt(prompt):
223223
messages.append({"role": "user", "content": prompt})
224224
messages.insert(0, systemCtx)
225225

226-
try:
227-
client = create_client()
228-
response = client.chat.completions.create(
229-
model=model,
230-
messages=messages,
231-
temperature=temperature,
232-
max_tokens=max_tokens,
233-
stream=True
234-
)
235-
236-
# Iterate through the response chunks
237-
for chunk in response:
238-
# newer Azure API responses contain empty chunks in the first streamed
239-
# response
240-
if not chunk.choices:
241-
continue
242-
243-
chunk_session_id = session_id if session_id else chunk.id
244-
choice = chunk.choices[0]
245-
finish_reason = choice.finish_reason
226+
client = create_client()
227+
response = client.chat.completions.create(
228+
model=model,
229+
messages=messages,
230+
temperature=temperature,
231+
max_tokens=max_tokens,
232+
stream=True
233+
)
234+
235+
# Iterate through the response chunks
236+
for chunk in response:
237+
# newer Azure API responses contain empty chunks in the first streamed
238+
# response
239+
if not chunk.choices:
240+
continue
241+
242+
chunk_session_id = session_id if session_id else chunk.id
243+
choice = chunk.choices[0]
244+
finish_reason = choice.finish_reason
245+
246+
# Call DisplayChatGPTResponse with the finish_reason or content
247+
if finish_reason:
248+
vim.command("call DisplayChatGPTResponse('', '{0}', '{1}')".format(finish_reason.replace("'", "''"), chunk_session_id))
249+
elif choice.delta:
246250
content = choice.delta.content
251+
vim.command("call DisplayChatGPTResponse('{0}', '', '{1}')".format(content.replace("'", "''"), chunk_session_id))
247252

248-
# Call DisplayChatGPTResponse with the finish_reason or content
249-
if finish_reason:
250-
vim.command("call DisplayChatGPTResponse('', '{0}', '{1}')".format(finish_reason.replace("'", "''"), chunk_session_id))
251-
elif content:
252-
vim.command("call DisplayChatGPTResponse('{0}', '', '{1}')".format(content.replace("'", "''"), chunk_session_id))
253-
254-
vim.command("redraw")
255-
256-
except Exception as e:
257-
print("Error:", str(e))
253+
vim.command("redraw")
258254

259255
chat_gpt(vim.eval('a:prompt'))
260256
EOF

0 commit comments

Comments
 (0)