Hansimov commited on
Commit
caedafb
1 Parent(s): 06e3150

:gem: [Feature] HuggingchatStreamer: Support no-stream mode

Browse files
Files changed (1) hide show
  1. networks/huggingchat_streamer.py +21 -2
networks/huggingchat_streamer.py CHANGED
@@ -300,8 +300,27 @@ class HuggingchatStreamer:
300
  if not is_finished:
301
  yield self.message_outputer.output(content="", content_type="Finished")
302
 
303
- def chat_return_dict(self, stream_response):
304
- pass
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
305
 
306
 
307
  if __name__ == "__main__":
 
300
  if not is_finished:
301
  yield self.message_outputer.output(content="", content_type="Finished")
302
 
303
+ def chat_return_dict(self, stream_response: requests.Response):
304
+ final_output = self.message_outputer.default_data.copy()
305
+ final_output["choices"] = [
306
+ {
307
+ "index": 0,
308
+ "finish_reason": "stop",
309
+ "message": {"role": "assistant", "content": ""},
310
+ }
311
+ ]
312
+ final_content = ""
313
+ for item in self.chat_return_generator(stream_response):
314
+ try:
315
+ data = json.loads(item)
316
+ delta = data["choices"][0]["delta"]
317
+ delta_content = delta.get("content", "")
318
+ if delta_content:
319
+ final_content += delta_content
320
+ except Exception as e:
321
+ logger.warn(e)
322
+ final_output["choices"][0]["message"]["content"] = final_content.strip()
323
+ return final_output
324
 
325
 
326
  if __name__ == "__main__":