Skip to content

gabrielpreda/adk-sql-agent

Repository files navigation

Introduction

The application uses ADK, Gemini, LangChain tools to power an SQL Agent.

Architecture

Frontend: Streamlit app (streamlit_ui.py)
Backend: FastAPI service (main.py)
Agents:

  • Coordinator: sql_agent (sql_agent.py)
  • Subagents:
    • Rephraser agent (rewrite_prompt.py)
    • Generator agent (generator.py)
    • Analyzer agent (analyzer.py)
    • Reflexion agent (reflexion.py)
    • Routing agent (routing.py)

Function Tools:

  • get_schema tool (db_tools.py)
  • run_sql_query tool (db_tools.py)

Models:

  • Gemini 2.5 pro

Getting started

Clone the repo

git clone https://github.com/gabrielpreda/adk-sql-agent.git
cd adk-sql-agent

Create an .env file

The file should contain the following:

GOOGLE_GENAI_USE_VERTEXAI=TRUE
GOOGLE_CLOUD_PROJECT=YOUR_PROJECT
GOOGLE_CLOUD_LOCATION=YOUR_REGION

Install dependencies

Run:

pip install -r requirements.txt

Start the backend

Run:

uvicorn main:app --reload

Start the frontend

Run:

streamlit run streamlit_ui.py

Testing

From the root folder run:

adk web

Then select the sql_agent folder. ADK web will discover in agent.py the root_agent and you will be able to test, monitor, debug the application.

The Agentic workflow is shown in the following figure (current step: Generator agent receives the result from run_sql_query tool).

The next figure shows the result of Generator Agent.

The next figure shows:

  • Result of Analyzer Agent.
  • Resolution of the Reflection Agent, based on previous agent analysis.
  • The rationale for Routing Agent to route to Rewrite Query Agent.
  • The reasoning of Rewrite Query Agent and the new query generated.

Deployment

Option 1: Deploy to Agent Engine (Recommended)

Agent Engine is Google Cloud's managed service for deploying ADK agents.

Prerequisites

  • Google Cloud Project with billing enabled
  • gcloud CLI installed and configured
  • ADK CLI version 1.3.0+

Deploy

  1. Set your project ID and region:
export GOOGLE_CLOUD_PROJECT=your-project-id
export GOOGLE_CLOUD_LOCATION=us-central1
  1. Run the deployment script:
./deploy.sh
  1. Test your deployed agent:
adk run --project=$GOOGLE_CLOUD_PROJECT --region=$GOOGLE_CLOUD_LOCATION --agent=adk-sql-agent

Option 2: Deploy to Cloud Run

Alternative deployment using Google Cloud Run for broader region availability.

Deploy

  1. Set your project ID and region:
export GOOGLE_CLOUD_PROJECT=your-project-id
export GOOGLE_CLOUD_LOCATION=us-central1
  1. Run the Cloud Run deployment script:
./deploy_cloudrun.sh
  1. The script will output your service URL. Test it:
# Check status
curl https://your-service-url/status

# Send a query
curl -X POST https://your-service-url/query \
  -H 'Content-Type: application/json' \
  -d '{"query": "Show me the top 10 customers"}'

Manual Deployment

If you prefer manual deployment:

# Build container
gcloud builds submit --tag gcr.io/$GOOGLE_CLOUD_PROJECT/adk-sql-agent

# Deploy to Cloud Run
gcloud run deploy adk-sql-agent \
  --image gcr.io/$GOOGLE_CLOUD_PROJECT/adk-sql-agent \
  --platform managed \
  --region $GOOGLE_CLOUD_LOCATION \
  --allow-unauthenticated \
  --set-env-vars GOOGLE_GENAI_USE_VERTEXAI=TRUE \
  --set-env-vars GOOGLE_CLOUD_PROJECT=$GOOGLE_CLOUD_PROJECT \
  --set-env-vars GOOGLE_CLOUD_LOCATION=$GOOGLE_CLOUD_LOCATION \
  --memory 4Gi \
  --cpu 2

Demo

We show here the sequence of operations for one query.

Rewrite prompt

Generator results

Analyzer + Reflection

Reflection resolution

Results

About

An sql agent using ADK and Gemini

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published