From 640a4d2aa94b9fac388072b004047cc5bfb23283 Mon Sep 17 00:00:00 2001 From: Wyatt Lansford <22553069+wylansford@users.noreply.github.com> Date: Mon, 1 Jul 2024 14:23:46 -0700 Subject: [PATCH 1/3] updaing validator base for a bug fix --- guardrails/validator_base.py | 41 ++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/guardrails/validator_base.py b/guardrails/validator_base.py index 1cc5ea841..23b779734 100644 --- a/guardrails/validator_base.py +++ b/guardrails/validator_base.py @@ -79,7 +79,9 @@ def validate_wrapper(self, *args, **kwargs): return validator -def register_validator(name: str, data_type: Union[str, List[str]]): +def register_validator( + name: str, data_type: Union[str, List[str]], has_guardrails_endpoint: bool +): """Register a validator for a data type.""" from guardrails.datatypes import types_registry @@ -187,7 +189,6 @@ def __init__( f"{VALIDATOR_HUB_SERVICE}/validator/{validator_id}/inference" ) self.validation_endpoint = submission_url - self.on_fail_descriptor: Union[str, OnFailAction] = "custom" # chunking function returns empty list or list of 2 chunks @@ -271,6 +272,7 @@ def _inference(self, model_input: Any) -> Any: return self._inference_local(model_input) if not self.use_local and self.validation_endpoint: return self._inference_remote(model_input) + raise RuntimeError( "No inference endpoint set, but use_local was false. " "Please set either use_local=True or " @@ -324,7 +326,9 @@ def validate_stream( validation_result.validated_chunk = chunk_to_validate return validation_result - def _hub_inference_request(self, request_body: dict) -> Any: + def _hub_inference_request( + self, request_body: dict, validation_endpoint: str + ) -> Any: """Makes a request to the Validator Hub to run a ML based validation model. This request is authed through the hub and rerouted to a hosted ML model. The reply from the hosted endpoint is returned and sent to this client. @@ -332,6 +336,7 @@ def _hub_inference_request(self, request_body: dict) -> Any: Args: request_body (dict): A dictionary containing the required info for the final + validation_endpoint (str): The url to request as an endpoint inference endpoint to run. Raises: @@ -340,24 +345,18 @@ def _hub_inference_request(self, request_body: dict) -> Any: Returns: Any: Post request response from the ML based validation model. """ - - try: - submission_url = self.validation_endpoint - - headers = { - "Authorization": f"Bearer {self.hub_jwt_token}", - "Content-Type": "application/json", - } - req = requests.post(submission_url, json=request_body, headers=headers) - if not req.ok: - logging.error(req.status_code) - - return req.json() - - except Exception as e: - logging.error( - "An unexpected validation error occurred" f" in {self.rail_alias}: ", e - ) + headers = { + "Authorization": f"Bearer {self.hub_jwt_token}", + "Content-Type": "application/json", + } + print(request_body) + print(validation_endpoint) + print(headers) + req = requests.post(validation_endpoint, json=request_body, headers=headers) + if not req.ok: + logging.error(req.status_code) + + return req.json() def to_prompt(self, with_keywords: bool = True) -> str: """Convert the validator to a prompt. From 5e06ddcc8aa33e934734c0e937452ae554809081 Mon Sep 17 00:00:00 2001 From: Wyatt Lansford <22553069+wylansford@users.noreply.github.com> Date: Mon, 1 Jul 2024 14:24:56 -0700 Subject: [PATCH 2/3] remove print --- guardrails/validator_base.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/guardrails/validator_base.py b/guardrails/validator_base.py index 23b779734..61a9e80b3 100644 --- a/guardrails/validator_base.py +++ b/guardrails/validator_base.py @@ -15,9 +15,12 @@ import requests from langchain_core.runnables import Runnable -from guardrails.classes import ErrorSpan # noqa -from guardrails.classes import PassResult # noqa -from guardrails.classes import FailResult, ValidationResult +from guardrails.classes import ( + ErrorSpan, # noqa + FailResult, + PassResult, # noqa + ValidationResult, +) from guardrails.classes.credentials import Credentials from guardrails.constants import hub from guardrails.hub_token.token import VALIDATOR_HUB_SERVICE, get_jwt_token @@ -349,9 +352,6 @@ def _hub_inference_request( "Authorization": f"Bearer {self.hub_jwt_token}", "Content-Type": "application/json", } - print(request_body) - print(validation_endpoint) - print(headers) req = requests.post(validation_endpoint, json=request_body, headers=headers) if not req.ok: logging.error(req.status_code) From e36174a0f9aa2c5b1e49dd84f41f1852638a9765 Mon Sep 17 00:00:00 2001 From: Wyatt Lansford <22553069+wylansford@users.noreply.github.com> Date: Mon, 1 Jul 2024 16:48:19 -0700 Subject: [PATCH 3/3] adding default to register_validator --- guardrails/validator_base.py | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/guardrails/validator_base.py b/guardrails/validator_base.py index 61a9e80b3..0eef9fcd2 100644 --- a/guardrails/validator_base.py +++ b/guardrails/validator_base.py @@ -15,12 +15,9 @@ import requests from langchain_core.runnables import Runnable -from guardrails.classes import ( - ErrorSpan, # noqa - FailResult, - PassResult, # noqa - ValidationResult, -) +from guardrails.classes import ErrorSpan # noqa +from guardrails.classes import PassResult # noqa +from guardrails.classes import FailResult, ValidationResult from guardrails.classes.credentials import Credentials from guardrails.constants import hub from guardrails.hub_token.token import VALIDATOR_HUB_SERVICE, get_jwt_token @@ -83,7 +80,7 @@ def validate_wrapper(self, *args, **kwargs): def register_validator( - name: str, data_type: Union[str, List[str]], has_guardrails_endpoint: bool + name: str, data_type: Union[str, List[str]], has_guardrails_endpoint: bool = False ): """Register a validator for a data type.""" from guardrails.datatypes import types_registry