Module: Raif::Concerns::Llms::Anthropic::MessageFormatting

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

Instance Method Summary collapse

Instance Method Details

#format_model_file_input_message(file_input) ⇒ Object



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

def format_model_file_input_message(file_input)
  if file_input.source_type == :url
    {
      "type" => "document",
      "source" => {
        "type" => "url",
        "url" => file_input.url
      }
    }
  elsif file_input.source_type == :file_content
    {
      "type" => "document",
      "source" => {
        "type" => "base64",
        "media_type" => file_input.content_type,
        "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



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

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

#format_tool_call_message(tool_call) ⇒ Object



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

def format_tool_call_message(tool_call)
  content_array = []
  content_array << format_string_message(tool_call["assistant_message"]) if tool_call["assistant_message"].present?

  content_array << {
    "type" => "tool_use",
    "id" => tool_call["provider_tool_call_id"],
    "name" => tool_call["name"],
    "input" => tool_call["arguments"]
  }

  {
    "role" => "assistant",
    "content" => content_array
  }
end

#format_tool_call_result_message(tool_call_result) ⇒ Object



69
70
71
72
73
74
75
76
77
78
# File 'app/models/raif/concerns/llms/anthropic/message_formatting.rb', line 69

def format_tool_call_result_message(tool_call_result)
  {
    "role" => "user",
    "content" => [{
      "type" => "tool_result",
      "tool_use_id" => tool_call_result["provider_tool_call_id"],
      "content" => tool_call_result["result"].is_a?(String) ? tool_call_result["result"] : JSON.generate(tool_call_result["result"])
    }]
  }
end