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) ⇒ Object
Content could be a string or an array.
- #format_messages(messages) ⇒ Object
- #format_string_message(content) ⇒ Object
Instance Method Details
#format_message_content(content) ⇒ 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.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'app/models/raif/concerns/llms/message_formatting.rb', line 18 def (content) 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)] 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) else item end end end |
#format_messages(messages) ⇒ Object
6 7 8 9 10 11 12 13 |
# File 'app/models/raif/concerns/llms/message_formatting.rb', line 6 def () .map do || { "role" => ["role"] || [:role], "content" => (["content"] || [:content]) } end end |
#format_string_message(content) ⇒ Object
37 38 39 |
# File 'app/models/raif/concerns/llms/message_formatting.rb', line 37 def (content) { "type" => "text", "text" => content } end |