-
Notifications
You must be signed in to change notification settings - Fork 17
IBX-9846: Added search using embeddings #536
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
mikadamczyk
wants to merge
5
commits into
4.6
Choose a base branch
from
taxonomy-suggestions
base: 4.6
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
5 commits
Select commit
Hold shift + click to select a range
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
70 changes: 70 additions & 0 deletions
70
src/bundle/Core/DependencyInjection/Configuration/Parser/Embeddings.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,70 @@ | ||
| <?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\Bundle\Core\DependencyInjection\Configuration\Parser; | ||
|
|
||
| use Ibexa\Bundle\Core\DependencyInjection\Configuration\AbstractParser; | ||
| use Ibexa\Bundle\Core\DependencyInjection\Configuration\SiteAccessAware\ContextualizerInterface; | ||
| use Symfony\Component\Config\Definition\Builder\NodeBuilder; | ||
|
|
||
| /** | ||
| * Configuration parser for embedding models. | ||
| * | ||
| * Example configuration: | ||
| * ```yaml | ||
| * ibexa: | ||
| * system: | ||
| * default: # configuration per siteaccess or siteaccess group | ||
| * embedding_models: | ||
| * name: "text-embedding-3-small" | ||
| * dimensions: 1536 | ||
| * field_suffix: "3small" | ||
| * embedding_provider: "ibexa_openai" | ||
| * default_embedding_model: text-embedding-ada-002 | ||
| * ``` | ||
| */ | ||
| final class Embeddings extends AbstractParser | ||
| { | ||
| public function addSemanticConfig(NodeBuilder $nodeBuilder): void | ||
| { | ||
| $nodeBuilder | ||
| ->arrayNode('embedding_models') | ||
| ->normalizeKeys(false) | ||
| ->info('Defines available embedding models') | ||
| ->arrayPrototype() | ||
| ->children() | ||
| ->scalarNode('name')->isRequired()->end() | ||
| ->integerNode('dimensions')->isRequired()->end() | ||
| ->scalarNode('field_suffix')->isRequired()->end() | ||
| ->scalarNode('embedding_provider')->isRequired()->end() | ||
| ->end() | ||
| ->end() | ||
| ->end() | ||
| ->scalarNode('default_embedding_model') | ||
| ->info('Default embedding model identifier') | ||
| ->defaultValue('text-embedding-ada-002') | ||
| ->end(); | ||
| } | ||
|
|
||
| /** | ||
| * @param array<mixed> $config | ||
| */ | ||
| public function preMap(array $config, ContextualizerInterface $contextualizer): void | ||
| { | ||
| $contextualizer->mapConfigArray('embedding_models', $config); | ||
| $contextualizer->mapSetting('default_embedding_model', $config); | ||
| } | ||
|
|
||
| /** | ||
| * @param array<mixed> $scopeSettings | ||
| */ | ||
| public function mapConfig(array &$scopeSettings, $currentScope, ContextualizerInterface $contextualizer): void | ||
| { | ||
| // Nothing to do here. | ||
| } | ||
| } | ||
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,24 @@ | ||
| services: | ||
| _defaults: | ||
| autowire: true | ||
| autoconfigure: true | ||
| public: false | ||
|
|
||
| Ibexa\Contracts\Core\Search\Embedding\EmbeddingProviderRegistryInterface: | ||
| alias: Ibexa\Core\Search\Embedding\EmbeddingProviderRegistry | ||
|
|
||
| Ibexa\Core\Search\Embedding\EmbeddingProviderRegistry: | ||
| arguments: | ||
| $embeddingProviders: !tagged_iterator { tag: 'ibexa.embedding_provider', index_by: 'provider_name' } | ||
|
|
||
| Ibexa\Contracts\Core\Search\Embedding\EmbeddingProviderResolverInterface: | ||
| alias: Ibexa\Core\Search\Embedding\EmbeddingProviderResolver | ||
|
|
||
| Ibexa\Core\Search\Embedding\EmbeddingProviderResolver: ~ | ||
|
|
||
| Ibexa\Contracts\Core\Search\Embedding\EmbeddingConfigurationInterface: | ||
| alias: Ibexa\Core\Search\Embedding\EmbeddingConfiguration | ||
|
|
||
| Ibexa\Core\Search\Embedding\EmbeddingConfiguration: ~ | ||
|
|
||
| Ibexa\Contracts\Core\Search\FieldType\EmbeddingFieldFactory: ~ |
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
113 changes: 113 additions & 0 deletions
113
src/contracts/Repository/Values/Content/EmbeddingQuery.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,113 @@ | ||
| <?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\Contracts\Core\Repository\Values\Content; | ||
|
|
||
| use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion; | ||
| use Ibexa\Contracts\Core\Repository\Values\Content\Query\Embedding; | ||
| use InvalidArgumentException; | ||
|
|
||
| final class EmbeddingQuery extends Query | ||
| { | ||
| private ?Embedding $embedding = null; | ||
|
|
||
| public function getEmbedding(): ?Embedding | ||
| { | ||
| return $this->embedding; | ||
| } | ||
|
|
||
| public function setEmbedding(?Embedding $embedding): void | ||
| { | ||
| $this->embedding = $embedding; | ||
| } | ||
|
|
||
| public function getFilter(): Criterion | ||
| { | ||
| return $this->filter; | ||
| } | ||
|
|
||
| public function setFilter(Criterion $filter): void | ||
mikadamczyk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| $this->filter = $filter; | ||
| } | ||
|
|
||
| /** | ||
| * @return \Ibexa\Contracts\Core\Repository\Values\Content\Query\Aggregation[] | ||
| */ | ||
| public function getAggregations(): array | ||
| { | ||
| return $this->aggregations; | ||
| } | ||
|
|
||
| /** | ||
| * @param \Ibexa\Contracts\Core\Repository\Values\Content\Query\Aggregation[] $aggregations | ||
| */ | ||
| public function setAggregations(array $aggregations): void | ||
| { | ||
| $this->aggregations = $aggregations; | ||
| } | ||
|
|
||
| public function getOffset(): int | ||
| { | ||
| return $this->offset; | ||
| } | ||
|
|
||
| public function setOffset(int $offset): void | ||
| { | ||
| $this->offset = $offset; | ||
| } | ||
|
|
||
| public function getLimit(): int | ||
| { | ||
| return $this->limit; | ||
| } | ||
|
|
||
| public function setLimit(int $limit): void | ||
| { | ||
| $this->limit = $limit; | ||
| } | ||
|
|
||
| public function setPerformCount(bool $performCount): void | ||
| { | ||
| $this->performCount = $performCount; | ||
| } | ||
|
|
||
| public function getPerformCount(): bool | ||
| { | ||
| return $this->performCount; | ||
| } | ||
|
|
||
| public function isValid(): bool | ||
| { | ||
| $invalid = []; | ||
|
|
||
| if ($this->query !== null) { | ||
| $invalid[] = 'query'; | ||
| } | ||
| if (!empty($this->sortClauses)) { | ||
| $invalid[] = 'sortClauses'; | ||
| } | ||
| if (!empty($this->facetBuilders)) { | ||
| $invalid[] = 'facetBuilders'; | ||
| } | ||
| if ($this->spellcheck !== null) { | ||
| $invalid[] = 'spellcheck'; | ||
| } | ||
|
|
||
| if (count($invalid) > 0) { | ||
| throw new InvalidArgumentException( | ||
| sprintf( | ||
| 'EmbeddingQuery did not set [%s].', | ||
| implode(', ', $invalid) | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
| } | ||
111 changes: 111 additions & 0 deletions
111
src/contracts/Repository/Values/Content/EmbeddingQueryBuilder.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,111 @@ | ||
| <?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\Contracts\Core\Repository\Values\Content; | ||
|
|
||
| use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion; | ||
| use Ibexa\Contracts\Core\Repository\Values\Content\Query\Embedding; | ||
|
|
||
| final class EmbeddingQueryBuilder | ||
| { | ||
| private ?Embedding $embedding = null; | ||
|
|
||
| private ?int $limit = null; | ||
|
|
||
| private ?int $offset = null; | ||
|
|
||
| private ?Criterion $filter = null; | ||
|
|
||
| /** @var array<\Ibexa\Contracts\Core\Repository\Values\Content\Query\Aggregation> */ | ||
| private array $aggregations = []; | ||
|
|
||
| private bool $performCount = false; | ||
|
|
||
| private function __construct() | ||
| { | ||
| } | ||
|
|
||
| public static function create(): self | ||
| { | ||
| return new self(); | ||
| } | ||
|
|
||
| public function withEmbedding(Embedding $embed): self | ||
| { | ||
| $this->embedding = $embed; | ||
|
|
||
| return $this; | ||
| } | ||
|
|
||
| public function setLimit(int $limit): self | ||
| { | ||
| $this->limit = $limit; | ||
|
|
||
| return $this; | ||
| } | ||
|
|
||
| public function setOffset(int $offset): self | ||
| { | ||
| $this->offset = $offset; | ||
|
|
||
| return $this; | ||
| } | ||
|
|
||
| public function setFilter(Criterion $filter): self | ||
| { | ||
| $this->filter = $filter; | ||
|
|
||
| return $this; | ||
| } | ||
|
|
||
| /** | ||
| * @param array<\Ibexa\Contracts\Core\Repository\Values\Content\Query\Aggregation> $aggregations | ||
| */ | ||
| public function setAggregations(array $aggregations): self | ||
| { | ||
| $this->aggregations = $aggregations; | ||
|
|
||
| return $this; | ||
| } | ||
|
|
||
| public function setPerformCount(bool $performCount): self | ||
| { | ||
| $this->performCount = $performCount; | ||
|
|
||
| return $this; | ||
| } | ||
|
|
||
| public function build(): EmbeddingQuery | ||
| { | ||
| $query = new EmbeddingQuery(); | ||
|
|
||
| if ($this->embedding !== null) { | ||
| $query->setEmbedding($this->embedding); | ||
| } | ||
|
|
||
| if ($this->limit !== null) { | ||
| $query->setLimit($this->limit); | ||
| } | ||
|
|
||
| if ($this->offset !== null) { | ||
| $query->setOffset($this->offset); | ||
| } | ||
|
|
||
| if ($this->filter !== null) { | ||
| $query->setFilter($this->filter); | ||
| } | ||
|
|
||
| if (!empty($this->aggregations)) { | ||
| $query->setAggregations($this->aggregations); | ||
| } | ||
|
|
||
| $query->setPerformCount($this->performCount); | ||
|
|
||
| return $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
33 changes: 33 additions & 0 deletions
33
src/contracts/Repository/Values/Content/Query/Embedding.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,33 @@ | ||
| <?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\Contracts\Core\Repository\Values\Content\Query; | ||
|
|
||
| use Ibexa\Contracts\Core\Repository\Values\ValueObject; | ||
|
|
||
| abstract class Embedding extends ValueObject | ||
| { | ||
| /** @var float[] */ | ||
| protected array $value; | ||
|
|
||
| /** | ||
| * @param float[] $value | ||
| */ | ||
| public function __construct(array $value) | ||
| { | ||
| $this->value = $value; | ||
| } | ||
|
|
||
| /** | ||
| * @return float[] | ||
| */ | ||
| public function getValue(): array | ||
| { | ||
| return $this->value; | ||
| } | ||
| } |
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.