agentos-project / agentos

The Python Component System (PCS) is an API and CLI for building, running, and sharing Python code. AgentOS is a set of libraries built on top of PCS that make it easy to build, run, and share agents that use Reinforcement Learning.
https://agentos.org
Apache License 2.0
13 stars 4 forks source link

Create `setup_agent_main()` utility function making it easy to make agents runnable as python scripts #60

Closed andyk closed 2 years ago

andyk commented 3 years ago

Currently, most of our example agents define behavior that runs the agent when the file that contains the agent's code is run using python -m agent_main.py. For example, see:

https://github.com/agentos-project/agentos/blob/b7ea6b3bc586fe41abbb096662cf459eec528878/example_agents/predictive_coding/free_energy_tutorial/main.py#L187-L217

And also: https://github.com/agentos-project/agentos/blob/b7ea6b3bc586fe41abbb096662cf459eec528878/example_agents/rl_agents/reinforce_agent.py#L127-L153

Notice that they have similar structure and contain a decent amount of boilerplate code.

Also notice that they use the standard python convention:

if __name__ == "__main__":
    # parse command line args that define agent run behavior
    # use agentos.run() to actually run the agent.

Our test cases execute these files via the pytest virtual-env fixture's virtualenv.run() which in turn calls subprocess.Popen.

Since many agents will likely have similar command line args, it would be useful if we could make a utility function that handles defining command-line arguments, parses those same arguments, and runs the agent.

This would then provide a 3rd way to run an agent. That is, in addition to the options of (1) using an Agent Directory (which itself is an MLflow project), and (2) using the agentos CLI command agentos run, one could now just directly run the agent python file (e.g., python -m /my/agent/main.py).

The tricky part is keep the utility function super intuitive and easy to use.

One idea would be to define a function in agentos/core.py:

def setup_agent_main(agent_class, env_classes=[CartPoleEnv]):
Set up a main function for an agent file, including accepting common
command line arguments. To use this in an agent file to make the agent
easy to execute directly as a python script. 

code to parse args

code to run agent via agentos.run_agent()


This would be used from inside your `agent_main.py` file as follows:

if name == "main": from agentos.core import setup_agent_main setup_agent_main(YourAgentClass)



This would make example_agents easier to read and test, and would hopefully make agent development more approachable.

**Agent Directories** already provides a well defined way to specify agent params (in the `MLProject` file), so perhaps this could be written to play nicely with those conventions.

Note that this whole idea might not be necessary since the agent dev could just use the CLI or an **Agent Directory** to run/test their agent. **And perhaps we should instead be discouraging agent developers from making their agent files directly runnable as python scripts (i.e., via `python -m`).
andyk commented 2 years ago

Closing as out of date.