In the evolving landscape of 5G network, network slicing has been considered as a key technology for the realization of multiple virtual networks running on a shared physical infrastructure, each designed to fulfill a specific service or application. However, with such networks, the dynamic and real-time allocation of these resources remains a prime concern, particularly with respect to highly variable conditions of traffic. In this paper, an adaptive novel real-time resource allocation algorithm under 5G network slicing is proposed, constructed by a hybrid optimization framework with AI-based traffic prediction. The proposed approach combines the genetic algorithm (GA), particle swarm optimization (PSO) algorithm, and differential evolution (DE) with a heuristic adaptive adjustment mechanism to make it possible for robust and high-efficiency solutions to the problem of resource allocation optimization. An AI-based traffic prediction model is proposed, which utilizes ARIMA and LSTM techniques to accurately predict traffic demand in the future in order to proactively allocate resources. Wide simulation results indicate that the proposed methodology brings significant performance improvement in terms of resource utilization, latency, throughput, QoS, energy efficiency, and security level. The proposed approach ensures a consistent level of resource use, low latency, stable throughput, a high level of QoS, and efficient energy and security. Such results indicate the potential of our approach in improving the adaptability and efficiency of the 5G network slicing. Future work is aimed at energy efficiency improvement, followed by the model being fine-tuned through real-world datasets and adapted to a large scale of complex network environments.