This FastAPI application allows users to convert natural language queries into optimized SQL using IBM Watsonx Orchestrate AgenticAI, execute the queries via REST APIs, and get the results in structured and downloadable formats.
✅ Supports MSSQL,MySQL & PostgreSQL ✉️ Email result support 🌐 Web UI with basic and float layout 🔒 IBM Cloud IAM authentication
your_project/
.
├── Aldextra_agent_v2
│ ├── agents
│ │ └── native
│ │ ├── Aldextra_agent_v2.yaml
│ │ ├── authentication_agent.yaml
│ │ ├── emailResultAgent.yaml
│ │ └── NLQWXO_SQL.yaml
│ └── tools
│ └── user_auth_tool
│ ├── auth_tool.py
│ └── requirements.txt
├── arch.xml
├── db
│ ├── db_schema_llm_summary.txt
│ └── db_schema.txt
├── Dockerfile
├── examples
│ └── few_shot_examples.py
├── main.py
├── prompts
│ └── system_prompt_template.py
├── readme.md
├── requirements.txt
├── services
│ └── sql_rest_executor.py
├── static
│ ├── AI-Agents.jpg.webp
│ └── chat_logo.png
├── templates
│ ├── index.html
│ └── indexv2.html
└── v4_ald_openapi.json
- 🔁 Natural Language → SQL → Execution → Results
- 🧠 Integrated with IBM Watsonx LLM for generation
- 📩 Emailing of query result XLS
- 🗃 Uploadable DB schema (
.txt) - 🎨 Clean Web UI (Jinja2 templates)
- ✅ FastAPI backend with Pydantic validation
- 🤖 Configurable Watsonx Orchestrate Agents (via YAML)
This REST API is integrated as a Tool within Watsonx Orchestrate, enabling orchestration agents to use this app for natural language-to-SQL generation and execution.
- Endpoints
/generate-sql,/execute-sql,/generate-sql-execute-result, and/send-emailare registered as tools. - The OpenAPI spec
v4_ald_openapi.jsonis imported in Watsonx Orchestrate to define tool capabilities.
-
Agent YAMLs like
NLQWXO_SQL.yamlandemailResultAgent.yamlinAldextra_agent_v2/agents/native/define:- Task grounding and tool bindings
- Dialogue state flow and decisions
- Access to environment variables (via context)
-
Agents are grounded on uploaded schema using
/upload-schema.
- User Input: "List all failed transactions for June."
- Agent Trigger:
NLQWXO_SQL.yamlfires, generates SQL. - Tool Call: Executes
/generate-sql-execute-result. - Post-Process: Returns results, optionally triggers
/send-emailviaemailResultAgent.yaml.
- Seamless Watsonx integration with LLM & tools
- Reusable agent YAMLs for other workflows
- Task chaining (e.g. generate → execute → email)
- Context-driven invocation and control
git clone https://github.com/your-org/your-repo-name.git
cd your-repo-nameCreate a .env file in the root directory:
wx_api_key=YOUR_IBM_API_KEY
wx_service_url=YOUR_MODEL_INFERENCE_URL
wx_project_id=YOUR_WX_PROJECT_ID
wx_model_id=google/flan-ul2 # Or any other model
wx_model_param_max_tokens=300
DB_ENGINE=mysql
INCLUDE_SCHEMA_SUMMARY=true
##
REST_API_KEY = "api-key-for-rest"
REST_API_HOST = "http://x.x.x.x:5000"
DB_ENGINE=mssql
DB_TYPE=mysql
DB_NAME=db_name
DB_INSTANCE=db_instance
DB_USER=db_user
DB_PASSWORD=db_pass
DB_HOST=db_host_ip
DB_PORT=db_port
pip install -r requirements.txt✅ Recommended Python version:
>=3.9
uvicorn main:app --reloadAccess: http://localhost:8000
POST /upload-schema
Upload .txt schema file used for query generation.
POST /generate-sql
{
"user_query": "List top 5 customers by purchase"
}Returns:
{
"sql": "SELECT * FROM ..."
}POST /generate-sql-execute-result
Returns both the SQL and result (with XLS & HTML).
POST /send-email
{
"email": "[email protected]"
}POST /no-send-email
POST /execute-sql
{
"sql": "SELECT * FROM customers LIMIT 5"
}- Basic:
GET / - Float layout:
GET /v2
docker build -t nlsql-watsonx .
docker run -p 8000:8000 --env-file .env nlsql-watsonxDeveloped by Shan S,Muralidhar
This project is licensed under the MIT License. See LICENSE for details.