Class: OmniAI::Anthropic::Chat::Response::Stream::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/omniai/anthropic/chat/response/stream.rb

Overview

Process the stream into chunks by event.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



21
22
23
# File 'lib/omniai/anthropic/chat/response/stream.rb', line 21

def content
  @content
end

#idObject (readonly)

Returns the value of attribute id.



21
22
23
# File 'lib/omniai/anthropic/chat/response/stream.rb', line 21

def id
  @id
end

#indexObject (readonly)

Returns the value of attribute index.



21
22
23
# File 'lib/omniai/anthropic/chat/response/stream.rb', line 21

def index
  @index
end

#modelObject (readonly)

Returns the value of attribute model.



21
22
23
# File 'lib/omniai/anthropic/chat/response/stream.rb', line 21

def model
  @model
end

#roleObject (readonly)

Returns the value of attribute role.



21
22
23
# File 'lib/omniai/anthropic/chat/response/stream.rb', line 21

def role
  @role
end

Instance Method Details

#chunkOmniAI::Chat::Chunk

Returns:

  • (OmniAI::Chat::Chunk)


24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/omniai/anthropic/chat/response/stream.rb', line 24

def chunk
  OmniAI::Chat::Response::Chunk.new(data: {
    'id' => @id,
    'model' => @model,
    'choices' => [{
      'index' => @index,
      'delta' => {
        'role' => @role,
        'content' => @content,
      },
    }],
  })
end

#content_block_delta(data) ⇒ Object

Handler for Type::CONTENT_BLOCK_DELTA

Parameters:

  • data (Hash)


73
74
75
76
77
# File 'lib/omniai/anthropic/chat/response/stream.rb', line 73

def content_block_delta(data)
  return unless data['delta']['type'].eql?('text_delta')

  @content = data['delta']['text']
end

#content_block_start(data) ⇒ Object

Handler for Type::CONTENT_BLOCK_START

Parameters:

  • data (Hash)


59
60
61
# File 'lib/omniai/anthropic/chat/response/stream.rb', line 59

def content_block_start(data)
  @index = data['index']
end

#content_block_stop(_data) ⇒ Object

Handler for Type::CONTENT_BLOCK_STOP

Parameters:

  • _data (Hash)


66
67
68
# File 'lib/omniai/anthropic/chat/response/stream.rb', line 66

def content_block_stop(_data)
  @index = nil
end

#message_start(data) ⇒ Object

Handler for Type::MESSAGE_START

Parameters:

  • data (Hash)


41
42
43
44
45
# File 'lib/omniai/anthropic/chat/response/stream.rb', line 41

def message_start(data)
  @id = data['id']
  @model = data['model']
  @role = data['role']
end

#message_stop(_data) ⇒ Object

Handler for Type::MESSAGE_STOP

Parameters:

  • _data (Hash)


50
51
52
53
54
# File 'lib/omniai/anthropic/chat/response/stream.rb', line 50

def message_stop(_data)
  @id = nil
  @model = nil
  @role = nil
end