symfony/ai-redis-store
Redis-backed vector store for Symfony AI Store. Create and query vector indexes in Redis using RediSearch (FT.CREATE/FT.SEARCH) with KNN and DIALECT 2 support. Ideal for semantic search and retrieval workflows powered by Redis vector features.
Install the Package:
composer require symfony/ai-redis-store
(For Laravel, ensure Symfony’s RedisClientInterface is compatible; see Implementation Patterns.)
Configure Redis: Ensure your Redis server (7.0+) has the AI modules enabled. Verify with:
redis-cli MODULE LIST
(Should list RedisJSON, RedisSearch, and RedisTimeSeries modules.)
Define a Vector Index: Create a Redis index via CLI or code. Example CLI command:
redis-cli FT.CREATE ai_vectors ON HASH PREFIX 1 "vector:" SCHEMA vector FIELD HASH $ DIM 1536
(Replace DIM with your embedding dimension, e.g., 1536 for text-embedding-ada-002.)
Integrate with Symfony AI:
Configure the store in config/packages/ai.yaml:
framework:
ai:
store:
redis:
url: '%env(REDIS_DSN)%'
index_name: 'ai_vectors'
First Use Case: Insert and query vectors in a controller:
use Symfony\AI\Store\StoreInterface;
class VectorController {
public function __construct(private StoreInterface $store) {}
public function addVector(string $id, array $embedding): void {
$this->store->insert($id, $embedding);
}
public function findSimilar(string $queryEmbedding, int $limit = 5) {
return $this->store->search($queryEmbedding, $limit);
}
}
$this->store->insert('user_123', [0.1, 0.5, ..., 0.9]); // 1536-dim vector
$this->store->insertMany([
'user_123' => [0.1, 0.5, ...],
'user_456' => [0.2, 0.6, ...],
]);
$this->store->remove('user_123');
$results = $this->store->search([0.3, 0.7, ...], 3); // Top 3 matches
$results = $this->store->search(
[0.3, 0.7, ...],
3,
['@tag': ['recommendation']] // RedisSearch filter syntax
);
Combine vector similarity with Redis full-text search:
$results = $this->store->search(
[0.3, 0.7, ...],
5,
['@text': 'machine learning'] // Full-text query
);
Create a Laravel-compatible store by implementing Symfony\AI\Store\StoreInterface:
// app/Services/AiRedisStore.php
use Symfony\Component\Redis\ClientInterface;
use Symfony\AI\Store\StoreInterface;
class AiRedisStore implements StoreInterface {
public function __construct(private ClientInterface $redis) {}
public function insert(string $id, array $vector): void {
$this->redis->hSet('vector:' . $id, 'vector', json_encode($vector));
$this->redis->ftCreate('ai_vectors', 'ON HASH PREFIX 1 "vector:"');
}
// Implement other StoreInterface methods...
}
// app/Providers/AiServiceProvider.php
use Symfony\Component\Redis\ClientInterface;
public function register() {
$this->app->bind(StoreInterface::class, function ($app) {
return new AiRedisStore($app->make(ClientInterface::class));
});
}
Use symfony/redis with Laravel’s Redis:
composer require symfony/redis
Configure in config/services.php:
'redis' => [
'client' => Symfony\Component\Redis\Client::class,
'dsn' => env('REDIS_DSN'),
],
Recreate indices on schema changes:
public function recreateIndex(): void {
$this->redis->ftDropIndex('ai_vectors');
$this->redis->ftCreate('ai_vectors', 'ON HASH PREFIX 1 "vector:" SCHEMA vector FIELD HASH $ DIM 1536');
}
Use RedisSearch parameters for performance:
$results = $this->store->search(
$queryVector,
10,
[],
['DIALECT' => 2, 'LIMIT' => 10, 'SORTBY' => 'vector:>score'] // DIALECT 2 for KNN
);
Cache frequent queries to reduce Redis load:
use Symfony\Component\Cache\Adapter\RedisAdapter;
public function __construct(
private StoreInterface $store,
private RedisAdapter $cache
) {}
public function getCachedSimilarVectors(string $query, int $limit): array {
$cacheKey = "similarity:{$query}:{$limit}";
return $this->cache->get($cacheKey, function () use ($query, $limit) {
return $this->store->search($query, $limit);
}, 300); // Cache for 5 minutes
}
Redis Version Mismatch:
RedisSearch commands not supported.redis-cli MODULE LIST
Index Schema Mismatch:
WRONGTYPE Operation against a key holding the wrong kind of value.SCHEMA and DIM:
redis-cli FT.CREATE ai_vectors ON HASH PREFIX 1 "vector:" SCHEMA vector FIELD HASH $ DIM 1536
Memory Limits:
maxmemory policy.redis-cli INFO memory
Configure MAXENTRIES in the index:
redis-cli FT.CREATE ai_vectors ON HASH PREFIX 1 "vector:" MAXENTRIES 1000000
Vector Dimension Mismatch:
ERR Wrong number of arguments for 'FT.SEARCH' command.DIM (e.g., 1536 for OpenAI embeddings).Laravel-Symfony Redis Client Conflict:
Class 'Symfony\Component\Redis\ClientInterface' not found.symfony/redis and bind it to Laravel’s Redis:
$this->app->bind(\Symfony\Component\Redis\ClientInterface::class, function ($app) {
return new \Symfony\Component\Redis\Client($app['redis.client']);
});
Inspect Redis Index:
redis-cli FT._LIST
redis-cli FT.INFO ai_vectors
Log Raw Redis Commands: Enable Symfony’s Redis debug mode:
# config/packages/redis.yaml
framework.redis:
client:
logging: true
Validate Vectors: Check vector dimensions before insertion:
if (count($vector) !== 1536) {
throw new \InvalidArgumentException('Vector dimension must be 1536.');
}
Query Profiling:
Use Redis’s FT.SEARCH with PROFILE:
redis-cli FT.SEARCH ai_vectors "vector:[0.3 0.7 ...]" PROFILE
public function searchWithCustomScore(array $queryVector, int $limit, callable $scorer) {
$results = $this->redis->ftSearch('ai_vectors', '*=>[KNN 3 @vector:$queryVector AS score]');
return array_map($scorer, $results);
}
How can I help you explore Laravel packages today?