I know that in a Tor circuit, there are typically 3 relays: Guard, Middle, and Exit. If I want to run a Tor relay, can I configure it so that I can only serve as Exit node, or Guard node, but not Middle node?
No, and it will never be implemented: it would break the whole relay idea of Tor. Every relay - regardless of it's further roles - is a "regular node", i.e. the middle one. If it's allowed to go outside - then it also/additionally receives an Exit flag. If it's stable and not flapping - it receives a Stable flag, if it's stable-flagged and has a decent bandwidth and reacability - it receives a Guard flag. That's how it works! Every node is evaluated - and to be able actually evaluated - it must be present in network, actually do some work - and basing on the results and metrics of the working process it's additionally flagged.
Currently, no. If I remember correctly, exit relays are hard-coded to be used only as exits, and Tor will prefer to not use guard relays for a middle node.