File tree Expand file tree Collapse file tree 5 files changed +53
-5
lines changed Expand file tree Collapse file tree 5 files changed +53
-5
lines changed Original file line number Diff line number Diff line change @@ -50,8 +50,9 @@ Currently supported stores:
5050Provided 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
Original file line number Diff line number Diff 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 )],
Original file line number Diff line number Diff 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 (
Original file line number Diff line number Diff line change 1010interface 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}
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments