knobik/sql-agent
Self-learning text-to-SQL agent for Laravel that turns natural language into accurate, safe SQL. Uses schema introspection, a curated knowledge base, query patterns, and conversation memory; recovers from errors and saves learnings. Includes built-in chat UI.
A self-learning text-to-SQL agent that turns natural language into accurate SQL — with context, memory, and a built-in chat UI.
Beta Release — The core API is stabilizing but may still change before v1.0.
composer require knobik/sql-agent
php artisan sql-agent:install
Add your LLM provider to .env:
SQL_AGENT_LLM_PROVIDER=openai # or anthropic, ollama, gemini, mistral, xai...
SQL_AGENT_LLM_MODEL=gpt-4o
use Knobik\SqlAgent\Facades\SqlAgent;
$response = SqlAgent::run('Who are our top 5 customers by spending?');
$response->answer; // "Here are the top 5 customers..."
$response->sql; // "SELECT c.name, SUM(o.total_amount) / 100 AS total_spent..."
$response->results; // [['name' => 'Lowell Boyer', 'total_spent' => 3930.15], ...]
flowchart TD
A[User Question] --> B[Retrieve Knowledge + Learnings]
B --> C[Reason about intent]
C --> D[Generate grounded SQL]
D --> E[Execute and interpret]
E --> F{Result}
F -->|Success| G[Return insight]
F -->|Error| H[Diagnose & Fix]
H --> I[Save Learning]
I --> D
G --> J[Optionally save as Knowledge]
The agent uses six context layers to ground its SQL generation:
| # | Layer | What it contains | Source |
|---|---|---|---|
| 1 | Table Usage | Schema, columns, relationships | knowledge/tables/*.json |
| 2 | Human Annotations | Metrics, definitions, business rules | knowledge/business/*.json |
| 3 | Query Patterns | SQL known to work | knowledge/queries/*.json and *.sql |
| 4 | Learnings | Error patterns and discovered fixes | save_learning tool (on-demand) |
| 5 | Runtime Context | Live schema inspection | introspect_schema tool (on-demand) |
| 6 | Institutional Knowledge | Docs, wikis, external references | Custom tools (agent.tools config) |
Layers 1–3 are loaded from the knowledge base into the system prompt. Layer 4 is built up over time as the agent learns from errors. Layers 5 and 6 are available on-demand — the LLM calls them during the tool loop when it needs live schema details or external context.
# Run all tests
composer test
# Run with coverage
composer test-coverage
When testing code that uses SqlAgent, you can mock the facade:
use Knobik\SqlAgent\Facades\SqlAgent;
use Knobik\SqlAgent\Data\AgentResponse;
public function test_it_handles_sql_agent_response(): void
{
SqlAgent::shouldReceive('run')
->with('How many users?')
->andReturn(new AgentResponse(
answer: 'There are 100 users.',
sql: 'SELECT COUNT(*) FROM users',
results: [['count' => 100]],
));
$response = $this->post('/api/query', ['question' => 'How many users?']);
$response->assertJson(['answer' => 'There are 100 users.']);
}
Please see CONTRIBUTING.md for details.
SQL Agent for Laravel is open-sourced software licensed under the Apache-2.0 License.
How can I help you explore Laravel packages today?