Module: Raif::Concerns::Llms::OpenAiResponses::MessageFormatting
- Extended by:
- ActiveSupport::Concern
- Included in:
- Llms::OpenAiResponses
- Defined in:
- app/models/raif/concerns/llms/open_ai_responses/message_formatting.rb
Instance Method Summary collapse
- #format_messages(messages) ⇒ Object
- #format_model_file_input_message(file_input) ⇒ Object
- #format_model_image_input_message(image_input) ⇒ Object
- #format_string_message(content, role: nil) ⇒ Object
-
#format_tool_call_message(tool_call) ⇒ Object
The reasoning items that preceded the call go back in front of it, in their original order, so the model continues from its own reasoning.
- #format_tool_call_result_message(tool_call_result) ⇒ Object
Instance Method Details
#format_messages(messages) ⇒ Object
44 45 46 |
# File 'app/models/raif/concerns/llms/open_ai_responses/message_formatting.rb', line 44 def () super.flatten end |
#format_model_file_input_message(file_input) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'app/models/raif/concerns/llms/open_ai_responses/message_formatting.rb', line 30 def (file_input) if file_input.source_type == :url raise Raif::Errors::UnsupportedFeatureError, "#{self.class.name} does not support providing a file by URL" elsif file_input.source_type == :file_content { "type" => "input_file", "filename" => file_input.filename, "file_data" => "data:#{file_input.content_type};base64,#{file_input.base64_data}" } else raise Raif::Errors::InvalidModelFileInputError, "Invalid model image input source type: #{file_input.source_type}" end end |
#format_model_image_input_message(image_input) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'app/models/raif/concerns/llms/open_ai_responses/message_formatting.rb', line 14 def (image_input) if image_input.source_type == :url { "type" => "input_image", "image_url" => image_input.url } elsif image_input.source_type == :file_content { "type" => "input_image", "image_url" => "data:#{image_input.content_type};base64,#{image_input.base64_data}" } else raise Raif::Errors::InvalidModelImageInputError, "Invalid model image input source type: #{image_input.source_type}" end end |
#format_string_message(content, role: nil) ⇒ Object
6 7 8 9 10 11 12 |
# File 'app/models/raif/concerns/llms/open_ai_responses/message_formatting.rb', line 6 def (content, role: nil) if role == "assistant" { "type" => "output_text", "text" => content } else { "type" => "input_text", "text" => content } end end |
#format_tool_call_message(tool_call) ⇒ Object
The reasoning items that preceded the call go back in front of it, in their
original order, so the model continues from its own reasoning. The call keeps its
original id only then: OpenAI rejects an fc_ id sent without its reasoning item.
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'app/models/raif/concerns/llms/open_ai_responses/message_formatting.rb', line 51 def (tool_call) function_call = { "type" => "function_call", "call_id" => tool_call["provider_tool_call_id"], "name" => tool_call["name"], "arguments" => JSON.generate(tool_call["arguments"]) } = tool_call["provider_metadata"] || {} reasoning_items = Array(["reasoning_items"]) return function_call if reasoning_items.empty? function_call["id"] = ["item_id"] if ["item_id"].present? function_call["status"] = ["item_status"] if ["item_status"].present? [*reasoning_items, function_call] end |
#format_tool_call_result_message(tool_call_result) ⇒ Object
68 69 70 71 72 73 74 |
# File 'app/models/raif/concerns/llms/open_ai_responses/message_formatting.rb', line 68 def (tool_call_result) { "type" => "function_call_output", "call_id" => tool_call_result["provider_tool_call_id"], "output" => tool_call_result["result"].is_a?(String) ? tool_call_result["result"] : JSON.generate(tool_call_result["result"]) } end |