Skip to content

Commit d4d6131

Browse files
authored
function call
1 parent fe82510 commit d4d6131

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

src/Chat.php

+11
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ public static function formatResponse($buffer)
9696
$promptFilterResults = null;
9797
$contentFilterResults = null;
9898
$contentFilterOffsets = null;
99+
$toolCalls = [];
99100
foreach ($chunks as $chunk) {
100101
if ($chunk === "") {
101102
continue;
@@ -117,6 +118,13 @@ public static function formatResponse($buffer)
117118
} else {
118119
foreach ($data['choices'] ?? [] as $item) {
119120
$content .= $item['delta']['content'] ?? "";
121+
foreach ($item['delta']['tool_calls'] ?? [] as $function) {
122+
if (isset($function['function']['name'])) {
123+
$toolCalls[$function['index']] = $function;
124+
} elseif (isset($function['function']['arguments'])) {
125+
$toolCalls[$function['index']]['function']['arguments'] .= $function['function']['arguments'];
126+
}
127+
}
120128
if (isset($item['finish_reason'])) {
121129
$finishReason = $item['finish_reason'];
122130
}
@@ -154,6 +162,9 @@ public static function formatResponse($buffer)
154162
if ($contentFilterOffsets) {
155163
$result['choices'][0]['content_filter_offsets'] = $contentFilterOffsets;
156164
}
165+
if ($toolCalls) {
166+
$result['choices'][0]['message']['tool_calls'] = array_values($toolCalls);
167+
}
157168
return $result;
158169
}
159170

0 commit comments

Comments
 (0)