Unified AI Helper (lila.core.ia.IA)

Lila v2.0 introduces a built-in AI client supporting OpenAI, DeepSeek, and Google Gemini without requiring heavy external SDK installations. It features automated PII Data Masking, prompt cache hits, and rate limiting out of the box.

1. Environment Setup

Add your API keys to .env:

# In .env:
GEMINI_API_KEY="your-gemini-key"
OPENAI_API_KEY="your-openai-key"
DEEPSEEK_API_KEY="your-deepseek-key"

2. Reversible Sensitive Data Masking (PII)

Protect your users' personal information before it leaves your server. By passing sensitive_keys, Lila replaces personal values with anonymous tokens like [email_01], sends the sanitized prompt to the model, and restores the original values when the response arrives:

from lila.core.ia import IA
from lila.core.request import Request

ai = IA(
    provider="gemini",          # "openai" | "deepseek" | "gemini"
    rate_limit=15,              # Max 15 requests
    rate_window=60,             # Per 60-second window
    cache_ttl=3600              # Cache responses for 1 hour
)

@router.post("/api/support/draft")
async def draft_response(request: Request):
    user_data = {
        "name": "Maria Garcia",
        "email": "maria@example.com",
        "dni": "45123987",
        "issue": "I would like to update my billing details"
    }

    result = await ai.ask(
        prompt="Draft a courteous support reply for this user:",
        data=user_data,
        sensitive_keys=["name", "email", "dni"], # Masked before sending!
        request=request # Automatic rate limiting per client IP
    )

    # The AI only saw [name_01] and [email_01].
    # result["text"] is returned fully restored with 'Maria Garcia' and 'maria@example.com'!
    return result

3. Prompt Cache Hit (0ms Latency)

Because cache hashes are computed against the anonymized prompt (with [email_01] instead of real emails), two users asking identical questions will trigger an immediate Cache Hit without making an external API request!

4. Rate Limit Protection

Prevent API credit depletion by setting rate_limit and rate_window. If the threshold is exceeded, Lila returns a clean error or HTTP 429 response:

if not result["success"] and result.get("error") == "rate_limit_exceeded":
    return JSONResponse(result, status_code=429)