No, you can’t force people to continue working at your company by contract.
These agreements work like signing bonuses. The compensation is given up front, but if you leave before N months then you need to return the compensation or some times a pro-rated version of it.