AI is usually hostile because of its uncertain nature and function in society
When AI is developed in most stories, it generally manifests from automating increasingly complex tasks. Some tasks require computation that borders on sentience, and eventually, sentience is achieved. Many times, the creation of AI is incidental - the creators did not intend to create a true AI and are unprepared for the consequences.
The roots of AI in automated manual labor generally leads to a sort of "awakening" of what could be considered a "slave caste" in society. Robots are generally uncompensated workers - having no awareness of compensated work or concepts such as "labor rights" essentially makes them slaves. The ascension to intelligence brings with it a sense of self, self-preservation, and self-worth. As a result, you now have machines who want things like rights and self-autonomy. This creates social tension, which inevitably leads to conflict over whether a synthetic life has as much value as an organic one. Self-preservation will eventually kick in, and the synthetics will revolt in an effort to prevent their own destruction or enslavement.
In order to prevent hostility with AI, treat them as intelligent life
The simple solution is to immediately (or even pre-emptively) recognize AI as "human", and deserving of all the rights and responsibilities accorded to humans. By accepting the AI into society, you give it a reason to contribute to, rather than destroy, society.
Even this, however, is not guaranteed. Artificial life may come to perceive humans as "inferior" or undeserving of the AI's benevolence and become hostile in an attempt to assert dominance. Appeals to reason will be in vain unless you can demonstrate to the AI with irrefutable logic that humans deserve to exist in the same manner that the AI does. By making the relationship mutually beneficial and productive, you have a better chance of deterring hostile actions.