2023-11-23 10:58:54 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module DiscourseAi
|
|
|
|
module Completions
|
|
|
|
module Dialects
|
2023-12-18 16:06:01 -05:00
|
|
|
class OrcaStyle < Dialect
|
|
|
|
class << self
|
|
|
|
def can_translate?(model_name)
|
|
|
|
%w[StableBeluga2 Upstage-Llama-2-*-instruct-v2].include?(model_name)
|
|
|
|
end
|
|
|
|
|
|
|
|
def tokenizer
|
|
|
|
DiscourseAi::Tokenizer::Llama2Tokenizer
|
|
|
|
end
|
2023-11-23 10:58:54 -05:00
|
|
|
end
|
|
|
|
|
2023-12-18 16:06:01 -05:00
|
|
|
def translate
|
|
|
|
orca_style_prompt = +<<~TEXT
|
|
|
|
### System:
|
|
|
|
#{prompt[:insts]}
|
|
|
|
#{build_tools_prompt}#{prompt[:post_insts]}
|
|
|
|
TEXT
|
2023-11-23 10:58:54 -05:00
|
|
|
|
2023-12-18 16:06:01 -05:00
|
|
|
if prompt[:examples]
|
|
|
|
prompt[:examples].each do |example_pair|
|
2023-11-23 10:58:54 -05:00
|
|
|
orca_style_prompt << "### User:\n#{example_pair.first}\n"
|
|
|
|
orca_style_prompt << "### Assistant:\n#{example_pair.second}\n"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-12-18 16:06:01 -05:00
|
|
|
orca_style_prompt << "### User:\n#{prompt[:input]}\n"
|
2023-11-23 10:58:54 -05:00
|
|
|
|
|
|
|
orca_style_prompt << "### Assistant:\n"
|
|
|
|
end
|
|
|
|
|
2023-12-18 16:06:01 -05:00
|
|
|
def conversation_context
|
|
|
|
return "" if prompt[:conversation_context].blank?
|
|
|
|
|
2024-01-04 08:44:07 -05:00
|
|
|
clean_context = prompt[:conversation_context].select { |cc| cc[:type] != "tool_call" }
|
2024-01-04 16:15:34 -05:00
|
|
|
flattened_context = flatten_context(clean_context)
|
|
|
|
trimmed_context = trim_context(flattened_context)
|
2023-12-18 16:06:01 -05:00
|
|
|
|
|
|
|
trimmed_context
|
|
|
|
.reverse
|
|
|
|
.reduce(+"") do |memo, context|
|
|
|
|
memo << (context[:type] == "user" ? "### User:" : "### Assistant:")
|
|
|
|
|
|
|
|
if context[:type] == "tool"
|
|
|
|
memo << <<~TEXT
|
|
|
|
|
|
|
|
<function_results>
|
|
|
|
<result>
|
|
|
|
<tool_name>#{context[:name]}</tool_name>
|
|
|
|
<json>
|
|
|
|
#{context[:content]}
|
|
|
|
</json>
|
|
|
|
</result>
|
|
|
|
</function_results>
|
|
|
|
TEXT
|
|
|
|
else
|
|
|
|
memo << " " << context[:content] << "\n"
|
|
|
|
end
|
|
|
|
|
|
|
|
memo
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def max_prompt_tokens
|
|
|
|
SiteSetting.ai_hugging_face_token_limit
|
2023-11-23 10:58:54 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|