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
-
#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
#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 |