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 Llama2Classic < Dialect
|
|
|
|
class << self
|
|
|
|
def can_translate?(model_name)
|
|
|
|
%w[Llama2-*-chat-hf Llama2-chat-hf].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
|
2024-01-12 12:36:44 -05:00
|
|
|
messages = prompt.messages
|
2023-11-23 10:58:54 -05:00
|
|
|
|
2024-01-12 12:36:44 -05:00
|
|
|
llama2_prompt =
|
|
|
|
trim_messages(messages).reduce(+"") do |memo, msg|
|
|
|
|
next(memo) if msg[:type] == :tool_call
|
2023-12-18 16:06:01 -05:00
|
|
|
|
2024-01-12 12:36:44 -05:00
|
|
|
if msg[:type] == :system
|
|
|
|
memo << (<<~TEXT).strip
|
2023-12-18 16:06:01 -05:00
|
|
|
[INST]
|
2024-01-12 12:36:44 -05:00
|
|
|
<<SYS>>
|
|
|
|
#{msg[:content]}
|
|
|
|
#{build_tools_prompt}
|
|
|
|
<</SYS>>
|
|
|
|
[/INST]
|
|
|
|
TEXT
|
|
|
|
elsif msg[:type] == :model
|
|
|
|
memo << "\n#{msg[:content]}"
|
|
|
|
elsif msg[:type] == :tool
|
2024-04-10 23:26:58 -04:00
|
|
|
JSON.parse(msg[:content], symbolize_names: true)
|
2024-01-12 12:36:44 -05:00
|
|
|
memo << "\n[INST]\n"
|
|
|
|
|
|
|
|
memo << (<<~TEXT).strip
|
2023-12-18 16:06:01 -05:00
|
|
|
<function_results>
|
|
|
|
<result>
|
2024-01-12 12:36:44 -05:00
|
|
|
<tool_name>#{msg[:id]}</tool_name>
|
2023-12-18 16:06:01 -05:00
|
|
|
<json>
|
2024-01-12 12:36:44 -05:00
|
|
|
#{msg[:content]}
|
2023-12-18 16:06:01 -05:00
|
|
|
</json>
|
|
|
|
</result>
|
|
|
|
</function_results>
|
|
|
|
[/INST]
|
|
|
|
TEXT
|
|
|
|
else
|
2024-01-12 12:36:44 -05:00
|
|
|
memo << "\n[INST]#{msg[:content]}[/INST]"
|
2023-12-18 16:06:01 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
memo
|
|
|
|
end
|
2024-01-12 12:36:44 -05:00
|
|
|
|
|
|
|
llama2_prompt << "\n" if llama2_prompt.ends_with?("[/INST]")
|
|
|
|
|
|
|
|
llama2_prompt
|
2023-11-23 10:58:54 -05:00
|
|
|
end
|
|
|
|
|
2023-12-18 16:06:01 -05:00
|
|
|
def max_prompt_tokens
|
|
|
|
SiteSetting.ai_hugging_face_token_limit
|
2023-11-23 10:58:54 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|