Skip to content
Open
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
10 changes: 9 additions & 1 deletion src/Tools/SimilaritySearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function description(): string
*/
public function handle(Request $request): string
{
$results = call_user_func($this->using, $request->string('query'));
$results = $this->search($request);

$results = match (true) {
is_array($results) => new Collection($results),
Expand Down Expand Up @@ -137,6 +137,14 @@ public function schema(JsonSchema $schema): array
];
}

/**
* Run the similarity search for the given request.
*/
protected function search(Request $request): mixed
{
return call_user_func($this->using, $request->string('query'));
}

/**
* Determine whether the tool needs approval for the given request.
*/
Expand Down
17 changes: 17 additions & 0 deletions tests/Feature/SimilaritySearchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,23 @@
expect(str_contains($results, json_encode($data, JSON_PRETTY_PRINT)))->toBeTrue();
});

test('subclasses can override the search using the tool request', function (): void {
$search = new class(fn (string $query): array => []) extends SimilaritySearch
{
protected function search(Request $request): mixed
{
return [['tags' => $request->array('tags')]];
}
};

$results = $search->handle(new Request([
'query' => 'Test query',
'tags' => ['api_docs'],
]));

expect($results)->toContain('"api_docs"');
});

test('using model rejects blank model class', function (): void {
SimilaritySearch::usingModel('', 'embedding');
})->throws(InvalidArgumentException::class, 'A model class name is required for similarity search.');
Expand Down