This repository was archived by the owner on Jul 22, 2025. It is now read-only.
generated from discourse/discourse-plugin-skeleton
-
Notifications
You must be signed in to change notification settings - Fork 40
FEATURE: llm quotas #1047
Merged
Merged
FEATURE: llm quotas #1047
Changes from 26 commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
f0fd10a
FEATURE: llm quotas
SamSaffron 7937908
most of the backend for a quota system is now done
SamSaffron 4b834d8
add a controller
SamSaffron df65032
test for quotas
SamSaffron 667368b
working on quota creation
SamSaffron 6cc1840
quota update logic
SamSaffron 5b46db4
fix updating tokens etc
SamSaffron 18a90b2
created a quota selector
SamSaffron 7af4adc
This is all working now!
SamSaffron e6a31b9
use prettier (just set it up via conform)
SamSaffron 0514117
fix broken system spec
SamSaffron edcb34c
correct handling and error messages
SamSaffron 5e31f52
remove puts
SamSaffron 0d12b82
fix specs
SamSaffron 02787fb
Update app/models/llm_quota.rb
SamSaffron 710723f
Update config/locales/server.en.yml
SamSaffron 3f321a1
Update assets/javascripts/discourse/components/ai-llm-quota-editor.gjs
SamSaffron 44c2c0a
Update config/locales/client.en.yml
SamSaffron 45f543b
Update assets/javascripts/discourse/components/ai-llm-quota-editor.gjs
SamSaffron 52a21c2
Update spec/fabricators/llm_quota_fabricator.rb
SamSaffron dc51ea0
Update spec/fabricators/llm_quota_usage_fabricator.rb
SamSaffron e8425b5
Update spec/requests/admin/ai_llms_controller_spec.rb
SamSaffron 0e0cb14
Update spec/models/llm_quota_spec.rb
SamSaffron 5b5c38c
consistently use import { i18n } from "discourse-i18n"; in all
SamSaffron 45f368e
render modal without a service
SamSaffron e35b5dc
we need the import for lookup
SamSaffron 7d1244c
Update assets/javascripts/discourse/components/ai-llm-quota-editor.gjs
SamSaffron 073d160
Update assets/javascripts/discourse/components/ai-llm-editor-form.gjs
SamSaffron 564a22d
compatability
SamSaffron 4cb5a29
add tooltips remove invalid yaml
SamSaffron File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
app/controllers/discourse_ai/admin/ai_llm_quotas_controller.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module DiscourseAi | ||
| module Admin | ||
| class AiLlmQuotasController < ::Admin::AdminController | ||
| requires_plugin ::DiscourseAi::PLUGIN_NAME | ||
|
|
||
| def index | ||
| quotas = LlmQuota.includes(:group) | ||
|
|
||
| render json: { | ||
| quotas: | ||
| ActiveModel::ArraySerializer.new(quotas, each_serializer: LlmQuotaSerializer), | ||
| } | ||
| end | ||
|
|
||
| def create | ||
| quota = LlmQuota.new(quota_params) | ||
|
|
||
| if quota.save | ||
| render json: LlmQuotaSerializer.new(quota), status: :created | ||
| else | ||
| render_json_error quota | ||
| end | ||
| end | ||
|
|
||
| def update | ||
| quota = LlmQuota.find(params[:id]) | ||
|
|
||
| if quota.update(quota_params) | ||
| render json: LlmQuotaSerializer.new(quota) | ||
| else | ||
| render_json_error quota | ||
| end | ||
| end | ||
|
|
||
| def destroy | ||
| quota = LlmQuota.find(params[:id]) | ||
| quota.destroy! | ||
|
|
||
| head :no_content | ||
| rescue ActiveRecord::RecordNotFound | ||
| render json: { error: I18n.t("not_found") }, status: 404 | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def quota_params | ||
| params.require(:quota).permit( | ||
| :group_id, | ||
| :llm_model_id, | ||
| :max_tokens, | ||
| :max_usages, | ||
| :duration_seconds, | ||
| ) | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| class LlmQuota < ActiveRecord::Base | ||
| self.table_name = "llm_quotas" | ||
|
|
||
| belongs_to :group | ||
| belongs_to :llm_model | ||
| has_many :llm_quota_usages | ||
|
|
||
| validates :group_id, presence: true | ||
| # we can not validate on create cause it breaks build | ||
| validates :llm_model_id, presence: true, on: :update | ||
| validates :duration_seconds, presence: true, numericality: { greater_than: 0 } | ||
| validates :max_tokens, numericality: { only_integer: true, greater_than: 0, allow_nil: true } | ||
| validates :max_usages, numericality: { greater_than: 0, allow_nil: true } | ||
|
|
||
| validate :at_least_one_limit | ||
|
|
||
| def self.check_quotas!(llm, user) | ||
| return true if user.blank? | ||
| quotas = joins(:group).where(llm_model: llm).where(group: user.groups) | ||
|
|
||
| return true if quotas.empty? | ||
| errors = | ||
| quotas.map do |quota| | ||
| usage = LlmQuotaUsage.find_or_create_for(user: user, llm_quota: quota) | ||
| begin | ||
| usage.check_quota! | ||
| nil | ||
| rescue LlmQuotaUsage::QuotaExceededError => e | ||
| e | ||
| end | ||
| end | ||
|
|
||
| return if errors.include?(nil) | ||
|
|
||
| raise errors.first | ||
| end | ||
|
|
||
| def self.log_usage(llm, user, input_tokens, output_tokens) | ||
| return if user.blank? | ||
|
|
||
| quotas = joins(:group).where(llm_model: llm).where(group: user.groups) | ||
|
|
||
| quotas.each do |quota| | ||
| usage = LlmQuotaUsage.find_or_create_for(user: user, llm_quota: quota) | ||
| usage.increment_usage!(input_tokens: input_tokens, output_tokens: output_tokens) | ||
| end | ||
| end | ||
|
|
||
| def available_tokens | ||
| max_tokens | ||
| end | ||
|
|
||
| def available_usages | ||
| max_usages | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def at_least_one_limit | ||
| if max_tokens.nil? && max_usages.nil? | ||
| errors.add(:base, I18n.t("discourse_ai.errors.quota_required")) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| # == Schema Information | ||
| # | ||
| # Table name: llm_quotas | ||
| # | ||
| # id :bigint not null, primary key | ||
| # group_id :bigint not null | ||
| # llm_model_id :bigint not null | ||
| # max_tokens :integer | ||
| # max_usages :integer | ||
| # duration_seconds :integer not null | ||
| # created_at :datetime not null | ||
| # updated_at :datetime not null | ||
| # | ||
| # Indexes | ||
| # | ||
| # index_llm_quotas_on_group_id_and_llm_model_id (group_id,llm_model_id) UNIQUE | ||
| # index_llm_quotas_on_llm_model_id (llm_model_id) | ||
| # | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| class LlmQuotaUsage < ActiveRecord::Base | ||
| self.table_name = "llm_quota_usages" | ||
|
|
||
| QuotaExceededError = Class.new(StandardError) | ||
|
|
||
| belongs_to :user | ||
| belongs_to :llm_quota | ||
|
|
||
| validates :user_id, presence: true | ||
| validates :llm_quota_id, presence: true | ||
| validates :input_tokens_used, presence: true, numericality: { greater_than_or_equal_to: 0 } | ||
| validates :output_tokens_used, presence: true, numericality: { greater_than_or_equal_to: 0 } | ||
| validates :usages, presence: true, numericality: { greater_than_or_equal_to: 0 } | ||
| validates :started_at, presence: true | ||
| validates :reset_at, presence: true | ||
|
|
||
| def self.find_or_create_for(user:, llm_quota:) | ||
| usage = find_or_initialize_by(user: user, llm_quota: llm_quota) | ||
|
|
||
| if usage.new_record? | ||
| now = Time.current | ||
| usage.started_at = now | ||
| usage.reset_at = now + llm_quota.duration_seconds.seconds | ||
| usage.input_tokens_used = 0 | ||
| usage.output_tokens_used = 0 | ||
| usage.usages = 0 | ||
| usage.save! | ||
| end | ||
|
|
||
| usage | ||
| end | ||
|
|
||
| def reset_if_needed! | ||
| return if Time.current < reset_at | ||
|
|
||
| now = Time.current | ||
| update!( | ||
| input_tokens_used: 0, | ||
| output_tokens_used: 0, | ||
| usages: 0, | ||
| started_at: now, | ||
| reset_at: now + llm_quota.duration_seconds.seconds, | ||
| ) | ||
| end | ||
|
|
||
| def increment_usage!(input_tokens:, output_tokens:) | ||
| reset_if_needed! | ||
|
|
||
| increment!(:usages) | ||
| increment!(:input_tokens_used, input_tokens) | ||
| increment!(:output_tokens_used, output_tokens) | ||
| end | ||
|
|
||
| def check_quota! | ||
| reset_if_needed! | ||
|
|
||
| if quota_exceeded? | ||
| raise QuotaExceededError.new( | ||
| I18n.t( | ||
| "discourse_ai.errors.quota_exceeded", | ||
| relative_time: AgeWords.distance_of_time_in_words(reset_at, Time.now), | ||
| ), | ||
| ) | ||
| end | ||
| end | ||
|
|
||
| def quota_exceeded? | ||
| return false if !llm_quota | ||
|
|
||
| (llm_quota.max_tokens.present? && total_tokens_used > llm_quota.max_tokens) || | ||
| (llm_quota.max_usages.present? && usages > llm_quota.max_usages) | ||
| end | ||
|
|
||
| def total_tokens_used | ||
| input_tokens_used + output_tokens_used | ||
| end | ||
|
|
||
| def remaining_tokens | ||
| return nil if llm_quota.max_tokens.nil? | ||
| [0, llm_quota.max_tokens - total_tokens_used].max | ||
| end | ||
|
|
||
| def remaining_usages | ||
| return nil if llm_quota.max_usages.nil? | ||
| [0, llm_quota.max_usages - usages].max | ||
| end | ||
|
|
||
| def percentage_tokens_used | ||
| return 0 if llm_quota.max_tokens.nil? || llm_quota.max_tokens.zero? | ||
| [(total_tokens_used.to_f / llm_quota.max_tokens * 100).round, 100].min | ||
| end | ||
|
|
||
| def percentage_usages_used | ||
| return 0 if llm_quota.max_usages.nil? || llm_quota.max_usages.zero? | ||
| [(usages.to_f / llm_quota.max_usages * 100).round, 100].min | ||
| end | ||
| end | ||
|
|
||
| # == Schema Information | ||
| # | ||
| # Table name: llm_quota_usages | ||
| # | ||
| # id :bigint not null, primary key | ||
| # user_id :bigint not null | ||
| # llm_quota_id :bigint not null | ||
| # input_tokens_used :integer not null | ||
| # output_tokens_used :integer not null | ||
| # usages :integer not null | ||
| # started_at :datetime not null | ||
| # reset_at :datetime not null | ||
| # created_at :datetime not null | ||
| # updated_at :datetime not null | ||
| # | ||
| # Indexes | ||
| # | ||
| # index_llm_quota_usages_on_llm_quota_id (llm_quota_id) | ||
| # index_llm_quota_usages_on_user_id_and_llm_quota_id (user_id,llm_quota_id) UNIQUE | ||
| # |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| class LlmQuotaSerializer < ApplicationSerializer | ||
| attributes :id, :group_id, :llm_model_id, :max_tokens, :max_usages, :duration_seconds, :group_name | ||
|
|
||
| def group_name | ||
| object.group.name | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,7 +3,7 @@ import { tracked } from "@glimmer/tracking"; | |
| import { action } from "@ember/object"; | ||
| import { getOwner } from "@ember/owner"; | ||
| import { service } from "@ember/service"; | ||
| import I18n from "discourse-i18n"; | ||
| import { i18n } from "discourse-i18n"; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See internal: |
||
| import DToast from "float-kit/components/d-toast"; | ||
| import DToastInstance from "float-kit/lib/d-toast-instance"; | ||
| import AiHelperOptionsList from "../components/ai-helper-options-list"; | ||
|
|
@@ -45,7 +45,7 @@ export default class AiComposerHelperMenu extends Component { | |
| this.siteSettings.available_locales | ||
| ); | ||
| const locale = availableLocales.find((l) => l.value === siteLocale); | ||
| const translatedName = I18n.t( | ||
| const translatedName = i18n( | ||
| "discourse_ai.ai_helper.context_menu.translate_prompt", | ||
| { | ||
| language: locale.name, | ||
|
|
@@ -90,7 +90,7 @@ export default class AiComposerHelperMenu extends Component { | |
| data: { | ||
| theme: "error", | ||
| icon: "triangle-exclamation", | ||
| message: I18n.t("discourse_ai.ai_helper.no_content_error"), | ||
| message: i18n("discourse_ai.ai_helper.no_content_error"), | ||
| }, | ||
| }; | ||
|
|
||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.