Intelligence of agents produces a structural phase transition in collective behaviour