-
-
Notifications
You must be signed in to change notification settings - Fork 113
[Store][Meilisearch] Add configurable semantic ratio #782
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
ahmed-bhs
wants to merge
9
commits into
symfony:main
Choose a base branch
from
ahmed-bhs:feature/meilisearch-configurable-semantic-ratio
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
Show all changes
9 commits
Select commit
Hold shift + click to select a range
963ad12
[Store] Add configurable semantic ratio to Meilisearch Store
ahmed-bhs 4b40b49
feat(ai-bundle): add semantic_ratio to meilisearch store config
ahmed-bhs 44f6b07
docs: update changelogs for meilisearch semantic_ratio
ahmed-bhs 94790b7
style(ai-bundle): apply php-cs-fixer on AiBundleTest.php
ahmed-bhs d6c4375
docs: align wording for meilisearch hybrid search
ahmed-bhs 9401362
refactor: use static closures and inline query parameter
ahmed-bhs c540b21
optimize: remove intermediate variable in Meilisearch request
ahmed-bhs 8ed7f19
refactor(ai-bundle): simplify semantic_ratio validation
ahmed-bhs 6e56681
docs: add meilisearch hybrid search example
ahmed-bhs 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 |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| <?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\Fixtures\Movies; | ||
| use Symfony\AI\Platform\Bridge\OpenAi\PlatformFactory; | ||
| use Symfony\AI\Store\Bridge\Meilisearch\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'; | ||
|
|
||
| echo "=== Meilisearch Hybrid Search Demo ===\n\n"; | ||
| echo "This example demonstrates how to configure the semantic ratio to balance\n"; | ||
| echo "between semantic (vector) search and full-text search in Meilisearch.\n\n"; | ||
|
|
||
| // Initialize the store with a balanced hybrid search (50/50) | ||
| $store = new Store( | ||
| httpClient: http_client(), | ||
| endpointUrl: env('MEILISEARCH_HOST'), | ||
| apiKey: env('MEILISEARCH_API_KEY'), | ||
| indexName: 'movies_hybrid', | ||
| semanticRatio: 0.5, // Balanced hybrid search by default | ||
| ); | ||
|
|
||
| // 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 index | ||
| $store->setup(); | ||
|
|
||
| // 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); | ||
|
|
||
| // Create a query embedding | ||
| $queryText = 'futuristic technology and artificial intelligence'; | ||
| echo "Query: \"$queryText\"\n\n"; | ||
| $queryEmbedding = $vectorizer->vectorize($queryText); | ||
|
|
||
| // Test different semantic ratios to compare results | ||
| $ratios = [ | ||
| ['ratio' => 0.0, 'description' => '100% Full-text search (keyword matching)'], | ||
| ['ratio' => 0.5, 'description' => 'Balanced hybrid (50% semantic + 50% full-text)'], | ||
| ['ratio' => 1.0, 'description' => '100% Semantic search (vector similarity)'], | ||
| ]; | ||
|
|
||
| foreach ($ratios as $config) { | ||
| echo "--- {$config['description']} ---\n"; | ||
|
|
||
| // Override the semantic ratio for this specific query | ||
| $results = $store->query($queryEmbedding, [ | ||
| 'semanticRatio' => $config['ratio'], | ||
| 'q' => 'technology', // Full-text search keyword | ||
| ]); | ||
|
|
||
| echo "Top 3 results:\n"; | ||
| foreach (array_slice($results, 0, 3) as $i => $result) { | ||
| $metadata = $result->metadata->getArrayCopy(); | ||
| echo sprintf( | ||
| " %d. %s (Score: %.4f)\n", | ||
| $i + 1, | ||
| $metadata['title'] ?? 'Unknown', | ||
| $result->score ?? 0.0 | ||
| ); | ||
| } | ||
| echo "\n"; | ||
| } | ||
|
|
||
| echo "--- Custom query with pure semantic search ---\n"; | ||
| echo "Query: Movies about space exploration\n"; | ||
| $spaceEmbedding = $vectorizer->vectorize('space exploration and cosmic adventures'); | ||
| $results = $store->query($spaceEmbedding, [ | ||
| 'semanticRatio' => 1.0, // Pure semantic search | ||
| ]); | ||
|
|
||
| echo "Top 3 results:\n"; | ||
| foreach (array_slice($results, 0, 3) as $i => $result) { | ||
| $metadata = $result->metadata->getArrayCopy(); | ||
| echo sprintf( | ||
| " %d. %s (Score: %.4f)\n", | ||
| $i + 1, | ||
| $metadata['title'] ?? 'Unknown', | ||
| $result->score ?? 0.0 | ||
| ); | ||
| } | ||
| echo "\n"; | ||
|
|
||
| // Cleanup | ||
| $store->drop(); | ||
|
|
||
| echo "=== Summary ===\n"; | ||
| echo "- semanticRatio = 0.0: Best for exact keyword matches\n"; | ||
| echo "- semanticRatio = 0.5: Balanced approach combining both methods\n"; | ||
| echo "- semanticRatio = 1.0: Best for conceptual similarity searches\n"; | ||
| echo "\nYou can set the default ratio when instantiating the Store,\n"; | ||
| echo "and override it per query using the 'semanticRatio' option.\n"; |
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
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.
Uh oh!
There was an error while loading. Please reload this page.