how to stop motorola moto G5 from automatically rebooting? I have a Motorola Moto G5S (it is displayed exactly like this in the about/status config) running android  7.1.1 (stock ROM from my telecom)

Sometimes it reboots out of the blue, even when I am using an app. The device is not having any storage shortage, or overheating.

Is there a way to stop it from doing so?
Assuming you have keep a stock ROM on your phone (don't have root it), the cause can be both hardware and/or software. 

I explain myself: either the cause is material, or a heat problem putting the temperature sensor(s) on alert or even the components themselves overheating thus obliging them at a given moment to stop. In this case, you would already feel a significant increase in warmth with your bare hand when picking up your phone.
The last possible cause of this scenario would be a design flaw, it seems to me that this phone is recent, perhaps reason why this kind of trouble starts to make edge effects and therefore untimely restarts.
The battery may also be involved, voltage variations that would no longer power the phone enough for a few seconds, forcing Android to turn off.

The second hypothesis, namely the software, therefore in this case Android, could be the cause, in this case, the diagnosis could become more complicated than for a material cause.
Indeed, it could be that a system or user application poses a problem. If the last words i quoted don't scare you, then you might find an anomaly in the system log files, namely in the logcat, an explanatory tutorial has been written very well on XDA, i let you read it:
https://forum.xda-developers.com/showthread.php?t=1726238

Always in a software case, the problem could simply come from an old application, not updated for a certain time that you would use to perform a very precise task in a very precise way.

I would suggest you to make a complete backup of your data on your phone before anything else, and this in a separate way (a backup of each type of data such as SMS MMS and call log on one side and photos, videos, music on the other side). Then a simple cleanup of the data partition, cache, as well as the dalvik cache through your reovery menu, followed by a reboot to test your phone.

By hoping you find this information useful.