Skip to content

Shumatsurontek/neo-deep-agent-lab

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

7 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🧬 Neo Deep Agent Lab

Conversational SQL Agent with Sandboxed Execution & Deep Agents Middleware

Arthur Edmond Β· LLM Engineer @ Swapn A production-grade SQL agent powered by LangChain Deep Agents, executing queries in an isolated Modal sandbox with 7-layer middleware defense


⚑ TL;DR

Posez des questions en langage naturel sur votre base de donnees β€” l'agent genere le SQL, l'execute dans un sandbox PostgreSQL isole (Modal), et repond en francais. 7 middleware (SQL guard, tool retry, context editing, model fallback...) assurent securite et resilience. Frontend avec streaming SSE, tool panels interactifs, et export CSV/JSON.


πŸ—οΈ Architecture

graph LR
    U[Utilisateur] --> F["Frontend<br/>"]
    U --> C[CLI Rich]
    F --> S[FastAPI :8080]
    C --> A[Deep Agent<br/>LangGraph]
    S --> A

    subgraph mw [" 7-Layer Middleware Stack "]
        direction TB
        SG["πŸ›‘οΈ SQL Guard"]
        TR["πŸ”„ Tool Retry"]
        TL["🚫 Tool Call Limit"]
        LOG["πŸ“Š Tool Logger"]
        CE["βœ‚οΈ Context Editing"]
        MR["πŸ”„ Model Retry"]
        MF["πŸ”€ Model Fallback"]
    end

    A --> mw

    A --> T1["execute_sql"]
    A --> T2["get_database_schema"]
    A --> T3["export_csv"]
    A --> T4["export_json"]

    T1 & T2 & T3 --> PG_GW[PG Gateway]
    PG_GW --> SB[Modal Sandbox]
    SB --> PG[(PostgreSQL 15)]

    A -.-> LS[LangSmith]
    A -.-> ST[InMemoryStore]
    A -.-> CP[MemorySaver]
Loading

πŸ”₯ Features

πŸ€– Agent SQL Intelligent

Generation et execution de SQL a partir de questions en langage naturel. Support multi-tables, JOINs, CTEs, agregations.

πŸ”’ Sandbox Isole

PostgreSQL 15 dans un conteneur Modal ephemere. Utilisateur read-only, timeout 10s, aucun acces reseau externe.

πŸ“¦ Export CSV / JSON

Tools dedies pour l'export. L'agent genere des fichiers telechargeable avec liens directs dans le chat.

⚑ Streaming SSE Temps Reel

Tokens streames un par un. Tool panels interactifs avec spinner β†’ check, input SQL visible, boutons Copy/CSV/JSON.

πŸ›‘οΈ 7 Middleware en Serie

SQL guard, tool retry, tool call limit, logging, context editing, model retry, model fallback β€” chaque requete traverse 7 couches.

πŸ’Ύ Memoire & Historique

MemorySaver (checkpointer) + InMemoryStore (StoreBackend). Summarization automatique built-in Deep Agents.

πŸ” LangSmith Tracing

Traces completes de chaque appel agent, tool, et middleware. Optionnel, activable via .env.


πŸ›‘οΈ Middleware Stack

L'ordre d'execution est important β€” les middleware tools s'executent avant les middleware modele.

                    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                    β”‚              MIDDLEWARE EXECUTION ORDER                 β”‚
                    β”‚                                                         β”‚
  Tool calls:       β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β” β”‚
                    β”‚  β”‚ SQL Guard β”‚β†’ β”‚ Tool Retryβ”‚β†’ β”‚ Call Limitβ”‚β†’ β”‚ Log  β”‚ β”‚
                    β”‚  β”‚ block DDL β”‚  β”‚ 2x backoffβ”‚  β”‚ 20/run    β”‚  β”‚timingβ”‚ β”‚
                    β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”˜ β”‚
                    β”‚                                                         β”‚
  Context:          β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”       β”‚
                    β”‚  β”‚ Context Editing β€” clear tool results > 80k  β”‚       β”‚
                    β”‚  β”‚ keep=3 most recent, placeholder: [cleared]  β”‚       β”‚
                    β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜       β”‚
                    β”‚                                                         β”‚
  Model calls:      β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                 β”‚
                    β”‚  β”‚ Model Retry β”‚ β†’  β”‚ Model Fallback β”‚                 β”‚
                    β”‚  β”‚ 3x backoff  β”‚    β”‚ GPT β†’ Claude   β”‚                 β”‚
                    β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                 β”‚
                    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
# Middleware Source Role Configuration
1 SQLGuardMiddleware Custom Bloque les requetes destructives (DROP, DELETE, INSERT, ALTER...) Whitelist: SELECT, WITH, EXPLAIN
2 ToolRetryMiddleware Built-in Retry automatique des tools en echec (timeout sandbox, erreur reseau) 2 retries, backoff 2x, jitter
3 ToolCallLimitMiddleware Built-in Limite les appels tools par run β€” empeche les boucles infinies 20 calls/run, soft exit
4 LogToolCallsMiddleware Custom Log chaque tool call avec timing (ms), taille input/output Sync + async
5 ContextEditingMiddleware Built-in Efface les anciens resultats tools quand le contexte depasse le seuil 80k tokens, garde 3 derniers
6 ModelRetryMiddleware Built-in Retry automatique des appels LLM (rate limit, API timeout, 5xx) 3 retries, backoff 2x, jitter
7 ModelFallbackMiddleware Built-in Bascule sur un modele de secours si le principal echoue Optionnel (config .env)

πŸ› οΈ Tech Stack

Composant Technologie
Agent Framework LangChain Deep Agents + LangGraph v2
LLM Claude Sonnet 4 / GPT-5-mini (+ fallback configurable)
Middleware langchain.agents.middleware (5 built-in + 2 custom)
Sandbox Modal (conteneur isole, PG15, read-only)
Base de donnees PostgreSQL 15
API Server FastAPI + SSE streaming
Frontend HTML/JS vanilla
CLI Rich (panels, markdown, spinners)
Validation Pydantic v2 (strict mode)
Tracing LangSmith
Store InMemoryStore + StoreBackend (Deep Agents)

πŸ“‹ Prerequis

  • Python 3.11+
  • Compte Modal (gratuit pour commencer)
  • Cle API Anthropic ou OpenAI
  • (Optionnel) Cle API LangSmith

πŸš€ Quick Start

# Clone
git clone https://github.com/your-org/neo-deep-agent-lab.git
cd neo-deep-agent-lab

# Setup
python -m venv .venv && source .venv/bin/activate
make install

# Config
cp .env.example .env
# Remplir les cles API

Configuration (.env)

# ── LLM principal ──
LLM_PROVIDER=openai
LLM_MODEL=gpt-5-mini-2025-08-07
OPENAI_API_KEY=sk-...

# ── LLM fallback (optionnel) ──
LLM_FALLBACK_PROVIDER=anthropic
LLM_FALLBACK_MODEL=claude-sonnet-4-20250514
ANTHROPIC_API_KEY=sk-ant-...

# ── Middleware ──
CONTEXT_EDITING_TRIGGER=80000    # tokens seuil pour nettoyer le contexte
TOOL_CALL_LIMIT_PER_RUN=20      # max tool calls par run

# ── Modal ──
MODAL_TOKEN_ID=...
MODAL_TOKEN_SECRET=...

# ── LangSmith (optionnel) ──
LANGCHAIN_TRACING_V2=true
LANGCHAIN_API_KEY=lsv2_...
LANGCHAIN_PROJECT=neo-deep-agent-lab

πŸ’» Utilisation

Frontend Web

make serve
# β†’ http://localhost:8080
Feature Description
Streaming Tokens affiches un par un en temps reel
Tool panels Collapsibles avec spinner β†’ check, input SQL, output table
Actions Boutons Copy / CSV / JSON sur chaque resultat
Download Liens de telechargement pour les exports agent
Design (Fira Code, DM Mono, dark #111, borders 0.25px)

CLI Interactif

make cli
Commande Action
reset Reinitialiser la conversation
clear Effacer l'ecran
quit Quitter

Dump de la base

make dump   # Export PG depuis Docker β†’ data/neo_dump.sql

πŸ“ Architecture des Modules

src/
β”‚
β”œβ”€β”€ πŸ€– agent/
β”‚   β”œβ”€β”€ factory.py             # Factory Deep Agent + 7-middleware stack
β”‚   └── prompts.py             # System prompt SQL (francais, anti-echo)
β”‚
β”œβ”€β”€ πŸ’» cli/
β”‚   └── chat.py                # Chat terminal Rich (sync streaming)
β”‚
β”œβ”€β”€ βš™οΈ config.py                # Settings Pydantic (env vars, middleware config)
β”œβ”€β”€ πŸ“‹ constants.py             # Enums (LLMProvider, SSEEventType, SQL keywords)
β”‚
β”œβ”€β”€ πŸ›‘οΈ middleware/
β”‚   β”œβ”€β”€ logging_mw.py          # AgentMiddleware β€” sync+async logging avec timing
β”‚   └── sql_guard.py           # AgentMiddleware β€” bloque requetes destructives
β”‚
β”œβ”€β”€ πŸ”’ sandbox/
β”‚   β”œβ”€β”€ app.py                 # Lifecycle sandbox Modal (singleton, lazy init)
β”‚   β”œβ”€β”€ image.py               # Image Modal (Debian + PG15 + dump restore)
β”‚   β”œβ”€β”€ init_pg.sh             # Init PostgreSQL + read-only user
β”‚   └── pg.py                  # Gateway PG β€” QueryResult(stdout, stderr, exit_code)
β”‚
β”œβ”€β”€ 🌐 server/
β”‚   β”œβ”€β”€ app.py                 # FastAPI (SSE, /history, /reset, /download)
β”‚   └── static/
β”‚       └── index.html         # Frontend (Bittensor design language)
β”‚
β”œβ”€β”€ πŸ“‘ streaming/
β”‚   β”œβ”€β”€ events.py              # SSE event dataclasses (text-delta, tool-call-*)
β”‚   └── sse_encoder.py         # LangGraph v2 β†’ SSE (tool results exclus du texte)
β”‚
└── πŸ”§ tools/
    β”œβ”€β”€ schemas.py             # Pydantic v2 strict schemas (ExecuteSQLInput, etc.)
    β”œβ”€β”€ schema_tool.py         # @tool β€” introspection schema (tables, colonnes)
    β”œβ”€β”€ sql_tool.py            # @tool β€” execution SQL via PG gateway β†’ markdown
    └── export_tool.py         # @tool β€” export CSV/JSON avec download link

πŸ”’ Securite β€” Defense in Depth

Couche Protection Implementation
Validation Schemas Pydantic stricts sur les inputs tools model_config = {"strict": True}
SQL Guard Bloque DROP, DELETE, INSERT, UPDATE, ALTER, TRUNCATE, CREATE AgentMiddleware custom
PostgreSQL default_transaction_read_only = ON init_pg.sh
Timeout Statement timeout 10s cote PostgreSQL psql -v statement_timeout=10000
Tool Limit 20 tool calls max par run ToolCallLimitMiddleware built-in
Context Nettoyage auto des anciens resultats (80k tokens) ContextEditingMiddleware built-in
Retry Backoff exponentiel (pas de spam API/sandbox) ToolRetryMiddleware + ModelRetryMiddleware
Sandbox Conteneur Modal isole, detruit apres session Modal ephemeral sandbox
Reseau Aucun acces externe depuis le sandbox Modal network isolation
Fallback Bascule auto sur modele secondaire si le principal fail ModelFallbackMiddleware built-in

πŸ§ͺ Developpement

make test      # 19 tests (tools, middleware, schemas)
make lint      # Ruff linter
make format    # Ruff formatter

Arthur Edmond Β· Swapn Built with LangChain Deep Agents, Modal, and an obsession for clean middleware stacks

About

deepagent-sql-lab with modal implementation

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors