代码里面设置的 Transfer-Encoding:chunked
这里写问题描述
$ask = $request->input('ask');
if(!$ask){
return json(['code' => 1005,'msg' => '缺少必要参数']);
}
$apikey = '';
$secretKey = '';
$traceId = uniqid();//'A9AB29036A9D4E05545116DA32123dsf2';
$timestamp = time();
$url = 'xxxx' . $apikey;
$http = new \Workerman\Http\Client();
$connection = $request->connection;
$bufferSend = new Response(200, ['Content-Type' => 'text/event-stream'],"\r\n");
$connection->send($bufferSend,true);
$data = [
'traceId' => $traceId,
'timestamp' => $timestamp,
'stream' => true,
'messages' => [
[
'content' => $ask,
'role' => 'user'
]
],
];
$http->request($url, [
'method' => 'POST',
'data' => json_encode($data),
'headers' => [
'Content-Type' => 'application/json',
'App-Sign' => $this->makeXcSign($apikey,$secretKey,$traceId,$timestamp),
],
'progress' => function($buffer) use ($connection) {
$connection->send($buffer, true);
},
'success' => function($response) use ($connection) {
$connection->send(new Chunk(''));
$connection->close();
},
]);
return (new Response(200, [
"Transfer-Encoding" => "chunked",
]));
根据你的代码,http头是这块的代码发送的
我这边写到上面就不输入内容了,这个是什么原因呀
因为你发送了两个http头,是错误用法。
把
这段去掉
去掉的话就不是这个event-stream了
加在下面的话报错
能不能让content-type和这个Transfer-Encoding都改了呢
改了后流数据没返回
大模型聊天接口直接用chunked即可,参考
https://www.workerman.net/plugin/157
好的 麻烦您了