Spectrum sharing is a method to solve the problem of frequency spectrum deficiency. This paper studies a novel AI based spectrum sharing and energy harvesting system in which the freshness of information (AoI) is guaranteed. The system includes a primary user with access rights to the spectrum and a secondary user. The secondary user is an energy harvesting sensor that intends to use the primary user spectrum opportunistically. The problem is formulated as partially observable Markov decision processes (POMDPs) and solved using two methods: a deep Q-network (DQN) and dueling double deep Q-Network (D3QN) to achieve the optimal policy. The purpose is to choose the best action adaptively in every time slot based on its situation in both overlay and underlay modes to minimize the average AoI of the secondary user. Finally, simulation experiments are performed to evaluate the effectiveness of the proposed scheme compared to the overlay mode. According to the results, the average AoI in the proposed system is less than that of the existing models, including only overlay mode. The average user access improved from 30% in the overlay mode to 45% in the DQN and 48% in the D3QN.