Skip to content
Open
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
10 changes: 10 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,16 @@ Store
+$indexer->index('/new/source');
```

* Properties of `Symfony\AI\Store\Document\VectorDocument` and `\Symfony\AI\Store\Document\Loader\Rss\RssItem` have been changed to private, use getters instead of public properties:

```diff
-$document->id;
+$document->getId();

-$rssItem->title;
+$rssItem->getTitle();
```

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

metadata -> getMetadata()

UPGRADE FROM 0.2 to 0.3
=======================

Expand Down
2 changes: 1 addition & 1 deletion examples/document/vectorizing-text-documents.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@
$vectorizer = new Vectorizer($platform, 'text-embedding-3-large');
$vectorDocuments = $vectorizer->vectorize($textDocuments);

dump(array_map(static fn (VectorDocument $document) => $document->vector->getDimensions(), $vectorDocuments));
dump(array_map(static fn (VectorDocument $document) => $document->getVector()->getDimensions(), $vectorDocuments));
2 changes: 1 addition & 1 deletion examples/indexer/chunk-delay.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,5 @@

echo "Search results for 'machine learning artificial intelligence':\n";
foreach ($results as $i => $document) {
echo sprintf("%d. %s\n", $i + 1, substr($document->id, 0, 40).'...');
echo sprintf("%d. %s\n", $i + 1, substr($document->getId(), 0, 40).'...');
}
2 changes: 1 addition & 1 deletion examples/indexer/index-documents.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,5 @@
$vector = $vectorizer->vectorize('machine learning artificial intelligence');
$results = $store->query($vector);
foreach ($results as $i => $document) {
echo sprintf("%d. %s\n", $i + 1, substr($document->id, 0, 40).'...');
echo sprintf("%d. %s\n", $i + 1, substr($document->getId(), 0, 40).'...');
}
2 changes: 1 addition & 1 deletion examples/indexer/index-file-loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,5 @@
$vector = $vectorizer->vectorize('Roman gladiator revenge');
$results = $store->query($vector);
foreach ($results as $i => $document) {
echo sprintf("%d. %s\n", $i + 1, substr($document->id, 0, 40).'...');
echo sprintf("%d. %s\n", $i + 1, substr($document->getId(), 0, 40).'...');
}
2 changes: 1 addition & 1 deletion examples/indexer/index-rss-loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,5 @@
$vector = $vectorizer->vectorize('Week of Symfony');
$results = $store->query($vector);
foreach ($results as $i => $document) {
echo sprintf("%d. %s\n", $i + 1, substr($document->id, 0, 40).'...');
echo sprintf("%d. %s\n", $i + 1, substr($document->getId(), 0, 40).'...');
}
8 changes: 4 additions & 4 deletions examples/indexer/index-with-filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@

$filteredDocuments = 0;
foreach ($results as $i => $document) {
$title = $document->metadata['title'] ?? 'Unknown';
$category = $document->metadata['category'] ?? 'Unknown';
$title = $document->getMetadata()['title'] ?? 'Unknown';
$category = $document->getMetadata()['category'] ?? 'Unknown';
echo sprintf("%d. %s [%s]\n", $i + 1, $title, $category);
echo sprintf(" Content: %s\n", substr($document->metadata->getText() ?? 'No content', 0, 80).'...');
echo sprintf(" ID: %s\n\n", substr($document->id, 0, 8).'...');
echo sprintf(" Content: %s\n", substr($document->getMetadata()->getText() ?? 'No content', 0, 80).'...');
echo sprintf(" ID: %s\n\n", substr($document->getId(), 0, 8).'...');
++$filteredDocuments;
}

Expand Down
2 changes: 1 addition & 1 deletion examples/ollama/indexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,5 @@
$vector = $vectorizer->vectorize('Roman gladiator revenge');
$results = $store->query($vector);
foreach ($results as $i => $document) {
echo sprintf("%d. %s\n", $i + 1, substr($document->id, 0, 40).'...');
echo sprintf("%d. %s\n", $i + 1, substr($document->getId(), 0, 40).'...');
}
6 changes: 3 additions & 3 deletions examples/retriever/basic.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
$results = $retriever->retrieve('Roman gladiator revenge', ['maxItems' => 1]);

foreach ($results as $i => $document) {
echo sprintf("%d. Document ID: %s\n", $i + 1, $document->id);
echo sprintf(" Score: %s\n", $document->score ?? 'n/a');
echo sprintf(" Source: %s\n\n", $document->metadata->getSource() ?? 'unknown');
echo sprintf("%d. Document ID: %s\n", $i + 1, $document->getId());
echo sprintf(" Score: %s\n", $document->getScore() ?? 'n/a');
echo sprintf(" Source: %s\n\n", $document->getMetadata()->getSource() ?? 'unknown');
}
6 changes: 3 additions & 3 deletions examples/retriever/movies.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@
$results = $retriever->retrieve('crime family mafia');

foreach ($results as $i => $document) {
$title = $document->metadata['title'];
$director = $document->metadata['director'];
$score = $document->score;
$title = $document->getMetadata()['title'];
$director = $document->getMetadata()['director'];
$score = $document->getScore();

echo sprintf("%d. %s (Director: %s)\n", $i + 1, $title, $director);
echo sprintf(" Score: %.4f\n\n", $score ?? 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function __invoke(string $searchTerm): string

$result = 'Found documents with following information:'.\PHP_EOL;
foreach ($this->usedDocuments as $document) {
$result .= json_encode($document->metadata);
$result .= json_encode($document->getMetadata());
}

return $result;
Expand Down
2 changes: 1 addition & 1 deletion src/agent/src/Memory/EmbeddingProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function load(Input $input): array

$content = '';
foreach ($foundEmbeddingContent as $document) {
$content .= json_encode($document->metadata);
$content .= json_encode($document->getMetadata());
}

if ('' === $content) {
Expand Down
7 changes: 5 additions & 2 deletions src/agent/tests/Memory/EmbeddingProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@
use Symfony\AI\Platform\Result\DeferredResult;
use Symfony\AI\Platform\Result\RawResultInterface;
use Symfony\AI\Platform\Result\VectorResult;
use Symfony\AI\Platform\Vector\NullVector;
use Symfony\AI\Platform\Vector\Vector;
use Symfony\AI\Store\Document\Metadata;
use Symfony\AI\Store\Document\VectorDocument;
use Symfony\AI\Store\StoreInterface;

final class EmbeddingProviderTest extends TestCase
Expand Down Expand Up @@ -123,8 +126,8 @@ public function testItIsCreatingMemoryWithFoundVectors()

$store = $this->createMock(StoreInterface::class);
$generator = (static function () {
yield (object) ['metadata' => ['fact' => 'The sky is blue']];
yield (object) ['metadata' => ['fact' => 'Water is wet']];
yield new VectorDocument(123, new NullVector(), new Metadata(['fact' => 'The sky is blue']));
yield new VectorDocument(234, new NullVector(), new Metadata(['fact' => 'Water is wet']));
})();
$store->expects($this->once())
->method('query')
Expand Down
1 change: 1 addition & 0 deletions src/store/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ CHANGELOG
* Add `ConfiguredSourceIndexer` decorator for pre-configuring default sources on `SourceIndexer`
* [BC BREAK] Remove `Indexer` class - use `SourceIndexer` or `DocumentIndexer` instead
* [BC BREAK] Change `IndexerInterface::index()` signature - input parameter is no longer nullable
* [BC BREAK] Reduce visibility of `VectorDocument` and `RssItem` properties to `private` and add getters

0.3
---
Expand Down
6 changes: 3 additions & 3 deletions src/store/src/Bridge/AzureSearch/SearchStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ private function request(string $endpoint, array $payload): array
private function convertToIndexableArray(VectorDocument $document): array
{
return array_merge([
'id' => $document->id,
$this->vectorFieldName => $document->vector->getData(),
], $document->metadata->getArrayCopy());
'id' => $document->getId(),
$this->vectorFieldName => $document->getVector()->getData(),
], $document->getMetadata()->getArrayCopy());
}

/**
Expand Down
10 changes: 5 additions & 5 deletions src/store/src/Bridge/AzureSearch/Tests/SearchStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,10 @@ public function testQueryReturnsDocuments()
$this->assertCount(2, $results);
$this->assertInstanceOf(VectorDocument::class, $results[0]);
$this->assertInstanceOf(VectorDocument::class, $results[1]);
$this->assertEquals($uuid1, $results[0]->id);
$this->assertEquals($uuid2, $results[1]->id);
$this->assertSame('First Document', $results[0]->metadata['title']);
$this->assertSame('Second Document', $results[1]->metadata['title']);
$this->assertEquals($uuid1, $results[0]->getId());
$this->assertEquals($uuid2, $results[1]->getId());
$this->assertSame('First Document', $results[0]->getMetadata()['title']);
$this->assertSame('Second Document', $results[1]->getMetadata()['title']);
}

public function testQueryWithCustomVectorFieldName()
Expand Down Expand Up @@ -281,7 +281,7 @@ public function testQueryWithNullVector()

$this->assertCount(1, $results);
$this->assertInstanceOf(VectorDocument::class, $results[0]);
$this->assertInstanceOf(NullVector::class, $results[0]->vector);
$this->assertInstanceOf(NullVector::class, $results[0]->getVector());
}

public function testRemoveWithSingleId()
Expand Down
6 changes: 3 additions & 3 deletions src/store/src/Bridge/Cache/Store.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ public function add(VectorDocument|array $documents): void
$existingVectors = $this->cache->get($this->cacheKey, static fn (): array => []);

$newVectors = array_map(static fn (VectorDocument $document): array => [
'id' => $document->id->toRfc4122(),
'vector' => $document->vector->getData(),
'metadata' => $document->metadata->getArrayCopy(),
'id' => $document->getId()->toRfc4122(),
'vector' => $document->getVector()->getData(),
'metadata' => $document->getMetadata()->getArrayCopy(),
], $documents);

$cacheItem = $this->cache->getItem($this->cacheKey);
Expand Down
48 changes: 24 additions & 24 deletions src/store/src/Bridge/Cache/Tests/StoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function testStoreCanSearchUsingCosineDistance()

$result = iterator_to_array($store->query(new Vector([0.0, 0.1, 0.6])));
$this->assertCount(3, $result);
$this->assertSame([0.1, 0.1, 0.5], $result[0]->vector->getData());
$this->assertSame([0.1, 0.1, 0.5], $result[0]->getVector()->getData());

$store->add([
new VectorDocument(Uuid::v4(), new Vector([0.1, 0.1, 0.5])),
Expand All @@ -72,7 +72,7 @@ public function testStoreCanSearchUsingCosineDistance()

$result = iterator_to_array($store->query(new Vector([0.0, 0.1, 0.6])));
$this->assertCount(6, $result);
$this->assertSame([0.1, 0.1, 0.5], $result[0]->vector->getData());
$this->assertSame([0.1, 0.1, 0.5], $result[0]->getVector()->getData());
}

public function testStoreCanSearchUsingCosineDistanceAndReturnCorrectOrder()
Expand All @@ -88,11 +88,11 @@ public function testStoreCanSearchUsingCosineDistanceAndReturnCorrectOrder()

$result = iterator_to_array($store->query(new Vector([0.0, 0.1, 0.6])));
$this->assertCount(5, $result);
$this->assertSame([0.0, 0.1, 0.6], $result[0]->vector->getData());
$this->assertSame([0.1, 0.1, 0.5], $result[1]->vector->getData());
$this->assertSame([0.3, 0.1, 0.6], $result[2]->vector->getData());
$this->assertSame([0.3, 0.7, 0.1], $result[3]->vector->getData());
$this->assertSame([0.7, -0.3, 0.0], $result[4]->vector->getData());
$this->assertSame([0.0, 0.1, 0.6], $result[0]->getVector()->getData());
$this->assertSame([0.1, 0.1, 0.5], $result[1]->getVector()->getData());
$this->assertSame([0.3, 0.1, 0.6], $result[2]->getVector()->getData());
$this->assertSame([0.3, 0.7, 0.1], $result[3]->getVector()->getData());
$this->assertSame([0.7, -0.3, 0.0], $result[4]->getVector()->getData());
}

public function testStoreCanSearchUsingCosineDistanceWithMaxItems()
Expand Down Expand Up @@ -120,7 +120,7 @@ public function testStoreCanSearchUsingAngularDistance()
$result = iterator_to_array($store->query(new Vector([1.2, 2.3, 3.4])));

$this->assertCount(2, $result);
$this->assertSame([1.0, 2.0, 3.0], $result[0]->vector->getData());
$this->assertSame([1.0, 2.0, 3.0], $result[0]->getVector()->getData());
}

public function testStoreCanSearchUsingEuclideanDistance()
Expand All @@ -134,7 +134,7 @@ public function testStoreCanSearchUsingEuclideanDistance()
$result = iterator_to_array($store->query(new Vector([1.2, 2.3, 3.4])));

$this->assertCount(2, $result);
$this->assertSame([1.0, 2.0, 3.0], $result[0]->vector->getData());
$this->assertSame([1.0, 2.0, 3.0], $result[0]->getVector()->getData());
}

public function testStoreCanSearchUsingManhattanDistance()
Expand All @@ -148,7 +148,7 @@ public function testStoreCanSearchUsingManhattanDistance()
$result = iterator_to_array($store->query(new Vector([1.2, 2.3, 3.4])));

$this->assertCount(2, $result);
$this->assertSame([1.0, 2.0, 3.0], $result[0]->vector->getData());
$this->assertSame([1.0, 2.0, 3.0], $result[0]->getVector()->getData());
}

public function testStoreCanSearchUsingChebyshevDistance()
Expand All @@ -162,7 +162,7 @@ public function testStoreCanSearchUsingChebyshevDistance()
$result = iterator_to_array($store->query(new Vector([1.2, 2.3, 3.4])));

$this->assertCount(2, $result);
$this->assertSame([1.0, 2.0, 3.0], $result[0]->vector->getData());
$this->assertSame([1.0, 2.0, 3.0], $result[0]->getVector()->getData());
}

public function testStoreCanSearchWithFilter()
Expand All @@ -175,12 +175,12 @@ public function testStoreCanSearchWithFilter()
]);

$result = iterator_to_array($store->query(new Vector([0.0, 0.1, 0.6]), [
'filter' => static fn (VectorDocument $doc) => 'products' === $doc->metadata['category'],
'filter' => static fn (VectorDocument $doc) => 'products' === $doc->getMetadata()['category'],
]));

$this->assertCount(2, $result);
$this->assertSame('products', $result[0]->metadata['category']);
$this->assertSame('products', $result[1]->metadata['category']);
$this->assertSame('products', $result[0]->getMetadata()['category']);
$this->assertSame('products', $result[1]->getMetadata()['category']);
}

public function testStoreCanSearchWithFilterAndMaxItems()
Expand All @@ -194,13 +194,13 @@ public function testStoreCanSearchWithFilterAndMaxItems()
]);

$result = iterator_to_array($store->query(new Vector([0.0, 0.1, 0.6]), [
'filter' => static fn (VectorDocument $doc) => 'products' === $doc->metadata['category'],
'filter' => static fn (VectorDocument $doc) => 'products' === $doc->getMetadata()['category'],
'maxItems' => 2,
]));

$this->assertCount(2, $result);
$this->assertSame('products', $result[0]->metadata['category']);
$this->assertSame('products', $result[1]->metadata['category']);
$this->assertSame('products', $result[0]->getMetadata()['category']);
$this->assertSame('products', $result[1]->getMetadata()['category']);
}

public function testStoreCanSearchWithComplexFilter()
Expand All @@ -213,7 +213,7 @@ public function testStoreCanSearchWithComplexFilter()
]);

$result = iterator_to_array($store->query(new Vector([0.0, 0.1, 0.6]), [
'filter' => static fn (VectorDocument $doc) => $doc->metadata['price'] <= 150 && $doc->metadata['stock'] > 0,
'filter' => static fn (VectorDocument $doc) => $doc->getMetadata()['price'] <= 150 && $doc->getMetadata()['stock'] > 0,
]));

$this->assertCount(2, $result);
Expand All @@ -229,12 +229,12 @@ public function testStoreCanSearchWithNestedMetadataFilter()
]);

$result = iterator_to_array($store->query(new Vector([0.0, 0.1, 0.6]), [
'filter' => static fn (VectorDocument $doc) => 'S' === $doc->metadata['options']['size'],
'filter' => static fn (VectorDocument $doc) => 'S' === $doc->getMetadata()['options']['size'],
]));

$this->assertCount(2, $result);
$this->assertSame('S', $result[0]->metadata['options']['size']);
$this->assertSame('S', $result[1]->metadata['options']['size']);
$this->assertSame('S', $result[0]->getMetadata()['options']['size']);
$this->assertSame('S', $result[1]->getMetadata()['options']['size']);
}

public function testStoreCanSearchWithInArrayFilter()
Expand All @@ -248,7 +248,7 @@ public function testStoreCanSearchWithInArrayFilter()

$allowedBrands = ['Nike', 'Adidas', 'Puma'];
$result = iterator_to_array($store->query(new Vector([0.0, 0.1, 0.6]), [
'filter' => static fn (VectorDocument $doc) => \in_array($doc->metadata['brand'] ?? '', $allowedBrands, true),
'filter' => static fn (VectorDocument $doc) => \in_array($doc->getMetadata()['brand'] ?? '', $allowedBrands, true),
]));

$this->assertCount(2, $result);
Expand Down Expand Up @@ -277,7 +277,7 @@ public function testRemoveWithStringId()
$result = iterator_to_array($store->query(new Vector([0.0, 0.1, 0.6])));
$this->assertCount(2, $result);

$remainingIds = array_map(static fn (VectorDocument $doc) => $doc->id, $result);
$remainingIds = array_map(static fn (VectorDocument $doc) => $doc->getId(), $result);
$this->assertNotContains($id2->toRfc4122(), $remainingIds);
$this->assertContains($id1->toRfc4122(), $remainingIds);
$this->assertContains($id3->toRfc4122(), $remainingIds);
Expand Down Expand Up @@ -308,7 +308,7 @@ public function testRemoveWithArrayOfIds()
$result = iterator_to_array($store->query(new Vector([0.0, 0.1, 0.6])));
$this->assertCount(2, $result);

$remainingIds = array_map(static fn (VectorDocument $doc) => $doc->id, $result);
$remainingIds = array_map(static fn (VectorDocument $doc) => $doc->getId(), $result);
$this->assertNotContains($id2->toRfc4122(), $remainingIds);
$this->assertNotContains($id4->toRfc4122(), $remainingIds);
$this->assertContains($id1->toRfc4122(), $remainingIds);
Expand Down
8 changes: 4 additions & 4 deletions src/store/src/Bridge/ChromaDb/Store.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ public function add(VectorDocument|array $documents): void
$metadata = [];
$originalDocuments = [];
foreach ($documents as $document) {
$ids[] = (string) $document->id;
$vectors[] = $document->vector->getData();
$metadataCopy = $document->metadata->getArrayCopy();
$originalDocuments[] = $document->metadata->getText() ?? '';
$ids[] = (string) $document->getId();
$vectors[] = $document->getVector()->getData();
$metadataCopy = $document->getMetadata()->getArrayCopy();
$originalDocuments[] = $document->getMetadata()->getText() ?? '';
unset($metadataCopy[Metadata::KEY_TEXT]);
$metadata[] = $metadataCopy;
}
Expand Down
Loading