|
| 1 | +# Restack AI - Gemini Example for Autonomous Agents |
| 2 | + |
| 3 | +This example demonstrates how to build reliable and scalable autonomous AI agents using Google's Gemini API with Restack. It shows how to handle rate limits (10 requests per minute for free tier with 1500 requests per day limit, concurrency limit), concurrent API calls, and workflow orchestration effectively to create agents that can independently perform complex tasks. |
| 4 | + |
| 5 | +Example workflows in `src/workflows/`: |
| 6 | +- [Content Generation](src/workflows/generate_content.py): Basic natural language understanding |
| 7 | +- [Function Calling](src/workflows/function_call.py): Toolexecution |
| 8 | +- [Multi-Function Calling](src/workflows/multi_function_call.py): Complex decision making |
| 9 | +- [Multi-Function Calling](src/workflows/multi_function_call_advanced.py): Complex decision making with tools, executed as restack functions (e.g. third party APIs) |
| 10 | +- [Swarm](src/workflows/swarm.py): Parallel execution of multiple coordinated agents |
| 11 | + |
| 12 | + |
| 13 | +## Motivation |
| 14 | + |
| 15 | +When building AI applications with Gemini, you need to handle various production challenges: |
| 16 | + |
| 17 | +1. **API Rate Limits**: Gemini API has concurrent request limits that need to be managed across multiple workflows. |
| 18 | + |
| 19 | +2. **Reliability**: AI workflows need retry mechanisms and graceful failure handling for production use. |
| 20 | + |
| 21 | +3. **Scalability**: As your application grows to handle thousands of concurrent agents or requests, you need robust queue management and execution control. |
| 22 | + |
| 23 | +4. **Scheduling**: Coordinating and managing multiple workflows running at different frequencies requires robust scheduling capabilities. |
| 24 | + |
| 25 | +### How Restack Helps |
| 26 | + |
| 27 | +Restack provides built-in solutions for these challenges: |
| 28 | + |
| 29 | +1. **Automated Rate Limiting**: Define rate limits in service options and let Restack handle the queuing: |
| 30 | +```python |
| 31 | +client.start_service( |
| 32 | + task_queue="gemini", |
| 33 | + functions=[generate_content, function_call], |
| 34 | + options=ServiceOptions( |
| 35 | + rate_limit=3, # Match Gemini's concurrent call limit |
| 36 | + max_concurrent_function_runs=3 |
| 37 | + ) |
| 38 | +) |
| 39 | +``` |
| 40 | + |
| 41 | +2. **Built-in Retries**: Restack automatically handles retries and failure scenarios. |
| 42 | + |
| 43 | +3. **Queue Management**: Efficiently manages thousands of concurrent workflows while respecting API limits. |
| 44 | + |
| 45 | +4. **Scheduling**: Run workflows on various schedules (minutely, hourly, daily) or with custom cron expressions. |
| 46 | + |
| 47 | +## Prerequisites |
| 48 | + |
| 49 | +- Python 3.9 or higher |
| 50 | +- Poetry (for dependency management) |
| 51 | +- Docker (for running the Restack services) |
| 52 | +- Active [Google AI Studio](https://aistudio.google.com) account with API key |
| 53 | + |
| 54 | +## Usage |
| 55 | + |
| 56 | +1. Run Restack local engine with Docker: |
| 57 | + |
| 58 | + ```bash |
| 59 | + docker run -d --pull always --name restack -p 5233:5233 -p 6233:6233 -p 7233:7233 ghcr.io/restackio/restack:main |
| 60 | + ``` |
| 61 | + |
| 62 | +2. Open the web UI to see the workflows: |
| 63 | + |
| 64 | + ```bash |
| 65 | + http://localhost:5233 |
| 66 | + ``` |
| 67 | + |
| 68 | +3. Clone this repository: |
| 69 | + |
| 70 | + ```bash |
| 71 | + git clone https://github.com/restackio/examples-python |
| 72 | + cd examples-python/community/gemini |
| 73 | + |
| 74 | +4. Install dependencies using Poetry: |
| 75 | + |
| 76 | + ```bash |
| 77 | + poetry env use 3.12 |
| 78 | + ``` |
| 79 | + |
| 80 | + ```bash |
| 81 | + poetry shell |
| 82 | + ``` |
| 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 | +5. Set `GEMINI_API_KEY` as an environment variable from [Google AI Studio](https://aistudio.google.com) |
| 93 | + |
| 94 | + ```bash |
| 95 | + export GEMINI_API_KEY=<your-api-key> |
| 96 | + ``` |
| 97 | + |
| 98 | +6. Run the services in dev: |
| 99 | + |
| 100 | + ```bash |
| 101 | + poetry run dev |
| 102 | + ``` |
| 103 | + |
| 104 | + This will start the Restack service with the defined workflows and functions. |
| 105 | + |
| 106 | +8. Schedule workflows via the UI |
| 107 | + |
| 108 | +All workflows are auto-exposed with an RestAPI endpoint, ready to be run or scheduled via api request, or directly from the UI during development. |
| 109 | + |
| 110 | + |
| 111 | + |
| 112 | +Run or schedule individual workflows. |
| 113 | + |
| 114 | + |
| 115 | + |
| 116 | +Run or schedule a workflow, triggering many agents as childworkflows. |
| 117 | + |
| 118 | + |
| 119 | + |
| 120 | + |
| 121 | + |
| 122 | +## Project Structure |
| 123 | + |
| 124 | +- `src/`: Main source code directory |
| 125 | + - `client.py`: Initializes the Restack client |
| 126 | + - `functions/`: Contains function definitions |
| 127 | + - `workflows/`: Contains workflow definitions |
| 128 | + - `services.py`: Sets up and runs the Restack services |
| 129 | +- `schedule_workflow.py`: Example script to schedule and run a workflow |
0 commit comments