mostlyserious / craft-promptly

Revolutionize content creation in Craft by leveraging the power of AI, giving superpowers to your content creators to help them brainstorm and edit content with an AI assistant directly within Craft CMS.
Other
0 stars 1 forks source link

Request streaming breaks Openai API response #6

Open ddnetters opened 7 months ago

ddnetters commented 7 months ago

When using the plugin to translate text the GenerateController::actionIndex() returns the following. Would just sending and waiting for the request suffice instead of streaming it? It'd remove some complexity and users have to wait for the final result anyways.

data: {"id":"****","object":"chat.completion.chunk","created":1707917150,"model":"gpt-4-0613","system_fingerprint":null,"choices":[{"index":0,"delta":{"rol
data: null

data: null

data: null

data: null

data: null

data: null

data: null

data: null

data: null

data: null

<pre>An Error occurred while handling another error:
yii\web\HeadersAlreadySentException: Headers already sent in /var/www/html/vendor/mostlyserious/craft-promptly/src/Controllers/Traits/HasFetching.php on line 83. in /var/www/html/vendor/yiisoft/yii2/web/Response.php:369
Stack trace:
#0 /var/www/html/vendor/yiisoft/yii2/web/Response.php(342): yii\web\Response-&gt;sendHeaders()
#1 /var/www/html/vendor/yiisoft/yii2/web/ErrorHandler.php(136): yii\web\Response-&gt;send()
#2 /var/www/html/vendor/craftcms/cms/src/web/ErrorHandler.php(192): yii\web\ErrorHandler-&gt;renderException(Object(yii\web\HeadersAlreadySentException))
#3 /var/www/html/vendor/yiisoft/yii2/base/ErrorHandler.php(152): craft\web\ErrorHandler-&gt;renderException(Object(yii\web\HeadersAlreadySentException))
#4 /var/www/html/vendor/craftcms/cms/src/web/ErrorHandler.php(66): yii\base\ErrorHandler-&gt;handleException(Object(yii\web\HeadersAlreadySentException))
#5 [internal function]: craft\web\ErrorHandler-&gt;handleException(Object(yii\web\HeadersAlreadySentException))
#6 {main}
Previous exception:
yii\web\HeadersAlreadySentException: Headers already sent in /var/www/html/vendor/mostlyserious/craft-promptly/src/Controllers/Traits/HasFetching.php on line 83. in /var/www/html/vendor/yiisoft/yii2/web/Response.php:369
Stack trace:
#0 /var/www/html/vendor/yiisoft/yii2/web/Response.php(342): yii\web\Response-&gt;sendHeaders()
#1 /var/www/html/vendor/yiisoft/yii2/base/Application.php(390): yii\web\Response-&gt;send()
#2 /var/www/html/web/index.php(33): yii\base\Application-&gt;run()
#3 {main}</pre>
corneliusio commented 7 months ago

@ddnetters Yeah, working with streaming in PHP has been a chore and unfortunately this was pretty much the first go at an OpenAI integration. Would agree but we had a number of other stakeholders who felt streaming would be important for perceived performance. I've had the opportunity to work on a number of other OpenAI integrations since initially releasing this plugin and will be coming back through to refactor quite a bit to improve these hiccups. At the moment my agency is slammed with development work and I haven't been able to dedicate the time I'd like to making improvements here but it is at the top of my list of priorities as soon as I have the chance.