@@ -142,46 +142,40 @@ impl<S: Stream<Item = Result<bytes::Bytes, reqwest::Error>> + Unpin> Stream
142142 self . buffer . push_str ( trimmed_str) ;
143143 let json_result: Result < Value , _ > = serde_json:: from_str ( & self . buffer ) ;
144144
145- match json_result {
146- Ok ( json) => {
147- self . buffer . clear ( ) ;
148-
149- if let Some ( choices) = json. get ( "choices" ) {
150- if let Some ( choice) = choices. get ( 0 ) {
151- if let Some ( delta) = choice. get ( "delta" ) {
152- if let Some ( tool_calls) = delta. get ( "tool_calls" ) {
153- if let Some ( tool_calls_array) = tool_calls. as_array ( ) {
154- let tool_calls_vec: Vec < ToolCall > =
155- tool_calls_array
156- . iter ( )
157- . filter_map ( |v| {
158- serde_json:: from_value ( v. clone ( ) ) . ok ( )
159- } )
160- . collect ( ) ;
161-
162- return Poll :: Ready ( Some (
163- ChatCompletionStreamResponse :: ToolCall (
164- tool_calls_vec,
165- ) ,
166- ) ) ;
167- }
168- }
145+ if let Ok ( json) = json_result {
146+ self . buffer . clear ( ) ;
147+
148+ if let Some ( choices) = json. get ( "choices" ) {
149+ if let Some ( choice) = choices. get ( 0 ) {
150+ if let Some ( delta) = choice. get ( "delta" ) {
151+ if let Some ( tool_calls) = delta. get ( "tool_calls" ) {
152+ if let Some ( tool_calls_array) = tool_calls. as_array ( ) {
153+ let tool_calls_vec: Vec < ToolCall > = tool_calls_array
154+ . iter ( )
155+ . filter_map ( |v| {
156+ serde_json:: from_value ( v. clone ( ) ) . ok ( )
157+ } )
158+ . collect ( ) ;
169159
170- if let Some ( content) =
171- delta. get ( "content" ) . and_then ( |c| c. as_str ( ) )
172- {
173- let output = content. replace ( "\\ n" , "\n " ) ;
174160 return Poll :: Ready ( Some (
175- ChatCompletionStreamResponse :: Content ( output) ,
161+ ChatCompletionStreamResponse :: ToolCall (
162+ tool_calls_vec,
163+ ) ,
176164 ) ) ;
177165 }
178166 }
167+
168+ if let Some ( content) =
169+ delta. get ( "content" ) . and_then ( |c| c. as_str ( ) )
170+ {
171+ let output = content. replace ( "\\ n" , "\n " ) ;
172+ return Poll :: Ready ( Some (
173+ ChatCompletionStreamResponse :: Content ( output) ,
174+ ) ) ;
175+ }
179176 }
180177 }
181178 }
182- Err ( err) => {
183- eprintln ! ( "Failed to parse response: {:?}" , err)
184- }
185179 }
186180 }
187181 Poll :: Ready ( Some ( Err ( error) ) ) => {
0 commit comments