|
3 | 3 | # SPDX-License-Identifier: Apache-2.0 |
4 | 4 |
|
5 | 5 | import asyncio |
| 6 | +from typing import Any |
6 | 7 |
|
7 | 8 | import yaml |
8 | 9 | from pydantic import BaseModel |
@@ -367,3 +368,52 @@ async def search_pipeline( |
367 | 368 | return f"Failed to search using pipeline '{pipeline_name}': {e}" |
368 | 369 | except Exception as e: |
369 | 370 | return f"An unexpected error occurred while searching with pipeline '{pipeline_name}': {str(e)}" |
| 371 | + |
| 372 | + |
| 373 | +async def search_pipeline_with_filters( |
| 374 | + *, |
| 375 | + client: AsyncClientProtocol, |
| 376 | + workspace: str, |
| 377 | + pipeline_name: str, |
| 378 | + query: str, |
| 379 | + filters: dict[str, Any] | None = None, |
| 380 | +) -> DeepsetSearchResponse | str: |
| 381 | + """Searches using a pipeline with filters. |
| 382 | +
|
| 383 | + Uses the specified pipeline to perform a search with the given query and filters. |
| 384 | + Filters follow the Haystack filter syntax: https://docs.haystack.deepset.ai/docs/metadata-filtering. |
| 385 | + Before executing the search, checks if the pipeline is deployed (status = DEPLOYED). |
| 386 | + Returns search results. |
| 387 | +
|
| 388 | + :param client: The async client for API communication. |
| 389 | + :param workspace: The workspace name. |
| 390 | + :param pipeline_name: Name of the pipeline to use for search. |
| 391 | + :param query: The search query to execute. |
| 392 | + :param filters: The filters to apply to the search. |
| 393 | +
|
| 394 | + :returns: Search results or error message. |
| 395 | + """ |
| 396 | + try: |
| 397 | + # First, check if the pipeline exists and get its status |
| 398 | + pipeline = await client.pipelines(workspace=workspace).get(pipeline_name=pipeline_name) |
| 399 | + |
| 400 | + # Check if pipeline is deployed |
| 401 | + if pipeline.status != "DEPLOYED": |
| 402 | + return ( |
| 403 | + f"Pipeline '{pipeline_name}' is not deployed (current status: {pipeline.status}). " |
| 404 | + f"Please deploy the pipeline first using the deploy_pipeline tool before attempting to search." |
| 405 | + ) |
| 406 | + |
| 407 | + # Execute the search |
| 408 | + return await client.pipelines(workspace=workspace).search( |
| 409 | + pipeline_name=pipeline_name, query=query, filters=filters if filters is not None else None |
| 410 | + ) |
| 411 | + |
| 412 | + except ResourceNotFoundError: |
| 413 | + return f"There is no pipeline named '{pipeline_name}' in workspace '{workspace}'." |
| 414 | + except BadRequestError as e: |
| 415 | + return f"Failed to search using pipeline '{pipeline_name}': {e}" |
| 416 | + except UnexpectedAPIError as e: |
| 417 | + return f"Failed to search using pipeline '{pipeline_name}': {e}" |
| 418 | + except Exception as e: |
| 419 | + return f"An unexpected error occurred while searching with pipeline '{pipeline_name}': {str(e)}" |
0 commit comments