Search by

x-laravel / embedding-sqlsrv-driver

X-Adam

SQL Server 2025 native vector similarity driver for x-laravel/embedding.

Package info

github.com/x-laravel/embedding-sqlsrv-driver

pkg:composer/x-laravel/embedding-sqlsrv-driver

Statistics

Installs: 0

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v1.0.0 2026-09-24 16:22 UTC

This package is auto-updated.

Last update: 2026-09-24 16:28:13 UTC


README

Tests PHP Laravel License

SQL Server 2025 / Azure SQL native vector driver for x-laravel/embedding.

How It Works

  • Implements SimilarityDriver — registers as the sqlsrv driver, similarity search runs entirely in SQL Server using VECTOR_DISTANCE('cosine', ...)
  • Implements VectorStore — writes embeddings via MERGE INTO with CAST(? AS VECTOR(n)), reads via a CAST(vector AS NVARCHAR(MAX)) global scope
  • Translates payload filter: constraints to type-strict OPENJSON SQL against the embeddables table

Requirements

  • PHP ^8.3 + ext-sqlsrv + ext-pdo_sqlsrv + Microsoft ODBC Driver 18
  • Laravel ^12.0 | ^13.0
  • x-laravel/embedding ^1.0
  • SQL Server 2025 or Azure SQL Database (native VECTOR type required)

Installation

composer require x-laravel/embedding-sqlsrv-driver

The SqlServerEmbeddingServiceProvider is auto-discovered and registers the sqlsrv driver automatically.

Setup

1. Configure x-laravel/embedding

Publish the config if you haven't already:

php artisan vendor:publish --tag=embedding-config

Set the similarity driver and database connection in config/embedding.php:

'database' => [
    'connection' => env('EMBEDDINGS_DATABASE_CONNECTION', env('DB_CONNECTION', 'sqlsrv')),
    'embeddings_table' => env('EMBEDDINGS_DB_TABLE', 'embeddings'),
    'embeddables_table' => env('EMBEDDABLES_DB_TABLE', 'embeddables'),
],

'similarity' => [
    'driver' => env('EMBEDDING_SIMILARITY_DRIVER', 'sqlsrv'),
],

2. Create the tables

This driver ships its own SQL Server-native migrations that replace the default ones from x-laravel/embedding: embeddings with a VECTOR(n) column and embeddables with a native JSON payload column.

Publish and run the migrations (migrations are not loaded automatically — publish the driver migrations, not the core ones):

php artisan vendor:publish --tag=embedding-sqlsrv-migrations
php artisan migrate

The published files are plain migrations in database/migrations/ — customise the DDL there if needed before running migrate.

Note: The native VECTOR and JSON types and VECTOR_DISTANCE require SQL Server 2025 or Azure SQL Database. They are not available in SQL Server 2019/2022.

3. Model

Follow the standard x-laravel/embedding setup. No SQL Server-specific changes are needed on your models.

use XLaravel\Embedding\Attributes\EmbedOn;
use XLaravel\Embedding\Concerns\Embeddable;
use XLaravel\Embedding\Contracts\HasEmbeddings;

#[EmbedOn(['title', 'body'])]
class Post extends Model implements HasEmbeddings
{
    use Embeddable;

    public function toEmbeddingText(string $slot = 'default'): string
    {
        return $this->title.' '.$this->body;
    }
}

Usage

The driver is transparent — use the standard x-laravel/embedding API:

Post::similarToText('web framework', limit: 10);
Post::similarTo($vector, limit: 10, threshold: 0.8);
Post::rankByRelevance($posts, 'web framework');

$post->mostSimilar(limit: 5);
$post->similarityTo($otherPost);

All methods set a similarity_score float attribute on each returned model.

Payload filtering

Models using #[EmbedPayload] can filter similarity searches at the database level. The driver translates filter: to a whereExists subquery with OPENJSON — every condition checks the JSON value type, and strings compare with a binary collation, so comparisons are type-strict (34 never matches "34") and case-sensitive:

use XLaravel\Embedding\Attributes\EmbedPayload;

#[EmbedOn('name')]
#[EmbedPayload(['province_id', 'category_id', 'active'])]
class Venue extends Model implements HasEmbeddings { ... }

Venue::similarTo($vector, limit: 300, filter: ['province_id' => 34]);            // equality
Venue::similarToText('kebap', filter: ['category_id' => [3, 7]]);                // IN
$venue->mostSimilar(limit: 5, filter: ['province_id' => 34, 'active' => true]);  // AND

Note: pdo_sqlsrv returns integer columns to PHP as strings. Add an integer cast to numeric payload fields on your models — otherwise a queued payload sync writes "34" instead of 34 after the DB round-trip, and type-strict filters stop matching.

Testing

# Build first (once per PHP version)
DOCKER_BUILDKIT=0 docker compose --profile php83 build

# Run tests
docker compose --profile php83 up
docker compose --profile php84 up
docker compose --profile php85 up

License

This package is open-sourced software licensed under the MIT license.