ryomo commited on
Commit
1824dd0
·
1 Parent(s): 25e14ff

fix: handle HarmonyError bug in token processing

Browse files
Files changed (1) hide show
  1. src/unpredictable_lord/chat/chat.py +12 -1
src/unpredictable_lord/chat/chat.py CHANGED
@@ -114,11 +114,19 @@ async def _stream_and_yield(
114
 
115
  parser = oh.StreamableParser(encoding, role=oh.Role.ASSISTANT)
116
 
 
117
  response_text = ""
118
  async for token in generate_stream(input_tokens):
119
  if token is None:
120
  continue
121
- parser.process(token)
 
 
 
 
 
 
 
122
 
123
  if parser.current_channel == "final":
124
  delta = parser.last_content_delta
@@ -127,6 +135,9 @@ async def _stream_and_yield(
127
  partial_history[-1]["content"] = response_text
128
  yield partial_history
129
 
 
 
 
130
  # Store results in partial_history for caller to retrieve
131
  partial_history.append(
132
  {"_response_text": response_text, "_messages": parser.messages}
 
114
 
115
  parser = oh.StreamableParser(encoding, role=oh.Role.ASSISTANT)
116
 
117
+ oh_error_count = 0
118
  response_text = ""
119
  async for token in generate_stream(input_tokens):
120
  if token is None:
121
  continue
122
+ try:
123
+ parser.process(token)
124
+ except oh.HarmonyError:
125
+ # This is workaround for the issue where the parser fails on certain tokens.
126
+ # https://github.com/openai/harmony/pull/76
127
+ # TODO: THIS SHOULD BE FIXED PROPERLY LATER
128
+ oh_error_count += 1
129
+ continue
130
 
131
  if parser.current_channel == "final":
132
  delta = parser.last_content_delta
 
135
  partial_history[-1]["content"] = response_text
136
  yield partial_history
137
 
138
+ if oh_error_count > 0:
139
+ logger.warning(f"Parser encountered {oh_error_count} HarmonyError exceptions.")
140
+
141
  # Store results in partial_history for caller to retrieve
142
  partial_history.append(
143
  {"_response_text": response_text, "_messages": parser.messages}