我使用AsyncTcpConnection請求gpt的接口,gpt接口是流式返回?cái)?shù)據(jù)的,但是有小概率會(huì)把一條完整的數(shù)據(jù),分成兩次來讀。
$con->onMessage = function(AsyncTcpConnection $con, $buffer)use ($userConnection,$util){
static $header = '',$headerCompleted = false;
if (!$headerCompleted){
$header .= $buffer;
if (!$position = strpos($header, "\r\n\r\n")) {
return;
}
if(!$buffer = substr($header, $position + 4)) {
return;
}
$headerCompleted =true;
}
$date=date('Y-m-d H:i:s');
file_put_contents("2.txt",$date.PHP_EOL.$buffer.PHP_EOL,FILE_APPEND);
$content=$util->parsedData($buffer);//解析數(shù)據(jù)
if ($content!=""){
$userConnection->send(new Chunk($content));
}
$con->buffer.=$content;
};
這是我file_put_contents寫的數(shù)據(jù)
data: {"id":"chatcmpl-8znRUdNBk3H87SRt3Yg2UuGWiBpb5","object":"chat.completion.chunk","created":1709738756,"model":"gpt-3.5-turbo-0125","system_fingerprint":"fp_2b778c6b35","choices":[{"index":0,"delta":{"content":"達(dá)"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-8znRUdNBk3H87SRt3Yg2UuGWiBpb5","object":"chat.completion.chunk","created":1709738756,"model":"gpt-3.5-turbo-0125","system_fingerprint":"fp_2b778c6b35","choices":[{"index":0,"delta":
2024-03-06 23:25:57
{"content":"到"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-8znRUdNBk3H87SRt3Yg2UuGWiBpb5","object":"chat.completion.chunk","created":1709738756,"model":"gpt-3.5-turbo-0125","system_fingerprint":"fp_2b778c6b35","choices":[{"index":0,"delta":{"content":"了"},"logprobs":null,"finish_reason":null}]}
可以看到第二行數(shù)據(jù)明顯是被切分成兩次了,小概率事件,挺難復(fù)現(xiàn)的,不知道是我用的中轉(zhuǎn)api的問題還是AsyncTcpConnection讀數(shù)據(jù)時(shí)候產(chǎn)生的問題
tcp就這樣,它是基于流的,沒有邊界,收到得數(shù)據(jù)可能是部分,也可能是多條。需要用協(xié)議或者自己區(qū)分
用 https://github.com/webman-php/openai 這個(gè)庫