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
|
|
|
|
2023-03-17 14:14:19 -04:00
|
|
|
def prompts
|
|
|
|
render json:
|
|
|
|
ActiveModel::ArraySerializer.new(
|
2023-04-10 10:04:42 -04:00
|
|
|
DiscourseAi::AiHelper::LlmPrompt.new.available_prompts,
|
2023-03-17 14:14:19 -04:00
|
|
|
root: false,
|
|
|
|
),
|
|
|
|
status: 200
|
|
|
|
end
|
|
|
|
|
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-09-25 14:12:54 -04:00
|
|
|
if prompt.prompt_type == "custom_prompt" && params[:custom_prompt].blank?
|
|
|
|
raise Discourse::InvalidParameters.new(:custom_prompt)
|
|
|
|
end
|
2023-03-15 16:02:20 -04:00
|
|
|
|
|
|
|
hijack do
|
2023-09-25 14:12:54 -04:00
|
|
|
render json:
|
|
|
|
DiscourseAi::AiHelper::LlmPrompt.new.generate_and_send_prompt(prompt, params),
|
2023-03-15 16:02:20 -04:00
|
|
|
status: 200
|
|
|
|
end
|
2023-04-10 10:04:42 -04:00
|
|
|
rescue ::DiscourseAi::Inference::OpenAiCompletions::CompletionFailed,
|
2023-08-25 14:54:51 -04:00
|
|
|
::DiscourseAi::Inference::HuggingFaceTextGeneration::CompletionFailed,
|
2023-04-10 10:04:42 -04:00
|
|
|
::DiscourseAi::Inference::AnthropicCompletions::CompletionFailed => e
|
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-25 14:12:54 -04:00
|
|
|
input_hash = { text: input }
|
2023-09-01 20:10:58 -04:00
|
|
|
|
|
|
|
llm_prompt =
|
|
|
|
DiscourseAi::AiHelper::LlmPrompt
|
|
|
|
.new
|
|
|
|
.available_prompts(name_filter: "generate_titles")
|
|
|
|
.first
|
|
|
|
prompt = CompletionPrompt.find_by(id: llm_prompt[:id])
|
|
|
|
raise Discourse::InvalidParameters.new(:mode) if !prompt || !prompt.enabled?
|
|
|
|
|
|
|
|
hijack do
|
2023-09-25 14:12:54 -04:00
|
|
|
render json:
|
|
|
|
DiscourseAi::AiHelper::LlmPrompt.new.generate_and_send_prompt(
|
|
|
|
prompt,
|
|
|
|
input_hash,
|
|
|
|
),
|
2023-09-01 20:10:58 -04:00
|
|
|
status: 200
|
|
|
|
end
|
|
|
|
rescue ::DiscourseAi::Inference::OpenAiCompletions::CompletionFailed,
|
|
|
|
::DiscourseAi::Inference::HuggingFaceTextGeneration::CompletionFailed,
|
|
|
|
::DiscourseAi::Inference::AnthropicCompletions::CompletionFailed => e
|
|
|
|
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-09-14 11:53:44 -04:00
|
|
|
def suggest_thumbnails
|
|
|
|
input = get_text_param!
|
|
|
|
|
|
|
|
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!
|
|
|
|
text = get_text_param!
|
|
|
|
post = Post.find_by(id: post_id)
|
|
|
|
|
|
|
|
raise Discourse::InvalidParameters.new(:post_id) unless post
|
|
|
|
|
|
|
|
render json:
|
|
|
|
DiscourseAi::AiHelper::TopicHelper.new(
|
|
|
|
{ text: text },
|
|
|
|
current_user,
|
|
|
|
post: post,
|
|
|
|
).explain,
|
|
|
|
status: 200
|
|
|
|
rescue ::DiscourseAi::Inference::OpenAiCompletions::CompletionFailed,
|
|
|
|
::DiscourseAi::Inference::HuggingFaceTextGeneration::CompletionFailed,
|
|
|
|
::DiscourseAi::Inference::AnthropicCompletions::CompletionFailed => e
|
|
|
|
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
|