leolaish commited on
Commit
06a091f
·
verified ·
1 Parent(s): 86114e2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -20
app.py CHANGED
@@ -565,26 +565,25 @@ def generate(message, temperature):
565
 
566
  # The reason why the library method is not used here is that if an error occurs,
567
  # the returned data will not be a stream, and using the official library will result in an error.
568
- for chunk in response.iter_bytes():
569
- chunk = chunk.decode("utf-8")
570
- data_chunks = parse_data_chunk(chunk)
571
-
572
- try:
573
- for data_chunk in data_chunks:
574
- chune_json = json.loads(data_chunk)
575
-
576
- if "error" in chune_json and chune_json["error"]:
577
- yield chune_json["error"], True
578
- break
579
-
580
- delta = chune_json["choices"][0]["delta"]
581
- content = delta["content"] if "content" in delta else ""
582
-
583
- if content != "":
584
- yield content, False
585
- except Exception as e:
586
- print(f"func: generate error occurred\nchunk:{chunk}\nerror:{e}")
587
- raise e
588
 
589
 
590
  def get_majority_text(data):
 
565
 
566
  # The reason why the library method is not used here is that if an error occurs,
567
  # the returned data will not be a stream, and using the official library will result in an error.
568
+ for chunk in str(response).encode().iter_bytes():
569
+ chunk = chunk.decode("utf-8")
570
+ data_chunks = parse_data_chunk(chunk)
571
+ try:
572
+ for data_chunk in data_chunks:
573
+ chunk_json = json.loads(data_chunk)
574
+ if "error" in chunk_json and chunk_json["error"]:
575
+ yield chunk_json["error"], True
576
+ break
577
+ delta = chunk_json["choices"][0]["delta"]
578
+ content = delta["content"] if "content" in delta else ""
579
+ if content != "":
580
+ yield content, False
581
+ except json.JSONDecodeError as e:
582
+ print(f"func: generate error occurred\nchunk:{chunk}\nerror:{e}")
583
+ raise e
584
+ except KeyError as e:
585
+ print(f"func: generate error occurred\nchunk:{chunk}\nerror:{e}")
586
+ raise e
 
587
 
588
 
589
  def get_majority_text(data):