Comments (6)
Hi @mfauveau ,
Thanks for your bug report. I'm working on fixing it.
from laravel-swoole.
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.
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.
This fix has been merged into the newest release.
from laravel-swoole.
Hi @albertcht, thanks for the fixing this so quickly! What would it take to make it asynchronous?
from laravel-swoole.
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);
}
浏览器显示错误结果:
laravel不使用swoole的情况是对的:
from laravel-swoole.
Related Issues (20)
- WARNING Server::check_worker_exit_status(): worker(pid=690, id=14) abnormal exit, status=0, signal=9
- Routes file changes not picked up until I restart my Docker container HOT 1
- [question] does this support mysql persistent connection or mysql connection pooling HOT 1
- Hot reload not working HOT 2
- Swoole Kill Process Without Waiting For Http Call
- How did I know swoole is succefull applied on my environment HOT 1
- streamDownload not working and sometimes also throw "allowed memory size ... exhausted" HOT 2
- high latency after loop
- can a controller run out swoole HOT 1
- Err in line 8 of tests/fixtures/bootstrap/app.php
- Auth user problem on web socket connection HOT 1
- auth user class redis not fund
- [Question] How to change default base_path so file upload $directory will be default on public folder rather than the root laravel folder? HOT 2
- Socket.io client not work with Websockets implementation out of box
- timer
- 调用chatgpt的流式响应输出有问题 HOT 1
- 为什么在使用websocket服务时,触发on connect回调的时候发送消息给指定userid的人,自己也会收到消息 HOT 1
- facades are not safe to use inside coroutine for sure in this package. HOT 1
- swoole HOT 1
- 响应时间较长的接口出现504或者502,后台程序则依然正常执行
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from laravel-swoole.