Skip to content

Commit 81ed634

Browse files
authored
Merge pull request #11 from LukerChen/main
兼容各大模型关于tool_calls的调用
2 parents cacd15c + 0f6cf69 commit 81ed634

1 file changed

Lines changed: 17 additions & 4 deletions

File tree

src/Chat.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function completions(array $data, array $options)
4040
}
4141
preg_match_all('/data: *?(\{.+?\})[ \r]*?\n/', $tmp, $matches);
4242
$tmp = '';
43-
foreach ($matches[1]?:[] as $match) {
43+
foreach ($matches[1] ?: [] as $match) {
4444
if ($json = json_decode($match, true)) {
4545
$options['stream']($json);
4646
}
@@ -55,7 +55,7 @@ public function completions(array $data, array $options)
5555
'error' => [
5656
'code' => 'exception',
5757
'message' => $exception->getMessage(),
58-
'detail' => (string) $exception
58+
'detail' => (string)$exception
5959
],
6060
], new Response(0));
6161
}
@@ -105,12 +105,18 @@ public static function formatResponse($buffer)
105105
}
106106
$chunks = explode("\n", $buffer);
107107
$content = '';
108+
$reasoning_content = '';//思考内容
108109
$finishReason = null;
109110
$model = '';
110111
$promptFilterResults = null;
111112
$contentFilterResults = null;
112113
$contentFilterOffsets = null;
113114
$toolCalls = [];
115+
$usage = [
116+
'prompt_tokens' => 0,
117+
'completion_tokens' => 0,
118+
'total_tokens' => 0
119+
];//使用量统计
114120
foreach ($chunks as $chunk) {
115121
$chunk = trim($chunk);
116122
if ($chunk === "") {
@@ -137,10 +143,11 @@ public static function formatResponse($buffer)
137143
} else {
138144
foreach ($data['choices'] ?? [] as $item) {
139145
$content .= $item['delta']['content'] ?? "";
146+
$reasoning_content .= $item['delta']['reasoning_content'] ?? "";
140147
foreach ($item['delta']['tool_calls'] ?? [] as $function) {
141-
if (isset($function['function']['name'])) {
148+
if (isset($function['function']['name']) && $function['function']['name'] !== '') {
142149
$toolCalls[$function['index']] = $function;
143-
} elseif (isset($function['function']['arguments'])) {
150+
} elseif (isset($function['function']['arguments']) && $function['function']['arguments'] !== '') {
144151
$toolCalls[$function['index']]['function']['arguments'] .= $function['function']['arguments'];
145152
}
146153
}
@@ -154,6 +161,10 @@ public static function formatResponse($buffer)
154161
$contentFilterOffsets = $item['content_filter_offsets'];
155162
}
156163
}
164+
//统计数据
165+
if (isset($data['usage'])) {
166+
$usage = $data['usage'];
167+
}
157168
}
158169
} catch (Throwable $e) {
159170
echo $e;
@@ -167,10 +178,12 @@ public static function formatResponse($buffer)
167178
'message' => [
168179
'role' => 'assistant',
169180
'content' => $content,
181+
'reasoning_content' => $reasoning_content,
170182
],
171183
]
172184
],
173185
'model' => $model,
186+
'usage' => $usage
174187
];
175188
if ($promptFilterResults) {
176189
$result['prompt_filter_results'] = $promptFilterResults;

0 commit comments

Comments
 (0)