77
78
79
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
|
# File 'lib/intelligence/adapters/open_ai/chat_response_methods.rb', line 77
def stream_result_chunk_attributes( context, chunk )
context ||= {}
buffer = context[ :buffer ] || ''
metrics = context[ :metrics ] || {
input_tokens: 0,
output_tokens: 0
}
choices = context[ :choices ] || Array.new( 1 , { message: {} } )
choices.each do | choice |
choice[ :message ][ :contents ] = choice[ :message ][ :contents ]&.map do | content |
{ type: content[ :type ] }
end
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 ]
next if line.end_with?( '[DONE]' )
data = JSON.parse( line ) rescue nil
if data.is_a?( Hash )
data[ 'choices' ]&.each do | data_choice |
data_choice_index = data_choice[ 'index' ]
data_choice_delta = data_choice[ 'delta' ]
data_choice_finish_reason = data_choice[ 'finish_reason' ]
choices.fill( { message: {} }, choices.size, data_choice_index + 1 ) \
if choices.size <= data_choice_index
contents = choices[ data_choice_index ][ :message ][ :contents ] || []
text_content = contents.first&.[]( :type ) == :text ? contents.first : nil
if data_choice_content = data_choice_delta[ 'content' ]
if text_content.nil?
contents.unshift( text_content = { type: :text, text: data_choice_content } )
else
text_content[ :text ] = ( text_content[ :text ] || '' ) + data_choice_content
end
end
if data_choice_tool_calls = data_choice_delta[ 'tool_calls' ]
data_choice_tool_calls.each do | data_choice_tool_call |
if data_choice_tool_call_function = data_choice_tool_call[ 'function' ]
data_choice_tool_index = data_choice_tool_call[ 'index' ]
data_choice_tool_id = data_choice_tool_call[ 'id' ]
data_choice_tool_name = data_choice_tool_call_function[ 'name' ]
data_choice_tool_parameters = data_choice_tool_call_function[ 'arguments' ]
tool_call_content_index = ( text_content.nil? ? 0 : 1 ) + data_choice_tool_index
if tool_call_content_index >= contents.length
contents.push( {
type: :tool_call,
tool_call_id: data_choice_tool_id,
tool_name: data_choice_tool_name,
tool_parameters: data_choice_tool_parameters
} )
else
tool_call = contents[ tool_call_content_index ]
tool_call[ :tool_call_id ] = ( tool_call[ :tool_call_id ] || '' ) + data_choice_tool_id \
if data_choice_tool_id
tool_call[ :tool_name ] = ( tool_call[ :tool_name ] || '' ) + data_choice_tool_name \
if data_choice_tool_name
tool_call[ :tool_parameters ] = ( tool_call[ :tool_parameters ] || '' ) + data_choice_tool_parameters \
if data_choice_tool_parameters
end
end
end
end
choices[ data_choice_index ][ :message ][ :contents ] = contents
choices[ data_choice_index ][ :end_reason ] ||=
translate_end_result( data_choice_finish_reason )
end
if usage = data[ 'usage' ]
metrics[ :input_tokens ] += usage[ 'prompt_tokens' ]
metrics[ :output_tokens ] += usage[ 'completion_tokens' ]
end
end
end
context[ :buffer ] = buffer
context[ :metrics ] = metrics
context[ :choices ] = choices
[ context, choices.empty? ? nil : { choices: choices.dup } ]
end
|