vuongxuongminh / laravel-async

Package provide simple way to run code asynchronously for your Laravel application.
MIT License
154 stars 26 forks source link

Issue with async with http call #45

Open mubeenali5 opened 8 months ago

mubeenali5 commented 8 months ago

Async::run(function() { $search = $this->dataAbc; // dd($search->uuid); // \Log::info('This is some useful information.');

        // Fetch the API key from the environment variable
        $apiKey = api_key;

        if (!$apiKey) {
            \Log::error('OpenAI API key not found.');
            // Handle the error, either notify the user or handle it as per your application's logic
            // For example:
            // return $this->sendErrorResponse('OpenAI API key not found.');
        }

        $data = Http::withHeaders([
            'Content-Type' => 'application/json',
            'Authorization' => 'Bearer ' . $apiKey,
        ])
        ->post("https://api.openai.com/v1/chat/completions", [
            "model" => "gpt-4-1106-preview",
            "messages" => $search['messages'],
            'temperature' => 0.1,
            'stream' => false,
            "max_tokens" => 1200,
            "top_p" => 1,
            "frequency_penalty" => 0,
            "presence_penalty" => 0,
            "response_format" => [
                "type" => "json_object"
            ]
        ])->json();

        if (isset($data)) {
            $query = ChatgptUserResponse::create([
                'uuid' => $search['uuid'],
                'user_json_response' => json_encode($data),
                'status' => true,
            ]);

            \Log::info('Query result:', ['query' => $query]);

            // Return success response or handle the data further
            // return $this->sendSuccessResponse($data, 'user response.');
        } else {
            \Log::warning('No data received from OpenAI API.');
            // Handle the case where no data is received from the API
            // For example:
            // return $this->sendErrorResponse('No data received from OpenAI API.');
        }

    });

    This is my laravel code for with an api call, so i need to return an instant reponse to user and call this api in async, but after using this it still wait for inner block completation. Could you check why ?