Deploy an Agent System as an API

Deploying an agent system means exporting the trained agent system, loading it into your production environment, and then asking the agent system for decisions. In Composabl, the export is a json file called agent.json that contains all you need to deploy your agent system. You can load the agent system file to use in your IT infrastructure with many ways.

This document will show how you can deploy your agent system as an API using Python and Flask.

Step 1: Accessing and Preparing the Files

To deploy the agent system 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 ***

  1. Log into the Composabl UI (https://app.composabl.com/onboarding), train your agent system, and navigate to the training sessions section.

  2. Check the status of the agent system:

    • Green status (Done) indicates finished training.

  3. Download the agent system essential file:

    • The agent system file (a .gz compressed file).

    • Extract the agent system file agent.json to the model folder

Step 2: Get the API python file

import asyncio
import os

from composabl import Agent, Trainer
import numpy as np

from flask import Flask, request, jsonify

# Initialize Flask app
app = Flask(__name__)

# Global objects (initialized once)
trainer = None
trained_agent = None

license_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 starts
async def init_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'])
async def predict():
    """
    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 initialized
    if not trained_agent:
        return jsonify({"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 in list(obs.values())] )

    # Validate that the observation was provided in the request
    if obs is None:
        return jsonify({"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 response
    return jsonify({"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 the requirement packages

Run the following in your terminal:

pip install --no-cache-dir -r requirements.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 system.

export COMPOSABL_LICENSE='<your license here>'

python agent_api.py

Step 5: Test your API

After running the API, you can test it by opening the terminal and run the script below:

curl -X POST http://localhost:8000/predict -H "Content-Type: application/json" -d '{"observation": {"T": 311.0, "Tc": 292.0, "Ca": 8.56, "Cref": 8.56, "Tref": 311.0, "Conc_Error": 0.0, "Eps_Yield": 0.0, "Cb_Prod": 0.0}}'

In the POST request, we pass the use case "observation" with sensor variables and their values to receive an action from the agent system. The code above is related to the agent.json demo for Chemical Process Control.

Last updated