Skip to content

Commit

Permalink
fix: token usage error (#503)
Browse files Browse the repository at this point in the history
  • Loading branch information
RaoHai authored Nov 22, 2024
1 parent c84b836 commit d8647f2
Showing 1 changed file with 21 additions and 18 deletions.
39 changes: 21 additions & 18 deletions server/core/service/user_token_usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,25 @@ def create_token_recorder(user: User, bot: Bot):
user_token_usage_dao = UserTokenUsageDAO()
async def record_token_usage(generator: AsyncGenerator[Dict, None]):
async for value in generator:

match value['type']:
case "usage":
try:
token_usage = UserTokenUsage(
user_id=user.id if user else "Anonymous",
token_id=bot.token_id or "DEFAULT_TOKEN",
bot_id=bot.id,
input_token=value['input_tokens'],
output_token=value['output_tokens'],
total_token=value['total_tokens'],
)

user_token_usage_dao.create(token_usage)
except Exception as e:
print(f"An error occurred: {e}")
case _:
yield value
try:
match value['type']:
case "usage":
try:
token_usage = UserTokenUsage(
user_id=user.id if user else "Anonymous",
token_id=bot.token_id or "DEFAULT_TOKEN",
bot_id=bot.id,
input_token=value['input_tokens'],
output_token=value['output_tokens'],
total_token=value['total_tokens'],
)

user_token_usage_dao.create(token_usage)
except Exception as e:
print(f"An error occurred: {e}")
case _:
yield value
except Exception as e:
print(f"record_token_usage error: {e}")
yield value
return record_token_usage

0 comments on commit d8647f2

Please sign in to comment.