Skip to content

Commit 5049394

Browse files
authored
Merge pull request #283 from frcdr/fix-duplicate-branches
Fix duplicate dialogue branches and retry logic in _stream_query
2 parents 4c2c66c + 6a8fd5c commit 5049394

1 file changed

Lines changed: 17 additions & 9 deletions

File tree

‎src/hugchat/hugchat.py‎

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,7 @@ def _stream_query(
664664
) -> typing.Generator[dict, None, None]:
665665
if conversation is None:
666666
conversation = self.current_conversation
667-
667+
668668
if retry_count <= 0:
669669
raise Exception(
670670
"the parameter retry_count must be greater than 0.")
@@ -677,7 +677,7 @@ def _stream_query(
677677
message_id = conversation.history[-1].id
678678

679679
logging.debug(f'message_id: {message_id}')
680-
680+
681681
req_json = {
682682
"id": message_id,
683683
"inputs": text,
@@ -702,8 +702,14 @@ def _stream_query(
702702
obj = {}
703703
break_flag = False
704704
has_started = False
705-
705+
706+
initial_retry_count = retry_count # Track initial retry count
707+
706708
while retry_count > 0:
709+
# Update is_retry flag for subsequent attempts
710+
if retry_count < initial_retry_count:
711+
req_json["is_retry"] = True
712+
707713
resp = self.session.post(
708714
self.hf_base_url + f"/chat/conversation/{conversation}",
709715
files={ "data": (None, json.dumps(req_json)) },
@@ -712,14 +718,14 @@ def _stream_query(
712718
cookies=self.session.cookies.get_dict(),
713719
)
714720
resp.encoding = 'utf-8'
715-
721+
716722
if resp.status_code != 200:
717-
718723
retry_count -= 1
719724
if retry_count <= 0:
720725
raise exceptions.ChatError(
721726
f"Failed to chat. ({resp.status_code})")
722-
727+
continue # Skip processing on non-200 status
728+
723729
try:
724730
for line in resp.iter_lines(decode_unicode=True):
725731
if not line:
@@ -728,7 +734,7 @@ def _stream_query(
728734
obj = json.loads(res)
729735
if "type" in obj:
730736
_type = obj["type"]
731-
737+
732738
if _type == "finalAnswer":
733739
break_flag = True
734740
break
@@ -746,6 +752,8 @@ def _stream_query(
746752
else:
747753
logging.error(f"No `type` found in response: {obj}")
748754
yield obj
755+
# After processing all lines, mark completion
756+
break_flag = True
749757
except requests.exceptions.ChunkedEncodingError:
750758
pass
751759
except BaseException as e:
@@ -760,8 +768,8 @@ def _stream_query(
760768
raise exceptions.ChatError(f"Failed to parse response: {res}")
761769
if break_flag:
762770
break
763-
764-
# update the history of current conversation
771+
772+
# Update the history of current conversation
765773
self.get_conversation_info(conversation)
766774
yield obj
767775

0 commit comments

Comments
 (0)