Hi,
The laptop may be thinking that you've closed the lid and causes it to go to sleep. And in your case there might be a problem with the hinge of your laptop which makes it think that it's being closed. Try Disabling Sleep Mode first when you close the lid and see if the same problem will happen.
- Right click on the battery icon and select Power Options.
- On the left pane click on Choose what closing the lid does.
- On When I close the lid make sure to set them on Do nothing.
- Click Save changes then test it again.