Skip to content

Installation

Terminal window
composer require knobik/sql-agent
Terminal window
php artisan sql-agent:install

This will:

  1. Publish the configuration file (config/sql-agent.php)
  2. Publish the Prism config (config/prism.php) for LLM provider credentials
  3. Publish and run migrations
  4. Create the knowledge directory structure at resources/sql-agent/knowledge/

SqlAgent uses Prism PHP to communicate with LLM providers. Configure your provider credentials in config/prism.php (published by the install command), then set the provider and model in your .env:

# For OpenAI (default)
SQL_AGENT_LLM_PROVIDER=openai
SQL_AGENT_LLM_MODEL=gpt-4o
# Or for Anthropic
SQL_AGENT_LLM_PROVIDER=anthropic
SQL_AGENT_LLM_MODEL=claude-sonnet-4-20250514
# Or for Ollama (local)
SQL_AGENT_LLM_PROVIDER=ollama
SQL_AGENT_LLM_MODEL=llama3.1

API keys and base URLs are configured in config/prism.php. See the Prism documentation for provider-specific setup.

Create resources/sql-agent/knowledge/tables/users.json:

{
"table": "users",
"description": "Contains user account information",
"columns": {
"id": "Primary key, auto-incrementing integer",
"name": "User's full name",
"email": "User's email address (unique)",
"created_at": "Account creation timestamp",
"updated_at": "Last update timestamp"
}
}
Terminal window
php artisan sql-agent:load-knowledge
use Knobik\SqlAgent\Facades\SqlAgent;
$response = SqlAgent::run('How many users signed up this month?');
echo $response->answer; // "There are 42 users who signed up this month."
echo $response->sql; // "SELECT COUNT(*) as count FROM users WHERE created_at >= '2026-01-01'"

For the most accurate knowledge retrieval, you can use PostgreSQL’s pgvector extension for semantic similarity search. This requires a separate package:

Terminal window
composer require pgvector/pgvector

Then follow the pgvector setup guide to configure the connection and generate embeddings.

The install command publishes the config, migrations, and knowledge directory automatically. You can also publish individual assets at any time:

TagCommandPublishes To
sql-agent-configphp artisan vendor:publish --tag=sql-agent-configconfig/sql-agent.php
sql-agent-migrationsphp artisan vendor:publish --tag=sql-agent-migrationsdatabase/migrations/
sql-agent-pgvector-migrationsphp artisan vendor:publish --tag=sql-agent-pgvector-migrationsdatabase/migrations/
sql-agent-viewsphp artisan vendor:publish --tag=sql-agent-viewsresources/views/vendor/sql-agent/
sql-agent-knowledgephp artisan vendor:publish --tag=sql-agent-knowledgeresources/sql-agent/knowledge/
sql-agent-promptsphp artisan vendor:publish --tag=sql-agent-promptsresources/views/vendor/sql-agent/prompts/

Published views and prompts override the package defaults, so you can customize the chat UI and the system prompt without modifying the package.