Class: LLMs::Parsers::AnthropicChatResponseStreamParser

Inherits:
Object
  • Object
show all
Includes:
PartialJsonParser
Defined in:
lib/llms/parsers/anthropic_chat_response_stream_parser.rb

Instance Method Summary collapse

Methods included from PartialJsonParser

#attempt_parse_json

Constructor Details

#initialize(emitter) ⇒ AnthropicChatResponseStreamParser

Returns a new instance of AnthropicChatResponseStreamParser.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/llms/parsers/anthropic_chat_response_stream_parser.rb', line 9

def initialize(emitter)
  @emitter = emitter
  @received_jsons = []

  # Message metadata
  @id = nil
  @type = nil
  @role = nil
  @model = nil
  @content = []
  @stop_reason = nil
  @stop_sequence = nil
  @usage = nil
end

Instance Method Details

#full_responseObject



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/llms/parsers/anthropic_chat_response_stream_parser.rb', line 24

def full_response
  {
    'id' => @id,
    'type' => @type,
    'role' => @role,
    'model' => @model,
    'content' => @content,
    'stop_reason' => @stop_reason,
    'stop_sequence' => @stop_sequence,
    'usage' => @usage
  }
end

#handle_json(json) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/llms/parsers/anthropic_chat_response_stream_parser.rb', line 37

def handle_json(json)
  @received_jsons << json

  case json['type']
  when 'message_start'
    handle_message_start(json['message'])
  when 'content_block_start'
    handle_content_block_start(json)
  when 'content_block_delta'
    handle_content_block_delta(json)
  when 'content_block_stop'
    handle_content_block_stop(json)
  when 'message_delta'
    handle_message_delta(json)
  when 'message_stop'
    handle_message_stop
  end
end