Class: OpenAI::Helpers::Streaming::ChatCompletionStreamState
- Inherits:
-
Object
- Object
- OpenAI::Helpers::Streaming::ChatCompletionStreamState
- Defined in:
- lib/openai/helpers/streaming/chat_completion_stream.rb
Instance Attribute Summary collapse
-
#current_completion_snapshot ⇒ Object
readonly
Returns the value of attribute current_completion_snapshot.
Instance Method Summary collapse
- #get_final_completion ⇒ Object
-
#handle_chunk(chunk) ⇒ Object
Transforms raw streaming chunks into higher-level events that represent content changes, tool calls, and completion states.
-
#initialize(response_format: nil, input_tools: nil) ⇒ ChatCompletionStreamState
constructor
A new instance of ChatCompletionStreamState.
Constructor Details
#initialize(response_format: nil, input_tools: nil) ⇒ ChatCompletionStreamState
Returns a new instance of ChatCompletionStreamState.
78 79 80 81 82 83 84 |
# File 'lib/openai/helpers/streaming/chat_completion_stream.rb', line 78 def initialize(response_format: nil, input_tools: nil) @current_completion_snapshot = nil @choice_event_states = [] @input_tools = Array(input_tools) @response_format = response_format @rich_response_format = response_format.is_a?(Class) ? response_format : nil end |
Instance Attribute Details
#current_completion_snapshot ⇒ Object (readonly)
Returns the value of attribute current_completion_snapshot.
76 77 78 |
# File 'lib/openai/helpers/streaming/chat_completion_stream.rb', line 76 def current_completion_snapshot @current_completion_snapshot end |
Instance Method Details
#get_final_completion ⇒ Object
86 87 88 89 90 91 |
# File 'lib/openai/helpers/streaming/chat_completion_stream.rb', line 86 def get_final_completion parse_chat_completion( chat_completion: current_completion_snapshot, response_format: @rich_response_format ) end |
#handle_chunk(chunk) ⇒ Object
Transforms raw streaming chunks into higher-level events that represent content changes, tool calls, and completion states. It maintains a running snapshot of the complete response by accumulating data from each chunk.
The method performs the following steps:
1. Unwraps the chunk if it's wrapped in a ChatChunkEvent
2. Filters out non-ChatCompletionChunk objects
3. Accumulates the chunk data into the current completion snapshot
4. Generates appropriate events based on the chunk's content
102 103 104 105 106 107 108 109 |
# File 'lib/openai/helpers/streaming/chat_completion_stream.rb', line 102 def handle_chunk(chunk) chunk = chunk.chunk if chunk.is_a?(ChatChunkEvent) return [] unless chunk.is_a?(OpenAI::Chat::ChatCompletionChunk) @current_completion_snapshot = accumulate_chunk(chunk) build_events(chunk: chunk, completion_snapshot: @current_completion_snapshot) end |