A drugged-up lunatic allegedly sicced his vicious pit bull on his pal last week — because he believed the victim was a robot who was out to kill him, police sources said Monday.

Police unearthed a trove of illegal drugs inside a ninth-floor apartment in the Biltmore on West 47th St. near Eighth Avenue after responding to the dog attack around 9 a.m. Friday.

Patrick Walsh, 35, who was staying in the apartment, gave cops the kooky explanation as to why he unleashed his pooch, named Astro, on his 27-year-old friend, who suffered multiple bite wounds.

The victim told police that he and Walsh were using ketamine the night before, according to a criminal complaint first reported by DNA Info.

Ketamine, straws, empty Ziplock bags, money and a scale were found on a coffee table inside the apartment living room, the complaint said.

Police also recovered from the apartment enough drugs to open up a pharmacy — 34 Xanax pills, 29 amphetamine pills, 96 Oxycodone pills, a pipe with meth residue, three plastic baggies of MDMA, six bags of crystal meth, 68 bottles of steroids, three bags of ketamine, two glass vials of ketamine, one bag of mushrooms and nearly 700 ecstasy tablets, according to court papers.

Walsh is charged with assault and multiple counts of criminal possession of a controlled substance and criminal use of drug paraphernalia.

He is also alleged to have kicked the victim in the face.

Walsh, who suffered an injury to his left arm, is being held on $25,000 bail.

His lawyer, David Cohen, denied that he lived at the apartment and said the two men knew each other.

Walsh was extremely close with Astro, who was taken to Animal Care and Control. The dog’s fate is unknown.

“Patrick treated it like a best friend. Dogs are trained to be loyal to their owners,” said Cohen. “Clearly, the dog sensed his owner was in some kind of peril and acted out of loyalty to his owner.”

Witnesses at the luxury high-rise building – which is comprised of a mix of market-rate and mixed-income apartments – recalled hearing “blood-curdling” screams coming from the ninth floor.

Additional reporting by Rebecca Rosenberg