diff --git a/.github/workflows/examples_check.yml b/.github/workflows/examples_check.yml index f01651382..de4c9a31c 100644 --- a/.github/workflows/examples_check.yml +++ b/.github/workflows/examples_check.yml @@ -9,6 +9,11 @@ jobs: execute_notebooks: runs-on: ubuntu-latest + env: + COHERE_API_KEY: ${{ secrets.COHERE_API_KEY }} + OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }} + + steps: - name: Checkout repository uses: actions/checkout@v2 @@ -16,11 +21,11 @@ jobs: - name: Set up Python uses: actions/setup-python@v2 with: - python-version: 3.x + python-version: 3.11.x - name: Install dependencies run: | - pip install jupyter nbconvert + make full; pip install jupyter nbconvert; pip install . - name: Execute notebooks and check for errors run: | diff --git a/docs/examples/generate_structured_data_cohere.ipynb b/docs/examples/generate_structured_data_cohere.ipynb index 5ca1cbbb1..60043e819 100644 --- a/docs/examples/generate_structured_data_cohere.ipynb +++ b/docs/examples/generate_structured_data_cohere.ipynb @@ -141,12 +141,7 @@ { "name": "stderr", "output_type": "stream", - "text": [ - "/Users/zaydsimjee/workspace/shreya-gr/guardrails/.venv/lib/python3.11/site-packages/guardrails/schema.py:228: UserWarning: Validator 1-indexed is not valid for element integer.\n", - " warnings.warn(\n", - "/Users/zaydsimjee/workspace/shreya-gr/guardrails/.venv/lib/python3.11/site-packages/guardrails/prompt/prompt.py:23: UserWarning: Prompt does not have any variables, if you are migrating follow the new variable convention documented here: https://docs.getguardrails.ai/0-2-migration/\n", - " warnings.warn(\n" - ] + "text": [] } ], "source": [ diff --git a/docs/examples/provenance.ipynb b/docs/examples/provenance.ipynb index 92256efc6..460bf1a32 100644 --- a/docs/examples/provenance.ipynb +++ b/docs/examples/provenance.ipynb @@ -298,9 +298,12 @@ "from guardrails import Guard\n", "from guardrails.validators import ProvenanceV0\n", "from typing import List, Union\n", + "import os\n", + "\n", + "api_key = os.environ[\"COHERE_API_KEY\"]\n", "\n", "# Create a cohere client\n", - "cohere_client = cohere.Client(api_key=\"\")\n", + "cohere_client = cohere.Client(api_key=api_key)\n", "\n", "\n", "def embed_function(text: Union[str, List[str]]) -> np.ndarray:\n", diff --git a/guardrails/guard.py b/guardrails/guard.py index c3260438a..99f6a72f5 100644 --- a/guardrails/guard.py +++ b/guardrails/guard.py @@ -444,7 +444,7 @@ def parse( llm_output: str, metadata: Optional[Dict] = None, llm_api: None = None, - num_reasks: int = 1, + num_reasks: Optional[int] = None, prompt_params: Optional[Dict] = None, full_schema_reask: Optional[bool] = None, *args, @@ -458,7 +458,7 @@ def parse( llm_output: str, metadata: Optional[Dict] = None, llm_api: Callable[[Any], Awaitable[Any]] = ..., - num_reasks: int = 1, + num_reasks: Optional[int] = None, prompt_params: Optional[Dict] = None, full_schema_reask: Optional[bool] = None, *args, @@ -472,7 +472,7 @@ def parse( llm_output: str, metadata: Optional[Dict] = None, llm_api: Optional[Callable] = None, - num_reasks: int = 1, + num_reasks: Optional[int] = None, prompt_params: Optional[Dict] = None, full_schema_reask: Optional[bool] = None, *args, @@ -485,7 +485,7 @@ def parse( llm_output: str, metadata: Optional[Dict] = None, llm_api: Optional[Callable] = None, - num_reasks: int = 1, + num_reasks: Optional[int] = None, prompt_params: Optional[Dict] = None, full_schema_reask: Optional[bool] = None, *args, diff --git a/guardrails/validator_base.py b/guardrails/validator_base.py index 12e19b354..abba4295f 100644 --- a/guardrails/validator_base.py +++ b/guardrails/validator_base.py @@ -1,3 +1,4 @@ +import inspect from collections import defaultdict from typing import Any, Callable, Dict, List, Literal, Optional, Type, Union @@ -245,11 +246,14 @@ def to_xml_attrib(self): return self.rail_alias validator_args = [] - for arg in self.__init__.__code__.co_varnames[1:]: + init_args = inspect.getfullargspec(self.__init__) + for arg in init_args.args[1:]: if arg not in ("on_fail", "args", "kwargs"): - str_arg = str(self._kwargs[arg]) - str_arg = "{" + str_arg + "}" if " " in str_arg else str_arg - validator_args.append(str_arg) + arg_value = self._kwargs.get(arg) + str_arg = str(arg_value) + if str_arg is not None: + str_arg = "{" + str_arg + "}" if " " in str_arg else str_arg + validator_args.append(str_arg) params = " ".join(validator_args) return f"{self.rail_alias}: {params}" diff --git a/guardrails/validators.py b/guardrails/validators.py index 506516af0..7f090bd19 100644 --- a/guardrails/validators.py +++ b/guardrails/validators.py @@ -19,6 +19,7 @@ import rstr from tenacity import retry, stop_after_attempt, wait_random_exponential +from guardrails.utils.casting_utils import to_int from guardrails.utils.docs_utils import get_chunks_from_text, sentence_split from guardrails.utils.sql_utils import SQLDriver, create_sql_driver from guardrails.utils.validator_utils import PROVENANCE_V1_PROMPT @@ -277,8 +278,8 @@ def __init__( on_fail: Optional[Callable] = None, ): super().__init__(on_fail=on_fail, min=min, max=max) - self._min = int(min) if min is not None else None - self._max = int(max) if max is not None else None + self._min = to_int(min) + self._max = to_int(max) def validate(self, value: Union[str, List], metadata: Dict) -> ValidationResult: """Validates that the length of value is within the expected range.""" diff --git a/guardrails/version.py b/guardrails/version.py index fe404ae57..01ef12070 100644 --- a/guardrails/version.py +++ b/guardrails/version.py @@ -1 +1 @@ -__version__ = "0.2.5" +__version__ = "0.2.6"