From 8cf4fbf48bf018bb9731e7d2041bdf27e85c4cc9 Mon Sep 17 00:00:00 2001 From: Mark Date: Mon, 16 Sep 2024 11:15:36 +0100 Subject: [PATCH 1/6] Adding a failing test. --- tests/test_search_adaptors.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/test_search_adaptors.py b/tests/test_search_adaptors.py index 2828397..5524881 100644 --- a/tests/test_search_adaptors.py +++ b/tests/test_search_adaptors.py @@ -37,6 +37,18 @@ def profile_search( return avg_ms +@pytest.mark.parametrize( + "search_adaptor_params", + [ + {"instance_url": "http://localhost:8080", "cert_directory": "/tmp"}, + {"instance_url": "http://localhost:8080"}, + ], +) +@pytest.mark.vespa +def test_vespa_search_adaptor_instantiation(search_adaptor_params: dict) -> None: + VespaSearchAdapter(**search_adaptor_params) + + @pytest.mark.vespa def test_vespa_search_adaptor__works(test_vespa): request = SearchParameters(query_string="the") From 7cb479dd0120434d42d68e1bdc336d879b9194d7 Mon Sep 17 00:00:00 2001 From: Mark Date: Mon, 16 Sep 2024 11:16:03 +0100 Subject: [PATCH 2/6] Bumping the cpr_sdk version. --- src/cpr_sdk/version.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cpr_sdk/version.py b/src/cpr_sdk/version.py index 893b208..e5f46df 100644 --- a/src/cpr_sdk/version.py +++ b/src/cpr_sdk/version.py @@ -1,6 +1,6 @@ _MAJOR = "1" _MINOR = "4" -_PATCH = "2" +_PATCH = "3" _SUFFIX = "" VERSION_SHORT = "{0}.{1}".format(_MAJOR, _MINOR) From e7a46fb26a38d7a9da40b35cce7f2468f1681a32 Mon Sep 17 00:00:00 2001 From: Mark Date: Mon, 16 Sep 2024 11:29:03 +0100 Subject: [PATCH 3/6] Adding fix. --- src/cpr_sdk/search_adaptors.py | 17 +++++++++-------- src/cpr_sdk/vespa.py | 16 ++++++++++++---- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/src/cpr_sdk/search_adaptors.py b/src/cpr_sdk/search_adaptors.py index 187f7c5..b04b78c 100644 --- a/src/cpr_sdk/search_adaptors.py +++ b/src/cpr_sdk/search_adaptors.py @@ -1,24 +1,25 @@ """Adaptors for searching CPR data""" +import logging import time from abc import ABC from pathlib import Path from typing import Optional -import logging + +from requests.exceptions import HTTPError +from vespa.application import Vespa +from vespa.exceptions import VespaError from cpr_sdk.embedding import Embedder from cpr_sdk.exceptions import DocumentNotFoundError, FetchError, QueryError from cpr_sdk.models.search import Hit, SearchParameters, SearchResponse from cpr_sdk.vespa import ( + VespaErrorDetails, build_vespa_request_body, find_vespa_cert_paths, parse_vespa_response, split_document_id, - VespaErrorDetails, ) -from requests.exceptions import HTTPError -from vespa.application import Vespa -from vespa.exceptions import VespaError LOGGER = logging.getLogger(__name__) @@ -67,10 +68,10 @@ def __init__( if cert_directory is None: cert_path, key_path = find_vespa_cert_paths() else: - cert_path = Path(cert_directory) / "cert.pem" - key_path = Path(cert_directory) / "key.pem" + cert_path = (Path(cert_directory) / "cert.pem").__str__() + key_path = (Path(cert_directory) / "key.pem").__str__() - self.client = Vespa(url=instance_url, cert=str(cert_path), key=str(key_path)) + self.client = Vespa(url=instance_url, cert=cert_path, key=key_path) self.embedder = embedder or Embedder() def search(self, parameters: SearchParameters) -> SearchResponse: diff --git a/src/cpr_sdk/vespa.py b/src/cpr_sdk/vespa.py index 3bca577..05cf7ee 100644 --- a/src/cpr_sdk/vespa.py +++ b/src/cpr_sdk/vespa.py @@ -1,5 +1,5 @@ from pathlib import Path -from typing import Any, List +from typing import Any, List, Union import yaml from vespa.exceptions import VespaError @@ -36,7 +36,7 @@ def split_document_id(document_id: str) -> tuple[str, str, str]: return namespace, schema, data_id -def find_vespa_cert_paths() -> tuple[Path, Path]: +def find_vespa_cert_paths() -> tuple[Union[str, None], Union[str, None]]: """ Automatically find the certificate and key files for the vespa instance @@ -57,8 +57,16 @@ def find_vespa_cert_paths() -> tuple[Path, Path]: application_name = data["application"] cert_directory = vespa_directory / application_name - cert_path = list(cert_directory.glob("*cert.pem"))[0] - key_path = list(cert_directory.glob("*key.pem"))[0] + + cert_directory_certs = list(cert_directory.glob("*cert.pem")) + cert_path = ( + str(list(cert_directory.glob("*cert.pem"))[0]) if cert_directory_certs else None + ) + cert_directory_keys = list(cert_directory.glob("*key.pem")) + key_path = ( + str(list(cert_directory.glob("*key.pem"))[0]) if cert_directory_keys else None + ) + return cert_path, key_path From bd9ee3b2d6632fce2ae68c4b1c0ae8bf8012a5fa Mon Sep 17 00:00:00 2001 From: NextGenEng <58440325+THOR300@users.noreply.github.com> Date: Mon, 16 Sep 2024 11:53:22 +0100 Subject: [PATCH 4/6] Update src/cpr_sdk/vespa.py Co-authored-by: Jesse Claven --- src/cpr_sdk/vespa.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cpr_sdk/vespa.py b/src/cpr_sdk/vespa.py index 05cf7ee..bfc7921 100644 --- a/src/cpr_sdk/vespa.py +++ b/src/cpr_sdk/vespa.py @@ -36,7 +36,7 @@ def split_document_id(document_id: str) -> tuple[str, str, str]: return namespace, schema, data_id -def find_vespa_cert_paths() -> tuple[Union[str, None], Union[str, None]]: +def find_vespa_cert_paths() -> tuple[Optional[str], Optional[str]]: """ Automatically find the certificate and key files for the vespa instance From a42b97c6fe02fb7ed1a2b552436acc03be9bfc88 Mon Sep 17 00:00:00 2001 From: Mark Date: Mon, 16 Sep 2024 11:54:36 +0100 Subject: [PATCH 5/6] Adding import for Optional. --- src/cpr_sdk/vespa.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cpr_sdk/vespa.py b/src/cpr_sdk/vespa.py index bfc7921..b071a30 100644 --- a/src/cpr_sdk/vespa.py +++ b/src/cpr_sdk/vespa.py @@ -1,5 +1,5 @@ from pathlib import Path -from typing import Any, List, Union +from typing import Any, List, Optional import yaml from vespa.exceptions import VespaError From d0061e447000f7128413ad359c61465a37f0e5a8 Mon Sep 17 00:00:00 2001 From: Mark Date: Mon, 16 Sep 2024 11:59:21 +0100 Subject: [PATCH 6/6] Return no cert path if we can't find application name. --- src/cpr_sdk/vespa.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/cpr_sdk/vespa.py b/src/cpr_sdk/vespa.py index b071a30..7534df9 100644 --- a/src/cpr_sdk/vespa.py +++ b/src/cpr_sdk/vespa.py @@ -54,6 +54,8 @@ def find_vespa_cert_paths() -> tuple[Optional[str], Optional[str]]: # read the config.yaml file to find the application name with open(vespa_directory / "config.yaml", "r", encoding="utf-8") as yaml_file: data = yaml.safe_load(yaml_file) + if "application" not in data: + return None, None application_name = data["application"] cert_directory = vespa_directory / application_name