Skip to content

A platform for community discussion. Free, open, simple.

DEV: Separate usage for models with credit allocations (#36542)

Keegan Georgee6ab9ba524db
diff --git a/plugins/discourse-ai/app/serializers/ai_usage_serializer.rb b/plugins/discourse-ai/app/serializers/ai_usage_serializer.rb
index c1a46d4c54b..3d5b930be3c 100644
--- a/plugins/discourse-ai/app/serializers/ai_usage_serializer.rb
+++ b/plugins/discourse-ai/app/serializers/ai_usage_serializer.rb
@@ -44,7 +44,7 @@ class AiUsageSerializer < ApplicationSerializer
def models
object.model_breakdown.map do |row|
- {
+ model_data = {
id: row.llm_id,
llm: row.llm_label,
usage_count: row.usage_count,
@@ -58,6 +58,18 @@ class AiUsageSerializer < ApplicationSerializer
cache_read_spending: row.cache_read_spending,
cache_write_spending: row.cache_write_spending,
}
+
+ if row.llm_id.present? && row.llm_id.to_i.negative?
+ llm_model = LlmModel.find_by(id: row.llm_id)
+ if llm_model&.llm_credit_allocation.present?
+ model_data[:credit_allocation] = LlmCreditAllocationSerializer.new(
+ llm_model.llm_credit_allocation,
+ root: false,
+ ).as_json
+ end
+ end
+
+ model_data
end
end
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-credit-bar.gjs b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-credit-bar.gjs
index 7152b068abe..ea102347866 100644
--- a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-credit-bar.gjs
+++ b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-credit-bar.gjs
@@ -11,13 +11,18 @@ import { i18n } from "discourse-i18n";
* @component AiCreditBar
* @param {Object} allocation - LlmCreditAllocation object with daily_credits, credits_remaining, percentage_remaining, soft_limit_reached, hard_limit_reached, next_reset_at
* @param {Boolean} showTooltip - Whether to show tooltip on hover (default: true)
+ * @param {Boolean} compact - Whether to show compact format (just percentage) for use in tables (default: false)
*/
export default class AiCreditBar extends Component {
get barClass() {
+ const classes = [];
if (this.args.allocation.soft_limit_reached) {
- return "ai-credit-bar--warning";
+ classes.push("ai-credit-bar--warning");
}
- return "";
+ if (this.args.compact) {
+ classes.push("ai-credit-bar--compact");
+ }
+ return classes.join(" ");
}
get fillStyle() {
@@ -25,6 +30,9 @@ export default class AiCreditBar extends Component {
}
get barText() {
+ if (this.args.compact) {
+ return `${this.args.allocation.percentage_remaining}%`;
+ }
return i18n("discourse_ai.llms.credit_allocation.credits_remaining", {
remaining: number(this.args.allocation.credits_remaining),
total: number(this.args.allocation.daily_credits),
@@ -33,6 +41,13 @@ export default class AiCreditBar extends Component {
}
get tooltipText() {
+ if (this.args.compact) {
+ return i18n("discourse_ai.llms.credit_allocation.credits_remaining", {
+ remaining: number(this.args.allocation.credits_remaining),
+ total: number(this.args.allocation.daily_credits),
+ percentage: this.args.allocation.percentage_remaining,
+ });
+ }
const resetDate = new Date(this.args.allocation.next_reset_at);
const options = {
month: "long",
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-usage.gjs b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-usage.gjs
index e0ae55f1c0f..3d19812c935 100644
--- a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-usage.gjs
+++ b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-usage.gjs
@@ -26,7 +26,6 @@ import AiCreditBar from "./ai-credit-bar";
export default class AiUsage extends Component {
@service currentUser;
- @service store;
@tracked startDate = moment().subtract(30, "days").toDate();
@tracked endDate = new Date();
@@ -36,36 +35,10 @@ export default class AiUsage extends Component {
@tracked selectedPeriod = "month";
@tracked isCustomDateActive = false;
@tracked loadingData = true;
- @tracked llmsWithCredits = [];
constructor() {
super(...arguments);
this.fetchData();
- this.fetchLlmsWithCredits();
- }
-
- formatResetDate(dateString) {
- const resetDate = new Date(dateString);
- const options = {
- month: "long",
- day: "numeric",
- hour: "numeric",
- minute: "2-digit",
- };
- return resetDate.toLocaleString(undefined, options);
- }
-
- @action
- async fetchLlmsWithCredits() {
- try {
- const llms = await this.store.findAll("ai-llm");
- this.llmsWithCredits = llms.filter(
- (llm) => llm.llm_credit_allocation != null
- );
- } catch {
- // Silently fail if LLMs can't be loaded
- this.llmsWithCredits = [];
- }
}
@action
@@ -294,6 +267,14 @@ export default class AiUsage extends Component {
};
}
+ get modelsWithCredits() {
+ return (this.data?.models || []).filter((m) => m.credit_allocation);
+ }
+
+ get modelsWithoutCredits() {
+ return (this.data?.models || []).filter((m) => !m.credit_allocation);
+ }
+
get availableFeatures() {
// when you switch we don't want the list to change
// only when you switch durations
@@ -570,19 +551,52 @@ export default class AiUsage extends Component {
/>
{{/unless}}
- {{#if this.data.models.length}}
+ {{#if this.modelsWithCredits.length}}
<table class="ai-usage__models-table">
<thead>
<tr>
<th>{{i18n "discourse_ai.usage.model"}}</th>
<th>{{i18n "discourse_ai.usage.usage_count"}}</th>
<th>{{i18n "discourse_ai.usage.total_tokens"}}</th>
- <th>{{i18n "discourse_ai.usage.total_spending"}}</th>
+ <th>{{i18n "discourse_ai.usage.credits_remaining"}}</th>
+ </tr>
+ </thead>
+ <tbody>
+ {{#each this.modelsWithCredits as |model|}}
+ <tr class="ai-usage__models-row">
+ <td class="ai-usage__models-cell">{{model.llm}}</td>
+ <td
+ class="ai-usage__models-cell"
+ title={{model.usage_count}}
+ >{{number model.usage_count}}</td>
+ <td
+ class="ai-usage__models-cell"
+ title={{model.total_tokens}}
+ >{{number model.total_tokens}}</td>
+ <td class="ai-usage__models-credit-bar">
+ <AiCreditBar
+ @allocation={{model.credit_allocation}}
+ @compact={{true}}
+ />
+ </td>
+ </tr>
+ {{/each}}
+ </tbody>
+ </table>
+ {{/if}}
+ {{#if this.modelsWithoutCredits.length}}
+ <table class="ai-usage__models-table">
+ <thead>
+ <tr>
+ <th>{{i18n "discourse_ai.usage.model"}}</th>
+ <th>{{i18n "discourse_ai.usage.usage_count"}}</th>
+ <th>{{i18n "discourse_ai.usage.total_tokens"}}</th>
+ <th>{{i18n "discourse_ai.usage.total_spending"}}</th>
</tr>
</thead>
<tbody>
- {{#each this.data.models as |model|}}
+ {{#each this.modelsWithoutCredits as |model|}}
<tr class="ai-usage__models-row">
<td class="ai-usage__models-cell">{{model.llm}}</td>
<td
@@ -733,33 +747,6 @@ export default class AiUsage extends Component {
</:content>
</AdminConfigAreaCard>
</div>
-
- {{#if this.llmsWithCredits.length}}
- <AdminConfigAreaCard
- class="ai-usage__credit-allocations"
- @heading="discourse_ai.usage.credit_allocations"
- >
- <:content>
- {{#each this.llmsWithCredits as |llm|}}
- <div class="ai-usage__credit-model">
- <h4>{{llm.display_name}}</h4>
- <AiCreditBar
- @allocation={{llm.llm_credit_allocation}}
- @showTooltip={{false}}
- />
- <div class="ai-usage__credit-details">
- <span>{{i18n
- "discourse_ai.llms.credit_allocation.next_reset"
- time=(this.formatResetDate
- llm.llm_credit_allocation.next_reset_at
- )
- }}</span>
- </div>
- </div>
- {{/each}}
- </:content>
- </AdminConfigAreaCard>
- {{/if}}
</ConditionalLoadingSpinner>
</div>
</div>
diff --git a/plugins/discourse-ai/assets/stylesheets/modules/llms/common/ai-credit-bar.scss b/plugins/discourse-ai/assets/stylesheets/modules/llms/common/ai-credit-bar.scss
index 5e3b3b9d8e2..01568c92d19 100644
--- a/plugins/discourse-ai/assets/stylesheets/modules/llms/common/ai-credit-bar.scss
+++ b/plugins/discourse-ai/assets/stylesheets/modules/llms/common/ai-credit-bar.scss
@@ -34,6 +34,25 @@
background: var(--danger);
}
}
+
+ &--compact {
+ display: flex;
+ align-items: center;
+ gap: var(--space-2);
+ margin-top: 0;
+
+ .ai-credit-bar__progress {
+ flex: 1;
+ margin-bottom: 0;
+ min-width: 60px;
+ }
+
+ .ai-credit-bar__text {
+ flex-shrink: 0;
+ font-size: var(--font-down-2);
+ white-space: nowrap;
+ }
+ }
}
.ai-credit-warning {
diff --git a/plugins/discourse-ai/assets/stylesheets/modules/llms/common/usage.scss b/plugins/discourse-ai/assets/stylesheets/modules/llms/common/usage.scss
index 3bd087b1669..dd95d6cdf8b 100644
--- a/plugins/discourse-ai/assets/stylesheets/modules/llms/common/usage.scss
+++ b/plugins/discourse-ai/assets/stylesheets/modules/llms/common/usage.scss
@@ -176,28 +176,14 @@
width: 100%;
}
- &__credit-allocations {
- margin-top: 2em;
- grid-column: span 2;
- }
-
- &__credit-model {
- padding: 1em;
+ &__models-credit-bar {
+ padding: var(--space-2);
border-bottom: 1px solid var(--primary-low);
-
- &:last-child {
- border-bottom: none;
- }
-
- h4 {
- margin: 0 0 0.5em 0;
- font-size: var(--font-0);
- }
+ min-width: 120px;
+ max-width: 200px;
}
- &__credit-details {
- margin-top: 0.5em;
- font-size: var(--font-down-1);
- color: var(--primary-medium);
+ &__models-table + &__models-table {
+ margin-top: var(--space-6);
}
}
diff --git a/plugins/discourse-ai/config/locales/client.en.yml b/plugins/discourse-ai/config/locales/client.en.yml
index f061ece86ea..b7665fef318 100644
--- a/plugins/discourse-ai/config/locales/client.en.yml
+++ b/plugins/discourse-ai/config/locales/client.en.yml
@@ -425,6 +425,7 @@ en:
feature: "Feature"
usage_count: "Usage count"
model: "Model"
+ credits_remaining: "Credits remaining"
models_breakdown: "Usage per model"
users_breakdown: "Usage per user"
all_features: "All features"