2024-05-13 14:54:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module DiscourseAi
|
|
|
|
module Completions
|
|
|
|
module Dialects
|
|
|
|
class OpenAiCompatible < Dialect
|
|
|
|
class << self
|
|
|
|
def can_translate?(_model_name)
|
|
|
|
true
|
|
|
|
end
|
2024-05-16 08:50:22 -04:00
|
|
|
end
|
2024-05-13 14:54:42 -04:00
|
|
|
|
2024-05-16 08:50:22 -04:00
|
|
|
def tokenizer
|
|
|
|
llm_model&.tokenizer_class || DiscourseAi::Tokenizer::Llama3Tokenizer
|
2024-05-13 14:54:42 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def tools
|
|
|
|
@tools ||= tools_dialect.translated_tools
|
|
|
|
end
|
|
|
|
|
|
|
|
def max_prompt_tokens
|
2024-05-16 08:50:22 -04:00
|
|
|
return llm_model.max_prompt_tokens if llm_model&.max_prompt_tokens
|
2024-05-13 14:54:42 -04:00
|
|
|
|
|
|
|
32_000
|
|
|
|
end
|
|
|
|
|
2024-08-21 10:41:55 -04:00
|
|
|
def translate
|
|
|
|
translated = super
|
|
|
|
|
|
|
|
return translated unless llm_model.lookup_custom_param("disable_system_prompt")
|
|
|
|
|
2024-08-23 15:41:57 -04:00
|
|
|
system_msg, user_msg = translated.shift(2)
|
|
|
|
|
|
|
|
if user_msg[:content].is_a?(Array) # Has inline images.
|
|
|
|
user_msg[:content].first[:text] = [
|
|
|
|
system_msg[:content],
|
|
|
|
user_msg[:content].first[:text],
|
|
|
|
].join("\n")
|
|
|
|
else
|
|
|
|
user_msg[:content] = [system_msg[:content], user_msg[:content]].join("\n")
|
|
|
|
end
|
2024-08-21 10:41:55 -04:00
|
|
|
|
|
|
|
translated.unshift(user_msg)
|
|
|
|
end
|
|
|
|
|
2024-05-13 14:54:42 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
def system_msg(msg)
|
2024-08-02 08:52:33 -04:00
|
|
|
msg = { role: "system", content: msg[:content] }
|
|
|
|
|
|
|
|
if tools_dialect.instructions.present?
|
|
|
|
msg[:content] = msg[:content].dup << "\n\n#{tools_dialect.instructions}"
|
|
|
|
end
|
|
|
|
|
|
|
|
msg
|
2024-05-13 14:54:42 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def model_msg(msg)
|
|
|
|
{ role: "assistant", content: msg[:content] }
|
|
|
|
end
|
|
|
|
|
|
|
|
def tool_call_msg(msg)
|
2024-08-02 08:52:33 -04:00
|
|
|
translated = tools_dialect.from_raw_tool_call(msg)
|
|
|
|
{ role: "assistant", content: translated }
|
2024-05-13 14:54:42 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def tool_msg(msg)
|
2024-08-02 08:52:33 -04:00
|
|
|
translated = tools_dialect.from_raw_tool(msg)
|
|
|
|
{ role: "user", content: translated }
|
2024-05-13 14:54:42 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def user_msg(msg)
|
|
|
|
content = +""
|
|
|
|
content << "#{msg[:id]}: " if msg[:id]
|
|
|
|
content << msg[:content]
|
|
|
|
|
2024-07-24 15:29:47 -04:00
|
|
|
message = { role: "user", content: content }
|
|
|
|
|
|
|
|
message[:content] = inline_images(message[:content], msg) if vision_support?
|
|
|
|
|
|
|
|
message
|
|
|
|
end
|
|
|
|
|
|
|
|
def inline_images(content, message)
|
|
|
|
encoded_uploads = prompt.encoded_uploads(message)
|
|
|
|
return content if encoded_uploads.blank?
|
|
|
|
|
2024-09-11 21:28:08 -04:00
|
|
|
encoded_uploads.reduce([{ type: "text", text: message[:content] }]) do |memo, details|
|
|
|
|
memo << {
|
|
|
|
type: "image_url",
|
|
|
|
image_url: {
|
|
|
|
url: "data:#{details[:mime_type]};base64,#{details[:base64]}",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
end
|
2024-05-13 14:54:42 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|