Spaces:
Sleeping
Sleeping
Melika Kheirieh
commited on
Commit
·
78914bb
1
Parent(s):
c76014a
feat(api): add /schema endpoint for database introspection
Browse files- app/routers/nl2sql.py +26 -1
app/routers/nl2sql.py
CHANGED
|
@@ -10,7 +10,7 @@ import uuid
|
|
| 10 |
from typing import Any, Dict, Optional, TypedDict, Union, cast, Callable
|
| 11 |
|
| 12 |
# --- Third-party ---
|
| 13 |
-
from fastapi import APIRouter, HTTPException, UploadFile, File, Depends
|
| 14 |
|
| 15 |
# --- Local ---
|
| 16 |
from app.schemas import NL2SQLRequest, NL2SQLResponse, ClarifyResponse
|
|
@@ -169,6 +169,31 @@ def _select_adapter(db_id: Optional[str]) -> Union[PostgresAdapter, SQLiteAdapte
|
|
| 169 |
return SQLiteAdapter(str(default_path))
|
| 170 |
|
| 171 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
# -------------------------------
|
| 173 |
# LLM & Pipeline builders (lazy)
|
| 174 |
# -------------------------------
|
|
|
|
| 10 |
from typing import Any, Dict, Optional, TypedDict, Union, cast, Callable
|
| 11 |
|
| 12 |
# --- Third-party ---
|
| 13 |
+
from fastapi import APIRouter, HTTPException, UploadFile, File, Depends, Query
|
| 14 |
|
| 15 |
# --- Local ---
|
| 16 |
from app.schemas import NL2SQLRequest, NL2SQLResponse, ClarifyResponse
|
|
|
|
| 169 |
return SQLiteAdapter(str(default_path))
|
| 170 |
|
| 171 |
|
| 172 |
+
# -------------------------------
|
| 173 |
+
# Schema preview endpoint
|
| 174 |
+
# -------------------------------
|
| 175 |
+
|
| 176 |
+
|
| 177 |
+
@router.get("/schema")
|
| 178 |
+
def get_schema(db_id: Optional[str] = Query(default=None)):
|
| 179 |
+
"""
|
| 180 |
+
Return a schema preview for a given db_id (SQLite only).
|
| 181 |
+
If db_id is omitted, returns the default database schema.
|
| 182 |
+
"""
|
| 183 |
+
try:
|
| 184 |
+
adapter = _select_adapter(db_id)
|
| 185 |
+
preview = _derive_schema_preview(adapter)
|
| 186 |
+
if not preview.strip():
|
| 187 |
+
raise HTTPException(
|
| 188 |
+
status_code=404, detail="Schema preview not available or empty"
|
| 189 |
+
)
|
| 190 |
+
return {"db_id": db_id or "default", "schema_preview": preview}
|
| 191 |
+
except HTTPException:
|
| 192 |
+
raise
|
| 193 |
+
except Exception as e:
|
| 194 |
+
raise HTTPException(status_code=500, detail=f"Schema introspection failed: {e}")
|
| 195 |
+
|
| 196 |
+
|
| 197 |
# -------------------------------
|
| 198 |
# LLM & Pipeline builders (lazy)
|
| 199 |
# -------------------------------
|