Skip to content

[Store] Add support for Redis #269

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion src/ai-bundle/config/options.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use MongoDB\Client as MongoDbClient;
use Probots\Pinecone\Client as PineconeClient;
use Symfony\AI\Platform\PlatformInterface;
use Symfony\AI\Store\Bridge\Redis\Distance;
use Symfony\AI\Store\StoreInterface;

return static function (DefinitionConfigurator $configurator): void {
Expand Down Expand Up @@ -252,10 +253,30 @@
->end()
->end()
->end()
->arrayNode('redis')
->normalizeKeys(false)
->useAttributeAsKey('name')
->arrayPrototype()
->children()
->variableNode('connection_parameters')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about going with a DSN here instead? For Symfony cache we use REDIS_URL etc. this way we can just reuse an existing Redis connection

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice indeed.

But Symfony cache has a huge amount of code to parse the the DSN.

I don't want to bloat the the store for now

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's good that the RedisStore only takes an instance of Redis. It is not its responsibility to instantiate the Redis client.
The bundle configuration should accept a redis service name, which could be created directly in the application or with SncRedisBundle (which accepts dsn in config).

->info('see https://github.com/phpredis/phpredis?tab=readme-ov-file#example-1')
->isRequired()
->cannotBeEmpty()
->end()
->scalarNode('index_name')->isRequired()->cannotBeEmpty()->end()
->scalarNode('key_prefix')->defaultValue('vector:')->end()
->enumNode('distance')
->info('Distance metric to use for vector similarity search')
->values(Distance::cases())
->defaultValue(Distance::Cosine)
->end()
->end()
->end()
->end()
->arrayNode('surreal_db')
->normalizeKeys(false)
->useAttributeAsKey('name')
->arrayPrototype()
->arrayPrototype()
->children()
->scalarNode('endpoint')->cannotBeEmpty()->end()
->scalarNode('username')->cannotBeEmpty()->end()
Expand Down
23 changes: 23 additions & 0 deletions src/ai-bundle/src/AiBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
use Symfony\AI\Store\Bridge\Neo4j\Store as Neo4jStore;
use Symfony\AI\Store\Bridge\Pinecone\Store as PineconeStore;
use Symfony\AI\Store\Bridge\Qdrant\Store as QdrantStore;
use Symfony\AI\Store\Bridge\Redis\Store as RedisStore;
use Symfony\AI\Store\Bridge\SurrealDb\Store as SurrealDbStore;
use Symfony\AI\Store\Document\Vectorizer;
use Symfony\AI\Store\Indexer;
Expand Down Expand Up @@ -638,6 +639,28 @@ private function processStoreConfig(string $type, array $stores, ContainerBuilde
}
}

if ('redis' === $type) {
foreach ($stores as $name => $store) {
$connectionDefinition = new Definition(\Redis::class);
$connectionDefinition->setArguments([$store['connection_parameters']]);

$arguments = [
$connectionDefinition,
$store['index_name'],
$store['key_prefix'],
$store['distance'],
];

$definition = new Definition(RedisStore::class);
$definition
->addTag('ai.store')
->setArguments($arguments)
;

$container->setDefinition('ai.store.'.$type.'.'.$name, $definition);
}
}

if ('surreal_db' === $type) {
foreach ($stores as $name => $store) {
$arguments = [
Expand Down
9 changes: 9 additions & 0 deletions src/ai-bundle/tests/DependencyInjection/AiBundleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,15 @@ private function getFullConfig(): array
'distance' => 'Cosine',
],
],
'redis' => [
'my_redis_store' => [
'connection_parameters' => [
'host' => '1.2.3.4',
'port' => 6379,
],
'index_name' => 'my_vector_index',
],
],
'surreal_db' => [
'my_surreal_db_store' => [
'endpoint' => 'http://127.0.0.1:8000',
Expand Down
1 change: 1 addition & 0 deletions src/store/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ CHANGELOG
- Qdrant
- SurrealDB
- Neo4j
- Redis
* Add Retrieval Augmented Generation (RAG) support:
- Document embedding storage
- Similarity search for relevant documents
Expand Down
1 change: 1 addition & 0 deletions src/store/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"symfony/ai-platform": "@dev",
"symfony/clock": "^6.4 || ^7.1",
"symfony/http-client": "^6.4 || ^7.1",
"symfony/polyfill-php83": "^1.32",
"symfony/uid": "^6.4 || ^7.1"
},
"require-dev": {
Expand Down
31 changes: 31 additions & 0 deletions src/store/src/Bridge/Redis/Distance.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\AI\Store\Bridge\Redis;

use OskarStark\Enum\Trait\Comparable;

/**
* @author Grégoire Pineau <[email protected]>
*/
enum Distance: string
{
use Comparable;

case Cosine = 'COSINE';
case L2 = 'L2';
case Ip = 'IP';

public function getRedisMetric(): string
{
return $this->value;
}
}
173 changes: 173 additions & 0 deletions src/store/src/Bridge/Redis/Store.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\AI\Store\Bridge\Redis;

use Symfony\AI\Platform\Vector\Vector;
use Symfony\AI\Platform\Vector\VectorInterface;
use Symfony\AI\Store\Document\Metadata;
use Symfony\AI\Store\Document\VectorDocument;
use Symfony\AI\Store\Exception\RuntimeException;
use Symfony\AI\Store\InitializableStoreInterface;
use Symfony\AI\Store\VectorStoreInterface;
use Symfony\Component\Uid\Uuid;

/**
* @author Grégoire Pineau <[email protected]>
*/
class Store implements VectorStoreInterface, InitializableStoreInterface
{
public function __construct(
private readonly \Redis $redis,
private readonly string $indexName,
private readonly string $keyPrefix = 'vector:',
private readonly Distance $distance = Distance::Cosine,
) {
}

/**
* @param array{vector_size?: positive-int, index_method?: string, extra_schema?: list<string>} $options
*
* - For Mistral: ['vector_size' => 1024]
* - For OpenAI: ['vector_size' => 1536]
* - For Gemini: ['vector_size' => 3072] (default)
* - For other models: adjust vector_size accordingly
*/
public function initialize(array $options = []): void
{
$vectorSize = $options['vector_size'] ?? 3072;
$indexMethod = $options['index_method'] ?? 'FLAT'; // Or 'HNSW' for approximate search
$distanceMetric = $this->distance->getRedisMetric();
$extraSchema = $options['extra_schema'] ?? [];

// Create the index with vector field for JSON documents
try {
$this->redis->rawCommand(
'FT.CREATE', $this->indexName, 'ON', 'JSON',
'PREFIX', '1', $this->keyPrefix,
'SCHEMA',
'$.id', 'AS', 'id', 'TEXT',
// '$.metadata', 'AS', 'metadata', 'TEXT',
'$.embedding', 'AS', 'embedding', 'VECTOR', $indexMethod, '6', 'TYPE', 'FLOAT32', 'DIM', $vectorSize, 'DISTANCE_METRIC', $distanceMetric,
...$extraSchema,
);
} catch (\RedisException $e) {
// Index might already exist, check if it's a "already exists" error
if (!str_contains($e->getMessage(), 'Index already exists')) {
throw new RuntimeException(\sprintf('Failed to create Redis index: "%s".', $e->getMessage()), 0, $e);
}
}
}

public function add(VectorDocument ...$documents): void
{
$pipeline = $this->redis->multi(\Redis::PIPELINE);

foreach ($documents as $document) {
$key = $this->keyPrefix.$document->id->toRfc4122();
$data = [
'id' => $document->id->toRfc4122(),
'metadata' => $document->metadata->getArrayCopy(),
'embedding' => $document->vector->getData(),
];

$pipeline->rawCommand('JSON.SET', $key, '$', json_encode($data, \JSON_THROW_ON_ERROR));
}

$pipeline->exec();
}

/**
* @param array<string, mixed> $options
*
* @return VectorDocument[]
*/
public function query(Vector $vector, array $options = []): array
{
$limit = $options['limit'] ?? 5;
$maxScore = $options['maxScore'] ?? null;
$whereFilter = $options['where'] ?? '*';

$query = "({$whereFilter}) => [KNN {$limit} @embedding \$query_vector AS vector_score]";

try {
$results = $this->redis->rawCommand(
'FT.SEARCH',
$this->indexName,
$query,
'PARAMS', 2, 'query_vector', $this->toRedisVector($vector),
'RETURN', 4, '$.id', '$.metadata', '$.embedding', 'vector_score',
'SORTBY', 'vector_score', 'ASC',
'LIMIT', 0, $limit,
'DIALECT', 2
);
} catch (\RedisException $e) {
throw new RuntimeException(\sprintf('Failed to execute query: "%s".', $e->getMessage()), 0, $e);
}

if (!\is_array($results) || \count($results) < 2) {
return [];
}

$documents = [];
$numResults = $results[0];

// Parse results (skip first element which is the count)
for ($i = 1; $i <= $numResults; $i += 2) {
// $docKey = $results[$i];
$docData = $results[$i + 1];

// Convert flat array to associative array
$data = [];
for ($j = 0; $j < \count($docData); $j += 2) {
$fieldName = $docData[$j];
$fieldValue = $docData[$j + 1] ?? null;

if (\is_string($fieldValue) && json_validate($fieldValue)) {
$fieldValue = json_decode($fieldValue, true);
}

$data[$fieldName] = $fieldValue;
}

if (!isset($data['$.id'], $data['vector_score'])) {
continue;
}

$score = (float) $data['vector_score'];

// Apply max score filter if specified
if (null !== $maxScore && $score > $maxScore) {
continue;
}

$documents[] = new VectorDocument(
id: Uuid::fromString($data['$.id']),
vector: new Vector($data['$.embedding'] ?? []),
metadata: new Metadata($data['$.metadata'] ?? []),
score: $score,
);
}

return $documents;
}

private function toRedisVector(VectorInterface $vector): string
{
$data = $vector->getData();
$bytes = '';
foreach ($data as $value) {
$bytes .= pack('f', $value);
}

return $bytes;
}
}
Loading