diff --git a/bin/V2/SearchModelsCommand.php b/bin/V2/SearchModelsCommand.php index 4e3501e3..042f9d5e 100644 --- a/bin/V2/SearchModelsCommand.php +++ b/bin/V2/SearchModelsCommand.php @@ -86,7 +86,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int try { $response = $client->search( - ModelSearchResponse::class, new ModelSearchParameters($name ?: null, $modelType ?: null) ); } catch (MindeeV2HttpException $e) { diff --git a/bin/V2/SearchRagDocumentsCommand.php b/bin/V2/SearchRagDocumentsCommand.php index 20373cfa..e4c78fab 100644 --- a/bin/V2/SearchRagDocumentsCommand.php +++ b/bin/V2/SearchRagDocumentsCommand.php @@ -84,7 +84,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int try { $response = $client->search( - RagDocumentSearchResponse::class, new RagDocumentSearchParameters($modelId, $filename ?: null) ); } catch (MindeeV2HttpException $e) { diff --git a/src/V2/Client.php b/src/V2/Client.php index c2021149..98754e5a 100644 --- a/src/V2/Client.php +++ b/src/V2/Client.php @@ -60,7 +60,6 @@ public function enqueue( return $this->mindeeApi->reqPostEnqueue($inputSource, $params); } - /** * @template T of BaseResponse * @param string $responseClass The response class to construct. @@ -255,14 +254,12 @@ public function deleteExtractionRagDocument(string $documentId): bool * Searches for resources matching the given criteria. * * @template T of BaseSearchResponse - * @param string $responseClass The response class to construct. - * @phpstan-param class-string $responseClass - * @param BaseSearchParameters $params Search parameters. + * @param BaseSearchParameters $params Search parameters. * @return T */ - public function search(string $responseClass, BaseSearchParameters $params): BaseSearchResponse + public function search(BaseSearchParameters $params): BaseSearchResponse { - return $this->mindeeApi->reqGetSearch($responseClass, $params); + return $this->mindeeApi->reqGetSearch($params); } /** @@ -270,12 +267,11 @@ public function search(string $responseClass, BaseSearchParameters $params): Bas * @param string|null $modelName Optional model name to filter by. * @param string|null $modelType Optional model type to filter by. * @return ModelSearchResponse The list of models matching the criteria. - * @deprecated Use search(ModelSearchResponse::class, new ModelSearchParameters(...)) instead. + * @deprecated Use search(new ModelSearchParameters(...)) instead. */ public function searchModels(?string $modelName = null, ?string $modelType = null): ModelSearchResponse { return $this->mindeeApi->reqGetSearch( - ModelSearchResponse::class, new ModelSearchParameters($modelName, $modelType) ); } diff --git a/src/V2/ClientOptions/BaseSearchParameters.php b/src/V2/ClientOptions/BaseSearchParameters.php index 9d1da486..4845fae3 100644 --- a/src/V2/ClientOptions/BaseSearchParameters.php +++ b/src/V2/ClientOptions/BaseSearchParameters.php @@ -4,8 +4,11 @@ namespace Mindee\V2\ClientOptions; +use Mindee\V2\Parsing\Search\BaseSearchResponse; + /** * Base parameters for searches. + * @template TSearchResponse of BaseSearchResponse */ abstract class BaseSearchParameters { @@ -14,6 +17,11 @@ abstract class BaseSearchParameters */ public static string $slug; + /** + * @var class-string $responseClass Response class. + */ + protected static string $responseClass; + /** * @param integer|null $page 1-based page index. * @param integer|null $perPage Number of items per page. @@ -24,11 +32,21 @@ public function __construct( ) {} /** - * Gets the query parameters for the search request. + * Gets the response class associated with the parameters. + * + * @return class-string Response class. + */ + public function getResponseClass(): string + { + return static::$responseClass; + } + + /** + * Gets the request parameters for the search request. * * @return array Query parameters. */ - public function getQueryParams(): array + public function getRequestParameters(): array { $params = []; if ($this->page !== null && $this->page > 0) { diff --git a/src/V2/Http/MindeeApiV2.php b/src/V2/Http/MindeeApiV2.php index 302d24ab..bf90be4c 100644 --- a/src/V2/Http/MindeeApiV2.php +++ b/src/V2/Http/MindeeApiV2.php @@ -524,14 +524,12 @@ public function reqDeleteExtractionRagDocument(string $documentId): bool * Makes a GET call to a search endpoint and returns the deserialized response. * * @template T of BaseSearchResponse - * @param string $responseClass The response class to construct. - * @phpstan-param class-string $responseClass - * @param BaseSearchParameters $params Search parameters (slug and query params derived from this). + * @param BaseSearchParameters $params Search parameters (slug and query params derived from this). * @return T */ - public function reqGetSearch(string $responseClass, BaseSearchParameters $params): BaseResponse + public function reqGetSearch(BaseSearchParameters $params): BaseResponse { - $queryParams = $params->getQueryParams(); + $queryParams = $params->getRequestParameters(); $url = $this->baseUrl . "/v2/search/" . $params::$slug; if (!empty($queryParams)) { $url .= '?' . http_build_query($queryParams); @@ -546,6 +544,6 @@ public function reqGetSearch(string $responseClass, BaseSearchParameters $params 'code' => curl_getinfo($ch, CURLINFO_HTTP_CODE), ]; curl_close($ch); - return $this->deserializeResponse($responseClass, $resp); + return $this->deserializeResponse($params->getResponseClass(), $resp); } } diff --git a/src/V2/Search/Models/ModelSearchParameters.php b/src/V2/Search/Models/ModelSearchParameters.php index 4756f1f1..3b13b32a 100644 --- a/src/V2/Search/Models/ModelSearchParameters.php +++ b/src/V2/Search/Models/ModelSearchParameters.php @@ -7,7 +7,14 @@ use Mindee\V2\ClientOptions\BaseSearchParameters; /** - * Search parameters for models. + * Search for models within the organization linked to the API key. + * + * All search filters are optional. + * If no search filters are given, all models belonging to the organization are returned. + * + * Results are paginated. + * + * @extends BaseSearchParameters */ class ModelSearchParameters extends BaseSearchParameters { @@ -16,6 +23,11 @@ class ModelSearchParameters extends BaseSearchParameters */ public static string $slug = "models"; + /** + * @var class-string Response class. + */ + protected static string $responseClass = ModelSearchResponse::class; + /** * @param string|null $name Case-insensitive search term for the model name. * @param string|null $modelType Case-insensitive search term for the model type. @@ -34,9 +46,9 @@ public function __construct( /** * @return array Query parameters. */ - public function getQueryParams(): array + public function getRequestParameters(): array { - $params = parent::getQueryParams(); + $params = parent::getRequestParameters(); if (!empty($this->name)) { $params['name'] = $this->name; } diff --git a/src/V2/Search/RagDocuments/RagDocumentSearchParameters.php b/src/V2/Search/RagDocuments/RagDocumentSearchParameters.php index d25a9cd7..1863f309 100644 --- a/src/V2/Search/RagDocuments/RagDocumentSearchParameters.php +++ b/src/V2/Search/RagDocuments/RagDocumentSearchParameters.php @@ -9,7 +9,14 @@ use Mindee\V2\ClientOptions\BaseSearchParameters; /** - * Search parameters for RAG documents. + * Search for RAG documents within the organization linked to the API key. + * + * The model ID is required, search filters are optional. + * If no search filters are given, all documents linked to the model are returned. + * + * Results are paginated. + * + * @extends BaseSearchParameters */ class RagDocumentSearchParameters extends BaseSearchParameters { @@ -18,11 +25,16 @@ class RagDocumentSearchParameters extends BaseSearchParameters */ public static string $slug = "rag-documents"; + /** + * @var class-string Response class. + */ + protected static string $responseClass = RagDocumentSearchResponse::class; + /** * @param string|null $modelId Model identifier to search in (required). * @param string|null $filename Case-insensitive substring search on filename. * @param integer|null $page 1-based page index. - * @param integer|null $perPage Number of items per page. + * @param integer|null $perPage Number of result items per page. */ public function __construct( public ?string $modelId = null, @@ -37,9 +49,9 @@ public function __construct( * @return array Query parameters. * @throws MindeeException Throws if the model ID is not provided. */ - public function getQueryParams(): array + public function getRequestParameters(): array { - $params = parent::getQueryParams(); + $params = parent::getRequestParameters(); if (!empty($this->modelId)) { $params['model_id'] = $this->modelId; } else { diff --git a/tests/V2/Search/ModelSearchFunctional.php b/tests/V2/Search/ModelSearchFunctional.php index ac47660c..93ce394f 100644 --- a/tests/V2/Search/ModelSearchFunctional.php +++ b/tests/V2/Search/ModelSearchFunctional.php @@ -6,7 +6,6 @@ use Mindee\V2\Client; use Mindee\V2\Search\Models\ModelSearchParameters; -use Mindee\V2\Search\Models\ModelSearchResponse; use PHPUnit\Framework\TestCase; class ModelSearchFunctional extends TestCase @@ -20,7 +19,7 @@ protected function setUp(): void public function testModelSearch_mustHaveResults(): void { - $response = $this->client->search(ModelSearchResponse::class, new ModelSearchParameters()); + $response = $this->client->search(new ModelSearchParameters()); self::assertNotNull($response); self::assertNotNull($response->models); @@ -33,7 +32,6 @@ public function testModelSearch_mustHaveResults(): void public function testModelSearch_mustReturnEmpty(): void { $response = $this->client->search( - ModelSearchResponse::class, new ModelSearchParameters(name: "je n'existe pas tralala") ); diff --git a/tests/V2/Search/RagDocumentSearchFunctional.php b/tests/V2/Search/RagDocumentSearchFunctional.php index 74ad3c44..ff595338 100644 --- a/tests/V2/Search/RagDocumentSearchFunctional.php +++ b/tests/V2/Search/RagDocumentSearchFunctional.php @@ -6,7 +6,6 @@ use Mindee\V2\Client; use Mindee\V2\Search\RagDocuments\RagDocumentSearchParameters; -use Mindee\V2\Search\RagDocuments\RagDocumentSearchResponse; use PHPUnit\Framework\TestCase; class RagDocumentSearchFunctional extends TestCase @@ -23,7 +22,6 @@ protected function setUp(): void public function testRagDocumentSearch_mustHaveResults(): void { $response = $this->client->search( - RagDocumentSearchResponse::class, new RagDocumentSearchParameters(modelId: $this->findocModelId) );