With the rapid development of artificial intelligence technology, the research on personalized learning in the field of ideological and political intelligence education is increasingly active. In this paper, an improved locust optimization algorithm is proposed, which is applied to the intelligent grouping strategy of ideological and political education. Then a knowledge state-oriented hypergraph self attention knowledge tracking model is proposed, which consists of a hypergraph module and a self attention module, and is capable of predicting students’ future interaction sequences through their past interaction sequences. In order to realize students’ personalized test question matching needs, a Civics test question recommendation algorithm based on the neural graph model is proposed, based on which a personalized Civics test question recommendation exam system is designed and implemented. The intelligent grouping strategy based on the optimized locust algorithm achieves a total score accuracy of 100% in the Civics grouping task. The knowledge tracking model accurately predicts students’ knowledge status, and the attention weights of students’ learning paths based on this paper’s recommendation algorithm are all higher than 0.5. It shows the effectiveness of this paper’s strategy of automatic generation of Civics education content based on the locust optimization algorithm and the personalized test question matching model on the students’ in-depth understanding of the Civics knowledge and improvement of learning efficiency.