From bed044448c98c77bb0f7417a10e9076636468e1e Mon Sep 17 00:00:00 2001 From: Roman Rizzi Date: Tue, 30 Jul 2024 13:44:57 -0300 Subject: [PATCH] DEV: Remove old code now that features rely on LlmModels. (#729) * DEV: Remove old code now that features rely on LlmModels. * Hide old settings and migrate persona llm overrides * Remove shadowing special URL + seeding code. Use srv:// prefix instead. --- .../discourse_ai/admin/ai_llms_controller.rb | 4 +- app/models/llm_model.rb | 36 +----- app/serializers/llm_model_serializer.rb | 2 +- .../components/ai-llm-editor-form.gjs | 16 +-- config/locales/client.en.yml | 1 - config/settings.yml | 48 +++++--- db/fixtures/602_srv_backed_llm_model.rb | 8 -- .../20240724174343_migrate_vision_llms.rb | 3 +- ...0729202857_migrate_persona_llm_override.rb | 46 ++++++++ lib/completions/dialects/chat_gpt.rb | 35 +----- lib/completions/dialects/claude.rb | 15 +-- lib/completions/dialects/command.rb | 27 +---- lib/completions/dialects/dialect.rb | 21 ++-- lib/completions/dialects/gemini.rb | 21 +--- lib/completions/endpoints/anthropic.rb | 28 +---- lib/completions/endpoints/aws_bedrock.rb | 77 +++++-------- lib/completions/endpoints/base.rb | 39 ++----- lib/completions/endpoints/canned_response.rb | 4 - lib/completions/endpoints/cohere.rb | 24 +--- lib/completions/endpoints/fake.rb | 18 +-- lib/completions/endpoints/gemini.rb | 35 +----- lib/completions/endpoints/hugging_face.rb | 27 +---- lib/completions/endpoints/ollama.rb | 22 +--- lib/completions/endpoints/open_ai.rb | 90 ++------------- lib/completions/endpoints/vllm.rb | 31 ++---- lib/completions/llm.rb | 104 ++++-------------- lib/configuration/llm_validator.rb | 13 +-- plugin.rb | 4 - spec/fabricators/llm_model_fabricator.rb | 62 +++++++++++ .../lib/completions/dialects/chat_gpt_spec.rb | 4 +- spec/lib/completions/dialects/claude_spec.rb | 10 +- .../completions/dialects/dialect_context.rb | 6 +- spec/lib/completions/dialects/dialect_spec.rb | 6 +- spec/lib/completions/dialects/gemini_spec.rb | 9 +- .../completions/endpoints/anthropic_spec.rb | 20 +--- .../completions/endpoints/aws_bedrock_spec.rb | 25 ++--- spec/lib/completions/endpoints/cohere_spec.rb | 7 +- .../endpoints/endpoint_compliance.rb | 6 +- spec/lib/completions/endpoints/gemini_spec.rb | 17 +-- .../endpoints/hugging_face_spec.rb | 19 +--- .../lib/completions/endpoints/open_ai_spec.rb | 9 +- spec/lib/completions/endpoints/vllm_spec.rb | 17 +-- spec/lib/completions/llm_spec.rb | 9 +- .../discourse_automation/llm_report_spec.rb | 6 +- .../discourse_automation/llm_triage_spec.rb | 4 +- spec/lib/modules/ai_bot/bot_spec.rb | 4 +- .../modules/ai_bot/personas/persona_spec.rb | 4 +- spec/lib/modules/ai_bot/playground_spec.rb | 15 +-- .../ai_bot/question_consolidator_spec.rb | 2 +- spec/lib/modules/ai_bot/tools/dall_e_spec.rb | 4 +- .../modules/ai_bot/tools/db_schema_spec.rb | 5 +- .../tools/discourse_meta_search_spec.rb | 10 +- .../ai_bot/tools/github_file_content_spec.rb | 3 +- .../tools/github_pull_request_diff_spec.rb | 3 +- .../ai_bot/tools/github_search_code_spec.rb | 3 +- .../ai_bot/tools/github_search_files_spec.rb | 3 +- spec/lib/modules/ai_bot/tools/google_spec.rb | 5 +- spec/lib/modules/ai_bot/tools/image_spec.rb | 2 +- .../ai_bot/tools/javascript_evaluator_spec.rb | 5 +- .../ai_bot/tools/list_categories_spec.rb | 5 +- .../modules/ai_bot/tools/list_tags_spec.rb | 5 +- spec/lib/modules/ai_bot/tools/read_spec.rb | 5 +- .../ai_bot/tools/search_settings_spec.rb | 8 +- spec/lib/modules/ai_bot/tools/search_spec.rb | 7 +- .../ai_bot/tools/setting_context_spec.rb | 6 +- .../modules/ai_bot/tools/summarize_spec.rb | 5 +- spec/lib/modules/ai_bot/tools/time_spec.rb | 5 +- .../modules/ai_bot/tools/web_browser_spec.rb | 10 +- .../lib/modules/automation/llm_triage_spec.rb | 13 ++- .../modules/automation/report_runner_spec.rb | 12 +- spec/models/ai_tool_spec.rb | 2 +- spec/models/llm_model_spec.rb | 64 ----------- spec/plugin_helper.rb | 2 +- 73 files changed, 439 insertions(+), 813 deletions(-) delete mode 100644 db/fixtures/602_srv_backed_llm_model.rb create mode 100644 db/post_migrate/20240729202857_migrate_persona_llm_override.rb delete mode 100644 spec/models/llm_model_spec.rb diff --git a/app/controllers/discourse_ai/admin/ai_llms_controller.rb b/app/controllers/discourse_ai/admin/ai_llms_controller.rb index 72cd7b13..2292e98c 100644 --- a/app/controllers/discourse_ai/admin/ai_llms_controller.rb +++ b/app/controllers/discourse_ai/admin/ai_llms_controller.rb @@ -110,9 +110,7 @@ module DiscourseAi ) provider = updating ? updating.provider : permitted[:provider] - permit_url = - (updating && updating.url != LlmModel::RESERVED_VLLM_SRV_URL) || - provider != LlmModel::BEDROCK_PROVIDER_NAME + permit_url = provider != LlmModel::BEDROCK_PROVIDER_NAME permitted[:url] = params.dig(:ai_llm, :url) if permit_url diff --git a/app/models/llm_model.rb b/app/models/llm_model.rb index 73666dd5..8e6049b4 100644 --- a/app/models/llm_model.rb +++ b/app/models/llm_model.rb @@ -2,44 +2,10 @@ class LlmModel < ActiveRecord::Base FIRST_BOT_USER_ID = -1200 - RESERVED_VLLM_SRV_URL = "https://vllm.shadowed-by-srv.invalid" BEDROCK_PROVIDER_NAME = "aws_bedrock" belongs_to :user - validates :url, exclusion: { in: [RESERVED_VLLM_SRV_URL] }, if: :url_changed? - - def self.seed_srv_backed_model - srv = SiteSetting.ai_vllm_endpoint_srv - srv_model = find_by(url: RESERVED_VLLM_SRV_URL) - - if srv.present? - if srv_model.present? - current_key = SiteSetting.ai_vllm_api_key - srv_model.update!(api_key: current_key) if current_key != srv_model.api_key - else - record = - new( - display_name: "vLLM SRV LLM", - name: "mistralai/Mixtral-8x7B-Instruct-v0.1", - provider: "vllm", - tokenizer: "DiscourseAi::Tokenizer::MixtralTokenizer", - url: RESERVED_VLLM_SRV_URL, - max_prompt_tokens: 8000, - api_key: SiteSetting.ai_vllm_api_key, - ) - - record.save(validate: false) # Ignore reserved URL validation - end - else - # Clean up companion users - srv_model&.enabled_chat_bot = false - srv_model&.toggle_companion_user - - srv_model&.destroy! - end - end - def self.provider_params { aws_bedrock: { @@ -54,7 +20,7 @@ class LlmModel < ActiveRecord::Base end def to_llm - DiscourseAi::Completions::Llm.proxy_from_obj(self) + DiscourseAi::Completions::Llm.proxy("custom:#{id}") end def toggle_companion_user diff --git a/app/serializers/llm_model_serializer.rb b/app/serializers/llm_model_serializer.rb index 71a1c1b9..2ef1b28b 100644 --- a/app/serializers/llm_model_serializer.rb +++ b/app/serializers/llm_model_serializer.rb @@ -19,6 +19,6 @@ class LlmModelSerializer < ApplicationSerializer has_one :user, serializer: BasicUserSerializer, embed: :object def shadowed_by_srv - object.url == LlmModel::RESERVED_VLLM_SRV_URL + object.url.to_s.starts_with?("srv://") end end diff --git a/assets/javascripts/discourse/components/ai-llm-editor-form.gjs b/assets/javascripts/discourse/components/ai-llm-editor-form.gjs index 20ce95db..48d239a5 100644 --- a/assets/javascripts/discourse/components/ai-llm-editor-form.gjs +++ b/assets/javascripts/discourse/components/ai-llm-editor-form.gjs @@ -60,17 +60,9 @@ export default class AiLlmEditorForm extends Component { return this.testRunning || this.testResult !== null; } - get displaySRVWarning() { - return this.args.model.shadowed_by_srv && !this.args.model.isNew; - } - get canEditURL() { // Explicitly false. - if (this.metaProviderParams.url_editable === false) { - return false; - } - - return !this.args.model.shadowed_by_srv || this.args.model.isNew; + return this.metaProviderParams.url_editable !== false; } @computed("args.model.provider") @@ -174,12 +166,6 @@ export default class AiLlmEditorForm extends Component { }