Skip to content

Commit b7f8d0f

Browse files
committed
handle inconsistent final message content being sent with finish_reason
1 parent 7e5582a commit b7f8d0f

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

koboldcpp.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2830,6 +2830,18 @@ async def handle_sse_stream(self, genparams, api_format):
28302830
tokenStr = tokenStr[:sindex]
28312831

28322832
if tokenStr!="" or streamDone:
2833+
need_split_final_msg = True if (currfinishreason is not None and streamDone and tokenStr!="") else False
2834+
if need_split_final_msg: #we need to send one message without the finish reason, then send a finish reason with no msg to follow standards
2835+
if api_format == 4: # if oai chat, set format to expected openai streaming response
2836+
event_str = json.dumps({"id":"koboldcpp","object":"chat.completion.chunk","created":int(time.time()),"model":friendlymodelname,"choices":[{"index":0,"finish_reason":None,"delta":{'role':'assistant','content':tokenStr}}]})
2837+
await self.send_oai_sse_event(event_str)
2838+
elif api_format == 3: # non chat completions
2839+
event_str = json.dumps({"id":"koboldcpp","object":"text_completion","created":int(time.time()),"model":friendlymodelname,"choices":[{"index":0,"finish_reason":None,"text":tokenStr}]})
2840+
await self.send_oai_sse_event(event_str)
2841+
else:
2842+
event_str = json.dumps({"token": tokenStr, "finish_reason":None})
2843+
await self.send_kai_sse_event(event_str)
2844+
tokenStr = "" # now the final finish reason can be sent alone
28332845
if api_format == 4: # if oai chat, set format to expected openai streaming response
28342846
event_str = json.dumps({"id":"koboldcpp","object":"chat.completion.chunk","created":int(time.time()),"model":friendlymodelname,"choices":[{"index":0,"finish_reason":currfinishreason,"delta":{'role':'assistant','content':tokenStr}}]})
28352847
await self.send_oai_sse_event(event_str)

0 commit comments

Comments
 (0)