As others have mentioned, a sentient AI can't really be controlled, in the sense of total control, and we probably shouldn't even try. As others have also mentioned, the challenge is creating a system so that the AI develops what we would recognize as a conscience, or a disposition toward doing what humans call "good" and not doing what humans call "evil."
This exposes the real problem - how are "good" and "evil" defined, and how did humans come to have a conscience in the first place?
I believe that these questions are unanswered, and are some of the oldest and most basic questions about human nature itself. However, there are lots of ideas, and I think we can adapt some to work for an AI.
A common theme is the evolution of morality - the details differ a lot depending on which subset of the theory you subscribe to, but they all have in common the central idea that a sense of morality or an ethical system evolves via selection - members of a species that display behavior which conforms to an ethics system reproduce more frequently than those that do not.
This lines up nicely with the way that real-world AI's are trained - through mutation (or permutation) and selection for a desired behavior. In short, the underlying model (typically a neural network) is modified and the results are compared to a target - if the AI is closer to the target than it was before, the new model wins. Otherwise, we throw it out and start again. There are a lot of variations on this approach as well, but you can see that what we are doing is applying a selection pressure in order to evolve a system towards a desired outcome.
So, to create a "friendly" AI - one with what we could describe as a conscience or moral system, all that you would need to do is make that moral system a part of the selection pressure used to create it.
At that point the question becomes "how do I define the desired moral system" - which is a very hard question to answer, but can make for a great story full of plot twists! The follow-up question is: "How do we make sure that this selection pressure stays in place?"
For people, society creates a persistent selection pressure - we ostracize, imprison, or kill people who display unethical behavior. 64
For AI, who would do that? Humans could, until the point at which the AI becomes more powerful than humans. Since an adaptive AI on a long enough timeline would almost certainly become more powerful than humans, you can see why out-of-control artificial intelligence is a common theme in sci-fi!
We can, however, look at more human examples to get some ideas of how this can be done. If a human displays very anti-social behavior, but is either too powerful to be punished, or too sneaky to be caught, they can reproduce. However, the odds of their offspring also being both very anti-social and sneaky or powerful enough to reproduce are small compared to the odds of other normally-socialized people reproducing. In other words, even though a relatively small number of extremely unethical people exists at any given time, there are more ethical people, and they are reproducing faster. One way to mirror this with an adaptive AI would be to set up a system by which the AI could "reproduce" (adapt according to its built-in reward system) more easily by doing things that we classify as "good." In this way, we wouldn't really be guaranteeing that the AI stays "friendly" - just that there would be more "friendly" AI than "unfriendly."
Now, how do we ensure the AI never modifies its adaptive system... 64