2024-06-21 17:32:15 +10:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2024-10-24 05:58:27 +09:00
|
|
|
RSpec.describe "Managing LLM configurations", type: :system, js: true do
|
2024-06-21 17:32:15 +10:00
|
|
|
fab!(:admin)
|
2025-02-04 11:51:01 +01:00
|
|
|
|
2024-12-18 17:39:31 +10:00
|
|
|
let(:page_header) { PageObjects::Components::DPageHeader.new }
|
2025-02-04 11:51:01 +01:00
|
|
|
let(:form) { PageObjects::Components::FormKit.new("form") }
|
2024-06-21 17:32:15 +10:00
|
|
|
|
2024-08-22 11:31:28 -03:00
|
|
|
before do
|
2024-06-24 09:59:42 +10:00
|
|
|
SiteSetting.ai_bot_enabled = true
|
2024-06-21 17:32:15 +10:00
|
|
|
sign_in(admin)
|
2024-08-22 11:31:28 -03:00
|
|
|
end
|
2024-06-21 17:32:15 +10:00
|
|
|
|
2024-08-22 11:31:28 -03:00
|
|
|
it "correctly sets defaults" do
|
|
|
|
visit "/admin/plugins/discourse-ai/ai-llms"
|
|
|
|
|
2024-11-19 09:22:39 +11:00
|
|
|
find("[data-llm-id='anthropic-claude-3-5-haiku'] button").click()
|
2025-02-04 11:51:01 +01:00
|
|
|
form.field("api_key").fill_in("abcd")
|
|
|
|
form.field("enabled_chat_bot").toggle
|
|
|
|
form.submit
|
2024-06-21 17:32:15 +10:00
|
|
|
|
|
|
|
expect(page).to have_current_path("/admin/plugins/discourse-ai/ai-llms")
|
|
|
|
|
|
|
|
llm = LlmModel.order(:id).last
|
2025-02-04 11:51:01 +01:00
|
|
|
|
2024-06-21 17:32:15 +10:00
|
|
|
expect(llm.api_key).to eq("abcd")
|
|
|
|
|
|
|
|
preset = DiscourseAi::Completions::Llm.presets.find { |p| p[:id] == "anthropic" }
|
2024-11-19 09:22:39 +11:00
|
|
|
model_preset = preset[:models].find { |m| m[:name] == "claude-3-5-haiku" }
|
2024-06-21 17:32:15 +10:00
|
|
|
|
2024-11-19 09:22:39 +11:00
|
|
|
expect(llm.name).to eq("claude-3-5-haiku")
|
2024-06-21 17:32:15 +10:00
|
|
|
expect(llm.url).to eq(preset[:endpoint])
|
|
|
|
expect(llm.tokenizer).to eq(preset[:tokenizer].to_s)
|
|
|
|
expect(llm.max_prompt_tokens.to_i).to eq(model_preset[:tokens])
|
|
|
|
expect(llm.provider).to eq("anthropic")
|
|
|
|
expect(llm.display_name).to eq(model_preset[:display_name])
|
2024-06-24 09:59:42 +10:00
|
|
|
expect(llm.user_id).not_to be_nil
|
2024-06-21 17:32:15 +10:00
|
|
|
end
|
2024-08-22 11:31:28 -03:00
|
|
|
|
|
|
|
it "manually configures an LLM" do
|
|
|
|
visit "/admin/plugins/discourse-ai/ai-llms"
|
2025-02-04 11:51:01 +01:00
|
|
|
|
2024-12-18 17:39:31 +10:00
|
|
|
expect(page_header).to be_visible
|
2024-08-22 11:31:28 -03:00
|
|
|
|
2024-09-30 03:15:11 -04:00
|
|
|
find("[data-llm-id='none'] button").click()
|
2024-08-22 11:31:28 -03:00
|
|
|
|
2025-02-04 11:51:01 +01:00
|
|
|
expect(page_header).to be_hidden
|
2024-08-22 11:31:28 -03:00
|
|
|
|
2025-02-04 11:51:01 +01:00
|
|
|
form.field("display_name").fill_in("Self-hosted LLM")
|
|
|
|
form.field("name").fill_in("llava-hf/llava-v1.6-mistral-7b-hf")
|
|
|
|
form.field("url").fill_in("srv://self-hostest.test")
|
|
|
|
form.field("api_key").fill_in("1234")
|
|
|
|
form.field("max_prompt_tokens").fill_in(8000)
|
|
|
|
form.field("provider").select("vllm")
|
|
|
|
form.field("tokenizer").select("DiscourseAi::Tokenizer::Llama3Tokenizer")
|
2025-04-17 14:44:15 -07:00
|
|
|
form.field("max_output_tokens").fill_in(2000)
|
2025-02-04 11:51:01 +01:00
|
|
|
form.field("vision_enabled").toggle
|
|
|
|
form.field("enabled_chat_bot").toggle
|
|
|
|
form.submit
|
2024-08-22 11:31:28 -03:00
|
|
|
|
|
|
|
expect(page).to have_current_path("/admin/plugins/discourse-ai/ai-llms")
|
|
|
|
|
|
|
|
llm = LlmModel.order(:id).last
|
|
|
|
|
|
|
|
expect(llm.display_name).to eq("Self-hosted LLM")
|
|
|
|
expect(llm.name).to eq("llava-hf/llava-v1.6-mistral-7b-hf")
|
|
|
|
expect(llm.url).to eq("srv://self-hostest.test")
|
|
|
|
expect(llm.tokenizer).to eq("DiscourseAi::Tokenizer::Llama3Tokenizer")
|
|
|
|
expect(llm.max_prompt_tokens.to_i).to eq(8000)
|
|
|
|
expect(llm.provider).to eq("vllm")
|
2025-04-17 14:44:15 -07:00
|
|
|
expect(llm.max_output_tokens.to_i).to eq(2000)
|
2024-08-22 11:31:28 -03:00
|
|
|
expect(llm.vision_enabled).to eq(true)
|
|
|
|
expect(llm.user_id).not_to be_nil
|
|
|
|
end
|
2024-10-24 05:58:27 +09:00
|
|
|
|
2025-02-13 12:03:13 +01:00
|
|
|
context "when changing the provider" do
|
2025-02-24 16:38:23 +11:00
|
|
|
it "has the correct provider params when visiting the edit page" do
|
2025-02-25 17:32:12 +11:00
|
|
|
llm =
|
|
|
|
Fabricate(:llm_model, provider: "anthropic", provider_params: { enable_reasoning: true })
|
2025-02-24 16:38:23 +11:00
|
|
|
visit "/admin/plugins/discourse-ai/ai-llms/#{llm.id}/edit"
|
|
|
|
|
|
|
|
expect(form).to have_field_with_name("provider_params.disable_native_tools")
|
2025-02-25 17:32:12 +11:00
|
|
|
expect(form).to have_field_with_name("provider_params.reasoning_tokens")
|
|
|
|
|
|
|
|
reasoning = form.field("provider_params.enable_reasoning")
|
|
|
|
expect(reasoning).to be_checked
|
2025-02-24 16:38:23 +11:00
|
|
|
end
|
2025-02-13 12:03:13 +01:00
|
|
|
it "correctly changes the provider params" do
|
|
|
|
visit "/admin/plugins/discourse-ai/ai-llms"
|
|
|
|
find("[data-llm-id='none'] button").click()
|
|
|
|
form.field("provider").select("vllm")
|
|
|
|
|
|
|
|
expect(form).to have_field_with_name("provider_params.disable_system_prompt")
|
|
|
|
expect(form).to have_no_field_with_name("provider_params.disable_native_tools")
|
|
|
|
|
|
|
|
form.field("provider").select("open_router")
|
|
|
|
|
|
|
|
expect(form).to have_field_with_name("provider_params.disable_streaming")
|
|
|
|
expect(form).to have_field_with_name("provider_params.disable_native_tools")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "updates if the url can be edited" do
|
|
|
|
visit "/admin/plugins/discourse-ai/ai-llms"
|
|
|
|
find("[data-llm-id='none'] button").click()
|
|
|
|
form.field("provider").select("vllm")
|
|
|
|
|
|
|
|
expect(form).to have_field_with_name("url")
|
|
|
|
|
|
|
|
form.field("provider").select("aws_bedrock")
|
|
|
|
|
|
|
|
expect(form).to have_no_field_with_name("url")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2025-02-04 11:51:01 +01:00
|
|
|
context "with quotas" do
|
|
|
|
fab!(:llm_model_1) { Fabricate(:llm_model, name: "claude-2") }
|
|
|
|
fab!(:group_1) { Fabricate(:group) }
|
|
|
|
|
|
|
|
before { Fabricate(:llm_quota, group: group_1, llm_model: llm_model_1, max_tokens: 1000) }
|
|
|
|
|
|
|
|
it "prefills the quotas form" do
|
|
|
|
visit "/admin/plugins/discourse-ai/ai-llms/#{llm_model_1.id}/edit"
|
|
|
|
|
|
|
|
expect(page).to have_selector(
|
|
|
|
".ai-llm-quotas__table .ai-llm-quotas__cell",
|
|
|
|
text: group_1.name,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "can remove a quota" do
|
|
|
|
visit "/admin/plugins/discourse-ai/ai-llms/#{llm_model_1.id}/edit"
|
|
|
|
|
|
|
|
find(".ai-llm-quotas__delete-btn:nth-child(1)").click
|
|
|
|
|
|
|
|
expect(page).to have_no_selector(
|
|
|
|
".ai-llm-quotas__table .ai-llm-quotas__cell",
|
|
|
|
text: group_1.name,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "can add a quota" do
|
|
|
|
visit "/admin/plugins/discourse-ai/ai-llms/#{llm_model_1.id}/edit"
|
|
|
|
find(".ai-llm-editor__add-quota-btn").click
|
|
|
|
select_kit = PageObjects::Components::SelectKit.new(".group-chooser")
|
|
|
|
select_kit.expand
|
|
|
|
select_kit.select_row_by_value(1)
|
|
|
|
form = PageObjects::Components::FormKit.new(".ai-llm-quota-modal form")
|
|
|
|
form.field("max_tokens").fill_in(2000)
|
|
|
|
form.submit
|
|
|
|
|
|
|
|
expect(page).to have_selector(
|
|
|
|
".ai-llm-quotas__table .ai-llm-quotas__cell",
|
|
|
|
text: Group.find(1).name,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-10-24 05:58:27 +09:00
|
|
|
context "when seeded LLM is present" do
|
|
|
|
fab!(:llm_model) { Fabricate(:seeded_model) }
|
|
|
|
|
|
|
|
it "shows the provider as CDCK in the UI" do
|
|
|
|
visit "/admin/plugins/discourse-ai/ai-llms"
|
|
|
|
expect(page).to have_css(
|
2024-11-27 13:34:56 +10:00
|
|
|
"[data-llm-id='cdck-hosted']",
|
2024-10-24 05:58:27 +09:00
|
|
|
text: I18n.t("js.discourse_ai.llms.providers.CDCK"),
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2024-12-20 19:33:45 -05:00
|
|
|
it "seeded LLM has a description" do
|
2024-10-24 05:58:27 +09:00
|
|
|
visit "/admin/plugins/discourse-ai/ai-llms"
|
2024-12-20 19:33:45 -05:00
|
|
|
|
|
|
|
desc = I18n.t("js.discourse_ai.llms.preseeded_model_description", model: llm_model.name)
|
|
|
|
|
2024-10-24 05:58:27 +09:00
|
|
|
expect(page).to have_css(
|
2024-12-20 19:33:45 -05:00
|
|
|
"[data-llm-id='#{llm_model.name}'] .ai-llm-list__description",
|
|
|
|
text: desc,
|
2024-10-24 05:58:27 +09:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2024-12-20 19:33:45 -05:00
|
|
|
it "seeded LLM has a disabled edit button" do
|
2024-10-24 05:58:27 +09:00
|
|
|
visit "/admin/plugins/discourse-ai/ai-llms"
|
2024-12-20 19:33:45 -05:00
|
|
|
expect(page).to have_css("[data-llm-id='cdck-hosted'] .ai-llm-list__edit-disabled-tooltip")
|
2024-10-24 05:58:27 +09:00
|
|
|
end
|
|
|
|
end
|
2024-06-21 17:32:15 +10:00
|
|
|
end
|