2023-04-10 10:04:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module DiscourseAi
|
|
|
|
module AiHelper
|
|
|
|
class LlmPrompt
|
2023-09-01 20:10:58 -04:00
|
|
|
def available_prompts(name_filter: nil)
|
|
|
|
cp = CompletionPrompt
|
|
|
|
cp = cp.where(name: name_filter) if name_filter.present?
|
|
|
|
cp
|
2023-04-10 10:04:42 -04:00
|
|
|
.where(provider: enabled_provider)
|
|
|
|
.where(enabled: true)
|
|
|
|
.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,
|
2023-09-25 14:12:54 -04:00
|
|
|
icon: icon_map(prompt.name),
|
2023-04-10 10:04:42 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-09-25 14:12:54 -04:00
|
|
|
def generate_and_send_prompt(prompt, params)
|
2023-08-25 14:54:51 -04:00
|
|
|
case enabled_provider
|
|
|
|
when "openai"
|
2023-09-25 14:12:54 -04:00
|
|
|
openai_call(prompt, params)
|
2023-08-25 14:54:51 -04:00
|
|
|
when "anthropic"
|
2023-09-25 14:12:54 -04:00
|
|
|
anthropic_call(prompt, params)
|
2023-08-25 14:54:51 -04:00
|
|
|
when "huggingface"
|
2023-09-25 14:12:54 -04:00
|
|
|
huggingface_call(prompt, params)
|
2023-04-10 10:04:42 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def enabled_provider
|
2023-08-25 14:54:51 -04:00
|
|
|
case SiteSetting.ai_helper_model
|
|
|
|
when /gpt/
|
2023-04-10 10:04:42 -04:00
|
|
|
"openai"
|
2023-08-25 14:54:51 -04:00
|
|
|
when /claude/
|
2023-04-10 10:04:42 -04:00
|
|
|
"anthropic"
|
2023-08-25 14:54:51 -04:00
|
|
|
else
|
|
|
|
"huggingface"
|
2023-04-10 10:04:42 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2023-09-25 14:12:54 -04: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"
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-04-10 10:04:42 -04:00
|
|
|
def generate_diff(text, suggestion)
|
|
|
|
cooked_text = PrettyText.cook(text)
|
|
|
|
cooked_suggestion = PrettyText.cook(suggestion)
|
|
|
|
|
|
|
|
DiscourseDiff.new(cooked_text, cooked_suggestion).inline_html
|
|
|
|
end
|
|
|
|
|
|
|
|
def parse_content(prompt, content)
|
|
|
|
return "" if content.blank?
|
|
|
|
|
2023-08-25 14:54:51 -04:00
|
|
|
case enabled_provider
|
|
|
|
when "openai"
|
2023-04-10 10:04:42 -04:00
|
|
|
return content.strip if !prompt.list?
|
|
|
|
|
|
|
|
content.gsub("\"", "").gsub(/\d./, "").split("\n").map(&:strip)
|
2023-08-25 14:54:51 -04:00
|
|
|
when "anthropic"
|
2023-04-10 10:04:42 -04:00
|
|
|
parse_antropic_content(prompt, content)
|
2023-08-25 14:54:51 -04:00
|
|
|
when "huggingface"
|
|
|
|
return [content.strip.delete_prefix('"').delete_suffix('"')] if !prompt.list?
|
|
|
|
|
|
|
|
content.gsub("\"", "").gsub(/\d./, "").split("\n").map(&:strip)
|
2023-04-10 10:04:42 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-09-25 14:12:54 -04:00
|
|
|
def openai_call(prompt, params)
|
2023-04-10 10:04:42 -04:00
|
|
|
result = { type: prompt.prompt_type }
|
|
|
|
|
2023-09-25 14:12:54 -04:00
|
|
|
messages = prompt.messages_with_user_input(params)
|
2023-04-10 10:04:42 -04:00
|
|
|
|
|
|
|
result[:suggestions] = DiscourseAi::Inference::OpenAiCompletions
|
2023-05-11 09:03:03 -04:00
|
|
|
.perform!(messages, SiteSetting.ai_helper_model)
|
2023-04-10 10:04:42 -04:00
|
|
|
.dig(:choices)
|
|
|
|
.to_a
|
|
|
|
.flat_map { |choice| parse_content(prompt, choice.dig(:message, :content).to_s) }
|
|
|
|
.compact_blank
|
|
|
|
|
2023-09-25 14:12:54 -04:00
|
|
|
result[:diff] = generate_diff(params[:text], result[:suggestions].first) if prompt.diff?
|
2023-04-10 10:04:42 -04:00
|
|
|
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
2023-09-25 14:12:54 -04:00
|
|
|
def anthropic_call(prompt, params)
|
2023-04-10 10:04:42 -04:00
|
|
|
result = { type: prompt.prompt_type }
|
|
|
|
|
2023-09-25 14:12:54 -04:00
|
|
|
filled_message = prompt.messages_with_user_input(params)
|
2023-04-10 10:04:42 -04:00
|
|
|
|
|
|
|
message =
|
|
|
|
filled_message.map { |msg| "#{msg["role"]}: #{msg["content"]}" }.join("\n\n") +
|
|
|
|
"Assistant:"
|
|
|
|
|
|
|
|
response = DiscourseAi::Inference::AnthropicCompletions.perform!(message)
|
|
|
|
|
|
|
|
result[:suggestions] = parse_content(prompt, response.dig(:completion))
|
|
|
|
|
2023-09-25 14:12:54 -04:00
|
|
|
result[:diff] = generate_diff(params[:text], result[:suggestions].first) if prompt.diff?
|
2023-04-10 10:04:42 -04:00
|
|
|
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
2023-09-25 14:12:54 -04:00
|
|
|
def huggingface_call(prompt, params)
|
2023-08-25 14:54:51 -04:00
|
|
|
result = { type: prompt.prompt_type }
|
|
|
|
|
2023-09-25 14:12:54 -04:00
|
|
|
message = prompt.messages_with_user_input(params)
|
2023-08-25 14:54:51 -04:00
|
|
|
|
|
|
|
response =
|
|
|
|
DiscourseAi::Inference::HuggingFaceTextGeneration.perform!(
|
|
|
|
message,
|
|
|
|
SiteSetting.ai_helper_model,
|
|
|
|
)
|
|
|
|
|
|
|
|
result[:suggestions] = parse_content(prompt, response.dig(:generated_text))
|
|
|
|
|
2023-09-25 14:12:54 -04:00
|
|
|
result[:diff] = generate_diff(params[:text], result[:suggestions].first) if prompt.diff?
|
2023-08-25 14:54:51 -04:00
|
|
|
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
2023-04-10 10:04:42 -04:00
|
|
|
def parse_antropic_content(prompt, content)
|
|
|
|
if prompt.list?
|
|
|
|
suggestions = Nokogiri::HTML5.fragment(content).search("ai").map(&:text)
|
|
|
|
|
|
|
|
if suggestions.length > 1
|
|
|
|
suggestions
|
|
|
|
else
|
2023-04-10 15:02:44 -04:00
|
|
|
suggestions.first.split("\n").map(&:strip)
|
2023-04-10 10:04:42 -04:00
|
|
|
end
|
|
|
|
else
|
|
|
|
[Nokogiri::HTML5.fragment(content).at("ai").text]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|