Skip to content

Commit be69771

Browse files
committed
docs: update CLAUDE.md for Issue #18 agent implementation
Update project documentation to reflect completed agent features: - Add agent module to directory structure - Document AgentText2SQL usage and API - Add ReAct loop flow description - Update API endpoints list with agent routes - Add new agent configuration options - Add agent test commands - Mark Issue #18 as COMPLETED Part of Issue #18: smolagents Agent Framework
1 parent b44f69d commit be69771

1 file changed

Lines changed: 86 additions & 39 deletions

File tree

.claude/CLAUDE.md

Lines changed: 86 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ This file provides guidance to Claude Code (claude.ai/code) when working with co
66

77
Arctic Text2SQL Agent: A production-grade AI agent that converts natural language to SQL using Snowflake's Arctic-Text2SQL-R1-7B model with a ReAct (Reasoning + Acting) framework for multi-step reasoning and self-correction.
88

9-
**Current State**: Core orchestration layer (`Text2SQLEngine`) is implemented with query intent classification, SQL validation, confidence-based retry logic, and schema alignment checking. The engine is integrated with API routes.
10-
11-
**Next Critical Piece**: Issue #18 (smolagents agent framework) for full ReAct loop implementation with self-correction capabilities.
9+
**Current State**: Full agent-based architecture implemented with smolagents integration. Both the core `Text2SQLEngine` and the new `AgentText2SQL` engine are available, with the agent version providing multi-step reasoning, self-correction, and query history for retry functionality.
1210

1311
## Commands
1412

@@ -45,10 +43,15 @@ app/
4543
├── main.py # FastAPI entry point, lifespan management
4644
├── config.py # 8 nested Pydantic settings classes
4745
├── routes.py # API endpoints (integrated with engine)
48-
├── text2sql_engine.py # Core orchestrator (NEW - Issue #4)
46+
├── text2sql_engine.py # Core orchestrator (Issue #4)
4947
├── middleware.py # CORS, logging, security headers
5048
├── exceptions.py # Custom exception hierarchy → HTTP status codes
51-
└── security/ # JWT auth, rate limiting, input validation
49+
├── security/ # JWT auth, rate limiting, input validation
50+
└── agent/ # Agent-based architecture (Issue #18)
51+
├── __init__.py # Module exports
52+
├── models.py # AgentResult, AgentStep, QueryHistoryEntry
53+
├── tools.py # SQL executor, validator, schema inspector tools
54+
└── engine.py # AgentText2SQL with ReAct loop
5255
5356
db/
5457
├── connection.py # DatabaseManager singleton, async pooling
@@ -61,42 +64,68 @@ models/
6164
└── prompts.py # Schema-aware prompt templates
6265
```
6366

64-
### Text2SQL Engine (app/text2sql_engine.py)
67+
### Agent-Based Architecture (app/agent/)
6568

66-
The central orchestrator that coordinates all SQL generation:
69+
The new agent module (Issue #18) provides self-correction capabilities:
6770

6871
```python
69-
from app.text2sql_engine import get_text2sql_engine, Text2SQLEngine
72+
from app.agent import get_agent_engine, AgentText2SQL
7073

71-
engine = await get_text2sql_engine()
74+
engine = await get_agent_engine()
7275
result = await engine.generate_sql(
7376
natural_query="Show all customers from California",
7477
database_id="my_db",
75-
execute=False,
78+
execute=True,
7679
show_reasoning=True,
7780
)
78-
print(result.sql) # Generated SQL
79-
print(result.confidence) # Model confidence (0.0-1.0)
80-
print(result.valid_syntax) # Validation result
81-
print(result.intent) # QueryIntent enum (SELECT, AGGREGATE, JOIN, SUBQUERY)
81+
print(result.sql) # Generated SQL
82+
print(result.confidence) # Overall confidence (0.0-1.0)
83+
print(result.reasoning_trace) # Full ReAct reasoning steps
84+
print(result.validation_result) # Validation outcome
8285
```
8386

8487
**Key Components**:
85-
- `QueryIntent` enum: Classifies queries (SELECT, AGGREGATE, JOIN, SUBQUERY, UNKNOWN)
86-
- `SQLValidator`: Syntax checks, injection detection, schema alignment
87-
- `SchemaContext`: Database schema formatted for prompts
88-
- `SQLResult`: Complete result with metadata, warnings, reasoning trace
88+
- `AgentText2SQL`: Main engine with ReAct loop
89+
- `AgentStep`: Individual reasoning step (thought, action, observation)
90+
- `AgentResult`: Complete result with trace and validation
91+
- `QueryHistoryEntry`: Stored queries for retry functionality
92+
- Tools: `sql_executor`, `result_validator`, `schema_inspector`
93+
94+
**ReAct Loop Flow**:
95+
1. **Thought**: Analyze query and determine approach
96+
2. **Action**: Generate SQL using Text2SQL model
97+
3. **Observation**: Execute and inspect results
98+
4. **Validation**: Check if results answer the question
99+
5. **Self-Correction**: If validation fails, iterate with hints
100+
101+
### Text2SQL Engine (app/text2sql_engine.py)
102+
103+
The original orchestrator (still available for simpler use cases):
104+
105+
```python
106+
from app.text2sql_engine import get_text2sql_engine
107+
108+
engine = await get_text2sql_engine()
109+
result = await engine.generate_sql(
110+
natural_query="Show all customers from California",
111+
database_id="my_db",
112+
)
113+
```
89114

90115
### Key Patterns
91116

92-
- **Singletons**: `get_database()`, `get_settings()`, `get_model_loader()`, `get_text2sql_engine()` use `@lru_cache` or global instance
117+
- **Singletons**: `get_database()`, `get_settings()`, `get_model_loader()`, `get_text2sql_engine()`, `get_agent_engine()`
93118
- **Async-First**: All I/O is async; use `AsyncSession` from SQLAlchemy
94119
- **Exception Mapping**: `Text2SQLException` subclasses map to HTTP status codes automatically
95120
- **Rate Limiting**: Slowapi requires first parameter named exactly `request: Request`
96121

97122
### What's Implemented
98123

99124
**Fully Working**:
125+
- Agent-based Text2SQL with ReAct framework (Issue #18)
126+
- Multi-step reasoning with self-correction
127+
- Query history and retry functionality
128+
- Result validation (checks if SQL answers the question)
100129
- Text2SQL Engine with orchestration pipeline
101130
- Query intent classification (SELECT, AGGREGATE, JOIN, SUBQUERY)
102131
- SQL validation (syntax, security, schema alignment)
@@ -106,42 +135,49 @@ print(result.intent) # QueryIntent enum (SELECT, AGGREGATE, JOIN, SUBQUER
106135
- Schema introspection
107136
- Model loading with quantization
108137
- JWT authentication, rate limiting
109-
- Comprehensive test suite (277 tests)
138+
- Comprehensive test suite (300+ tests)
139+
140+
**API Endpoints**:
141+
- `POST /api/v1/query` - Generate SQL with optional execution
142+
- `POST /api/v1/validate` - Validate SQL syntax and schema
143+
- `GET /api/v1/schema/{database_id}` - Get database schema
144+
- `GET /api/v1/agent/reasoning/{query_id}` - Get reasoning trace
145+
- `POST /api/v1/agent/retry` - Retry with correction hints
146+
- `POST /api/v1/auth/token` - JWT authentication
147+
- `GET /api/v1/health` - Health check
110148

111149
**Partially Implemented**:
112150
- `POST /api/v1/schema/register` - Basic placeholder
113-
- `GET /api/v1/agent/reasoning/{query_id}` - Needs storage backend
114-
- `POST /api/v1/agent/retry` - Returns 501 (needs query history storage)
115-
116-
**Planned for Issue #18**:
117-
- Full smolagents ReAct loop
118-
- Multi-step reasoning with self-correction
119-
- Tool-based SQL execution and validation
120151

121152
## Configuration
122153

123154
Settings loaded via Pydantic from environment variables:
124155

125156
```python
126157
settings = get_settings()
127-
settings.huggingface.token # HUGGINGFACE_TOKEN
128-
settings.database.url # DATABASE_URL
129-
settings.api.debug # API_DEBUG
130-
settings.agent.max_steps # AGENT_MAX_STEPS (default: 5)
131-
settings.agent.min_confidence # AGENT_MIN_CONFIDENCE (default: 0.7)
132-
settings.security.secret_key # SECRET_KEY
158+
settings.huggingface.token # HUGGINGFACE_TOKEN
159+
settings.database.url # DATABASE_URL
160+
settings.api.debug # API_DEBUG
161+
settings.agent.max_steps # AGENT_MAX_STEPS (default: 5)
162+
settings.agent.min_confidence # AGENT_MIN_CONFIDENCE (default: 0.7)
163+
settings.agent.enable_validation # AGENT_ENABLE_VALIDATION (default: True)
164+
settings.agent.enable_self_correction # AGENT_ENABLE_SELF_CORRECTION (default: True)
165+
settings.agent.verbosity # AGENT_VERBOSITY (default: 1)
166+
settings.security.secret_key # SECRET_KEY
133167
```
134168

135169
**Required env vars**: `HUGGINGFACE_TOKEN`, `DATABASE_URL`
136170

137171
## Testing
138172

139173
```bash
140-
pytest # All tests
141-
pytest tests/unit/test_text2sql_engine.py -v # Engine tests
142-
pytest tests/unit/test_inference.py -v # Single file
143-
pytest -k "test_config" -v # By name pattern
144-
pytest --pdb # Debug on failure
174+
pytest # All tests
175+
pytest tests/unit/test_agent_engine.py -v # Agent tests
176+
pytest tests/unit/test_agent_tools.py -v # Agent tools tests
177+
pytest tests/unit/test_agent_models.py -v # Agent models tests
178+
pytest tests/unit/test_text2sql_engine.py -v # Core engine tests
179+
pytest -k "test_config" -v # By name pattern
180+
pytest --pdb # Debug on failure
145181
```
146182

147183
**Fixtures** (in `tests/conftest.py`): `test_settings`, `async_engine`, `db_manager`, `sample_schema`, `test_client`
@@ -191,10 +227,21 @@ if password == "demo":
191227
if password == "demo": # nosec B105
192228
```
193229

230+
### Agent Self-Correction
231+
232+
The agent automatically attempts correction when validation fails:
233+
234+
```python
235+
# Validation detects: "Question asks for aggregation but SQL has none"
236+
# Agent will regenerate with hints:
237+
# - "Previous SQL was: SELECT amount FROM orders"
238+
# - "Consider using SUM(), COUNT(), AVG(), etc."
239+
```
240+
194241
## Project Tracking
195242

196243
See GitHub Issues:
197244
- **#17**: Meta tracker with all issues
198245
- **#4**: Core Text2SQL Engine ✅ COMPLETED
199-
- **#18**: smolagents Agent Framework (CRITICAL - next priority)
246+
- **#18**: smolagents Agent Framework ✅ COMPLETED
200247
- **#12**: CI/CD Pipeline (needs deployment workflow)

0 commit comments

Comments
 (0)