Closed yusuftechmero closed 8 months ago
Hi All. I want to provide streaming text through api to mobile app which I'm getting from chatgpt.
API Route::post('stream', [OpenAIController::class, 'stream']);
Route::post('stream', [OpenAIController::class, 'stream']);
Function which call on api
function stream() { $yourApiKey = env('KEY'); // dd($yourApiKey); $client = \OpenAI::client($yourApiKey); $stream = $client->chat()->createStreamed([ 'model' => 'gpt-4', 'messages' => [ ['role' => 'system', 'content' => 'You are a helpful assistant.'], ['role' => 'user', 'content' => 'Hello!'], ['role' => 'assistant', 'content' => 'Hello! How can I assist you today?'], ['role' => 'user', 'content' => 'I can\'t controll my emotion, how can I?'], ], ]); header('Content-Type: text/event-stream'); header('Cache-Control: no-cache'); header('Connection: keep-alive'); // Send the chat responses to the client foreach ($stream as $response) { Log::info('STTTTTTTTTT'.json_encode($response->choices[0])); $responseData = $response->choices[0]->delta->content; echo "$responseData"; // Send each response as an SSE event if($response->choices[0]->finishReason != null || connection_aborted()){ exit(); } ob_flush(); flush(); } exit(); }
will it work for mobile app or how can do for direct streaming to mobile via api?
Hi @yusuftechmero
This is out of scope of this library, but maybe you can find someone in the discussions section to help you with that.
Hi All. I want to provide streaming text through api to mobile app which I'm getting from chatgpt.
API
Route::post('stream', [OpenAIController::class, 'stream']);
Function which call on api
will it work for mobile app or how can do for direct streaming to mobile via api?