Skip to content

Commit d26c7ac

Browse files
authored
FEATURE: Add spending metrics to AI usage (#1268)
This update adds metrics for estimated spending in AI usage. To make use of it, admins must add cost details to the LLM config page (input, output, and cached input costs per 1M tokens). After doing so, the metrics will appear in the AI usage dashboard as the AI plugin is used.
1 parent e2b0287 commit d26c7ac

File tree

14 files changed

+546
-248
lines changed

14 files changed

+546
-248
lines changed

admin/assets/javascripts/discourse/routes/admin-plugins-show-discourse-ai-llms-edit.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,17 @@ import DiscourseRoute from "discourse/routes/discourse";
22

33
export default class AdminPluginsShowDiscourseAiLlmsEdit extends DiscourseRoute {
44
async model(params) {
5-
const allLlms = this.modelFor("adminPlugins.show.discourse-ai-llms");
65
const id = parseInt(params.id, 10);
6+
7+
if (id < 0) {
8+
// You shouldn't be able to access the edit page
9+
// if the model is seeded
10+
return this.router.transitionTo(
11+
"adminPlugins.show.discourse-ai-llms.index"
12+
);
13+
}
14+
15+
const allLlms = this.modelFor("adminPlugins.show.discourse-ai-llms");
716
const record = allLlms.findBy("id", id);
817
record.provider_params = record.provider_params || {};
918
return record;

app/controllers/discourse_ai/admin/ai_llms_controller.rb

+3
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,9 @@ def ai_llm_params(updating: nil)
161161
:api_key,
162162
:enabled_chat_bot,
163163
:vision_enabled,
164+
:input_cost,
165+
:cached_input_cost,
166+
:output_cost,
164167
)
165168

166169
provider = updating ? updating.provider : permitted[:provider]

app/models/llm_model.rb

+11-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,14 @@ class LlmModel < ActiveRecord::Base
1313
validates :url, presence: true, unless: -> { provider == BEDROCK_PROVIDER_NAME }
1414
validates_presence_of :name, :api_key
1515
validates :max_prompt_tokens, numericality: { greater_than: 0 }
16-
validates :max_output_tokens, numericality: { greater_than_or_equal_to: 0 }, allow_nil: true
16+
validates :input_cost,
17+
:cached_input_cost,
18+
:output_cost,
19+
:max_output_tokens,
20+
numericality: {
21+
greater_than_or_equal_to: 0,
22+
},
23+
allow_nil: true
1724
validate :required_provider_params
1825
scope :in_use,
1926
-> do
@@ -184,5 +191,8 @@ def required_provider_params
184191
# enabled_chat_bot :boolean default(FALSE), not null
185192
# provider_params :jsonb
186193
# vision_enabled :boolean default(FALSE), not null
194+
# input_cost :float
195+
# cached_input_cost :float
196+
# output_cost :float
187197
# max_output_tokens :integer
188198
#

app/serializers/ai_usage_serializer.rb

+10
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ def features
2222
total_cached_tokens
2323
total_request_tokens
2424
total_response_tokens
25+
input_spending
26+
output_spending
27+
cached_input_spending
2528
],
2629
)
2730
end
@@ -35,6 +38,9 @@ def models
3538
total_cached_tokens
3639
total_request_tokens
3740
total_response_tokens
41+
input_spending
42+
output_spending
43+
cached_input_spending
3844
],
3945
)
4046
end
@@ -49,6 +55,9 @@ def users
4955
total_cached_tokens: user.total_cached_tokens,
5056
total_request_tokens: user.total_request_tokens,
5157
total_response_tokens: user.total_response_tokens,
58+
input_spending: user.input_spending,
59+
output_spending: user.output_spending,
60+
cached_input_spending: user.cached_input_spending,
5261
}
5362
end
5463
end
@@ -60,6 +69,7 @@ def summary
6069
total_request_tokens: object.total_request_tokens,
6170
total_response_tokens: object.total_response_tokens,
6271
total_requests: object.total_requests,
72+
total_spending: object.total_spending,
6373
date_range: {
6474
start: object.start_date,
6575
end: object.end_date,

app/serializers/llm_model_serializer.rb

+3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ class LlmModelSerializer < ApplicationSerializer
1818
:enabled_chat_bot,
1919
:provider_params,
2020
:vision_enabled,
21+
:input_cost,
22+
:output_cost,
23+
:cached_input_cost,
2124
:used_by
2225

2326
has_one :user, serializer: BasicUserSerializer, embed: :object

assets/javascripts/discourse/admin/models/ai-llm.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ export default class AiLlm extends RestModel {
1515
"api_key",
1616
"enabled_chat_bot",
1717
"provider_params",
18-
"vision_enabled"
18+
"vision_enabled",
19+
"input_cost",
20+
"cached_input_cost",
21+
"output_cost"
1922
);
2023
}
2124

0 commit comments

Comments
 (0)