Module: Raif::Concerns::Llms::MessageFormatting
- Extended by:
- ActiveSupport::Concern
- Included in:
- Llm
- Defined in:
- app/models/raif/concerns/llms/message_formatting.rb
Instance Method Summary collapse
- #consolidate_consecutive_role_messages(messages, content_key:) ⇒ Object
-
#format_message_content(content, role: nil) ⇒ Object
Content could be a string or an array.
- #format_messages(messages) ⇒ Object
- #format_string_message(content, role: nil) ⇒ Object
Instance Method Details
#consolidate_consecutive_role_messages(messages, content_key:) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'app/models/raif/concerns/llms/message_formatting.rb', line 48 def (, content_key:) # Bedrock, Anthropic, and Google all model tool results as normal role-based # message content blocks. After formatting, a tool result can therefore be a # "user" message immediately followed by the next user turn. Those providers # expect alternating roles, so their adapters collapse adjacent same-role blocks. return if .size <= 1 .each_with_object([]) do |, consolidated| candidate = .deep_dup = consolidated.last if (, candidate, content_key:) [content_key] += candidate[content_key] else consolidated << candidate end end end |
#format_message_content(content, role: nil) ⇒ Object
Content could be a string or an array. If it’s an array, it could contain Raif::ModelImageInput or Raif::ModelFileInput objects, which need to be formatted according to each model provider’s API.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'app/models/raif/concerns/llms/message_formatting.rb', line 25 def (content, role: nil) raise ArgumentError, "Message content must be an array or a string. Content was: #{content.inspect}" unless content.is_a?(Array) || content.is_a?(String) return [(content, role: role)] if content.is_a?(String) content.map do |item| if item.is_a?(Raif::ModelImageInput) (item) elsif item.is_a?(Raif::ModelFileInput) (item) elsif item.is_a?(String) (item, role: role) else item end end end |
#format_messages(messages) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'app/models/raif/concerns/llms/message_formatting.rb', line 6 def () .map do || if .is_a?(Hash) && ["type"] == "tool_call" () elsif .is_a?(Hash) && ["type"] == "tool_call_result" () else role = ["role"] || [:role] { "role" => role, "content" => (["content"] || [:content], role: role) } end end end |
#format_string_message(content, role: nil) ⇒ Object
44 45 46 |
# File 'app/models/raif/concerns/llms/message_formatting.rb', line 44 def (content, role: nil) { "type" => "text", "text" => content } end |