webman能不能像前端那样拿到stream流中的数据

原地起飞

截图
类似这种前端可以轻易拿到data里面的值
webman有没有什么方法实现呢 有知道的大神还望不吝赐教

测试的代码是

 public function c3(Request $request)
    {
        $http = new \Workerman\Http\Client();
        $result = [];
        $connection = $request->connection;
        $http->request('http://0.0.0.0:8806/api/Test/c5', [
            'method' => 'POST',
            'data' => json_encode([
                'stream' => true,
                'messages' => [['role' => 'user', 'content' => 'hello']],
            ]),
            'progress' => function($buffer) use ($connection,&$result) {
                var_dump($buffer);
                $result[] = $buffer;

            },
            'success' => function($response) use ($connection,&$result) {
                var_dump($result);
                //处理 result
                //处理逻辑
                //集中返回
                $connection->send(new Chunk($result));
                $connection->send(new Chunk(''));
            },
        ]);
        $connection->send(new Response(200, [
            //"Content-Type" => "application/octet-stream",
            "Transfer-Encoding" => "chunked",
        ], '123'));
    }

里面的东西打印不到 有懂这个的麻烦给指点指点 还是我这么干本来就是错的

715 4 1
4个回答

Gin
yongdao35
  • 原地起飞 2024-05-29

    好的 我试一下

  • 原地起飞 2024-05-29

    还是拿不到 不知道哪里的问题

  • yongdao35 2024-05-29

    http://0.0.0.0:8806/api/Test/c5
    0.0.0.0改成具体的ip

  • 原地起飞 2024-05-30

    改成127.0.0.1了也不行 就是一请求立马就结束了 好像里面的代码都没走

Gin
$http = new Client();
    $result = [];
    $http->request('https://api.ai.com/v1/chat/completions', [
        'method' => 'POST',
        'data' => json_encode([
            'model' => 'gpt-3.5-turbo',
            'temperature' => 1,
            'stream' => true,
            'messages' => [['role' => 'user', 'content' => 'hello']],
        ]),
        'headers' => [
            'Content-Type' => 'application/json',
            'Authorization' => 'Bearer sk-xxx',
        ],
        'progress' => function($buffer) use ($connection,&$result) {
            $result[] = $buffer;

        },
        'success' => function($response) use ($connection,&$result) {
            //处理 result  
            //处理逻辑
            //集中返回
            $connection->send(new Chunk($result));
            $connection->send(new Chunk(''));
        },
    ]);
    $connection->send(new Response(200, [
        //"Content-Type" => "application/octet-stream",
        "Transfer-Encoding" => "chunked",
    ], ''));
walkor 打赏
    public function c3(Request $request)
    {
        $http = new \Workerman\Http\Client();
        $connection = $request->connection;
        $http->request('https://baidu.com', [
            'method' => 'POST',
            'data' => json_encode([
                'stream' => true,
                'messages' => [['role' => 'user', 'content' => 'hello']],
            ]),
            'progress' => function($buffer) use ($connection) {
                echo $buffer;
                $connection->send(new Chunk($buffer));
            },
            'success' => function($response) use ($connection) {
                $connection->send(new Chunk(''));
            },
        ]);
        return (new Response(200, [
            //"Content-Type" => "application/octet-stream",
            "Transfer-Encoding" => "chunked",
        ]));
    }

webman里代码类似这样

×
🔝