Skip to content
Merged
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
5 changes: 5 additions & 0 deletions examples/compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ services:
ports:
- '6333:6333'

redis:
image: redis:8.0.3
ports:
- '6379:6379'

surrealdb:
image: surrealdb/surrealdb:v2
command: [ 'start', '--user', 'symfony', '--pass', 'symfony' ]
Expand Down
73 changes: 73 additions & 0 deletions examples/rag/redis.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

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

use Symfony\AI\Agent\Agent;
use Symfony\AI\Agent\Toolbox\AgentProcessor;
use Symfony\AI\Agent\Toolbox\Tool\SimilaritySearch;
use Symfony\AI\Agent\Toolbox\Toolbox;
use Symfony\AI\Fixtures\Movies;
use Symfony\AI\Platform\Bridge\OpenAi\Embeddings;
use Symfony\AI\Platform\Bridge\OpenAi\PlatformFactory;
use Symfony\AI\Platform\Message\Message;
use Symfony\AI\Platform\Message\MessageBag;
use Symfony\AI\Store\Bridge\Redis\Store;
use Symfony\AI\Store\Document\Loader\InMemoryLoader;
use Symfony\AI\Store\Document\Metadata;
use Symfony\AI\Store\Document\TextDocument;
use Symfony\AI\Store\Document\Vectorizer;
use Symfony\AI\Store\Indexer;
use Symfony\Component\Uid\Uuid;

require_once dirname(__DIR__).'/bootstrap.php';

// initialize the store
$redis = new Redis([
'host' => 'localhost',
'port' => 6379,
]);
$store = new Store(
redis: $redis,
indexName: 'my_index',
);

// create embeddings and documents
$documents = [];
foreach (Movies::all() as $i => $movie) {
$documents[] = new TextDocument(
id: Uuid::v4(),
content: 'Title: '.$movie['title'].\PHP_EOL.'Director: '.$movie['director'].\PHP_EOL.'Description: '.$movie['description'],
metadata: new Metadata($movie),
);
}

// initialize the table
$store->setup(['vector_size' => 1536]);

// create embeddings for documents
$platform = PlatformFactory::create(env('OPENAI_API_KEY'), http_client());
$vectorizer = new Vectorizer($platform, 'text-embedding-3-small', logger());
$indexer = new Indexer(new InMemoryLoader($documents), $vectorizer, $store, logger: logger());
$indexer->index($documents);

$similaritySearch = new SimilaritySearch($vectorizer, $store);
$toolbox = new Toolbox([$similaritySearch], logger: logger());
$processor = new AgentProcessor($toolbox);
$agent = new Agent($platform, 'gpt-4o-mini', [$processor], [$processor], logger: logger());

$messages = new MessageBag(
Message::forSystem('Please answer all user questions only using SimilaritySearch function.'),
Message::ofUser('Which movie fits the theme of technology?')
);
$result = $agent->call($messages);

echo $result->getContent().\PHP_EOL;

$store->drop();
31 changes: 31 additions & 0 deletions src/ai-bundle/config/options.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Symfony\AI\Platform\Bridge\OpenAi\PlatformFactory;
use Symfony\AI\Platform\Capability;
use Symfony\AI\Platform\PlatformInterface;
use Symfony\AI\Store\Bridge\Redis\Distance;
use Symfony\AI\Store\Document\VectorizerInterface;
use Symfony\AI\Store\StoreInterface;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
Expand Down Expand Up @@ -598,6 +599,36 @@
->end()
->end()
->end()
->arrayNode('redis')
->useAttributeAsKey('name')
->arrayPrototype()
->children()
->variableNode('connection_parameters')
->info('see https://github.com/phpredis/phpredis?tab=readme-ov-file#example-1')
->cannotBeEmpty()
->end()
->stringNode('client')
->info('a service id of a Redis client')
->cannotBeEmpty()
->end()
->stringNode('index_name')->isRequired()->cannotBeEmpty()->end()
->stringNode('key_prefix')->defaultValue('vector:')->end()
->enumNode('distance')
->info('Distance metric to use for vector similarity search')
->values(Distance::cases())
->defaultValue(Distance::Cosine)
->end()
->end()
->validate()
->ifTrue(static fn ($v) => !isset($v['connection_parameters']) && !isset($v['client']))
->thenInvalid('Either "connection_parameters" or "client" must be configured.')
->end()
->validate()
->ifTrue(static fn ($v) => isset($v['connection_parameters']) && isset($v['client']))
->thenInvalid('Either "connection_parameters" or "client" can be configured, but not both.')
->end()
->end()
->end()
->arrayNode('surreal_db')
->useAttributeAsKey('name')
->arrayPrototype()
Expand Down
25 changes: 25 additions & 0 deletions src/ai-bundle/src/AiBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,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\Bridge\Typesense\Store as TypesenseStore;
use Symfony\AI\Store\Bridge\Weaviate\Store as WeaviateStore;
Expand Down Expand Up @@ -1075,6 +1076,30 @@ private function processStoreConfig(string $type, array $stores, ContainerBuilde
}
}

if ('redis' === $type) {
foreach ($stores as $name => $store) {
if (isset($store['client'])) {
$redisClient = new Reference($store['client']);
} else {
$redisClient = new Definition(\Redis::class);
$redisClient->setArguments([$store['connection_parameters']]);
}

$definition = new Definition(RedisStore::class);
$definition
->addTag('ai.store')
->setArguments([
$redisClient,
$store['index_name'],
$store['key_prefix'],
$store['distance'],
])
;

$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 @@ -2855,6 +2855,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
- Pinecone
- PostgreSQL with pgvector extension
- Qdrant
- Redis
- SurrealDB
- Typesense
- Weaviate
Expand Down
1 change: 1 addition & 0 deletions src/store/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"symfony/ai-platform": "@dev",
"symfony/clock": "^7.3|^8.0",
"symfony/http-client": "^7.3|^8.0",
"symfony/polyfill-php83": "^1.32",
"symfony/uid": "^7.3|^8.0"
},
"require-dev": {
Expand Down
26 changes: 26 additions & 0 deletions src/store/src/Bridge/Redis/Distance.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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 <lyrixx@lyrixx.info>
*/
enum Distance: string
{
use Comparable;

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