Update app.py
Browse files
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 |
-
|
| 569 |
-
|
| 570 |
-
|
| 571 |
-
|
| 572 |
-
|
| 573 |
-
|
| 574 |
-
|
| 575 |
-
|
| 576 |
-
|
| 577 |
-
|
| 578 |
-
|
| 579 |
-
|
| 580 |
-
|
| 581 |
-
|
| 582 |
-
|
| 583 |
-
|
| 584 |
-
|
| 585 |
-
|
| 586 |
-
|
| 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):
|