Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: token usage error #503

Merged
merged 1 commit into from
Nov 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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(

Check warning on line 38 in server/core/service/user_token_usage.py

View check run for this annotation

Codecov / codecov/patch

server/core/service/user_token_usage.py#L35-L38

Added lines #L35 - L38 were not covered by tests
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:

Check warning on line 52 in server/core/service/user_token_usage.py

View check run for this annotation

Codecov / codecov/patch

server/core/service/user_token_usage.py#L47-L52

Added lines #L47 - L52 were not covered by tests
print(f"record_token_usage error: {e}")
yield value
return record_token_usage