Skip to content

Commit e5a02e4

Browse files
authored
feat: adding basic similarity search as tool (#9)
1 parent 0e93021 commit e5a02e4

File tree

5 files changed

+53
-5
lines changed

5 files changed

+53
-5
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,9 @@ Currently supported stores:
5050
Provided Tools
5151
--------------
5252

53-
* [x] SerpApi
5453
* [x] Clock
54+
* [x] SerpApi
55+
* [x] Similarity Search (Basic)
5556
* [x] Wikipedia
5657
* [x] Weather
5758
* [x] YouTube Transcriber

src/Store/Azure/SearchStore.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function addDocuments(array $documents): void
3737
/**
3838
* @return list<Document>
3939
*/
40-
public function query(Vector $vector): array
40+
public function query(Vector $vector, array $options = []): array
4141
{
4242
$result = $this->request('search', [
4343
'vectorQueries' => [$this->buildVectorQuery($vector)],

src/Store/ChromaDb/Store.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function addDocuments(array $documents): void
4545
$collection->add($ids, $vectors, $metadata);
4646
}
4747

48-
public function query(Vector $vector): array
48+
public function query(Vector $vector, array $options = []): array
4949
{
5050
$collection = $this->client->getOrCreateCollection($this->collectionName);
5151
$queryResponse = $collection->query(

src/Store/VectorStoreInterface.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
interface VectorStoreInterface extends StoreInterface
1111
{
1212
/**
13-
* @return list<Document>
13+
* @param array<string, mixed> $options
14+
*
15+
* @return Document[]
1416
*/
15-
public function query(Vector $vector): array;
17+
public function query(Vector $vector, array $options = []): array;
1618
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpLlm\LlmChain\ToolBox\Tool;
6+
7+
use PhpLlm\LlmChain\Document\Document;
8+
use PhpLlm\LlmChain\EmbeddingModel;
9+
use PhpLlm\LlmChain\Store\VectorStoreInterface;
10+
use PhpLlm\LlmChain\ToolBox\AsTool;
11+
12+
#[AsTool('similarity_search', description: 'Searches for documents similar to a query or sentence.')]
13+
final class SimilaritySearch
14+
{
15+
/**
16+
* @var Document[]
17+
*/
18+
public array $usedDocuments = [];
19+
20+
public function __construct(
21+
private readonly EmbeddingModel $embedding,
22+
private readonly VectorStoreInterface $vectorStore,
23+
) {
24+
}
25+
26+
/**
27+
* @param string $searchTerm string used for similarity search
28+
*/
29+
public function __invoke(string $searchTerm): string
30+
{
31+
$vector = $this->embedding->create($searchTerm);
32+
$this->usedDocuments = $this->vectorStore->query($vector);
33+
34+
if (0 === count($this->usedDocuments)) {
35+
return 'No results found';
36+
}
37+
38+
$result = 'Found documents with following information:'.PHP_EOL;
39+
foreach ($this->usedDocuments as $document) {
40+
$result .= json_encode($document->metadata);
41+
}
42+
43+
return $result;
44+
}
45+
}

0 commit comments

Comments
 (0)