Skip to content

Commit

Permalink
šŸ› Bug: Fix the bug where there is no error handling for response JSONā€¦
Browse files Browse the repository at this point in the history
ā€¦ parsing errors.
  • Loading branch information
yym68686 committed Sep 29, 2024
1 parent d7169b0 commit ea47d28
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,18 +268,19 @@ async def _logging_iterator(self):
if line.startswith("data:"):
line = line.lstrip("data: ")
if not line.startswith("[DONE]"):
resp: dict = json.loads(line)
input_tokens = safe_get(resp, "message", "usage", "input_tokens", default=0)
input_tokens = safe_get(resp, "usage", "prompt_tokens", default=0)
output_tokens = safe_get(resp, "usage", "completion_tokens", default=0)
total_tokens = input_tokens + output_tokens

model = self.current_info.get("model", "")
# total_cost = calculate_cost(model, input_tokens, output_tokens)
self.current_info["prompt_tokens"] = input_tokens
self.current_info["completion_tokens"] = output_tokens
self.current_info["total_tokens"] = total_tokens
# self.current_info["cost"] = total_cost
try:
resp: dict = json.loads(line)
input_tokens = safe_get(resp, "message", "usage", "input_tokens", default=0)
input_tokens = safe_get(resp, "usage", "prompt_tokens", default=0)
output_tokens = safe_get(resp, "usage", "completion_tokens", default=0)
total_tokens = input_tokens + output_tokens

self.current_info["prompt_tokens"] = input_tokens
self.current_info["completion_tokens"] = output_tokens
self.current_info["total_tokens"] = total_tokens
except Exception as e:
logger.error(f"Error parsing response: {str(e)}, line: {repr(line)}")
continue
yield chunk
except Exception as e:
raise
Expand Down

0 comments on commit ea47d28

Please sign in to comment.