Update app.py
Browse files
app.py
CHANGED
@@ -566,17 +566,17 @@ def generate(message, temperature):
|
|
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 |
-
|
570 |
-
|
571 |
-
|
572 |
-
|
573 |
-
|
574 |
-
|
575 |
-
|
576 |
-
|
577 |
-
|
578 |
-
|
579 |
-
|
580 |
yield content, False
|
581 |
except json.JSONDecodeError as e:
|
582 |
print(f"func: generate error occurred\nchunk:{chunk}\nerror:{e}")
|
@@ -585,7 +585,6 @@ def generate(message, temperature):
|
|
585 |
print(f"func: generate error occurred\nchunk:{chunk}\nerror:{e}")
|
586 |
raise e
|
587 |
|
588 |
-
|
589 |
def get_majority_text(data):
|
590 |
from collections import Counter
|
591 |
|
|
|
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}")
|
|
|
585 |
print(f"func: generate error occurred\nchunk:{chunk}\nerror:{e}")
|
586 |
raise e
|
587 |
|
|
|
588 |
def get_majority_text(data):
|
589 |
from collections import Counter
|
590 |
|