There are couple of ways to do it. Simplest would be to make one boolean like player_controlled
and simply check if it's true or not. If true, then make your whole routine of checking for inputs and other stuff, if false, then skip this and run your AI functions instead.
You could also attach different script to a node, depending on whether it should be player controlled or not. One script would be for the player, one for AI.
Which implementation would be optimal depends on your specific game.
I've done something similar, where I switch AI behavior this way - I got one variable, that takes whole script through one of many different paths, to create more passive or aggressive AI, change AI goal between killing player, attacking NPC, protecting important location ect.