Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue with async with http call #45

Open
mubeenali5 opened this issue Dec 19, 2023 · 0 comments
Open

Issue with async with http call #45

mubeenali5 opened this issue Dec 19, 2023 · 0 comments

Comments

@mubeenali5
Copy link

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 ?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant