Class: OpenAI::Helpers::Streaming::ChoiceEventState

Inherits:
Object
  • Object
show all
Defined in:
lib/openai/helpers/streaming/chat_completion_stream.rb

Instance Method Summary collapse

Constructor Details

#initialize(input_tools:) ⇒ ChoiceEventState

Returns a new instance of ChoiceEventState.



534
535
536
537
538
539
540
541
542
# File 'lib/openai/helpers/streaming/chat_completion_stream.rb', line 534

def initialize(input_tools:)
  @input_tools = Array(input_tools)
  @content_done = false
  @refusal_done = false
  @logprobs_content_done = false
  @logprobs_refusal_done = false
  @done_tool_calls = Set.new
  @current_tool_call_index = nil
end

Instance Method Details

#get_done_events(choice_chunk:, choice_snapshot:, response_format:) ⇒ Object



544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
# File 'lib/openai/helpers/streaming/chat_completion_stream.rb', line 544

def get_done_events(choice_chunk:, choice_snapshot:, response_format:)
  events = []

  if choice_snapshot.finish_reason
    events.concat(content_done_events(choice_snapshot, response_format))

    if @current_tool_call_index && !@done_tool_calls.include?(@current_tool_call_index)
      event = tool_done_event(choice_snapshot, @current_tool_call_index)
      events << event if event
    end
  end

  Array(choice_chunk.delta.tool_calls).each do |tool_call|
    if @current_tool_call_index != tool_call.index
      events.concat(content_done_events(choice_snapshot, response_format))

      if @current_tool_call_index
        event = tool_done_event(choice_snapshot, @current_tool_call_index)
        events << event if event
      end
    end

    @current_tool_call_index = tool_call.index
  end

  events
end