Coder Social home page Coder Social logo

Comments (6)

albertcht avatar albertcht commented on May 21, 2024 1

Hi @mfauveau ,

Thanks for your bug report. I'm working on fixing it.

from laravel-swoole.

albertcht avatar albertcht commented on May 21, 2024 1

Hi @joecohens ,

Basically you can push stream response to a Swoole task queue. The task delivering is totally asynchronous, but the process in task worker is still synchronous. At least it will not block the normal worker process.

Or you can create a process for stream response, and bind it to Swoole event, after the process is done, destroy the event binding. But in this way will consume more CPU resource.

So I'm still trying to find a balance.

from laravel-swoole.

albertcht avatar albertcht commented on May 21, 2024

Hi @mfauveau ,

I fixed it in the commit e63e9d3, and will merge into the next release.

However, the streamed response is synchronous now, so it's not recommended to do some heavy output in the response.

I am considering to make it asynchronous in the future.

from laravel-swoole.

albertcht avatar albertcht commented on May 21, 2024

This fix has been merged into the newest release.

from laravel-swoole.

joecohens avatar joecohens commented on May 21, 2024

Hi @albertcht, thanks for the fixing this so quickly! What would it take to make it asynchronous?

from laravel-swoole.

laraveladmin-cn avatar laraveladmin-cn commented on May 21, 2024
Route::get('/streamswoole', function () {
    $class = '\\Symfony\\Component\\HttpFoundation\\StreamedResponse';
    $response = new $class(function (){
        $open_ai_key = 'sk-5OL';
        $open_ai = new OpenAi($open_ai_key);
        $txt = "";
        $opts = [
            'model' => 'gpt-3.5-turbo',
            'messages' => json_decode('[{
	"role": "system",
	"content": "You are a helpful assistant."
}, {
	"role": "user",
	"content": "今天星期几?"
}]',true),
            'temperature' => 1.0,
            'max_tokens' => 100,
            'frequency_penalty' => 0,
            'presence_penalty' => 0,
            'stream' => true
        ];
        $complete = $open_ai->chat($opts, function ($curl_info, $data) use (&$txt) {
            if ($obj = json_decode($data) and $obj->error->message != "") {
                error_log(json_encode($obj->error->message));
            } else {
                echo $data;
                $clean = str_replace("data: ", "", $data);
                $arr = json_decode($clean, true);
                if ($data != "data: [DONE]\n\n" and isset($arr["choices"][0]["delta"]["content"])) {
                    $txt .= $arr["choices"][0]["delta"]["content"];
                }
            }

            echo PHP_EOL;
            @ob_flush();
            @flush();
            return strlen($data);
        });
    },200,[
        'Access-Control-Allow-Origin'=>'*',
        'X-Accel-Buffering'=>'no',
        'Content-type'=>'text/event-stream',
        'Cache-Control'=>'no-cache'
    ]);
    return $response->send();
});
 sendMessage(){
            const eventSource = new EventSource('/streamswoole');
            const div = this.$refs['message'];

            eventSource.onmessage =  function (e){
                dd(e,1);
                if (e.data == "[DONE]") {
                    eventSource.close();
                } else {
                    let txt = JSON.parse(e.data).choices[0].delta.content
                    if (txt !== undefined) {
                        div.innerHTML += txt.replace(/(?:\r\n|\r|\n)/g, '<br>');
                    }
                }
            };
            eventSource.onerror =  function(e) {
                dd(e,2);
                eventSource.close();
            };
            dd(eventSource,3);
        }

浏览器显示错误结果:

1694019591688
1694019688169
1694019744958

laravel不使用swoole的情况是对的:

image

from laravel-swoole.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.