Class: OpenAI::Helpers::Streaming::ChatCompletionStream
- Inherits:
-
Object
- Object
- OpenAI::Helpers::Streaming::ChatCompletionStream
show all
- Includes:
- Internal::Type::BaseStream
- Defined in:
- lib/openai/helpers/streaming/chat_completion_stream.rb
Instance Attribute Summary
#headers, #last_response, #status
Instance Method Summary
collapse
#close, #each, #inspect, #observe, #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
17
|
# 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
@last_response = raw_stream.last_response
@state = ChatCompletionStreamState.new(
response_format: response_format,
input_tools: input_tools
)
@iterator = iterator
end
|
Instance Method Details
#current_completion_snapshot ⇒ Object
41
42
43
|
# File 'lib/openai/helpers/streaming/chat_completion_stream.rb', line 41
def current_completion_snapshot
@state.current_completion_snapshot
end
|
#get_final_completion ⇒ Object
19
20
21
22
|
# File 'lib/openai/helpers/streaming/chat_completion_stream.rb', line 19
def get_final_completion
until_done
@state.get_final_completion
end
|
#get_output_text ⇒ Object
24
25
26
27
28
29
30
31
32
33
34
|
# File 'lib/openai/helpers/streaming/chat_completion_stream.rb', line 24
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
|
#text ⇒ Object
45
46
47
48
49
50
51
|
# File 'lib/openai/helpers/streaming/chat_completion_stream.rb', line 45
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_done ⇒ Object
36
37
38
39
|
# File 'lib/openai/helpers/streaming/chat_completion_stream.rb', line 36
def until_done
each { |_event| next }
self
end
|