@@ -32,7 +32,7 @@ public function supports(Model $model, array|string|object $input): bool
32
32
public function convert (HttpResponse $ response , array $ options = []): LlmResponse
33
33
{
34
34
if ($ options ['stream ' ] ?? false ) {
35
- return $ this ->convertStream ($ response );
35
+ return new StreamResponse ( $ this ->convertStream ($ response) );
36
36
}
37
37
38
38
try {
@@ -52,7 +52,7 @@ public function convert(HttpResponse $response, array $options = []): LlmRespons
52
52
}
53
53
54
54
/** @var Choice[] $choices */
55
- $ choices = \array_map ([ $ this , ' convertChoice ' ] , $ data ['choices ' ]);
55
+ $ choices = \array_map ($ this -> convertChoice (...) , $ data ['choices ' ]);
56
56
57
57
if (1 !== count ($ choices )) {
58
58
return new ChoiceResponse (...$ choices );
@@ -65,36 +65,35 @@ public function convert(HttpResponse $response, array $options = []): LlmRespons
65
65
return new TextResponse ($ choices [0 ]->getContent ());
66
66
}
67
67
68
- private function convertStream (HttpResponse $ response ): StreamResponse
69
- {
70
- $ stream = $ this ->streamResponse ($ response );
71
-
72
- return new StreamResponse ($ this ->convertStreamContent ($ stream ));
73
- }
74
-
75
- private function streamResponse (HttpResponse $ response ): \Generator
68
+ private function convertStream (HttpResponse $ response ): \Generator
76
69
{
70
+ $ toolCalls = [];
77
71
foreach ((new EventSourceHttpClient ())->stream ($ response ) as $ chunk ) {
78
72
if (!$ chunk instanceof ServerSentEvent || '[DONE] ' === $ chunk ->getData ()) {
79
73
continue ;
80
74
}
81
75
82
76
try {
83
77
$ data = $ chunk ->getArrayData ();
84
-
85
- yield $ data ;
86
78
} catch (JsonException ) {
87
79
// try catch only needed for Symfony 6.4
88
80
continue ;
89
81
}
90
- }
91
- }
92
82
93
- private function streamIsToolCall (\ Generator $ response ): bool
94
- {
95
- $ data = $ response -> current ();
83
+ if ( $ this -> streamIsToolCall ($ data )) {
84
+ $ toolCalls = $ this -> convertStreamToToolCalls ( $ toolCalls , $ data );
85
+ }
96
86
97
- return isset ($ data ['choices ' ][0 ]['delta ' ]['tool_calls ' ]);
87
+ if ([] !== $ toolCalls && $ this ->isToolCallsStreamFinished ($ data )) {
88
+ yield new ToolCallResponse (...\array_map ($ this ->convertToolCall (...), $ toolCalls ));
89
+ }
90
+
91
+ if (!isset ($ data ['choices ' ][0 ]['delta ' ]['content ' ])) {
92
+ continue ;
93
+ }
94
+
95
+ yield $ data ['choices ' ][0 ]['delta ' ]['content ' ];
96
+ }
98
97
}
99
98
100
99
/**
@@ -126,24 +125,12 @@ private function convertStreamToToolCalls(array $toolCalls, array $data): array
126
125
return $ toolCalls ;
127
126
}
128
127
129
- private function convertStreamContent (\Generator $ generator ): \Generator
128
+ /**
129
+ * @param array<string, mixed> $data
130
+ */
131
+ private function streamIsToolCall (array $ data ): bool
130
132
{
131
- $ toolCalls = [];
132
- foreach ($ generator as $ data ) {
133
- if ($ this ->streamIsToolCall ($ generator )) {
134
- $ toolCalls = $ this ->convertStreamToToolCalls ($ toolCalls , $ data );
135
- }
136
-
137
- if ([] !== $ toolCalls && $ this ->isToolCallsStreamFinished ($ data )) {
138
- yield new ToolCallResponse (...\array_map ([$ this , 'convertToolCall ' ], $ toolCalls ));
139
- }
140
-
141
- if (!isset ($ data ['choices ' ][0 ]['delta ' ]['content ' ])) {
142
- continue ;
143
- }
144
-
145
- yield $ data ['choices ' ][0 ]['delta ' ]['content ' ];
146
- }
133
+ return isset ($ data ['choices ' ][0 ]['delta ' ]['tool_calls ' ]);
147
134
}
148
135
149
136
/**
0 commit comments