2023-03-17 14:14:19 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class CompletionPrompt < ActiveRecord::Base
|
2023-11-27 07:33:31 -05:00
|
|
|
# TODO(roman): Remove may 2024.
|
|
|
|
self.ignored_columns = ["provider"]
|
|
|
|
|
|
|
|
TRANSLATE = -301
|
2023-11-28 10:52:22 -05:00
|
|
|
GENERATE_TITLES = -307
|
2023-11-27 07:33:31 -05:00
|
|
|
PROOFREAD = -303
|
|
|
|
MARKDOWN_TABLE = -304
|
|
|
|
CUSTOM_PROMPT = -305
|
|
|
|
EXPLAIN = -306
|
2023-12-19 14:17:34 -05:00
|
|
|
ILLUSTRATE_POST = -308
|
2023-03-21 11:04:59 -04:00
|
|
|
|
2023-03-17 14:14:19 -04:00
|
|
|
enum :prompt_type, { text: 0, list: 1, diff: 2 }
|
2023-03-21 11:04:59 -04:00
|
|
|
|
|
|
|
validates :messages, length: { maximum: 20 }
|
|
|
|
validate :each_message_length
|
|
|
|
|
2024-02-18 23:21:55 -05:00
|
|
|
after_commit { DiscourseAi::AiHelper::Assistant.clear_prompt_cache! }
|
|
|
|
|
2023-11-27 07:33:31 -05:00
|
|
|
def self.enabled_by_name(name)
|
|
|
|
where(enabled: true).find_by(name: name)
|
|
|
|
end
|
2023-10-23 10:41:36 -04:00
|
|
|
|
2023-12-11 17:26:56 -05:00
|
|
|
attr_accessor :custom_instruction
|
|
|
|
|
2023-11-27 07:33:31 -05:00
|
|
|
def messages_with_input(input)
|
|
|
|
return unless input
|
2023-09-25 14:12:54 -04:00
|
|
|
|
2023-12-11 17:26:56 -05:00
|
|
|
user_input =
|
|
|
|
if id == CUSTOM_PROMPT && custom_instruction.present?
|
|
|
|
"#{custom_instruction}:\n#{input}"
|
|
|
|
else
|
|
|
|
input
|
|
|
|
end
|
|
|
|
|
2024-01-12 12:36:44 -05:00
|
|
|
instructions = [messages_hash[:insts], messages_hash[:post_insts].to_s].join("\n")
|
|
|
|
|
|
|
|
prompt = DiscourseAi::Completions::Prompt.new(instructions)
|
|
|
|
|
|
|
|
messages_hash[:examples].to_a do |example_pair|
|
|
|
|
prompt.push(type: :user, content: example_pair.first)
|
|
|
|
prompt.push(type: :model, content: example_pair.second)
|
|
|
|
end
|
|
|
|
|
|
|
|
prompt.push(type: :user, content: "<input>#{user_input}</input>")
|
|
|
|
|
|
|
|
prompt
|
2023-03-21 11:04:59 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2023-11-27 07:33:31 -05:00
|
|
|
def messages_hash
|
|
|
|
@messages_hash ||= messages.symbolize_keys!
|
|
|
|
end
|
|
|
|
|
2023-03-21 11:04:59 -04:00
|
|
|
def each_message_length
|
|
|
|
messages.each_with_index do |msg, idx|
|
|
|
|
next if msg["content"].length <= 1000
|
|
|
|
|
|
|
|
errors.add(:messages, I18n.t("errors.prompt_message_length", idx: idx + 1))
|
|
|
|
end
|
|
|
|
end
|
2023-03-17 14:14:19 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: completion_prompts
|
|
|
|
#
|
|
|
|
# id :bigint not null, primary key
|
|
|
|
# name :string not null
|
|
|
|
# translated_name :string
|
|
|
|
# prompt_type :integer default("text"), not null
|
|
|
|
# enabled :boolean default(TRUE), not null
|
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
2023-04-10 10:04:42 -04:00
|
|
|
# messages :jsonb
|
2024-01-04 07:53:47 -05:00
|
|
|
# temperature :integer
|
|
|
|
# stop_sequences :string is an Array
|
2023-03-17 14:14:19 -04:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
2023-04-10 10:04:42 -04:00
|
|
|
# index_completion_prompts_on_name (name)
|
2023-03-17 14:14:19 -04:00
|
|
|
#
|