|
| 1 | +# Restack AI - LMNT Example |
| 2 | + |
| 3 | +This repository contains a simple example project to help you scale with Restack AI. |
| 4 | +It demonstrates how to scale reliably to millions of workflows on a local machine with LMNT voice generation. |
| 5 | + |
| 6 | +## Motivation |
| 7 | + |
| 8 | +When scaling AI workflows, you want to make sure that you can handle failures and retries gracefully. |
| 9 | +This example demonstrates how to do this with Restack AI and LMNT's voice generation API. |
| 10 | + |
| 11 | +### Workflow Steps |
| 12 | + |
| 13 | +The table below shows the execution of 50 workflows in parallel, each with three steps. |
| 14 | +Steps 2 is LMNT voice generation functions that must adhere to a rate limit of 1 concurrent call per second. |
| 15 | + |
| 16 | +| Step | Workflow 1 | Workflow 2 | ... | Workflow 50 | |
| 17 | +| ---- | ---------- | ---------- | --- | ----------- | |
| 18 | +| 1 | Basic | Basic | ... | Basic | |
| 19 | +| 2 | LMNT | LMNT | ... | LMNT | |
| 20 | + |
| 21 | +### Traditional Rate Limit Management |
| 22 | + |
| 23 | +When running multiple workflows in parallel, managing the rate limit for LMNT functions is crucial. Here are common strategies: |
| 24 | + |
| 25 | +1. **Task Queue**: Use a task queue (e.g., Celery, RabbitMQ) to schedule LMNT calls, ensuring only one is processed at a time. |
| 26 | +2. **Rate Limiting Middleware**: Implement middleware to queue requests and process them at the allowed rate. |
| 27 | +3. **Semaphore or Locking**: Use a semaphore or lock to control access, ensuring only one LMNT function runs per second. |
| 28 | + |
| 29 | +### With Restack |
| 30 | + |
| 31 | +Restack automates rate limit management, eliminating the need for manual strategies. Define the rate limit in the service options, and Restack handles queuing and execution: |
| 32 | + |
| 33 | +```python |
| 34 | +client.start_service( |
| 35 | + task_queue="lmnt", |
| 36 | + functions=[lmnt_list_voices, lmnt_synthesize], |
| 37 | + options=ServiceOptions( |
| 38 | + rate_limit=1, |
| 39 | + max_concurrent_function_runs=1 |
| 40 | + ) |
| 41 | +) |
| 42 | +``` |
| 43 | + |
| 44 | +Focus on building your logics while Restack ensures efficient and resilient workflow execution. |
| 45 | + |
| 46 | +### On Restack UI |
| 47 | + |
| 48 | +You can see from the parent workflow how long each child workflow stayed in queue and how long was the execution time. |
| 49 | + |
| 50 | + |
| 51 | + |
| 52 | +And for each child workflow, for each step you can see how long the function stayed in queue, how long the function took to execute and how many retries happened. |
| 53 | + |
| 54 | + |
| 55 | + |
| 56 | +## Prerequisites |
| 57 | + |
| 58 | +- Python 3.10 or higher |
| 59 | +- Poetry (for dependency management) |
| 60 | +- Docker (for running the Restack services) |
| 61 | +- LMNT API key (sign up at https://www.lmnt.com) |
| 62 | + |
| 63 | +## Prerequisites |
| 64 | + |
| 65 | +- Docker (for running Restack) |
| 66 | +- Python 3.10 or higher |
| 67 | + |
| 68 | +## Start Restack |
| 69 | + |
| 70 | +To start the Restack, use the following Docker command: |
| 71 | + |
| 72 | +```bash |
| 73 | +docker run -d --pull always --name restack -p 5233:5233 -p 6233:6233 -p 7233:7233 ghcr.io/restackio/restack:main |
| 74 | +``` |
| 75 | + |
| 76 | +## Start python shell |
| 77 | + |
| 78 | +```bash |
| 79 | +poetry env use 3.10 && poetry shell |
| 80 | +``` |
| 81 | + |
| 82 | +## Install dependencies |
| 83 | + |
| 84 | +```bash |
| 85 | +poetry install |
| 86 | +``` |
| 87 | + |
| 88 | +```bash |
| 89 | +poetry env info # Optional: copy the interpreter path to use in your IDE (e.g. Cursor, VSCode, etc.) |
| 90 | +``` |
| 91 | + |
| 92 | +```bash |
| 93 | +poetry run dev |
| 94 | +``` |
| 95 | + |
| 96 | +## Run workflows |
| 97 | + |
| 98 | +### from UI |
| 99 | + |
| 100 | +You can run workflows from the UI by clicking the "Run" button. |
| 101 | + |
| 102 | + |
| 103 | + |
| 104 | +### from API |
| 105 | + |
| 106 | +You can run one workflow from the API by using the generated endpoint: |
| 107 | + |
| 108 | +`POST http://localhost:6233/api/workflows/ChildWorkflow` |
| 109 | + |
| 110 | +or multiple workflows by using the generated endpoint: |
| 111 | + |
| 112 | +`POST http://localhost:6233/api/workflows/ExampleWorkflow` |
| 113 | + |
| 114 | +### from any client |
| 115 | + |
| 116 | +You can run workflows with any client connected to Restack, for example: |
| 117 | + |
| 118 | +```bash |
| 119 | +poetry run schedule |
| 120 | +``` |
| 121 | + |
| 122 | +executes `schedule_workflow.py` which will connect to Restack and execute the `ChildWorkflow` workflow. |
| 123 | + |
| 124 | +```bash |
| 125 | +poetry run scale |
| 126 | +``` |
| 127 | + |
| 128 | +executes `schedule_scale.py` which will connect to Restack and execute the `ExampleWorkflow` workflow. |
| 129 | + |
| 130 | +```bash |
| 131 | +poetry run interval |
| 132 | +``` |
| 133 | + |
| 134 | +executes `schedule_interval.py` which will connect to Restack and execute the `ChildWorkflow` workflow every second. |
| 135 | + |
| 136 | +## Deploy on Restack Cloud |
| 137 | + |
| 138 | +To deploy the application on Restack, you can create an account at [https://console.restack.io](https://console.restack.io) |
| 139 | + |
| 140 | +## Project Structure |
| 141 | + |
| 142 | +- `src/`: Main source code directory |
| 143 | + - `client.py`: Initializes the Restack client |
| 144 | + - `functions/`: Contains function definitions |
| 145 | + - `workflows/`: Contains workflow definitions |
| 146 | + - `services.py`: Sets up and runs the Restack services |
| 147 | +- `schedule_workflow.py`: Example script to schedule and run a workflow |
| 148 | +- `schedule_interval.py`: Example script to schedule and a workflow every second |
| 149 | +- `schedule_scale.py`: Example script to schedule and run 50 workflows at once |
| 150 | + |
| 151 | +# Deployment |
| 152 | + |
| 153 | +Create an account on [Restack Cloud](https://console.restack.io) and follow instructions on site to create a stack and deploy your application on Restack Cloud. |
0 commit comments