Class: OpenAI::Helpers::Streaming::ChatCompletionStream

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

Instance Attribute Summary

Attributes included from Internal::Type::BaseStream

#headers, #status

Instance Method Summary collapse

Methods included from Internal::Type::BaseStream

#close, #each, #inspect, #to_enum

Constructor Details

#initialize(raw_stream:, response_format: nil, input_tools: nil) ⇒ ChatCompletionStream

Returns a new instance of ChatCompletionStream.



9
10
11
12
13
14
15
16
# File 'lib/openai/helpers/streaming/chat_completion_stream.rb', line 9

def initialize(raw_stream:, response_format: nil, input_tools: nil)
  @raw_stream = raw_stream
  @state = ChatCompletionStreamState.new(
    response_format: response_format,
    input_tools: input_tools
  )
  @iterator = iterator
end

Instance Method Details

#current_completion_snapshotObject



40
41
42
# File 'lib/openai/helpers/streaming/chat_completion_stream.rb', line 40

def current_completion_snapshot
  @state.current_completion_snapshot
end

#get_final_completionObject



18
19
20
21
# File 'lib/openai/helpers/streaming/chat_completion_stream.rb', line 18

def get_final_completion
  until_done
  @state.get_final_completion
end

#get_output_textObject



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/openai/helpers/streaming/chat_completion_stream.rb', line 23

def get_output_text
  completion = get_final_completion
  text_parts = []

  completion.choices.each do |choice|
    next unless choice.message.content
    text_parts << choice.message.content
  end

  text_parts.join
end

#textObject



44
45
46
47
48
49
50
# File 'lib/openai/helpers/streaming/chat_completion_stream.rb', line 44

def text
  OpenAI::Internal::Util.chain_fused(@iterator) do |yielder|
    @iterator.each do |event|
      yielder << event.delta if event.is_a?(ChatContentDeltaEvent)
    end
  end
end

#until_doneObject



35
36
37
38
# File 'lib/openai/helpers/streaming/chat_completion_stream.rb', line 35

def until_done
  each {} # rubocop:disable Lint/EmptyBlock
  self
end