Skip to content

Commit c58daa5

Browse files
authored
Compatible with some non-standard GPT interfaces
1 parent bdaec41 commit c58daa5

File tree

1 file changed

+5
-7
lines changed

1 file changed

+5
-7
lines changed

src/Chat.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,7 @@ public function completions(array $data, array $options)
3838
if ($tmp === '' || $tmp[strlen($tmp) - 1] !== "\n") {
3939
return null;
4040
}
41-
if (preg_match('/qwen-/', $tmp)) {
42-
str_replace('data:','data: ', $tmp);
43-
}
44-
preg_match_all('/data: (\{.+?\})\n/', $tmp, $matches);
41+
preg_match_all('/data: *?(\{.+?\})[ \r]*?\n/', $tmp, $matches);
4542
$tmp = '';
4643
foreach ($matches[1]?:[] as $match) {
4744
if ($json = json_decode($match, true)) {
@@ -101,13 +98,14 @@ public static function formatResponse($buffer)
10198
$contentFilterOffsets = null;
10299
$toolCalls = [];
103100
foreach ($chunks as $chunk) {
101+
$chunk = trim($chunk);
104102
if ($chunk === "") {
105103
continue;
106104
}
107-
if (preg_match('/qwen-/', $chunk)) {
108-
$chunk = trim(substr($chunk, 5));
105+
if (strpos($chunk, 'data:{') === 0) {
106+
$chunk = substr($chunk, 5);
109107
} else {
110-
$chunk = trim(substr($chunk, 6));
108+
$chunk = substr($chunk, 6);
111109
}
112110
if ($chunk === "" || $chunk === "[DONE]") {
113111
continue;

0 commit comments

Comments
 (0)