Class: Raif::StreamingResponses::OpenAiCompletions
- Inherits:
-
Object
- Object
- Raif::StreamingResponses::OpenAiCompletions
- Defined in:
- app/models/raif/streaming_responses/open_ai_completions.rb
Instance Attribute Summary collapse
-
#raw_response ⇒ Object
readonly
Returns the value of attribute raw_response.
-
#tool_calls ⇒ Object
readonly
Returns the value of attribute tool_calls.
Instance Method Summary collapse
- #current_response_json ⇒ Object
-
#initialize ⇒ OpenAiCompletions
constructor
A new instance of OpenAiCompletions.
- #process_streaming_event(event_type, event) ⇒ Object
Constructor Details
#initialize ⇒ OpenAiCompletions
Returns a new instance of OpenAiCompletions.
6 7 8 9 10 11 12 13 |
# File 'app/models/raif/streaming_responses/open_ai_completions.rb', line 6 def initialize @id = nil @raw_response = "" @tool_calls = [] @usage = {} @finish_reason = nil @response_json = {} end |
Instance Attribute Details
#raw_response ⇒ Object (readonly)
Returns the value of attribute raw_response.
4 5 6 |
# File 'app/models/raif/streaming_responses/open_ai_completions.rb', line 4 def raw_response @raw_response end |
#tool_calls ⇒ Object (readonly)
Returns the value of attribute tool_calls.
4 5 6 |
# File 'app/models/raif/streaming_responses/open_ai_completions.rb', line 4 def tool_calls @tool_calls end |
Instance Method Details
#current_response_json ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'app/models/raif/streaming_responses/open_ai_completions.rb', line 44 def current_response_json = { "role" => "assistant", "content" => @raw_response } if @tool_calls.any? ["content"] = nil # Per OpenAI spec, content is null if tool_calls are present ["tool_calls"] = @tool_calls.map do |tc| # The streaming format for tool calls is slightly different from the final format. # We need to adjust it here. { "id" => tc["id"], "type" => "function", "function" => { "name" => tc.dig("function", "name"), "arguments" => tc.dig("function", "arguments") } } end end { "id" => @id, "choices" => [{ "index" => 0, "message" => , "finish_reason" => @finish_reason }], "usage" => @usage } end |
#process_streaming_event(event_type, event) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'app/models/raif/streaming_responses/open_ai_completions.rb', line 15 def process_streaming_event(event_type, event) @id ||= event["id"] delta_chunk = event.dig("choices", 0, "delta") finish_reason = event.dig("choices", 0, "finish_reason") @finish_reason ||= finish_reason delta_content = delta_chunk&.dig("content") @raw_response += delta_content if delta_content if delta_chunk&.key?("tool_calls") delta_chunk["tool_calls"].each do |tool_call_chunk| index = tool_call_chunk["index"] @tool_calls[index] ||= { "function" => {} } @tool_calls[index]["id"] ||= tool_call_chunk["id"] if tool_call_chunk.dig("function", "name") @tool_calls[index]["function"]["name"] = tool_call_chunk.dig("function", "name") end if tool_call_chunk.dig("function", "arguments") @tool_calls[index]["function"]["arguments"] ||= "" @tool_calls[index]["function"]["arguments"] += tool_call_chunk.dig("function", "arguments") end end end @usage = event["usage"] if event["usage"] [delta_content, finish_reason] end |