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.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'app/models/raif/concerns/llms/message_formatting.rb', line 19 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 |
# File 'app/models/raif/concerns/llms/message_formatting.rb', line 6 def () .map do || role = ["role"] || [:role] { "role" => role, "content" => (["content"] || [:content], role: role) } end end |
#format_string_message(content, role: nil) ⇒ Object
38 39 40 |
# File 'app/models/raif/concerns/llms/message_formatting.rb', line 38 def (content, role: nil) { "type" => "text", "text" => content } end |