Sonny's wondering about who he is reflects the conflict of his character within the story. Part of the new NS-5 Upgraded Robots who possess consciousness outside of human control, Sonny is distinct from the older robotic models who depended on human beings for their freedom. Sonny is outside of this, as he has his own freedom. However, unlike VIKI and the other NS-5, who seek to subvert human control, Sonny remains loyal to human beings, helping Spooner and Calvin in their quest to destroy the robotic movement which threatens humanity. Sonny's asking of "Who Am I" reflects the larger issue that is presented in the film. If human beings have cornered the market on rational though and progress, then it makes sense that their own creation (artificial intelligence) seeks to undermine them as it is the logical extension of the misapplications of human progress. The notion of preaching peace, yet initiating wars, the idea of protecting rights, but having a government which secretly takes them away is about as consistent as developing robotic technology to help, but actually desire to undermine human action. Sonny's question is one that seeks clarification of where we have come as human beings and where we are going, even though it is spoken by a robot. At the same time, his question strikes at the heart of his identity in terms of loyalty to humans or VIKI.
In my opinion Sonny asks this question based on an uncertainty of his identity and his ability to find a way to discover his identity. His main purpose in the film is to be shown as someone who relies upon Doctor Lanning and later on his subourdinates Spooner and Calvin who both are capable to grasp a broader idea of the notions that lead them to the moment at hand.