From 328afcad538204e4c10ffd829659740a55adb8ac Mon Sep 17 00:00:00 2001 From: Vladimir Blagojevic Date: Sat, 20 Jul 2024 18:27:17 +0200 Subject: [PATCH] Apply filters during spec parsing stage --- .../tools/openapi/_schema_conversion.py | 21 ++++++------------- .../components/tools/openapi/openapi_tool.py | 2 +- 2 files changed, 7 insertions(+), 16 deletions(-) diff --git a/haystack_experimental/components/tools/openapi/_schema_conversion.py b/haystack_experimental/components/tools/openapi/_schema_conversion.py index b53fd11d..59759f79 100644 --- a/haystack_experimental/components/tools/openapi/_schema_conversion.py +++ b/haystack_experimental/components/tools/openapi/_schema_conversion.py @@ -119,24 +119,15 @@ def _openapi_to_functions( if path_key.lower() in VALID_HTTP_METHODS: if "operationId" not in operation_spec: operation_spec["operationId"] = path_to_operation_id(path, path_key) + + # Apply the filter based on operationId before parsing the endpoint (operation) + if operation_filter and not operation_filter(operation_spec): + continue + + # parse (and register) this operation as it passed the filter function_dict = parse_endpoint_fn(operation_spec, parameters_name) if function_dict: operations.append(function_dict) - - # Filter operations to register with LLMs - if operation_filter: - len_prior = len(operations) - operations = [f for f in operations if operation_filter(f)] - logger.info( - "Registering {op_count} operations out of {len_prior} found in OpenAPI spec.", - op_count=len(operations), - len_prior=len_prior, - ) - if len(operations) == 0 and len_prior > 0: - logger.warning( - f"Filtered all operations for " - f"LLM registration in {service_openapi_spec['info']['title']}. Relax the filter criteria." - ) return operations diff --git a/haystack_experimental/components/tools/openapi/openapi_tool.py b/haystack_experimental/components/tools/openapi/openapi_tool.py index 0f5162fa..4409cc83 100644 --- a/haystack_experimental/components/tools/openapi/openapi_tool.py +++ b/haystack_experimental/components/tools/openapi/openapi_tool.py @@ -96,7 +96,7 @@ def __init__( openapi_spec=openapi_spec, credentials=credentials.resolve_value() if credentials else None, llm_provider=generator_api, - operations_filter=(lambda f: f["name"] in allowed_operations) if allowed_operations else None, + operations_filter=(lambda f: f["operationId"] in allowed_operations) if allowed_operations else None, ) self.open_api_service = OpenAPIServiceClient(self.config_openapi)