Skip to content
This repository was archived by the owner on Jul 16, 2025. It is now read-only.

Commit 1be8cb2

Browse files
authored
fix: streamed tool call (#126)
1 parent 4492fee commit 1be8cb2

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

src/OpenAI/Model/Gpt.php

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)