Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adding basic similarity search as tool #9

Merged
merged 1 commit into from
Sep 21, 2024
Merged
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
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
Comment on lines +12 to +13
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice one, exactly what I need :-)

{
/**
* @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;
}
}