LangGraph SQL 智能體只查看查詢所需的資料表
A LangGraph SQL agent that only sees the tables it needs
Ashish Sinha 為 schemagate 加入 LangGraph SQL 智能體範例,先離線選取問題所需資料表,再讓模型只用這些資料表編寫 SELECT。
Most text-to-SQL agents paste the whole schema into every prompt. On a real database, most of it is tables the question never touches, and you pay for them on every call.
I added a small LangGraph example to schemagate that does it differently. It has four nodes:
-
select_schema:
SchemagateRetrieverpicks the few tables the question needs. This step runs offline, with no model call. - write_sql: the model writes one SELECT using only those tables.
- run_sql: the agent runs the query. If it fails, the error goes back to the model once.
- answer: the model answers in one sentence from the returned rows.
On the bundled 42-table demo database:
| Question | Schema tokens sent |
|---|---|
| Which customers owe us money? | 489 vs 2,199 (78% fewer) |
| How many orders did each carrier ship? | 706 vs 2,199 (68% fewer) |
| Which products are below their reorder level, and in which warehouse? | 563 vs 2,199 (74% fewer) |
All three answers were correct with Claude Haiku. On databases with 50+ tables the saving reaches about 91%. The selection also respects roles: a table the caller can't read never appears in the prompt.
pip install langchain-schemagate langgraph langchain-anthropic tiktoken
python examples/langgraph_sql_agent.py
To use your own database, swap demo_catalog() for Catalog().bootstrap("postgresql://...").
schemagate is #1 on the BIRD Mini-Dev leaderboard (SQLite, PostgreSQL, MySQL), is listed in LangChain's official retriever integrations, and is Apache-2.0.
Example: examples/langgraph_sql_agent.py
來源:DEV Community · dev.to