-
Notifications
You must be signed in to change notification settings - Fork 81
IBX-9846: Describe Embeddings search API #3029
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
dabrt
wants to merge
13
commits into
5.0
Choose a base branch
from
IBX-9846
base: 5.0
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.
+321
−32
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
d5b4419
IBX-9846: Describe Embeddings search
dabrt 6fb06d4
Fix metadata
dabrt b4e31da
Typo fix
dabrt d018f87
Fix broken links
dabrt 51570c0
Implement reviewer comments
dabrt c12ae0e
Apply suggestions from code review
dabrt 6512ff2
Implemented reviewer comments
dabrt c210ba9
Fix snippets and move them to code_samples folder
dabrt ed5daa8
Fix code samples to clear out PHP Stan errors
dabrt c409888
Fix issue reported by Rector
dabrt 5db4c62
Extend the return in code sample
dabrt f6291bd
Replace code samples with code from Mikołaj
dabrt e45d0d1
PHP & JS CS Fixes
dabrt 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
77 changes: 77 additions & 0 deletions
77
code_samples/api/public_php_api/src/Command/FindByTaxonomyEmbeddingCommand.php
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,77 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
| * @license For full copyright and license information view LICENSE file distributed with this source code. | ||
| */ | ||
| declare(strict_types=1); | ||
|
|
||
| namespace Ibexa\Taxonomy; | ||
|
|
||
| use Ibexa\Contracts\Core\Repository\SearchService; | ||
| use Ibexa\Contracts\Core\Repository\Values\Content\EmbeddingQueryBuilder; | ||
| use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion\ContentTypeIdentifier; | ||
| use Ibexa\Contracts\Core\Repository\Values\Content\Search\SearchHit; | ||
| use Ibexa\Contracts\Taxonomy\Search\Query\Value\TaxonomyEmbedding; | ||
| use Symfony\Component\Console\Attribute\AsCommand; | ||
| use Symfony\Component\Console\Command\Command; | ||
| use Symfony\Component\Console\Input\InputInterface; | ||
| use Symfony\Component\Console\Output\OutputInterface; | ||
| use Symfony\Component\Console\Style\SymfonyStyle; | ||
|
|
||
| #[AsCommand( | ||
| name: 'ibexa:taxonomy:find-by-embedding', | ||
| description: 'Finds content using a taxonomy embedding query.' | ||
| )] | ||
| final class FindByTaxonomyEmbeddingCommand extends Command | ||
| { | ||
| public function __construct(private readonly SearchService $searchService) | ||
| { | ||
| parent::__construct(); | ||
| } | ||
|
|
||
| protected function execute( | ||
| InputInterface $input, | ||
| OutputInterface $output | ||
| ): int { | ||
| $io = new SymfonyStyle($input, $output); | ||
|
|
||
| // Example embedding vector. | ||
| // In a real-life scenario, generate it with an embedding provider | ||
| // and make sure its dimensions match the configured model. | ||
| $vector = [ | ||
| 0.0123, | ||
| -0.9876, | ||
| 0.4567, | ||
| 0.1111, | ||
| ]; | ||
|
|
||
| $query = EmbeddingQueryBuilder::create() | ||
| ->withEmbedding(new TaxonomyEmbedding($vector)) | ||
| ->setFilter(new ContentTypeIdentifier('article')) | ||
| ->setLimit(10) | ||
| ->setOffset(0) | ||
| ->setPerformCount(true) | ||
| ->build(); | ||
|
|
||
| $result = $this->searchService->findContent($query); | ||
|
|
||
| $io->success(sprintf('Found %d items.', $result->totalCount)); | ||
|
|
||
| foreach ($result->searchHits as $searchHit) { | ||
| assert($searchHit instanceof SearchHit); | ||
|
|
||
| /** @var \Ibexa\Contracts\Core\Repository\Values\Content\Content $content */ | ||
| $content = $searchHit->valueObject; | ||
| $contentInfo = $content->versionInfo->contentInfo; | ||
|
|
||
| $io->writeln(sprintf( | ||
| '%d: %s', | ||
| $contentInfo->id, | ||
| $contentInfo->name | ||
| )); | ||
| } | ||
|
|
||
| return self::SUCCESS; | ||
| } | ||
| } |
38 changes: 38 additions & 0 deletions
38
code_samples/api/public_php_api/src/Service/TaxonomyEmbeddingSearchService.php
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,38 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
| * @license For full copyright and license information view LICENSE file distributed with this source code. | ||
| */ | ||
| declare(strict_types=1); | ||
|
|
||
| namespace Ibexa\Taxonomy; | ||
|
|
||
| use Ibexa\Contracts\Core\Repository\SearchService; | ||
| use Ibexa\Contracts\Core\Repository\Values\Content\Content; | ||
| use Ibexa\Contracts\Core\Repository\Values\Content\EmbeddingQueryBuilder; | ||
| use Ibexa\Contracts\Core\Repository\Values\Content\Search\SearchResult; | ||
| use Ibexa\Contracts\Taxonomy\Search\Query\Value\TaxonomyEmbedding; | ||
|
|
||
| final class TaxonomyEmbeddingSearchService | ||
| { | ||
| public function __construct(private readonly SearchService $searchService) | ||
| { | ||
| } | ||
|
|
||
| /** | ||
| * @param float[] $vector | ||
| * | ||
| * @return SearchResult<Content> | ||
| */ | ||
| public function searchByEmbedding(array $vector): SearchResult | ||
| { | ||
| $query = EmbeddingQueryBuilder::create() | ||
| ->withEmbedding(new TaxonomyEmbedding($vector)) | ||
| ->setLimit(10) | ||
| ->setOffset(0) | ||
| ->build(); | ||
|
|
||
| return $this->searchService->findContent($query); | ||
| } | ||
| } |
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,50 @@ | ||
| --- | ||
| month_change: true | ||
| description: Embedding queries, embedding configuration, providers, and embedding search fields | ||
| --- | ||
|
|
||
| # Embeddings search reference | ||
|
|
||
| Embeddings provide vector representations of content or text, enabling semantic similarity search. | ||
| Foundational abstractions are provided for embedding-based search, while embedding providers generate vector representations. | ||
|
|
||
| ## EmbeddingQuery | ||
|
|
||
| - [`Ibexa\Contracts\Core\Repository\Values\Content\EmbeddingQuery`](/api/php_api/php_api_reference/classes/Ibexa-Contracts-Core-Repository-Values-Content-EmbeddingQuery.html): Represents a semantic similarity search request. | ||
| It encapsulates an [Embedding](#embedding) instance and supports filtering, pagination, aggregations, and result counting through the same API as standard content queries. | ||
| Embedding queries do not support criteria, Sort Clauses, facet builders, or spellcheck | ||
|
|
||
| ## Embedding | ||
|
|
||
| - [`Ibexa\Contracts\Core\Repository\Values\Content\Query\Embedding`](/api/php_api/php_api_reference/classes/Ibexa-Contracts-Core-Repository-Values-Content-Query-Embedding.html): Represents the vector input used | ||
| for similarity search. | ||
| It stores embedding values as float arrays, while providers generate those vectors from text input | ||
|
|
||
| ## Embedding providers | ||
|
|
||
| Embedding providers generate vector representations for inputs. | ||
| Out of the box, embedding search integration is provided for TaxonomyEmbedding. | ||
| If you use a custom embedding value type, implement matching embedding | ||
| visitors for your search engine (Solr/Elasticsearch). | ||
| Otherwise, query execution may fail with "No visitor available". | ||
|
|
||
| ### Provider contracts | ||
|
|
||
| - [`Ibexa\Contracts\Core\Search\Embedding\EmbeddingProviderInterface`](/api/php_api/php_api_reference/classes/Ibexa-Contracts-Core-Search-Embedding-EmbeddingProviderInterface.html): Generates embeddings | ||
|
|
||
| - [`Ibexa\Contracts\Core\Search\Embedding\EmbeddingProviderRegistryInterface`](/api/php_api/php_api_reference/classes/Ibexa-Contracts-Core-Search-Embedding-EmbeddingProviderRegistryInterface.html): Lists available embedding providers | ||
|
|
||
| - [`Ibexa\Contracts\Core\Search\Embedding\EmbeddingProviderResolverInterface`](/api/php_api/php_api_reference/classes/Ibexa-Contracts-Core-Search-Embedding-EmbeddingProviderResolverInterface.html): Resolves the provider for a given embedding configuration | ||
|
|
||
| ## Embedding fields | ||
|
|
||
| - [`Ibexa\Contracts\Core\Search\FieldType\EmbeddingFieldFactory`](/api/php_api/php_api_reference/classes/Ibexa-Contracts-Core-Search-FieldType-EmbeddingFieldFactory.html): Creates dedicated search fields that store embedding vectors | ||
|
|
||
| ## Validation | ||
|
|
||
| - [`Ibexa\Contracts\Core\Repository\Values\Content\QueryValidatorInterface`](/api/php_api/php_api_reference/classes/Ibexa-Contracts-Core-Repository-Values-Content-QueryValidatorInterface.html): Validates embedding queries before they reach the search engine | ||
|
|
||
| !!! note "Taxonomy embeddings" | ||
|
|
||
| Searching for embeddings can be used to support the [Taxonomy suggestions](taxonomy.md#taxonomy-suggestions) feature. | ||
| The [`Ibexa\Contracts\Taxonomy\Search\Query\Value\TaxonomyEmbedding`](/api/php_api/php_api_reference/classes/Ibexa-Contracts-Taxonomy-Search-Query-Value-TaxonomyEmbedding.html) allows embedding queries to target taxonomy data. | ||
Oops, something went wrong.
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.