discourse-ai/lib/modules/ai_helper/topic_helper.rb
Rafael dos Santos Silva 0e5764617a
FEATURE: AI helper on posts (#244)
Adds an AI Helper function when selecting text while viewing a topic.

---------

Co-authored-by: Keegan George <kgeorge13@gmail.com>
Co-authored-by: Roman Rizzi <roman@discourse.org>
2023-10-23 11:41:36 -03:00

34 lines
1.0 KiB
Ruby

# frozen_string_literal: true
module DiscourseAi
module AiHelper
class TopicHelper
def initialize(input, user, params = {})
@user = user
@text = input[:text]
@params = params
end
def explain
return nil if @text.blank?
return nil unless post = Post.find_by(id: @params[:post])
reply_to = post.topic.first_post
topic = reply_to.topic
llm_prompt =
DiscourseAi::AiHelper::LlmPrompt.new.available_prompts(name_filter: "explain").first
prompt = CompletionPrompt.find_by(id: llm_prompt[:id])
prompt.messages.first["content"].gsub!("{{search}}", @text)
prompt.messages.first["content"].gsub!("{{context}}", post.raw)
prompt.messages.first["content"].gsub!("{{topic}}", topic.title)
# TODO inject this conditionally
#prompt.messages.first["content"].gsub!("{{post}}", reply_to.raw)
DiscourseAi::AiHelper::LlmPrompt.new.generate_and_send_prompt(prompt, nil)
end
end
end
end