When you retreat, you could simply "run from nearest enemy" until you feel safe. But what if your only enemy in long range is a shotgun? Or the assault that you're scared of in melee is on low health, low shield and retreating themselves? Don't give up too much territory unnecessarily.
My recommendation is this: run from certain enemies at certain ranges in order.
For example, run from anyone in short, machine gun in med, then anyone in med, then sniper in long, then anyone not shotgun in long.
Just an idea