Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.

Commit 1c522db

Browse files
committed
DEV: add cost info to llm model
1 parent 1742f42 commit 1c522db

File tree

7 files changed

+76
-1
lines changed

7 files changed

+76
-1
lines changed

app/controllers/discourse_ai/admin/ai_llms_controller.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,9 @@ def ai_llm_params(updating: nil)
160160
:api_key,
161161
:enabled_chat_bot,
162162
:vision_enabled,
163+
:input_cost,
164+
:cached_input_cost,
165+
:output_cost,
163166
)
164167

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

app/models/llm_model.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ 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 :input_cost,
17+
:cached_input_cost,
18+
:output_cost,
19+
numericality: {
20+
greater_than_or_equal_to: 0,
21+
},
22+
allow_nil: true
1623
validate :required_provider_params
1724
scope :in_use,
1825
-> do
@@ -183,4 +190,7 @@ def required_provider_params
183190
# enabled_chat_bot :boolean default(FALSE), not null
184191
# provider_params :jsonb
185192
# vision_enabled :boolean default(FALSE), not null
193+
# input_cost :float
194+
# cached_input_cost :float
195+
# output_cost :float
186196
#

app/serializers/llm_model_serializer.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ class LlmModelSerializer < ApplicationSerializer
1717
:enabled_chat_bot,
1818
:provider_params,
1919
:vision_enabled,
20+
:input_cost,
21+
:output_cost,
22+
:cached_input_cost,
2023
:used_by
2124

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

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ export default class AiLlm extends RestModel {
1414
"api_key",
1515
"enabled_chat_bot",
1616
"provider_params",
17-
"vision_enabled"
17+
"vision_enabled",
18+
"input_cost",
19+
"cached_input_cost",
20+
"output_cost"
1821
);
1922
}
2023

assets/javascripts/discourse/components/ai-llm-editor-form.gjs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ export default class AiLlmEditorForm extends Component {
6161
provider: model.provider,
6262
enabled_chat_bot: model.enabled_chat_bot,
6363
vision_enabled: model.vision_enabled,
64+
input_cost: model.input_cost,
65+
output_cost: model.output_cost,
66+
cached_input_cost: model.cached_input_cost,
6467
provider_params: this.computeProviderParams(
6568
model.provider,
6669
model.provider_params
@@ -176,6 +179,7 @@ export default class AiLlmEditorForm extends Component {
176179

177180
@action
178181
async save(data) {
182+
console.log(data);
179183
this.isSaving = true;
180184
const isNew = this.args.model.isNew;
181185

@@ -258,6 +262,7 @@ export default class AiLlmEditorForm extends Component {
258262
class="ai-llm-editor"
259263
as |form data|
260264
>
265+
{{log this.formData}}
261266
{{#if this.modulesUsingModel}}
262267
<form.Alert @icon="circle-info">
263268
{{this.inUseWarning}}
@@ -382,6 +387,40 @@ export default class AiLlmEditorForm extends Component {
382387
<field.Input @type="number" step="any" min="0" lang="en" />
383388
</form.Field>
384389

390+
<form.InputGroup as |inputGroup|>
391+
<inputGroup.Field
392+
@name="input_cost"
393+
@title={{i18n "discourse_ai.llms.cost_input"}}
394+
@tooltip={{i18n "discourse_ai.llms.hints.cost_input"}}
395+
@helpText={{i18n "discourse_ai.llms.hints.cost_measure"}}
396+
as |field|
397+
>
398+
<field.Input @type="number" step="any" min="0" lang="en" />
399+
400+
</inputGroup.Field>
401+
402+
<inputGroup.Field
403+
@name="cached_input_cost"
404+
@title={{i18n "discourse_ai.llms.cost_cached_input"}}
405+
@tooltip={{i18n "discourse_ai.llms.hints.cost_cached_input"}}
406+
@helpText={{i18n "discourse_ai.llms.hints.cost_measure"}}
407+
as |field|
408+
>
409+
<field.Input @type="number" step="any" min="0" lang="en" />
410+
411+
</inputGroup.Field>
412+
413+
<inputGroup.Field
414+
@name="output_cost"
415+
@title={{i18n "discourse_ai.llms.cost_output"}}
416+
@tooltip={{i18n "discourse_ai.llms.hints.cost_output"}}
417+
@helpText={{i18n "discourse_ai.llms.hints.cost_measure"}}
418+
as |field|
419+
>
420+
<field.Input @type="number" step="any" min="0" lang="en" />
421+
</inputGroup.Field>
422+
</form.InputGroup>
423+
385424
<form.Field
386425
@name="vision_enabled"
387426
@title={{i18n "discourse_ai.llms.vision_enabled"}}

config/locales/client.en.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,10 @@ en:
403403
enabled_chat_bot: "Allow AI bot selector"
404404
vision_enabled: "Vision enabled"
405405
ai_bot_user: "AI bot User"
406+
cost_input: "Input cost"
407+
cost_cached_input: "Cached input cost"
408+
cost_output: "Output cost"
409+
406410
save: "Save"
407411
edit: "Edit"
408412
saved: "LLM model saved"
@@ -485,6 +489,10 @@ en:
485489
name: "We include this in the API call to specify which model we'll use"
486490
vision_enabled: "If enabled, the AI will attempt to understand images. It depends on the model being used supporting vision. Supported by latest models from Anthropic, Google, and OpenAI."
487491
enabled_chat_bot: "If enabled, users can select this model when creating PMs with the AI bot"
492+
cost_input: "The input cost per 1M tokens for this model"
493+
cost_cached_input: "The cached input cost per 1M tokens for this model"
494+
cost_output: "The output cost per 1M tokens for this model"
495+
cost_measure: "$/1M tokens"
488496
providers:
489497
aws_bedrock: "AWS Bedrock"
490498
anthropic: "Anthropic"
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# frozen_string_literal: true
2+
3+
class AddCostMetricsToLlmModel < ActiveRecord::Migration[7.2]
4+
def change
5+
add_column :llm_models, :input_cost, :float
6+
add_column :llm_models, :cached_input_cost, :float
7+
add_column :llm_models, :output_cost, :float
8+
end
9+
end

0 commit comments

Comments
 (0)