Skip to content

Commit

Permalink
feat: adding basic similarity search as tool
Browse files Browse the repository at this point in the history
  • Loading branch information
chr-hertel committed Sep 21, 2024
1 parent 0e93021 commit 205ff82
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 5 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ Currently supported stores:
Provided Tools
--------------

* [x] SerpApi
* [x] Clock
* [x] SerpApi
* [x] Similarity Search (Basic)
* [x] Wikipedia
* [x] Weather
* [x] YouTube Transcriber
Expand Down
2 changes: 1 addition & 1 deletion src/Store/Azure/SearchStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function addDocuments(array $documents): void
/**
* @return list<Document>
*/
public function query(Vector $vector): array
public function query(Vector $vector, array $options = []): array
{
$result = $this->request('search', [
'vectorQueries' => [$this->buildVectorQuery($vector)],
Expand Down
2 changes: 1 addition & 1 deletion src/Store/ChromaDb/Store.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function addDocuments(array $documents): void
$collection->add($ids, $vectors, $metadata);
}

public function query(Vector $vector): array
public function query(Vector $vector, array $options = []): array
{
$collection = $this->client->getOrCreateCollection($this->collectionName);
$queryResponse = $collection->query(
Expand Down
6 changes: 4 additions & 2 deletions src/Store/VectorStoreInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
interface VectorStoreInterface extends StoreInterface
{
/**
* @return list<Document>
* @param array<string, mixed> $options
*
* @return Document[]
*/
public function query(Vector $vector): array;
public function query(Vector $vector, array $options = []): array;
}
45 changes: 45 additions & 0 deletions src/ToolBox/Tool/SimilaritySearch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace PhpLlm\LlmChain\ToolBox\Tool;

use PhpLlm\LlmChain\Document\Document;
use PhpLlm\LlmChain\EmbeddingModel;
use PhpLlm\LlmChain\Store\VectorStoreInterface;
use PhpLlm\LlmChain\ToolBox\AsTool;

#[AsTool('similarity_search', description: 'Searches for documents similar to a query or sentence.')]
final class SimilaritySearch
{
/**
* @var Document[]
*/
public array $usedDocuments = [];

public function __construct(
private readonly EmbeddingModel $embedding,
private readonly VectorStoreInterface $vectorStore,
) {
}

/**
* @param string $searchTerm string used for similarity search
*/
public function __invoke(string $searchTerm): string
{
$vector = $this->embedding->create($searchTerm);
$this->usedDocuments = $this->vectorStore->query($vector);

if (0 === count($this->usedDocuments)) {
return 'No results found';
}

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

return $result;
}
}

0 comments on commit 205ff82

Please sign in to comment.