Module: Raif::Concerns::Llms::BedrockClaude::MessageFormatting

Extended by:
ActiveSupport::Concern
Included in:
Llms::BedrockClaude
Defined in:
app/models/raif/concerns/llms/bedrock_claude/message_formatting.rb

Instance Method Summary collapse

Instance Method Details

#format_for_content_type(content_type) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/models/raif/concerns/llms/bedrock_claude/message_formatting.rb', line 53

def format_for_content_type(content_type)
  {
    "image/png" => "png",
    "image/jpeg" => "jpeg",
    "image/gif" => "gif",
    "image/webp" => "webp",
    "application/pdf" => "pdf",
    "text/csv" => "csv",
    "application/msword" => "doc",
    "application/vnd.openxmlformats-officedocument.wordprocessingml.document" => "docx",
    "application/vnd.ms-excel" => "xls",
    "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" => "xlsx",
    "text/html" => "html",
    "text/plain" => "txt",
    "text/markdown" => "md"
  }[content_type]
end

#format_model_file_input_message(file_input) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/models/raif/concerns/llms/bedrock_claude/message_formatting.rb', line 31

def format_model_file_input_message(file_input)
  if file_input.source_type == :url
    raise Raif::Errors::UnsupportedFeatureError, "AWS Bedrock does not support providing a file by URL"
  elsif file_input.source_type == :file_content
    # The AWS Bedrock SDK requires data sent as bytes (and doesn't support base64 like everyone else)
    # The ModelCompletion stores the messages as JSON though, so it can't be raw bytes (it will throw an encoding error).
    # We store the image data as base64 and then it will get converted to bytes in Raif::Llms::BedrockClaude#perform_model_completion!
    # before sending to AWS.
    {
      "document" => {
        "format" => format_for_content_type(file_input.content_type),
        "name" => File.basename(file_input.filename, File.extname(file_input.filename)), # AWS requires a filename and it cannot include dots from the extension # rubocop:disable Layout/LineLength
        "source" => {
          "tmp_base64_data" => file_input.base64_data
        }
      }
    }
  else
    raise Raif::Errors::InvalidModelFileInputError, "Invalid model file input source type: #{file_input.source_type}"
  end
end

#format_model_image_input_message(image_input) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/models/raif/concerns/llms/bedrock_claude/message_formatting.rb', line 10

def format_model_image_input_message(image_input)
  if image_input.source_type == :url
    raise Raif::Errors::UnsupportedFeatureError, "AWS Bedrock does not support providing an image by URL"
  elsif image_input.source_type == :file_content
    # The AWS Bedrock SDK requires data sent as bytes (and doesn't support base64 like everyone else)
    # The ModelCompletion stores the messages as JSON though, so it can't be raw bytes (it will throw an encoding error).
    # We store the image data as base64 and then it will get converted to bytes in Raif::Llms::BedrockClaude#perform_model_completion!
    # before sending to AWS.
    {
      "image" => {
        "format" => format_for_content_type(image_input.content_type),
        "source" => {
          "tmp_base64_data" => image_input.base64_data
        }
      }
    }
  else
    raise Raif::Errors::InvalidModelImageInputError, "Invalid model image input source type: #{image_input.source_type}"
  end
end

#format_string_message(content) ⇒ Object



6
7
8
# File 'app/models/raif/concerns/llms/bedrock_claude/message_formatting.rb', line 6

def format_string_message(content)
  { "text" => content }
end