Skip to content

LLM & Search Drivers

SqlAgent uses Prism PHP for LLM integration and a driver-based architecture for knowledge search. You can switch providers and drivers via environment variables without changing any code.

SqlAgent delegates all LLM communication to Prism PHP, which provides a unified interface for many providers. Set the active provider and model using environment variables:

SQL_AGENT_LLM_PROVIDER=openai
SQL_AGENT_LLM_MODEL=gpt-4o

Provider credentials (API keys, base URLs) are configured in Prism’s own config file at config/prism.php. Publish it with:

Terminal window
php artisan vendor:publish --tag=prism-config

Prism supports a wide range of providers out of the box. Here are some common options:

ProviderSQL_AGENT_LLM_PROVIDERExample Model
OpenAIopenaigpt-4o, gpt-4o-mini
Anthropicanthropicclaude-sonnet-4-20250514
Ollamaollamallama3.1, qwen2.5
Google Geminigeminigemini-2.0-flash
Mistralmistralmistral-large-latest
xAIxaigrok-2

See the Prism documentation for the full list of supported providers and their configuration.

Use the provider_options config array to pass provider-specific options. For example, to enable thinking/reasoning mode on Ollama models:

config/sql-agent.php
'llm' => [
'provider' => 'ollama',
'model' => 'qwen2.5',
'provider_options' => ['thinking' => true],
],

These options are passed directly to Prism’s withProviderOptions() method.

Since SqlAgent uses Prism for all LLM communication, adding a new provider means adding it to Prism. See the Prism documentation for instructions on registering custom providers.

Search drivers control how SqlAgent finds relevant knowledge (table metadata, business rules, query patterns) based on the user’s question. Set the active driver using SQL_AGENT_SEARCH_DRIVER.

Uses your database’s native full-text search capabilities. No external services required:

SQL_AGENT_SEARCH_DRIVER=database

The behavior varies by database engine:

DatabaseImplementationNotes
MySQLMATCH ... AGAINSTSupports natural language and boolean mode
PostgreSQLto_tsvector / to_tsqueryConfigurable text search language
SQLiteLIKE queriesLess accurate, but functional for development
SQL ServerCONTAINS predicatesRequires a full-text catalog to be configured

Uses PostgreSQL’s pgvector extension for semantic similarity search via vector embeddings. This provides the most accurate search results by understanding the meaning of queries rather than just matching keywords.

The pgvector driver uses a dedicated PostgreSQL connection for storing embeddings, separate from your main application database. This means you can use MySQL, SQLite, or any other database for your app and SqlAgent storage while running pgvector on a specialized PostgreSQL instance.

The pgvector search driver requires the pgvector/pgvector Composer package, which is not installed by default:

Terminal window
composer require pgvector/pgvector

Add a PostgreSQL connection to config/database.php:

'connections' => [
'pgvector' => [
'driver' => 'pgsql',
'host' => env('PGVECTOR_HOST', '127.0.0.1'),
'port' => env('PGVECTOR_PORT', '5432'),
'database' => env('PGVECTOR_DATABASE', 'embeddings'),
'username' => env('PGVECTOR_USERNAME', 'postgres'),
'password' => env('PGVECTOR_PASSWORD', ''),
],
],

Set the environment variables in your .env:

SQL_AGENT_SEARCH_DRIVER=pgvector
SQL_AGENT_EMBEDDINGS_CONNECTION=pgvector
SQL_AGENT_EMBEDDINGS_PROVIDER=openai
SQL_AGENT_EMBEDDINGS_MODEL=text-embedding-3-small
SQL_AGENT_EMBEDDINGS_DIMENSIONS=1536

Run the setup command to publish the pgvector migration and create the embeddings table:

Terminal window
php artisan sql-agent:setup-pgvector

This command will:

  1. Verify that SQL_AGENT_EMBEDDINGS_CONNECTION points to a PostgreSQL database
  2. Publish the embeddings migration
  3. Run migrations (creates the extension, table, and HNSW index)

Then generate embeddings for any existing knowledge base records:

Terminal window
php artisan sql-agent:generate-embeddings

Embeddings are automatically kept in sync when records are created or updated.