mirror of
https://github.com/discourse/discourse-ai.git
synced 2025-02-12 06:24:42 +00:00
- Added a new admin interface to track AI usage metrics, including tokens, features, and models. - Introduced a new route `/admin/plugins/discourse-ai/ai-usage` and supporting API endpoint in `AiUsageController`. - Implemented `AiUsageSerializer` for structuring AI usage data. - Integrated CSS stylings for charts and tables under `stylesheets/modules/llms/common/usage.scss`. - Enhanced backend with `AiApiAuditLog` model changes: added `cached_tokens` column (implemented with OpenAI for now) with relevant DB migration and indexing. - Created `Report` module for efficient aggregation and filtering of AI usage metrics. - Updated AI Bot title generation logic to log correctly to user vs bot - Extended test coverage for the new tracking features, ensuring data consistency and access controls.
70 lines
1.6 KiB
Ruby
70 lines
1.6 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
class AiUsageSerializer < ApplicationSerializer
|
|
attributes :data, :features, :models, :users, :summary, :period
|
|
|
|
def data
|
|
object.tokens_by_period.as_json(
|
|
only: %i[period total_tokens total_cached_tokens total_request_tokens total_response_tokens],
|
|
)
|
|
end
|
|
|
|
def period
|
|
object.guess_period
|
|
end
|
|
|
|
def features
|
|
object.feature_breakdown.as_json(
|
|
only: %i[
|
|
feature_name
|
|
usage_count
|
|
total_tokens
|
|
total_cached_tokens
|
|
total_request_tokens
|
|
total_response_tokens
|
|
],
|
|
)
|
|
end
|
|
|
|
def models
|
|
object.model_breakdown.as_json(
|
|
only: %i[
|
|
llm
|
|
usage_count
|
|
total_tokens
|
|
total_cached_tokens
|
|
total_request_tokens
|
|
total_response_tokens
|
|
],
|
|
)
|
|
end
|
|
|
|
def users
|
|
object.user_breakdown.map do |user|
|
|
{
|
|
avatar_template: User.avatar_template(user.username, user.uploaded_avatar_id),
|
|
username: user.username,
|
|
usage_count: user.usage_count,
|
|
total_tokens: user.total_tokens,
|
|
total_cached_tokens: user.total_cached_tokens,
|
|
total_request_tokens: user.total_request_tokens,
|
|
total_response_tokens: user.total_response_tokens,
|
|
}
|
|
end
|
|
end
|
|
|
|
def summary
|
|
{
|
|
total_tokens: object.total_tokens,
|
|
total_cached_tokens: object.total_cached_tokens,
|
|
total_request_tokens: object.total_request_tokens,
|
|
total_response_tokens: object.total_response_tokens,
|
|
total_requests: object.total_requests,
|
|
date_range: {
|
|
start: object.start_date,
|
|
end: object.end_date,
|
|
},
|
|
}
|
|
end
|
|
end
|