-
-
Notifications
You must be signed in to change notification settings - Fork 51
[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
lyrixx
wants to merge
1
commit into
symfony:main
Choose a base branch
from
lyrixx:store-redis
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,5 +56,10 @@ services: | |
ports: | ||
- '8108:8108' | ||
|
||
redis: | ||
image: redis:8.0.3 | ||
ports: | ||
- '6379:6379' | ||
|
||
volumes: | ||
typesense_data: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 <[email protected]> | ||
* | ||
* 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\Gpt; | ||
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\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->initialize(['vector_size' => 1536]); | ||
|
||
// create embeddings for documents | ||
$platform = PlatformFactory::create(env('OPENAI_API_KEY'), http_client()); | ||
$vectorizer = new Vectorizer($platform, $embeddings = new Embeddings()); | ||
$indexer = new Indexer($vectorizer, $store, logger()); | ||
$indexer->index($documents); | ||
|
||
$model = new Gpt(Gpt::GPT_4O_MINI); | ||
|
||
$similaritySearch = new SimilaritySearch($platform, $embeddings, $store); | ||
$toolbox = new Toolbox([$similaritySearch], logger: logger()); | ||
$processor = new AgentProcessor($toolbox); | ||
$agent = new Agent($platform, $model, [$processor], [$processor], 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 ofRedis
. 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).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I update the config to support either