|
1 |
| -# Restack AI SDK - Gemini Generate Content Example |
| 1 | +# Restack AI - Gemini Example |
| 2 | + |
| 3 | +This example demonstrates how to build reliable and scalable AI applications using Google's Gemini API with Restack. It shows how to handle rate limits, concurrent API calls, and workflow orchestration effectively. |
| 4 | + |
| 5 | +We build four example workflows: a basic content generation workflow, a function calling workflow, a multi-function calling workflow, and a swarm workflow that demonstrates parallel execution. |
| 6 | + |
| 7 | + |
| 8 | +## Motivation |
| 9 | + |
| 10 | +When building AI applications with Gemini, you need to handle various production challenges: |
| 11 | + |
| 12 | +1. **API Rate Limits**: Gemini API has concurrent request limits (e.g., 3 concurrent calls)([1](https://googleapis.github.io/python-genai)) that need to be managed across multiple workflows. |
| 13 | + |
| 14 | +2. **Reliability**: AI workflows need retry mechanisms and graceful failure handling for production use. |
| 15 | + |
| 16 | +3. **Scalability**: As your application grows to handle thousands of concurrent agents or requests, you need robust queue management and execution control. |
| 17 | + |
| 18 | +### How Restack Helps |
| 19 | + |
| 20 | +Restack provides built-in solutions for these challenges: |
| 21 | + |
| 22 | +1. **Automated Rate Limiting**: Define rate limits in service options and let Restack handle the queuing: |
| 23 | +```python |
| 24 | +client.start_service( |
| 25 | + task_queue="gemini", |
| 26 | + functions=[generate_content, function_call], |
| 27 | + options=ServiceOptions( |
| 28 | + rate_limit=3, # Match Gemini's concurrent call limit |
| 29 | + max_concurrent_function_runs=3 |
| 30 | + ) |
| 31 | +) |
| 32 | +``` |
| 33 | + |
| 34 | +2. **Built-in Retries**: Restack automatically handles retries and failure scenarios. |
| 35 | + |
| 36 | +3. **Queue Management**: Efficiently manages thousands of concurrent workflows while respecting API limits. |
2 | 37 |
|
3 | 38 | ## Prerequisites
|
4 | 39 |
|
|
25 | 60 |
|
26 | 61 | ```bash
|
27 | 62 | git clone https://github.com/restackio/examples-python
|
28 |
| - cd examples-python/examples/gemini_generate_content |
29 |
| - ``` |
| 63 | + cd examples-python/community/gemini |
30 | 64 |
|
31 | 65 | 4. Install dependencies using Poetry:
|
32 | 66 |
|
|
52 | 86 | export GEMINI_API_KEY=<your-api-key>
|
53 | 87 | ```
|
54 | 88 |
|
55 |
| -6. Run the services: |
| 89 | +6. Run the services in dev: |
56 | 90 |
|
57 | 91 | ```bash
|
58 |
| - poetry run services |
| 92 | + poetry run dev |
59 | 93 | ```
|
60 | 94 |
|
61 | 95 | This will start the Restack service with the defined workflows and functions.
|
62 | 96 |
|
63 |
| -7. In a new terminal, schedule the workflow: |
64 |
| - |
65 |
| - ```bash |
66 |
| - poetry shell |
67 |
| - ``` |
68 |
| - |
69 |
| - ```bash |
70 |
| - poetry run schedule |
71 |
| - ``` |
72 |
| - |
73 |
| - This will schedule the `GeminiGenerateOppositeWorkflow` and print the result. |
| 97 | +8. Schedule workflows via the UI |
74 | 98 |
|
75 | 99 | ## Project Structure
|
76 | 100 |
|
|
0 commit comments