Skip to content

Commit 61a8c8b

Browse files
committed
Use iterator_to_array instead of changing property type
This keeps the array type for usedDocuments and converts the iterable from StoreInterface::query() to an array.
1 parent efaf97a commit 61a8c8b

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

src/agent/src/Bridge/SimilaritySearch/SimilaritySearch.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
final class SimilaritySearch
2424
{
2525
/**
26-
* @var iterable<VectorDocument>
26+
* @var VectorDocument[]
2727
*/
28-
public iterable $usedDocuments = [];
28+
public array $usedDocuments = [];
2929

3030
public function __construct(
3131
private readonly VectorizerInterface $vectorizer,
@@ -39,17 +39,17 @@ public function __construct(
3939
public function __invoke(string $searchTerm): string
4040
{
4141
$vector = $this->vectorizer->vectorize($searchTerm);
42-
$this->usedDocuments = $this->store->query($vector);
42+
$this->usedDocuments = iterator_to_array($this->store->query($vector));
4343

44-
$result = '';
45-
foreach ($this->usedDocuments as $document) {
46-
$result .= json_encode($document->metadata);
44+
if ([] === $this->usedDocuments) {
45+
return 'No results found';
4746
}
4847

49-
if ('' === $result) {
50-
return 'No results found';
48+
$result = 'Found documents with following information:'.\PHP_EOL;
49+
foreach ($this->usedDocuments as $document) {
50+
$result .= json_encode($document->metadata);
5151
}
5252

53-
return 'Found documents with following information:'.\PHP_EOL.$result;
53+
return $result;
5454
}
5555
}

0 commit comments

Comments
 (0)