在使用AsyncTcpConnection时出现的一个bug

问题描述

我使用AsyncTcpConnection请求gpt的接口,gpt接口是流式返回数据的,但是有小概率会把一条完整的数据,分成两次来读。

程序代码或配置

 $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);//解析数据

            if ($content!=""){
                $userConnection->send(new Chunk($content));
            }

            $con->buffer.=$content;

        };

这是我file_put_contents写的数据

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}]}

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}]}

可以看到第二行数据明显是被切分成两次了,小概率事件,挺难复现的,不知道是我用的中转api的问题还是AsyncTcpConnection读数据时候产生的问题

144 1 0
1个回答

six

tcp就这样,它是基于流的,没有边界,收到得数据可能是部分,也可能是多条。需要用协议或者自己区分

https://github.com/webman-php/openai 这个库

🔝