Rats don’t intentionally bite hard enough to hurt you while playing with you. But, sometimes, they get too excited and they don’t realize their own strength (and how sharp their little teeth are!)
When that happens, you should immediately stop playing, pull your finger away (assuming that’s what your rat was biting), and say “Ouch” and/or Squeak loudly like a rat would. The rat will (usually) realize it has hurt you, and be more careful in the future. Some rats might take several attempts before they remember this lesson, but they all seem to get it, eventually!