Skip to content

Commit e2b0287

Browse files
authored
FEATURE: Enhance LLM context window settings (#1271)
### 🔍 Overview This update performs some enhancements to the LLM configuration screen. In particular, it renames the UI for the number of tokens for the prompt to "Context window" since the naming can be confusing to the user. Additionally, it adds a new optional field called "Max output tokens".
1 parent 0c8718e commit e2b0287

File tree

8 files changed

+41
-3
lines changed

8 files changed

+41
-3
lines changed

app/controllers/discourse_ai/admin/ai_llms_controller.rb

+1
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ def ai_llm_params(updating: nil)
157157
:provider,
158158
:tokenizer,
159159
:max_prompt_tokens,
160+
:max_output_tokens,
160161
:api_key,
161162
:enabled_chat_bot,
162163
:vision_enabled,

app/models/llm_model.rb

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ 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
1617
validate :required_provider_params
1718
scope :in_use,
1819
-> do
@@ -183,4 +184,5 @@ def required_provider_params
183184
# enabled_chat_bot :boolean default(FALSE), not null
184185
# provider_params :jsonb
185186
# vision_enabled :boolean default(FALSE), not null
187+
# max_output_tokens :integer
186188
#

app/serializers/llm_model_serializer.rb

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class LlmModelSerializer < ApplicationSerializer
1111
:name,
1212
:provider,
1313
:max_prompt_tokens,
14+
:max_output_tokens,
1415
:tokenizer,
1516
:api_key,
1617
:url,

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

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export default class AiLlm extends RestModel {
1010
"provider",
1111
"tokenizer",
1212
"max_prompt_tokens",
13+
"max_output_tokens",
1314
"url",
1415
"api_key",
1516
"enabled_chat_bot",

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

+23-1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export default class AiLlmEditorForm extends Component {
4040

4141
return {
4242
max_prompt_tokens: modelInfo.tokens,
43+
max_output_tokens: modelInfo.max_output_tokens,
4344
tokenizer: info.tokenizer,
4445
url: modelInfo.endpoint || info.endpoint,
4546
display_name: modelInfo.display_name,
@@ -53,6 +54,7 @@ export default class AiLlmEditorForm extends Component {
5354

5455
return {
5556
max_prompt_tokens: model.max_prompt_tokens,
57+
max_output_tokens: model.max_output_tokens,
5658
api_key: model.api_key,
5759
tokenizer: model.tokenizer,
5860
url: model.url,
@@ -183,8 +185,18 @@ export default class AiLlmEditorForm extends Component {
183185
this.isSaving = true;
184186
const isNew = this.args.model.isNew;
185187

188+
const updatedData = {
189+
...data,
190+
};
191+
192+
// If max_prompt_tokens input is cleared,
193+
// we want the db to store null
194+
if (!data.max_output_tokens) {
195+
updatedData.max_output_tokens = null;
196+
}
197+
186198
try {
187-
await this.args.model.save(data);
199+
await this.args.model.save(updatedData);
188200

189201
if (isNew) {
190202
this.args.llms.addObject(this.args.model);
@@ -399,6 +411,16 @@ export default class AiLlmEditorForm extends Component {
399411
<field.Input @type="number" step="any" min="0" lang="en" />
400412
</form.Field>
401413

414+
<form.Field
415+
@name="max_output_tokens"
416+
@title={{i18n "discourse_ai.llms.max_output_tokens"}}
417+
@tooltip={{i18n "discourse_ai.llms.hints.max_output_tokens"}}
418+
@format="large"
419+
as |field|
420+
>
421+
<field.Input @type="number" step="any" min="0" lang="en" />
422+
</form.Field>
423+
402424
<form.Field
403425
@name="vision_enabled"
404426
@title={{i18n "discourse_ai.llms.vision_enabled"}}

config/locales/client.en.yml

+4-2
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,8 @@ en:
397397
name: "Model id"
398398
provider: "Provider"
399399
tokenizer: "Tokenizer"
400-
max_prompt_tokens: "Number of tokens for the prompt"
400+
max_prompt_tokens: "Context window"
401+
max_output_tokens: "Max output tokens"
401402
url: "URL of the service hosting the model"
402403
api_key: "API Key of the service hosting the model"
403404
enabled_chat_bot: "Allow AI bot selector"
@@ -480,7 +481,8 @@ en:
480481
failure: "Trying to contact the model returned this error: %{error}"
481482

482483
hints:
483-
max_prompt_tokens: "Max numbers of tokens for the prompt. As a rule of thumb, this should be 50% of the model's context window."
484+
max_prompt_tokens: "The maximum number of tokens the model can process in a single request"
485+
max_output_tokens: "The maximum number of tokens the model can generate in a single request"
484486
display_name: "The name used to reference this model across your site's interface."
485487
name: "We include this in the API call to specify which model we'll use"
486488
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."
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# frozen_string_literal: true
2+
3+
class AddMaxOutputTokensToLlmModel < ActiveRecord::Migration[7.2]
4+
def change
5+
add_column :llm_models, :max_output_tokens, :integer
6+
end
7+
end

spec/system/llms/ai_llm_spec.rb

+2
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
form.field("max_prompt_tokens").fill_in(8000)
5454
form.field("provider").select("vllm")
5555
form.field("tokenizer").select("DiscourseAi::Tokenizer::Llama3Tokenizer")
56+
form.field("max_output_tokens").fill_in(2000)
5657
form.field("vision_enabled").toggle
5758
form.field("enabled_chat_bot").toggle
5859
form.submit
@@ -67,6 +68,7 @@
6768
expect(llm.tokenizer).to eq("DiscourseAi::Tokenizer::Llama3Tokenizer")
6869
expect(llm.max_prompt_tokens.to_i).to eq(8000)
6970
expect(llm.provider).to eq("vllm")
71+
expect(llm.max_output_tokens.to_i).to eq(2000)
7072
expect(llm.vision_enabled).to eq(true)
7173
expect(llm.user_id).not_to be_nil
7274
end

0 commit comments

Comments
 (0)