2023-03-15 16:02:20 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module DiscourseAi
|
|
|
|
module AiHelper
|
|
|
|
class AssistantController < ::ApplicationController
|
|
|
|
requires_plugin ::DiscourseAi::PLUGIN_NAME
|
|
|
|
requires_login
|
|
|
|
before_action :ensure_can_request_suggestions
|
2023-09-14 11:53:44 -04:00
|
|
|
before_action :rate_limiter_performed!, except: %i[prompts]
|
2023-03-15 16:02:20 -04:00
|
|
|
|
|
|
|
def suggest
|
2023-09-14 11:53:44 -04:00
|
|
|
input = get_text_param!
|
2023-03-15 16:02:20 -04:00
|
|
|
|
2023-04-10 10:04:42 -04:00
|
|
|
prompt = CompletionPrompt.find_by(id: params[:mode])
|
2023-09-25 14:12:54 -04:00
|
|
|
|
2023-03-17 14:14:19 -04:00
|
|
|
raise Discourse::InvalidParameters.new(:mode) if !prompt || !prompt.enabled?
|
2023-12-11 17:26:56 -05:00
|
|
|
|
|
|
|
if prompt.id == CompletionPrompt::CUSTOM_PROMPT
|
|
|
|
raise Discourse::InvalidParameters.new(:custom_prompt) if params[:custom_prompt].blank?
|
|
|
|
|
|
|
|
prompt.custom_instruction = params[:custom_prompt]
|
2023-09-25 14:12:54 -04:00
|
|
|
end
|
2023-03-15 16:02:20 -04:00
|
|
|
|
2023-12-19 14:17:34 -05:00
|
|
|
suggest_thumbnails(input) if prompt.id == CompletionPrompt::ILLUSTRATE_POST
|
|
|
|
|
2023-03-15 16:02:20 -04:00
|
|
|
hijack do
|
2023-09-25 14:12:54 -04:00
|
|
|
render json:
|
2023-11-27 07:33:31 -05:00
|
|
|
DiscourseAi::AiHelper::Assistant.new.generate_and_send_prompt(
|
|
|
|
prompt,
|
|
|
|
input,
|
|
|
|
current_user,
|
|
|
|
),
|
2023-03-15 16:02:20 -04:00
|
|
|
status: 200
|
|
|
|
end
|
2024-01-04 07:53:47 -05:00
|
|
|
rescue DiscourseAi::Completions::Endpoints::Base::CompletionFailed
|
2023-03-22 15:00:28 -04:00
|
|
|
render_json_error I18n.t("discourse_ai.ai_helper.errors.completion_request_failed"),
|
|
|
|
status: 502
|
2023-03-15 16:02:20 -04:00
|
|
|
end
|
|
|
|
|
2023-09-01 20:10:58 -04:00
|
|
|
def suggest_title
|
2023-09-14 11:53:44 -04:00
|
|
|
input = get_text_param!
|
2023-09-01 20:10:58 -04:00
|
|
|
|
2023-11-27 07:33:31 -05:00
|
|
|
prompt = CompletionPrompt.enabled_by_name("generate_titles")
|
|
|
|
raise Discourse::InvalidParameters.new(:mode) if !prompt
|
2023-09-01 20:10:58 -04:00
|
|
|
|
|
|
|
hijack do
|
2023-09-25 14:12:54 -04:00
|
|
|
render json:
|
2023-11-27 07:33:31 -05:00
|
|
|
DiscourseAi::AiHelper::Assistant.new.generate_and_send_prompt(
|
2023-09-25 14:12:54 -04:00
|
|
|
prompt,
|
2023-11-27 07:33:31 -05:00
|
|
|
input,
|
|
|
|
current_user,
|
2023-09-25 14:12:54 -04:00
|
|
|
),
|
2023-09-01 20:10:58 -04:00
|
|
|
status: 200
|
|
|
|
end
|
2024-01-04 07:53:47 -05:00
|
|
|
rescue DiscourseAi::Completions::Endpoints::Base::CompletionFailed
|
2023-09-01 20:10:58 -04:00
|
|
|
render_json_error I18n.t("discourse_ai.ai_helper.errors.completion_request_failed"),
|
|
|
|
status: 502
|
|
|
|
end
|
|
|
|
|
|
|
|
def suggest_category
|
2023-09-14 11:53:44 -04:00
|
|
|
input = get_text_param!
|
2023-09-25 14:12:54 -04:00
|
|
|
input_hash = { text: input }
|
2023-09-01 20:10:58 -04:00
|
|
|
|
2023-09-25 14:12:54 -04:00
|
|
|
render json:
|
|
|
|
DiscourseAi::AiHelper::SemanticCategorizer.new(
|
|
|
|
input_hash,
|
|
|
|
current_user,
|
|
|
|
).categories,
|
2023-09-01 20:10:58 -04:00
|
|
|
status: 200
|
|
|
|
end
|
|
|
|
|
|
|
|
def suggest_tags
|
2023-09-14 11:53:44 -04:00
|
|
|
input = get_text_param!
|
2023-09-25 14:12:54 -04:00
|
|
|
input_hash = { text: input }
|
2023-09-01 20:10:58 -04:00
|
|
|
|
2023-09-25 14:12:54 -04:00
|
|
|
render json: DiscourseAi::AiHelper::SemanticCategorizer.new(input_hash, current_user).tags,
|
2023-09-04 13:30:33 -04:00
|
|
|
status: 200
|
2023-09-01 20:10:58 -04:00
|
|
|
end
|
|
|
|
|
2023-12-19 14:17:34 -05:00
|
|
|
def suggest_thumbnails(input)
|
2023-09-14 11:53:44 -04:00
|
|
|
hijack do
|
|
|
|
thumbnails = DiscourseAi::AiHelper::Painter.new.commission_thumbnails(input, current_user)
|
|
|
|
|
|
|
|
render json: { thumbnails: thumbnails }, status: 200
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-10-23 10:41:36 -04:00
|
|
|
def explain
|
|
|
|
post_id = get_post_param!
|
2023-11-27 07:33:31 -05:00
|
|
|
term_to_explain = get_text_param!
|
|
|
|
post = Post.includes(:topic).find_by(id: post_id)
|
2023-10-23 10:41:36 -04:00
|
|
|
|
|
|
|
raise Discourse::InvalidParameters.new(:post_id) unless post
|
|
|
|
|
2023-12-12 12:28:39 -05:00
|
|
|
Jobs.enqueue(
|
|
|
|
:stream_post_helper,
|
|
|
|
post_id: post.id,
|
|
|
|
user_id: current_user.id,
|
|
|
|
term_to_explain: term_to_explain,
|
|
|
|
)
|
|
|
|
|
|
|
|
render json: { success: true }, status: 200
|
2024-01-04 07:53:47 -05:00
|
|
|
rescue DiscourseAi::Completions::Endpoints::Base::CompletionFailed
|
2023-10-23 10:41:36 -04:00
|
|
|
render_json_error I18n.t("discourse_ai.ai_helper.errors.completion_request_failed"),
|
|
|
|
status: 502
|
|
|
|
end
|
|
|
|
|
2023-03-15 16:02:20 -04:00
|
|
|
private
|
|
|
|
|
2023-09-14 11:53:44 -04:00
|
|
|
def get_text_param!
|
|
|
|
params[:text].tap { |t| raise Discourse::InvalidParameters.new(:text) if t.blank? }
|
|
|
|
end
|
|
|
|
|
2023-10-23 10:41:36 -04:00
|
|
|
def get_post_param!
|
|
|
|
params[:post_id].tap { |t| raise Discourse::InvalidParameters.new(:post_id) if t.blank? }
|
|
|
|
end
|
|
|
|
|
2023-09-14 11:53:44 -04:00
|
|
|
def rate_limiter_performed!
|
|
|
|
RateLimiter.new(current_user, "ai_assistant", 6, 3.minutes).performed!
|
|
|
|
end
|
|
|
|
|
2023-03-15 16:02:20 -04:00
|
|
|
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
|