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

Commit 80fbdd7

Browse files
committed
correct handling and error messages
1 parent a580ecd commit 80fbdd7

File tree

4 files changed

+16
-17
lines changed

4 files changed

+16
-17
lines changed

app/models/llm_quota.rb

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,25 @@ class LlmQuota < ActiveRecord::Base
1616

1717
validate :at_least_one_limit
1818

19-
def self.within_quota?(llm, user)
19+
def self.check_quotas!(llm, user)
2020
return true if user.blank?
2121
quotas = joins(:group).where(llm_model: llm).where(group: user.groups)
2222

2323
return true if quotas.empty?
24-
quotas.each do |quota|
25-
usage = LlmQuotaUsage.find_or_create_for(user: user, llm_quota: quota)
26-
begin
27-
usage.check_quota!
28-
rescue LlmQuotaUsage::QuotaExceededError
29-
return false
24+
errors =
25+
quotas.map do |quota|
26+
usage = LlmQuotaUsage.find_or_create_for(user: user, llm_quota: quota)
27+
begin
28+
usage.check_quota!
29+
nil
30+
rescue LlmQuotaUsage::QuotaExceededError => e
31+
e
32+
end
3033
end
31-
end
3234

33-
true
35+
return if errors.include?(nil)
36+
37+
raise errors.first
3438
end
3539

3640
def self.log_usage(llm, user, input_tokens, output_tokens)

app/models/llm_quota_usage.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,7 @@ def check_quota!
6060
raise QuotaExceededError.new(
6161
I18n.t(
6262
"discourse_ai.errors.quota_exceeded",
63-
group: llm_quota.group.name,
64-
reset_at: reset_at,
63+
relative_time: AgeWords.distance_of_time_in_words(reset_at, Time.now),
6564
),
6665
)
6766
end

config/locales/server.en.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ en:
446446
bedrock_invalid_url: "Please complete all the fields to use this model."
447447

448448
errors:
449-
quota_exceeded: "You have exceeded the quota for this model. Please try again after %{reset_at}."
449+
quota_exceeded: "You have exceeded the quota for this model. Please try again in %{relative_time}."
450450
quota_required: "You must specify maximum tokens or usages for this model."
451451
no_query_specified: The query parameter is required, please specify it.
452452
no_user_for_persona: The persona specified does not have a user associated with it.

lib/completions/endpoints/base.rb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,7 @@ def perform_completion!(
6565
partial_tool_calls: false,
6666
&blk
6767
)
68-
if !LlmQuota.within_quota?(@llm_model, user)
69-
raise LlmQuotaUsage::QuotaExceededError.new(
70-
I18n.t("discourse_ai.errors.quota_exceeded"),
71-
)
72-
end
68+
LlmQuota.check_quotas!(@llm_model, user)
7369

7470
@partial_tool_calls = partial_tool_calls
7571
model_params = normalize_model_params(model_params)

0 commit comments

Comments
 (0)