Skip to content

Commit c5aa431

Browse files
committed
[Store] Add support for Redis
1 parent 0b2a28c commit c5aa431

File tree

10 files changed

+880
-0
lines changed

10 files changed

+880
-0
lines changed

examples/compose.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,11 @@ services:
126126
ports:
127127
- '6333:6333'
128128

129+
redis:
130+
image: redis:8.0.3
131+
ports:
132+
- '6379:6379'
133+
129134
surrealdb:
130135
image: surrealdb/surrealdb:v2
131136
command: [ 'start', '--user', 'symfony', '--pass', 'symfony' ]

examples/rag/redis.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
use Symfony\AI\Agent\Agent;
13+
use Symfony\AI\Agent\Toolbox\AgentProcessor;
14+
use Symfony\AI\Agent\Toolbox\Tool\SimilaritySearch;
15+
use Symfony\AI\Agent\Toolbox\Toolbox;
16+
use Symfony\AI\Fixtures\Movies;
17+
use Symfony\AI\Platform\Bridge\OpenAi\Embeddings;
18+
use Symfony\AI\Platform\Bridge\OpenAi\PlatformFactory;
19+
use Symfony\AI\Platform\Message\Message;
20+
use Symfony\AI\Platform\Message\MessageBag;
21+
use Symfony\AI\Store\Bridge\Redis\Store;
22+
use Symfony\AI\Store\Document\Loader\InMemoryLoader;
23+
use Symfony\AI\Store\Document\Metadata;
24+
use Symfony\AI\Store\Document\TextDocument;
25+
use Symfony\AI\Store\Document\Vectorizer;
26+
use Symfony\AI\Store\Indexer;
27+
use Symfony\Component\Uid\Uuid;
28+
29+
require_once dirname(__DIR__).'/bootstrap.php';
30+
31+
// initialize the store
32+
$redis = new Redis([
33+
'host' => 'localhost',
34+
'port' => 6379,
35+
]);
36+
$store = new Store(
37+
redis: $redis,
38+
indexName: 'my_index',
39+
);
40+
41+
// create embeddings and documents
42+
$documents = [];
43+
foreach (Movies::all() as $i => $movie) {
44+
$documents[] = new TextDocument(
45+
id: Uuid::v4(),
46+
content: 'Title: '.$movie['title'].\PHP_EOL.'Director: '.$movie['director'].\PHP_EOL.'Description: '.$movie['description'],
47+
metadata: new Metadata($movie),
48+
);
49+
}
50+
51+
// initialize the table
52+
$store->setup(['vector_size' => 1536]);
53+
54+
// create embeddings for documents
55+
$platform = PlatformFactory::create(env('OPENAI_API_KEY'), http_client());
56+
$vectorizer = new Vectorizer($platform, 'text-embedding-3-small', logger());
57+
$indexer = new Indexer(new InMemoryLoader($documents), $vectorizer, $store, logger: logger());
58+
$indexer->index($documents);
59+
60+
$similaritySearch = new SimilaritySearch($vectorizer, $store);
61+
$toolbox = new Toolbox([$similaritySearch], logger: logger());
62+
$processor = new AgentProcessor($toolbox);
63+
$agent = new Agent($platform, 'gpt-4o-mini', [$processor], [$processor], logger: logger());
64+
65+
$messages = new MessageBag(
66+
Message::forSystem('Please answer all user questions only using SimilaritySearch function.'),
67+
Message::ofUser('Which movie fits the theme of technology?')
68+
);
69+
$result = $agent->call($messages);
70+
71+
echo $result->getContent().\PHP_EOL;
72+
73+
$store->drop();

src/ai-bundle/config/options.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\AI\Platform\Bridge\OpenAi\PlatformFactory;
1818
use Symfony\AI\Platform\Capability;
1919
use Symfony\AI\Platform\PlatformInterface;
20+
use Symfony\AI\Store\Bridge\Redis\Distance;
2021
use Symfony\AI\Store\Document\VectorizerInterface;
2122
use Symfony\AI\Store\StoreInterface;
2223
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
@@ -598,6 +599,36 @@
598599
->end()
599600
->end()
600601
->end()
602+
->arrayNode('redis')
603+
->useAttributeAsKey('name')
604+
->arrayPrototype()
605+
->children()
606+
->variableNode('connection_parameters')
607+
->info('see https://github.com/phpredis/phpredis?tab=readme-ov-file#example-1')
608+
->cannotBeEmpty()
609+
->end()
610+
->stringNode('client')
611+
->info('a service id of a Redis client')
612+
->cannotBeEmpty()
613+
->end()
614+
->stringNode('index_name')->isRequired()->cannotBeEmpty()->end()
615+
->stringNode('key_prefix')->defaultValue('vector:')->end()
616+
->enumNode('distance')
617+
->info('Distance metric to use for vector similarity search')
618+
->values(Distance::cases())
619+
->defaultValue(Distance::Cosine)
620+
->end()
621+
->end()
622+
->validate()
623+
->ifTrue(static fn ($v) => !isset($v['connection_parameters']) && !isset($v['client']))
624+
->thenInvalid('Either "connection_parameters" or "client" must be configured.')
625+
->end()
626+
->validate()
627+
->ifTrue(static fn ($v) => isset($v['connection_parameters']) && isset($v['client']))
628+
->thenInvalid('Either "connection_parameters" or "client" can be configured, but not both.')
629+
->end()
630+
->end()
631+
->end()
601632
->arrayNode('surreal_db')
602633
->useAttributeAsKey('name')
603634
->arrayPrototype()

src/ai-bundle/src/AiBundle.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
use Symfony\AI\Store\Bridge\Neo4j\Store as Neo4jStore;
7070
use Symfony\AI\Store\Bridge\Pinecone\Store as PineconeStore;
7171
use Symfony\AI\Store\Bridge\Qdrant\Store as QdrantStore;
72+
use Symfony\AI\Store\Bridge\Redis\Store as RedisStore;
7273
use Symfony\AI\Store\Bridge\SurrealDb\Store as SurrealDbStore;
7374
use Symfony\AI\Store\Bridge\Typesense\Store as TypesenseStore;
7475
use Symfony\AI\Store\Bridge\Weaviate\Store as WeaviateStore;
@@ -1075,6 +1076,30 @@ private function processStoreConfig(string $type, array $stores, ContainerBuilde
10751076
}
10761077
}
10771078

1079+
if ('redis' === $type) {
1080+
foreach ($stores as $name => $store) {
1081+
if (isset($store['client'])) {
1082+
$redisClient = new Reference($store['client']);
1083+
} else {
1084+
$redisClient = new Definition(\Redis::class);
1085+
$redisClient->setArguments([$store['connection_parameters']]);
1086+
}
1087+
1088+
$definition = new Definition(RedisStore::class);
1089+
$definition
1090+
->addTag('ai.store')
1091+
->setArguments([
1092+
$redisClient,
1093+
$store['index_name'],
1094+
$store['key_prefix'],
1095+
$store['distance'],
1096+
])
1097+
;
1098+
1099+
$container->setDefinition('ai.store.'.$type.'.'.$name, $definition);
1100+
}
1101+
}
1102+
10781103
if ('surreal_db' === $type) {
10791104
foreach ($stores as $name => $store) {
10801105
$arguments = [

src/ai-bundle/tests/DependencyInjection/AiBundleTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2855,6 +2855,15 @@ private function getFullConfig(): array
28552855
'distance' => 'Cosine',
28562856
],
28572857
],
2858+
'redis' => [
2859+
'my_redis_store' => [
2860+
'connection_parameters' => [
2861+
'host' => '1.2.3.4',
2862+
'port' => 6379,
2863+
],
2864+
'index_name' => 'my_vector_index',
2865+
],
2866+
],
28582867
'surreal_db' => [
28592868
'my_surreal_db_store' => [
28602869
'endpoint' => 'http://127.0.0.1:8000',

src/store/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ CHANGELOG
4545
- Pinecone
4646
- PostgreSQL with pgvector extension
4747
- Qdrant
48+
- Redis
4849
- SurrealDB
4950
- Typesense
5051
- Weaviate

src/store/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"symfony/ai-platform": "@dev",
3939
"symfony/clock": "^7.3|^8.0",
4040
"symfony/http-client": "^7.3|^8.0",
41+
"symfony/polyfill-php83": "^1.32",
4142
"symfony/uid": "^7.3|^8.0"
4243
},
4344
"require-dev": {
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\AI\Store\Bridge\Redis;
13+
14+
use OskarStark\Enum\Trait\Comparable;
15+
16+
/**
17+
* @author Grégoire Pineau <[email protected]>
18+
*/
19+
enum Distance: string
20+
{
21+
use Comparable;
22+
23+
case Cosine = 'COSINE';
24+
case L2 = 'L2';
25+
case Ip = 'IP';
26+
}

0 commit comments

Comments
 (0)