2023-12-15 12:32:01 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module DiscourseAi
|
|
|
|
module Completions
|
|
|
|
module Dialects
|
2023-12-18 16:06:01 -05:00
|
|
|
class Gemini < Dialect
|
|
|
|
class << self
|
|
|
|
def can_translate?(model_name)
|
2024-05-22 02:35:29 -04:00
|
|
|
%w[gemini-pro gemini-1.5-pro gemini-1.5-flash].include?(model_name)
|
2023-12-18 16:06:01 -05:00
|
|
|
end
|
2023-12-15 12:32:01 -05:00
|
|
|
end
|
|
|
|
|
2024-05-07 09:02:16 -04:00
|
|
|
def native_tool_support?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2024-05-16 08:50:22 -04:00
|
|
|
def tokenizer
|
|
|
|
llm_model&.tokenizer_class || DiscourseAi::Tokenizer::OpenAiTokenizer ## TODO Replace with GeminiTokenizer
|
|
|
|
end
|
|
|
|
|
2023-12-18 16:06:01 -05:00
|
|
|
def translate
|
2024-01-04 16:15:34 -05:00
|
|
|
# Gemini complains if we don't alternate model/user roles.
|
|
|
|
noop_model_response = { role: "model", parts: { text: "Ok." } }
|
2024-05-07 09:02:16 -04:00
|
|
|
messages = super
|
2024-01-04 16:15:34 -05:00
|
|
|
|
2024-05-07 09:02:16 -04:00
|
|
|
interleving_messages = []
|
|
|
|
previous_message = nil
|
2024-01-12 12:36:44 -05:00
|
|
|
|
2024-05-22 02:35:29 -04:00
|
|
|
system_instruction = nil
|
|
|
|
|
2024-05-07 09:02:16 -04:00
|
|
|
messages.each do |message|
|
2024-05-22 02:35:29 -04:00
|
|
|
if message[:role] == "system"
|
|
|
|
system_instruction = message[:content]
|
|
|
|
next
|
|
|
|
end
|
2024-05-07 09:02:16 -04:00
|
|
|
if previous_message
|
|
|
|
if (previous_message[:role] == "user" || previous_message[:role] == "function") &&
|
|
|
|
message[:role] == "user"
|
|
|
|
interleving_messages << noop_model_response.dup
|
|
|
|
end
|
2024-01-12 12:36:44 -05:00
|
|
|
end
|
2024-05-07 09:02:16 -04:00
|
|
|
interleving_messages << message
|
|
|
|
previous_message = message
|
2024-01-12 12:36:44 -05:00
|
|
|
end
|
2024-02-27 02:24:30 -05:00
|
|
|
|
2024-05-22 02:35:29 -04:00
|
|
|
{ messages: interleving_messages, system_instruction: system_instruction }
|
2023-12-15 12:32:01 -05:00
|
|
|
end
|
|
|
|
|
2023-12-18 16:06:01 -05:00
|
|
|
def tools
|
2024-01-12 12:36:44 -05:00
|
|
|
return if prompt.tools.blank?
|
2023-12-18 16:06:01 -05:00
|
|
|
|
|
|
|
translated_tools =
|
2024-01-12 12:36:44 -05:00
|
|
|
prompt.tools.map do |t|
|
2024-01-04 16:15:34 -05:00
|
|
|
tool = t.slice(:name, :description)
|
2023-12-18 16:06:01 -05:00
|
|
|
|
2024-01-04 16:15:34 -05:00
|
|
|
if t[:parameters]
|
|
|
|
tool[:parameters] = t[:parameters].reduce(
|
|
|
|
{ type: "object", required: [], properties: {} },
|
|
|
|
) do |memo, p|
|
|
|
|
name = p[:name]
|
|
|
|
memo[:required] << name if p[:required]
|
2023-12-18 16:06:01 -05:00
|
|
|
|
2024-01-04 16:15:34 -05:00
|
|
|
memo[:properties][name] = p.except(:name, :required, :item_type)
|
|
|
|
|
|
|
|
memo[:properties][name][:items] = { type: p[:item_type] } if p[:item_type]
|
|
|
|
memo
|
|
|
|
end
|
2023-12-18 16:06:01 -05:00
|
|
|
end
|
|
|
|
|
2024-01-04 16:15:34 -05:00
|
|
|
tool
|
2023-12-18 16:06:01 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
[{ function_declarations: 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 11:46:42 -04:00
|
|
|
|
2024-05-22 02:35:29 -04:00
|
|
|
if model_name.start_with?("gemini-1.5")
|
2024-04-17 01:37:19 -04:00
|
|
|
# technically we support 1 million tokens, but we're being conservative
|
|
|
|
800_000
|
|
|
|
else
|
|
|
|
16_384 # 50% of model tokens
|
|
|
|
end
|
2023-12-18 16:06:01 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def calculate_message_token(context)
|
2024-05-16 08:50:22 -04:00
|
|
|
self.tokenizer.size(context[:content].to_s + context[:name].to_s)
|
2023-12-15 12:32:01 -05:00
|
|
|
end
|
2024-05-07 09:02:16 -04:00
|
|
|
|
2024-05-22 02:35:29 -04:00
|
|
|
def beta_api?
|
|
|
|
@beta_api ||= model_name.start_with?("gemini-1.5")
|
|
|
|
end
|
|
|
|
|
2024-05-07 09:02:16 -04:00
|
|
|
def system_msg(msg)
|
2024-05-22 02:35:29 -04:00
|
|
|
if beta_api?
|
|
|
|
{ role: "system", content: msg[:content] }
|
|
|
|
else
|
|
|
|
{ role: "user", parts: { text: msg[:content] } }
|
|
|
|
end
|
2024-05-07 09:02:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def model_msg(msg)
|
2024-05-22 02:35:29 -04:00
|
|
|
if beta_api?
|
|
|
|
{ role: "model", parts: [{ text: msg[:content] }] }
|
|
|
|
else
|
|
|
|
{ role: "model", parts: { text: msg[:content] } }
|
|
|
|
end
|
2024-05-07 09:02:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def user_msg(msg)
|
2024-05-22 02:35:29 -04:00
|
|
|
if beta_api?
|
|
|
|
# support new format with multiple parts
|
|
|
|
result = { role: "user", parts: [{ text: msg[:content] }] }
|
2024-07-24 15:29:47 -04:00
|
|
|
return result unless vision_support?
|
|
|
|
|
2024-05-22 02:35:29 -04:00
|
|
|
upload_parts = uploaded_parts(msg)
|
|
|
|
result[:parts].concat(upload_parts) if upload_parts.present?
|
|
|
|
result
|
|
|
|
else
|
|
|
|
{ role: "user", parts: { text: msg[:content] } }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def uploaded_parts(message)
|
|
|
|
encoded_uploads = prompt.encoded_uploads(message)
|
|
|
|
result = []
|
|
|
|
if encoded_uploads.present?
|
|
|
|
encoded_uploads.each do |details|
|
|
|
|
result << { inlineData: { mimeType: details[:mime_type], data: details[:base64] } }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
result
|
2024-05-07 09:02:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def tool_call_msg(msg)
|
|
|
|
call_details = JSON.parse(msg[:content], symbolize_names: true)
|
2024-05-22 02:35:29 -04:00
|
|
|
part = {
|
|
|
|
functionCall: {
|
|
|
|
name: msg[:name] || call_details[:name],
|
|
|
|
args: call_details[:arguments],
|
2024-05-07 09:02:16 -04:00
|
|
|
},
|
|
|
|
}
|
2024-05-22 02:35:29 -04:00
|
|
|
|
|
|
|
if beta_api?
|
|
|
|
{ role: "model", parts: [part] }
|
|
|
|
else
|
|
|
|
{ role: "model", parts: part }
|
|
|
|
end
|
2024-05-07 09:02:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def tool_msg(msg)
|
2024-05-22 02:35:29 -04:00
|
|
|
part = {
|
|
|
|
functionResponse: {
|
|
|
|
name: msg[:name] || msg[:id],
|
|
|
|
response: {
|
|
|
|
content: msg[:content],
|
2024-05-07 09:02:16 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2024-05-22 02:35:29 -04:00
|
|
|
|
|
|
|
if beta_api?
|
|
|
|
{ role: "function", parts: [part] }
|
|
|
|
else
|
|
|
|
{ role: "function", parts: part }
|
|
|
|
end
|
2024-05-07 09:02:16 -04:00
|
|
|
end
|
2023-12-15 12:32:01 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|