request = $request; } public function getResponse(){ try { $response = $this->call(); return response()->json(['data' => $response, 'status' => 'OK'], 200); }catch (\Exception $e){ return response()->json(['data' => 'Failed to load the response', 'error' => $e->getMessage().'|'.$e->getFile().'|'.$e->getFile(), 'status' => 'FAIL'], 200); } } private function call(){ $cf_ai_response = DB::table('cf_ai_response')->where('application_id', $this->request->get('application_id'))->where('service', 'bureau')->first(['id', 'response']); if (isset($cf_ai_response->id)){ return $cf_ai_response->response; } $payload = $this->getPayload(); if (!$payload['context']){ return '

Bureau Not generated

'; } $curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_URL => _env('CF_AI_HOST').'/api/v0/gemini-chat', CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => '', CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 0, CURLOPT_FOLLOWLOCATION => true, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => 'POST', CURLOPT_POSTFIELDS => json_encode($payload), CURLOPT_HTTPHEADER => array( 'Content-Type: application/json' ), )); $response = curl_exec($curl); curl_close($curl); $out = json_decode($response)->response ?? $response; DB::insert('INSERT INTO cf_ai_response (application_id, service, response, created_at) VALUES (?, ?, ?, ?)', [ $this->request->get('application_id'), 'bureau', $out, Carbon::now()->toDateTimeString() ]); return $out; } private function getPayload(){ $bureau = json_decode($this->getBureauFile(), 1); unset($bureau['Body']['ProcessResponse']['Out']['INProfileResponse']['Current_Application']); $promptCFAI = DB::table('cf_ai_prompt')->where('service', 'bureau')->first(['prompt']); return [ "prompt" => $promptCFAI->prompt, "context" => $bureau, "chat_session" => "new" ]; } private function getBureauFile(){ $s3 = Storage::disk('s3'); $file_path = $this->request->get('application_id').'/'.$this->request->get('application_id').'_response.json'; if($s3->exists($file_path)) { return $s3->get($file_path); } $file_path = $this->request->get('application_id').'/'.$this->request->get('application_id').'_response_experian_soft_1.json'; if ($s3->exists($file_path)){ return $s3->get($file_path); } $file_path = $this->request->get('application_id').'/'.$this->request->get('application_id').'_response_experian.json'; if ($s3->exists($file_path)){ return $s3->get($file_path); } return false; } }