@@ -44,7 +44,11 @@ public function call(MessageBag $messages, array $options = []): ResponseInterfa
4444 $ response = $ this ->platform ->request ('chat/completions ' , $ body );
4545
4646 if ($ response instanceof \Generator) {
47- return new StreamResponse ($ this ->convertStream ($ response ));
47+ if ($ this ->streamIsToolCall ($ response )) {
48+ return new ToolCallResponse (...$ this ->convertStreamToToolCalls ($ response ));
49+ } else {
50+ return new StreamResponse ($ this ->convertStream ($ response ));
51+ }
4852 }
4953
5054 if (!isset ($ response ['choices ' ])) {
@@ -80,6 +84,13 @@ public function supportsStructuredOutput(): bool
8084 return $ this ->version ->supportStructuredOutput ;
8185 }
8286
87+ private function streamIsToolCall (\Generator $ response ): bool
88+ {
89+ $ data = $ response ->current ();
90+
91+ return isset ($ data ['choices ' ][0 ]['delta ' ]['tool_calls ' ]);
92+ }
93+
8394 private function convertStream (\Generator $ generator ): \Generator
8495 {
8596 foreach ($ generator as $ data ) {
@@ -91,6 +102,35 @@ private function convertStream(\Generator $generator): \Generator
91102 }
92103 }
93104
105+ /**
106+ * @return ToolCall[]
107+ */
108+ private function convertStreamToToolCalls (\Generator $ response ): array
109+ {
110+ $ toolCalls = [];
111+ foreach ($ response as $ data ) {
112+ if (!isset ($ data ['choices ' ][0 ]['delta ' ]['tool_calls ' ])) {
113+ continue ;
114+ }
115+
116+ foreach ($ data ['choices ' ][0 ]['delta ' ]['tool_calls ' ] as $ i => $ toolCall ) {
117+ if (isset ($ toolCall ['id ' ])) {
118+ // initialize tool call
119+ $ toolCalls [$ i ] = [
120+ 'id ' => $ toolCall ['id ' ],
121+ 'function ' => $ toolCall ['function ' ],
122+ ];
123+ continue ;
124+ }
125+
126+ // add arguments delta to tool call
127+ $ toolCalls [$ i ]['function ' ]['arguments ' ] .= $ toolCall ['function ' ]['arguments ' ];
128+ }
129+ }
130+
131+ return array_map ([$ this , 'convertToolCall ' ], $ toolCalls );
132+ }
133+
94134 /**
95135 * @param array{
96136 * index: integer,
0 commit comments