0) { $system_prompt .= '- Add at least 3 markdown images with article title as caption in every section except for Introduction'; $user_prompt .= "\n\nPHOTOS\n------------\n"; foreach ($photos as $photo) { $user_prompt .= "{$photo}\n"; } } $output = (self::chatCompletion($system_prompt, $user_prompt, 'gpt-3.5-turbo', 4000)); // dump($user_prompt); // dd($output); if (! is_null($output)) { try { return json_decode($output, false, 512, JSON_THROW_ON_ERROR); } catch (Exception $e) { Log::error($output); inspector()->reportException($e); return null; } } return null; } public static function chatCompletion($system_prompt, $user_prompt, $model, $max_token = 2500) { try { $response = Http::timeout(800)->withToken(config('platform.ai.openai.api_key')) ->post('https://api.openai.com/v1/chat/completions', [ 'model' => $model, 'max_tokens' => $max_token, 'messages' => [ ['role' => 'system', 'content' => $system_prompt], ['role' => 'user', 'content' => $user_prompt], ], ]); //dd($response->body()); $json_response = json_decode($response->body(), false, 512, JSON_THROW_ON_ERROR); $reply = $json_response?->choices[0]?->message?->content; return $reply; } catch (Exception $e) { Log::error($response->body()); inspector()->reportException($e); throw ($e); } return null; } }