Module: Raif::Concerns::Llms::OpenAi::MessageFormatting

Extended by:
ActiveSupport::Concern
Included in:
Llms::OpenAi, Llms::OpenRouter
Defined in:
app/models/raif/concerns/llms/open_ai/message_formatting.rb

Instance Method Summary collapse

Instance Method Details

#format_model_file_input_message(file_input) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/models/raif/concerns/llms/open_ai/message_formatting.rb', line 24

def format_model_file_input_message(file_input)
  if file_input.source_type == :url
    raise Raif::Errors::UnsupportedFeatureError, "#{self.class.name} does not support providing a file by URL"
  elsif file_input.source_type == :file_content
    file_params = {
      "filename" => file_input.filename,
      "file_data" => "data:#{file_input.content_type};base64,#{file_input.base64_data}"
    }.compact

    {
      "type" => "file",
      "file" => file_params
    }
  else
    raise Raif::Errors::InvalidModelFileInputError, "Invalid model image input source type: #{file_input.source_type}"
  end
end

#format_model_image_input_message(image_input) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'app/models/raif/concerns/llms/open_ai/message_formatting.rb', line 6

def format_model_image_input_message(image_input)
  if image_input.source_type == :url
    {
      "type" => "image_url",
      "image_url" => { "url" => image_input.url }
    }
  elsif image_input.source_type == :file_content
    {
      "type" => "image_url",
      "image_url" => {
        "url" => "data:#{image_input.content_type};base64,#{image_input.base64_data}"
      }
    }
  else
    raise Raif::Errors::InvalidModelImageInputError, "Invalid model image input source type: #{image_input.source_type}"
  end
end