Module: Intelligence::Anthropic::ChatResponseMethods

Defined in:
lib/intelligence/adapters/anthropic/chat_response_methods.rb

Instance Method Summary collapse

Instance Method Details

#chat_result_attributes(response) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/intelligence/adapters/anthropic/chat_response_methods.rb', line 5

def chat_result_attributes( response )
  return nil unless response.success?

  response_json = JSON.parse( response.body, symbolize_names: true ) rescue nil
  return nil if response_json.nil? 

  result = {}
  
  result_choice = { 
    end_reason: translate_end_result( response_json[ :stop_reason ] ),
    end_sequence: response_json[ :stop_sequence ], 
  }

  if response_json[ :content ] && 
     response_json[ :content ].is_a?( Array ) &&
     !response_json[ :content ].empty?

    result_content = []
    response_json[ :content ].each do | content |
      if content[ :type ] == 'text'
        result_content.push( { type: 'text', text: content[ :text ] } )
      elsif content[ :type ] == 'tool_use'
        result_content.push( { 
          type: :tool_call, 
          tool_call_id: content[ :id ],
          tool_name: content[ :name ],
          tool_parameters: content[ :input ]
        } )
      end
    end

    unless result_content.empty?
      result_choice[ :message ] = {
        role: response_json[ :role ] || 'assistant',
        contents: result_content
      }
    end

  end

  result[ :choices ] = [ result_choice ]

  metrics_json = response_json[ :usage ]
  unless metrics_json.nil?

    result_metrics = {}
    result_metrics[ :input_tokens ] = metrics_json[ :input_tokens ] 
    result_metrics[ :output_tokens ] = metrics_json[ :output_tokens ]
    result_metrics = result_metrics.compact

    result[ :metrics ] = result_metrics unless result_metrics.empty?

  end

  result 
end

#chat_result_error_attributes(response) ⇒ Object Also known as: stream_result_error_attributes



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/intelligence/adapters/anthropic/chat_response_methods.rb', line 62

def chat_result_error_attributes( response )
  error_type, error_description = translate_response_status( response.status )
  parsed_body = JSON.parse( response.body, symbolize_names: true ) rescue nil 
  if parsed_body && parsed_body.respond_to?( :include? ) && parsed_body.include?( :error )
    result = {
      error_type: error_type.to_s,
      error: parsed_body[ :error ][ :type ],
      error_description: parsed_body[ :error ][ :message ] || error_description
    }
  elsif 
    result = {
      error_type: error_type.to_s,
      error_description: error_description
    } 
  end
  result 
end

#stream_result_attributes(context) ⇒ Object



171
172
173
174
175
176
177
178
179
# File 'lib/intelligence/adapters/anthropic/chat_response_methods.rb', line 171

def stream_result_attributes( context )
  {
    choices: [ { 
      end_reason: translate_end_result( context[ :end_reason ] ),
      end_sequence: context[ :end_sequence ], 
    } ],
    metrics: context[ :metrics ]
  }
end

#stream_result_chunk_attributes(context, chunk) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/intelligence/adapters/anthropic/chat_response_methods.rb', line 80

def stream_result_chunk_attributes( context, chunk )
  context ||= {}

  buffer = context[ :buffer ] || ''
  contents = context[ :contents ] || []    
  end_reason = context[ :end_reason ]
  end_sequence = context[ :end_sequence ]
  metrics = context[ :metrics ] || {
    input_tokens: 0,
    output_tokens:  0
  }

  contents.map! do | content |
    { type: content[ :type ] }
  end

  buffer += chunk
  while ( eol_index = buffer.index( "\n" ) )
    
    line = buffer.slice!( 0..eol_index )
    line = line.strip 
    next if line.empty? || !line.start_with?( 'data:' )

    line = line[ 6..-1 ]
    data = JSON.parse( line )
    
    case data[ 'type' ]
      when 'message_start'
        metrics[ :input_tokens ] += data[ 'message' ]&.[]( 'usage' )&.[]( 'input_tokens' ) || 0
        metrics[ :output_tokens ] += data[ 'message' ]&.[]( 'usage' )&.[]( 'output_tokens' ) || 0
      when 'content_block_start'
        index = data[ 'index' ]
        contents.fill( {}, contents.size..index ) if contents.size <= index
        if content_block = data[ 'content_block' ]
          if content_block[ 'type' ] == 'text'
            contents[ index ] = {
              type: :text,
              text: content_block[ 'text' ] || '' 
            }
          elsif content_block[ 'type' ] == 'tool_use'
            contents[ index ] = {
              type: :tool_call,
              tool_name: content_block[ 'name' ],
              tool_call_id: content_block[ 'id' ],
              tool_parameters: ''
            }
          end
        end
      when 'content_block_delta'
        index = data[ 'index' ]
        contents.fill( {}, contents.size..index ) if contents.size <= index
        if delta = data[ 'delta' ]
          if delta[ 'type' ] == 'text_delta'
            contents[ index ][ :type ] = :text 
            contents[ index ][ :text ] = ( contents[ index ][ :text ] || '' ) + delta[ 'text' ]
          elsif delta[ 'type' ] == 'input_json_delta'
            contents[ index ][ :type ] = :tool_call 
            contents[ index ][ :tool_parameters ] = 
              ( contents[ index ][ :tool_parameters ] || '' ) + delta[ 'partial_json' ]                
          end
        end
      when 'message_delta'
        if delta = data[ 'delta' ]
          end_reason = delta[ 'stop_reason' ]
          end_sequence = delta[ 'stop_sequence' ]
        end
        metrics[ :output_tokens ] += data[ 'usage' ]&.[]( 'output_tokens' ) || 0
      when 'message_stop'
        
    end 
  end

  context = {
    buffer: buffer,
    contents: contents,
    end_reason: end_reason,
    end_sequence: end_sequence,
    metrics: metrics
  }
  choices = [ {
      end_reason: translate_end_result( end_reason ),
      end_sequence: end_sequence,
      message: {
        contents: contents.dup
      }
    }
  ]

  [ context, ( choices.empty? ? nil : { choices: choices } ) ]
end