$npx -y skills add One-Man-Company/Skills-ContextManager --skill dspy-ai-appsComprehensive guide for building AI applications using the DSPy framework with Google's Gemini API. This skill enforces strict rate limit management for the 'gemini-3-flash-preview' model (20 RPD) and provides best practices for "Intent-Oriented Programming", decomposition, agent
| 1 | # DSPy AI Applications with Gemini |
| 2 | |
| 3 | This skill guides you through building robust, self-optimizing AI applications using **DSPy** (Declarative Self-improving Python) and the **Gemini API**. It emphasizes **Intent-Oriented Programming**: defining *what* you want (Signatures) rather than *how* to prompt it. |
| 4 | |
| 5 | > [!WARNING] |
| 6 | > **STRICT RATE LIMITS**: The `gemini-3-flash-preview` model has extremely strict rate limits: |
| 7 | > - **5 Requests Per Minute (RPM)** |
| 8 | > - **20 Requests Per Day (RPD)** |
| 9 | > - **250,000 Tokens Per Minute (TPM)** |
| 10 | > |
| 11 | > You MUST enable caching and use "Dry Runs" to avoid exhaustion. |
| 12 | |
| 13 | ## 1. Core Philosophy: Intent-Oriented Programming |
| 14 | |
| 15 | DSPy shifts focus from "hand-crafting prompts" to "programming architectures". |
| 16 | |
| 17 | 1. **Decomposition**: Break complex tasks (Translation, RAG, Agents) into small, optimizable specific programs. Do not build monolithic "God Prompts". |
| 18 | 2. **Signatures**: Declare input/output specs (including types like `list[dict]`, `float`, `Pydantic`). |
| 19 | 3. **Optimization**: Use data (synthetic or real) to compiling programs into effective prompts automatically. |
| 20 | 4. **Modularity**: Swap backends (e.g., Gemini -> Local) or optimization strategies without rewriting code. |
| 21 | |
| 22 | ## 2. Quick Start (Safe Mode) |
| 23 | |
| 24 | To start a new project without hitting rate limits immediately: |
| 25 | |
| 26 | 1. **Install Dependencies**: |
| 27 | ```bash |
| 28 | pip install dspy-ai google-generativeai |
| 29 | ``` |
| 30 | |
| 31 | 2. **Use the Safe Boilerplate**: |
| 32 | Always start with the provided boilerplate which includes caching and rate limit handling. |
| 33 | See [assets/boilerplate.py](assets/boilerplate.py). |
| 34 | |
| 35 | 3. **Configure Environment**: |
| 36 | Ensure `GOOGLE_API_KEY` is set in your environment variables. |
| 37 | |
| 38 | ## 3. Workflow |
| 39 | |
| 40 | ### Step 1: Define Typed Signatures |
| 41 | Define explicit intents. Be specific with types. |
| 42 | *See [references/cheat_sheet.md](references/cheat_sheet.md) for examples (Invoice Parsing, Entity Extraction).* |
| 43 | |
| 44 | ### Step 2: Build Modular Programs |
| 45 | Connect modules like `dspy.Predict`, `dspy.ChainOfThought`, or `dspy.ReAct`. |
| 46 | For Agents, expose these programs as tools. |
| 47 | |
| 48 | ### Step 3: Run Once & Cache |
| 49 | Run your module on a single example. |
| 50 | - **ALWAYS** use `dspy.configure(experimental=True)` or standard settings to enable file-based caching. |
| 51 | - Verify the cache file was created before proceeding. |
| 52 | |
| 53 | ### Step 4: Synthetic Data & Optimization (Advanced) |
| 54 | **CRITICAL WARNING**: Optimization loops (BootstrapFewShot, MIPRO) consume massive RPD. |
| 55 | - **Strategy**: Use a "Teacher" model (stronger/different quota) to generate synthetic data if possible. |
| 56 | - **Micro-Optimization**: If you must optimize on Gemini Flash, use a tiny trainset (2-3 examples) and `BootstrapFewShot` with `max_bootstrapped_demos=1`. |
| 57 | |
| 58 | ## 4. Advanced Patterns |
| 59 | |
| 60 | ### A. Agents & MCP |
| 61 | DSPy programs can be exposed as MCP (Model Context Protocol) tools. |
| 62 | 1. Define a `dspy.Signature` for the tool. |
| 63 | 2. Wrap it in a `dspy.Predict`. |
| 64 | 3. Serve it via an MCP server (e.g., using `mcp2py`). |
| 65 | |
| 66 | ### B. Fine-Tuning Flow |
| 67 | 1. Define Signature. |
| 68 | 2. Optimize a Teacher program (strong model) to get high-quality traces. |
| 69 | 3. Generate synthetic data. |
| 70 | 4. Fine-tune a smaller Student model (e.g., Gemma 2B) using `BootstrapFinetune`. |
| 71 | |
| 72 | ### C. RAG (Retrieval Augmented Generation) |
| 73 | Combine `dspy.Retrieve` (e.g., ColBERTv2, VectorDB) with `dspy.ChainOfThought`. |
| 74 | Optimize the *entire pipeline* to improve retrieval queries and answer generation simultaneously. |
| 75 | |
| 76 | ## 5. Troubleshooting |
| 77 | |
| 78 | - **429 Errors**: You hit the rate limit. Stop immediately. |
| 79 | - **Empty Responses**: Check API key and safety settings. |
| 80 | - **"Context too long"**: Use `dspy.Retrieve` or decompose the task. |
| 81 | |
| 82 | ## 6. Artifacts & Resources |
| 83 | |
| 84 | - `assets/boilerplate.py`: **MANDATORY** starting point (includes Pydantic/Typed examples). |
| 85 | - `references/cheat_sheet.md`: Signatures for Invoice Parser, RAG, Agents, Entities. |