From e80ba5c75216461ceeb43d0ff6d9181e79f2a971 Mon Sep 17 00:00:00 2001 From: Rafael Irgolic Date: Tue, 21 Nov 2023 17:37:35 +0000 Subject: [PATCH 1/2] base_prompt: Remove warning on old schema --- guardrails/prompt/base_prompt.py | 15 --------------- tests/unit_tests/test_prompt.py | 26 -------------------------- 2 files changed, 41 deletions(-) diff --git a/guardrails/prompt/base_prompt.py b/guardrails/prompt/base_prompt.py index 643313eff..5240f5324 100644 --- a/guardrails/prompt/base_prompt.py +++ b/guardrails/prompt/base_prompt.py @@ -1,6 +1,5 @@ """Class for representing a prompt entry.""" import re -import warnings from string import Template from typing import Optional @@ -48,13 +47,6 @@ def substitute_constants(self, text): """Substitute constants in the prompt.""" # Substitute constants by reading the constants file. # Regex to extract all occurrences of ${gr.} - if self.uses_old_constant_schema(text): - warnings.warn( - "It appears that you are using an old schema for gaurdrails variables, " - "follow the new namespaced convention " - "documented here: https://docs.guardrailsai.com/0-2-migration/" - ) - matches = re.findall(r"\${gr\.(\w+)}", text) # Substitute all occurrences of ${gr.} @@ -66,13 +58,6 @@ def substitute_constants(self, text): return text - def uses_old_constant_schema(self, text) -> bool: - matches = re.findall(r"@(\w+)", text) - if len(matches) == 0: - return False - else: - return True - def get_prompt_variables(self): return self.variable_names diff --git a/tests/unit_tests/test_prompt.py b/tests/unit_tests/test_prompt.py index 40f2d1c08..3504fa82e 100644 --- a/tests/unit_tests/test_prompt.py +++ b/tests/unit_tests/test_prompt.py @@ -1,7 +1,6 @@ """Unit tests for prompt and instructions parsing.""" from string import Template -from unittest import mock import pytest from pydantic import BaseModel, Field @@ -232,31 +231,6 @@ def test_substitute_constants(prompt_str, final_prompt): assert prompt.source == final_prompt -# TODO: Deprecate when we can confirm migration off the old, non-namespaced standard -@pytest.mark.parametrize( - "text, is_old_schema", - [ - (RAIL_WITH_OLD_CONSTANT_SCHEMA, True), # Test with a single match - ( - RAIL_WITH_FORMAT_INSTRUCTIONS, - False, - ), # Test with no matches/correct namespacing - ], -) -def test_uses_old_constant_schema(text, is_old_schema): - with mock.patch("warnings.warn") as warn_mock: - guard = gd.Guard.from_rail_string(text) - assert guard.prompt.uses_old_constant_schema(text) == is_old_schema - if is_old_schema: - # we only check for the warning when we have an older schema - warn_mock.assert_called_once_with( - """It appears that you are using an old schema for gaurdrails\ - variables, follow the new namespaced convention documented here:\ - https://docs.guardrailsai.com/0-2-migration/\ -""" - ) - - class TestResponse(BaseModel): grade: int = Field(description="The grade of the response") From c3dd838d6f90f54a3b36fd48f8a982f71e635d1b Mon Sep 17 00:00:00 2001 From: Rafael Irgolic Date: Tue, 5 Dec 2023 14:27:38 +0000 Subject: [PATCH 2/2] Remove string formatting deprecation warning from prompt.py and instructions.py --- guardrails/prompt/instructions.py | 7 ------- guardrails/prompt/prompt.py | 7 ------- 2 files changed, 14 deletions(-) diff --git a/guardrails/prompt/instructions.py b/guardrails/prompt/instructions.py index f9bd612b5..1ef53bbac 100644 --- a/guardrails/prompt/instructions.py +++ b/guardrails/prompt/instructions.py @@ -1,6 +1,5 @@ """Instructions to the LLM, to be passed in the prompt.""" from string import Template -from warnings import warn from guardrails.utils.parsing_utils import get_template_variables @@ -30,12 +29,6 @@ def format(self, **kwargs): # Only use the keyword arguments that are present in the prompt. vars = get_template_variables(self.source) filtered_kwargs = {k: v for k, v in kwargs.items() if k in vars} - if len(filtered_kwargs) == 0: - warn( - "Instructions do not have any variables, " - "if you are migrating follow the new variable convention " - "documented here: https://docs.guardrailsai.com/0-2-migration/" - ) # Return another instance of the class with the formatted prompt. formatted_instructions = Template(self.source).safe_substitute( diff --git a/guardrails/prompt/prompt.py b/guardrails/prompt/prompt.py index 20947b767..484c489cf 100644 --- a/guardrails/prompt/prompt.py +++ b/guardrails/prompt/prompt.py @@ -1,5 +1,4 @@ """The LLM prompt.""" -import warnings from string import Template from guardrails.utils.parsing_utils import get_template_variables @@ -21,12 +20,6 @@ def format(self, **kwargs): # Only use the keyword arguments that are present in the prompt. vars = get_template_variables(self.source) filtered_kwargs = {k: v for k, v in kwargs.items() if k in vars} - if len(filtered_kwargs) == 0: - warnings.warn( - "Prompt does not have any variables, " - "if you are migrating follow the new variable convention " - "documented here: https://docs.guardrailsai.com/0-2-migration/" - ) # Return another instance of the class with the formatted prompt. formatted_prompt = Template(self.source).safe_substitute(**filtered_kwargs)