2023-11-27 07:33:31 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module DiscourseAi
|
|
|
|
module AiHelper
|
|
|
|
class Assistant
|
|
|
|
def available_prompts(name_filter: nil)
|
|
|
|
cp = CompletionPrompt
|
|
|
|
|
|
|
|
prompts = name_filter ? [cp.enabled_by_name(name_filter)] : cp.where(enabled: true)
|
|
|
|
|
|
|
|
prompts.map do |prompt|
|
|
|
|
translation =
|
|
|
|
I18n.t("discourse_ai.ai_helper.prompts.#{prompt.name}", default: nil) ||
|
|
|
|
prompt.translated_name || prompt.name
|
|
|
|
|
|
|
|
{
|
|
|
|
id: prompt.id,
|
|
|
|
name: prompt.name,
|
|
|
|
translated_name: translation,
|
|
|
|
prompt_type: prompt.prompt_type,
|
|
|
|
icon: icon_map(prompt.name),
|
|
|
|
location: location_map(prompt.name),
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-12-12 12:28:39 -05:00
|
|
|
def generate_prompt(completion_prompt, input, user, &block)
|
2023-11-28 23:17:46 -05:00
|
|
|
llm = DiscourseAi::Completions::Llm.proxy(SiteSetting.ai_helper_model)
|
2023-11-27 07:33:31 -05:00
|
|
|
generic_prompt = completion_prompt.messages_with_input(input)
|
|
|
|
|
2023-12-12 12:28:39 -05:00
|
|
|
llm.completion!(generic_prompt, user, &block)
|
|
|
|
end
|
|
|
|
|
|
|
|
def generate_and_send_prompt(completion_prompt, input, user)
|
|
|
|
completion_result = generate_prompt(completion_prompt, input, user)
|
2023-11-27 07:33:31 -05:00
|
|
|
result = { type: completion_prompt.prompt_type }
|
|
|
|
|
|
|
|
result[:diff] = parse_diff(input, completion_result) if completion_prompt.diff?
|
|
|
|
|
|
|
|
result[:suggestions] = (
|
|
|
|
if completion_prompt.list?
|
2023-12-12 12:28:39 -05:00
|
|
|
parse_list(completion_result).map { |suggestion| sanitize_result(suggestion) }
|
2023-11-27 07:33:31 -05:00
|
|
|
else
|
2023-12-12 12:28:39 -05:00
|
|
|
[sanitize_result(completion_result)]
|
2023-11-27 07:33:31 -05:00
|
|
|
end
|
|
|
|
)
|
|
|
|
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
2023-12-12 12:28:39 -05:00
|
|
|
def stream_prompt(completion_prompt, input, user, channel)
|
|
|
|
streamed_result = +""
|
|
|
|
start = Time.now
|
|
|
|
|
|
|
|
generate_prompt(completion_prompt, input, user) do |partial_response, cancel_function|
|
|
|
|
streamed_result << partial_response
|
|
|
|
|
|
|
|
# Throttle the updates
|
|
|
|
if (Time.now - start > 0.5) || Rails.env.test?
|
|
|
|
payload = { result: sanitize_result(streamed_result), done: false }
|
|
|
|
publish_update(channel, payload, user)
|
|
|
|
start = Time.now
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
sanitized_result = sanitize_result(streamed_result)
|
|
|
|
if sanitized_result.present?
|
|
|
|
publish_update(channel, { result: sanitized_result, done: true }, user)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-11-27 07:33:31 -05:00
|
|
|
private
|
|
|
|
|
2023-12-12 12:28:39 -05:00
|
|
|
def sanitize_result(result)
|
|
|
|
tags_to_remove = %w[
|
|
|
|
<term>
|
|
|
|
</term>
|
|
|
|
<context>
|
|
|
|
</context>
|
|
|
|
<topic>
|
|
|
|
</topic>
|
|
|
|
<replyTo>
|
|
|
|
</replyTo>
|
|
|
|
<input>
|
|
|
|
</input>
|
|
|
|
<output>
|
|
|
|
</output>
|
2023-12-14 11:47:20 -05:00
|
|
|
<result>
|
|
|
|
</result>
|
2023-12-12 12:28:39 -05:00
|
|
|
]
|
|
|
|
|
|
|
|
result.dup.tap { |dup_result| tags_to_remove.each { |tag| dup_result.gsub!(tag, "") } }
|
|
|
|
end
|
|
|
|
|
|
|
|
def publish_update(channel, payload, user)
|
|
|
|
MessageBus.publish(channel, payload, user_ids: [user.id])
|
|
|
|
end
|
|
|
|
|
2023-11-27 07:33:31 -05:00
|
|
|
def icon_map(name)
|
|
|
|
case name
|
|
|
|
when "translate"
|
|
|
|
"language"
|
|
|
|
when "generate_titles"
|
|
|
|
"heading"
|
|
|
|
when "proofread"
|
|
|
|
"spell-check"
|
|
|
|
when "markdown_table"
|
|
|
|
"table"
|
|
|
|
when "tone"
|
|
|
|
"microphone"
|
|
|
|
when "custom_prompt"
|
|
|
|
"comment"
|
|
|
|
when "rewrite"
|
|
|
|
"pen"
|
|
|
|
when "explain"
|
|
|
|
"question"
|
2023-12-19 14:17:34 -05:00
|
|
|
when "illustrate_post"
|
|
|
|
"images"
|
2023-11-27 07:33:31 -05:00
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def location_map(name)
|
|
|
|
case name
|
|
|
|
when "translate"
|
|
|
|
%w[composer post]
|
|
|
|
when "generate_titles"
|
|
|
|
%w[composer]
|
|
|
|
when "proofread"
|
2023-12-14 22:30:52 -05:00
|
|
|
%w[composer post]
|
2023-11-27 07:33:31 -05:00
|
|
|
when "markdown_table"
|
|
|
|
%w[composer]
|
|
|
|
when "tone"
|
|
|
|
%w[composer]
|
|
|
|
when "custom_prompt"
|
2023-12-14 11:47:20 -05:00
|
|
|
%w[composer post]
|
2023-11-27 07:33:31 -05:00
|
|
|
when "rewrite"
|
|
|
|
%w[composer]
|
|
|
|
when "explain"
|
|
|
|
%w[post]
|
|
|
|
when "summarize"
|
|
|
|
%w[post]
|
2023-12-19 15:55:43 -05:00
|
|
|
when "illustrate_post"
|
2023-12-19 14:17:34 -05:00
|
|
|
%w[composer]
|
2023-11-27 07:33:31 -05:00
|
|
|
else
|
|
|
|
%w[composer post]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def parse_diff(text, suggestion)
|
|
|
|
cooked_text = PrettyText.cook(text)
|
|
|
|
cooked_suggestion = PrettyText.cook(suggestion)
|
|
|
|
|
|
|
|
DiscourseDiff.new(cooked_text, cooked_suggestion).inline_html
|
|
|
|
end
|
|
|
|
|
|
|
|
def parse_list(list)
|
2023-11-28 10:52:22 -05:00
|
|
|
Nokogiri::HTML5.fragment(list).css("item").map(&:text)
|
2023-11-27 07:33:31 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|