Deploying an agent means exporting the trained agent, loading it into your production environment, and then asking the agent for decisions. In Composabl, the export is a json file called agent.json that contains all you need to deploy your agent. You can load the agent file to use in your IT infrastructure with many ways.
This document will show how you can deploy your agent as an API using Python and Flask.
Step 1: Accessing and Preparing the Files
To deploy the agent as an API, we need to extract the agent.json model, get the agent_api.py script to start the API, requirements.txt to install packages. You can find a sample for these files in our GitHub repo: https://github.com/Composabl/examples.composabl.io/tree/main/deploy_agent
This is the structure needed for the API:
*** How to extract the agent.json ***
Log into the Composabl UI (https://app.composabl.com/onboarding), train your agent, and navigate to the training sessions section.
Check the status of the agent:
Green status (Done) indicates finished training.
Download the agent essential file:
The agent file (a .gz compressed file).
Extract the agent file agent.json to the model folder
Step 2: Get the API python file
import asyncioimport osfrom composabl import Agent, Trainerimport numpy as npfrom flask import Flask, request, jsonify# Initialize Flask appapp =Flask(__name__)# Global objects (initialized once)trainer =Nonetrained_agent =Nonelicense_key = os.environ["COMPOSABL_LICENSE"]PATH = os.path.dirname(os.path.realpath(__file__))PATH_CHECKPOINTS =f"{PATH}/model/agent.json"# Initialize the runtime, load the model, and package it when the app startsasyncdefinit_runtime():""" Initializes the trainer and agent before the first request is processed. This sets up the AI model for inference, loading it from checkpoints and preparing the agent. """global trainer, trained_agent# Assuming 'config' is required to initialize the Trainer config ={"license": license_key,"target":{"local":{"address":"localhost:1337"}},"env":{"name":"sim-deploy",},"trainer":{"workers":1}}# Initialize the Trainer with the config trainer =Trainer(config)# Load the agent from the given checkpoint path agent = Agent.load(PATH_CHECKPOINTS)# Package the agent for inference using the Trainer's _package function (asynchronously) trained_agent =await trainer._package(agent)# Asynchronous POST route to receive observation and return action@app.route('/predict', methods=['POST'])asyncdefpredict():""" Receives an observation through a POST request, processes it using the pre-trained agent, and returns the corresponding action. """global trained_agent# Check if the agent has been successfully initializedifnot trained_agent:returnjsonify({"error": "Agent not initialized"}),500# Extract the observation from the request's JSON body obs = request.json.get("observation") obs =dict(obs) obs = np.array( [float(x) for x inlist(obs.values())] )# Validate that the observation was provided in the requestif obs isNone:returnjsonify({"error": "No observation provided"}),400# Asynchronously process the observation to generate the action action =await trained_agent._execute(obs)# Return the generated action in the responsereturnjsonify({"action": str(action)})if__name__=="__main__":# Run the Flask application with async support on localhost, port 8000 loop = asyncio.get_event_loop() loop.run_until_complete(init_runtime()) app.run(host="0.0.0.0", port=8000, debug=True)
Step 3: Install requirement packages
Run the following in your terminal:
pipinstall--no-cache-dir-rrequirements.txt
It will install these packages:
composabl
flask[async]
numpy
Step 4: Export your license and start the API
To start Composabl API, you will need to export your license as an environment variable and then use Python to start the Flask API with your agent.
In the POST request, we pass the use case "observation" with sensor variables and their values to receive an action from the agent. The code above is related to the agent.json demo for Chemical Process Control.