Skip to content

Commit 474c37f

Browse files
committed
fix: too many message exception error #275
1 parent 9f7687d commit 474c37f

2 files changed

Lines changed: 18 additions & 6 deletions

File tree

‎src/hugchat/hugchat.py‎

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import os
44
import datetime
55
import logging
6+
import time
67
import typing
78
import traceback
89

@@ -698,9 +699,9 @@ def _stream_query(
698699
'sec-fetch-site': 'same-origin',
699700
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36 Edg/119.0.0.0',
700701
}
701-
final_answer = {}
702-
702+
obj = {}
703703
break_flag = False
704+
has_started = False
704705

705706
while retry_count > 0:
706707
resp = self.session.post(
@@ -725,13 +726,23 @@ def _stream_query(
725726
continue
726727
res = line
727728
obj = json.loads(res)
728-
if obj.__contains__("type"):
729+
if "type" in obj:
729730
_type = obj["type"]
730731

731732
if _type == "finalAnswer":
732-
final_answer = obj
733733
break_flag = True
734734
break
735+
736+
if _type == "status" and obj["status"] == "started":
737+
if has_started:
738+
obj = {
739+
"type": "finalAnswer",
740+
"text": ""
741+
}
742+
break_flag = True
743+
break
744+
has_started = True
745+
735746
else:
736747
logging.error(f"No `type` found in response: {obj}")
737748
yield obj
@@ -752,7 +763,7 @@ def _stream_query(
752763

753764
# update the history of current conversation
754765
self.get_conversation_info(conversation)
755-
yield final_answer
766+
yield obj
756767

757768
def query(self) -> Message:
758769
"""

‎src/hugchat/message.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ def __next__(self) -> dict:
127127

128128
# set _result_text if this is the final iteration of the chat message
129129
if data_type == ResponseTypes.FINAL:
130-
self._result_text = data["text"]
130+
# self._result_text = data["text"]
131131
self.msg_status = MessageStatus.RESOLVED
132132

133133
# Handle web response type
@@ -156,6 +156,7 @@ def __next__(self) -> dict:
156156
# replace null characters with an empty string
157157
elif data_type == ResponseTypes.STREAM:
158158
data["token"] = data["token"].replace('\u0000', '')
159+
self._result_text += data["token"]
159160

160161
elif "messageType" in data:
161162
message_type: str = data["messageType"]

0 commit comments

Comments
 (0)