Database Insights Tool¶
Module:
neurosurfer.tools.sql.db_insights_tool.DBInsightsTool
Pairs with:BaseTool•ToolSpec•Toolkit
Overview¶
DBInsightsTool answers high-level, conceptual questions about your database using table summaries/metadata (not by executing SQL). It’s ideal for explaining the purpose of the database, the roles of tables, and relationships that may have architectural or security implications.
It builds a prompt from your schema store and queries your LLM to produce a structured, concise narrative answer.
When to Use¶
Use this tool when you need: - Conceptual insights about the overall database design. - Explanations about how entities relate (e.g., departments ↔ roles ↔ access control). - Architectural/security observations that cannot be answered by running a SQL query.
Not for: returning data rows or executing queries (use SQLExecutor for that).
Spec (Inputs & Returns)¶
| Field | Type | Required | Description |
|---|---|---|---|
query | str | ✓ | A natural-language question about the database's structure, design, or semantics. |
Returns: str — A natural-language explanation synthesizing table metadata and relationships.
Note:
ToolSpechere declares types asstr(alias ofstringsupported by the validator).
Runtime Dependencies & Config¶
- Constructor:
DBInsightsTool(llm: BaseModel, sql_schema_store: SQLSchemaStore, logger: logging.Logger | None = None) - Uses:
sql_schema_store.store: Dict[str, {{summary: str, ...}}] - Prompt: Built from
DATABASE_INSIGHT_PROMPT: system_prompt: senior DB architect personauser_prompt: injects yourqueryand aggregated table summaries- LLM call:
llm.ask(system_prompt, user_prompt, temperature=0.7, max_new_tokens=3000, stream=True) - Streaming:
Trueby default →observationmay be a generator. final_answer: Defaults toTrue(can be overridden viakwargs["final_answer"]).
Behavior¶
- Collects table summaries via
get_tables_summaries__(). - Renders system and user prompts.
- Calls
llm.ask(...)and returns aToolResponsewithfinal_answer=Trueby default (streaming enabled).
Special token: " [__DATABASE_INSIGHT__] " is available on the instance if you need to tag content for downstream parsing.
Usage¶
tool = DBInsightsTool(llm=chat_llm, sql_schema_store=schema_store)
resp = tool(query="How do departments relate to approvals?")
# resp.observation may be a streaming generator (depending on llm.ask)
Error Handling & Notes¶
- If the schema store lacks summaries, answers may be limited; the prompt asks the LLM to say “don’t know” where appropriate.
- This tool does not execute SQL and does not inspect live data.
- Ensure
sql_schema_store.storecontains{{table_name: {{'summary': '...'}}}}for best results.