@@ -304,26 +304,53 @@ def _convert_text_completion_chunks_to_chat(
304304 }
305305 ],
306306 }
307- yield {
308- "id" : "chat" + chunk ["id" ],
309- "model" : chunk ["model" ],
310- "created" : chunk ["created" ],
311- "object" : "chat.completion.chunk" ,
312- "choices" : [
313- {
314- "index" : 0 ,
315- "delta" : (
316- {
317- "content" : chunk ["choices" ][0 ]["text" ],
318- }
319- if chunk ["choices" ][0 ]["finish_reason" ] is None
320- else {}
321- ),
322- "logprobs" : chunk ["choices" ][0 ]["logprobs" ],
323- "finish_reason" : chunk ["choices" ][0 ]["finish_reason" ],
307+ if chunk ["choices" ][0 ]["finish_reason" ] is None :
308+ yield {
309+ "id" : "chat" + chunk ["id" ],
310+ "model" : chunk ["model" ],
311+ "created" : chunk ["created" ],
312+ "object" : "chat.completion.chunk" ,
313+ "choices" : [
314+ {
315+ "index" : 0 ,
316+ "delta" : (
317+ {
318+ "content" : chunk ["choices" ][0 ]["text" ],
319+ }
320+ if chunk ["choices" ][0 ]["finish_reason" ] is None
321+ else {}
322+ ),
323+ "logprobs" : chunk ["choices" ][0 ]["logprobs" ],
324+ "finish_reason" : chunk ["choices" ][0 ]["finish_reason" ],
325+ }
326+ ]
327+ }
328+ else :
329+ yield {
330+ "id" : "chat" + chunk ["id" ],
331+ "model" : chunk ["model" ],
332+ "created" : chunk ["created" ],
333+ "object" : "chat.completion.chunk" ,
334+ "choices" : [
335+ {
336+ "index" : 0 ,
337+ "delta" : (
338+ {
339+ "content" : chunk ["choices" ][0 ]["text" ],
340+ }
341+ if chunk ["choices" ][0 ]["finish_reason" ] is None
342+ else {}
343+ ),
344+ "logprobs" : chunk ["choices" ][0 ]["logprobs" ],
345+ "finish_reason" : chunk ["choices" ][0 ]["finish_reason" ],
346+ }
347+ ],
348+ "usage" : {
349+ "prompt_tokens" : chunk ["usage" ]["prompt_tokens" ],
350+ "completion_tokens" : chunk ["usage" ]["completion_tokens" ],
351+ "total_tokens" : chunk ["usage" ]["total_tokens" ],
324352 }
325- ],
326- }
353+ }
327354
328355
329356def _convert_completion_to_chat (
0 commit comments