From f99fe7e1ed5ce8c6cb03e008be43f425d0e86cc0 Mon Sep 17 00:00:00 2001 From: Roman Rizzi Date: Wed, 15 Mar 2023 17:02:20 -0300 Subject: [PATCH] FEATURE: Composer AI helper (#8) * FEATURE: Composer AI helper This change introduces a new composer button for the group members listed in the `ai_helper_allowed_groups` site setting. Users can use chatGPT to review, improve, or translate their posts to English. * Add a safeguard for PMs and don't rely on parentView --- .../ai_helper/assistant_controller.rb | 43 ++++++ .../discourse/components/ai-helper.hbs | 67 +++++++++ .../discourse/components/ai-helper.js | 128 ++++++++++++++++++ .../discourse/components/model-accuracies.hbs | 4 +- .../templates/modal/composer-ai-helper.hbs | 1 + .../initializers/composer-ai-helper.js | 70 ++++++++++ assets/stylesheets/.gitkeep | 0 .../modules/ai-helper/common/ai-helper.scss | 27 ++++ config/locales/client.en.yml | 10 +- config/locales/server.en.yml | 19 ++- config/routes.rb | 10 ++ config/settings.yml | 14 ++ lib/discourse_ai/engine.rb | 2 +- lib/modules/ai_helper/entry_point.rb | 14 ++ lib/modules/ai_helper/open_ai_prompt.rb | 92 +++++++++++++ lib/shared/inference/openai_completions.rb | 6 +- lib/shared/inference/openai_embeddings.rb | 2 +- plugin.rb | 4 + .../modules/ai_helper/open_ai_prompt_spec.rb | 64 +++++++++ .../ai_helper/assistant_controller_spec.rb | 68 ++++++++++ .../openai_completions_inference_stubs.rb | 96 +++++++++++++ .../ai_helper/ai_composer_helper_spec.rb | 86 ++++++++++++ spec/system/page_objects/modals/ai_helper.rb | 24 ++++ 23 files changed, 841 insertions(+), 10 deletions(-) create mode 100644 app/controllers/discourse_ai/ai_helper/assistant_controller.rb create mode 100644 assets/javascripts/discourse/components/ai-helper.hbs create mode 100644 assets/javascripts/discourse/components/ai-helper.js create mode 100644 assets/javascripts/discourse/templates/modal/composer-ai-helper.hbs create mode 100644 assets/javascripts/initializers/composer-ai-helper.js delete mode 100644 assets/stylesheets/.gitkeep create mode 100644 assets/stylesheets/modules/ai-helper/common/ai-helper.scss create mode 100644 config/routes.rb create mode 100644 lib/modules/ai_helper/entry_point.rb create mode 100644 lib/modules/ai_helper/open_ai_prompt.rb create mode 100644 spec/lib/modules/ai_helper/open_ai_prompt_spec.rb create mode 100644 spec/requests/ai_helper/assistant_controller_spec.rb create mode 100644 spec/support/openai_completions_inference_stubs.rb create mode 100644 spec/system/ai_helper/ai_composer_helper_spec.rb create mode 100644 spec/system/page_objects/modals/ai_helper.rb diff --git a/app/controllers/discourse_ai/ai_helper/assistant_controller.rb b/app/controllers/discourse_ai/ai_helper/assistant_controller.rb new file mode 100644 index 00000000..d9863e67 --- /dev/null +++ b/app/controllers/discourse_ai/ai_helper/assistant_controller.rb @@ -0,0 +1,43 @@ +# frozen_string_literal: true + +module DiscourseAi + module AiHelper + class AssistantController < ::ApplicationController + requires_plugin ::DiscourseAi::PLUGIN_NAME + requires_login + before_action :ensure_can_request_suggestions + + def suggest + raise Discourse::InvalidParameters.new(:text) if params[:text].blank? + + if !DiscourseAi::AiHelper::OpenAiPrompt::VALID_TYPES.include?(params[:mode]) + raise Discourse::InvalidParameters.new(:mode) + end + + RateLimiter.new(current_user, "ai_assistant", 6, 3.minutes).performed! + + hijack do + render json: + DiscourseAi::AiHelper::OpenAiPrompt.new.generate_and_send_prompt( + params[:mode], + params[:text], + ), + status: 200 + end + end + + private + + def ensure_can_request_suggestions + user_group_ids = current_user.group_ids + + allowed = + SiteSetting.ai_helper_allowed_groups_map.any? do |group_id| + user_group_ids.include?(group_id) + end + + raise Discourse::InvalidAccess if !allowed + end + end + end +end diff --git a/assets/javascripts/discourse/components/ai-helper.hbs b/assets/javascripts/discourse/components/ai-helper.hbs new file mode 100644 index 00000000..bcec19f8 --- /dev/null +++ b/assets/javascripts/discourse/components/ai-helper.hbs @@ -0,0 +1,67 @@ + + {{i18n "discourse_ai.ai_helper.description"}} + + + +
+