Skip to content

Commit 7963e3c

Browse files
authored
Merge pull request #444 from irgolic/resolve-string-formatting-deprecation
base_prompt: Stop warning on unsupported variables notation
2 parents 2edbffd + c3dd838 commit 7963e3c

File tree

4 files changed

+0
-55
lines changed

4 files changed

+0
-55
lines changed

guardrails/prompt/base_prompt.py

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
"""Class for representing a prompt entry."""
22
import re
3-
import warnings
43
from string import Template
54
from typing import Optional
65

@@ -49,13 +48,6 @@ def substitute_constants(self, text):
4948
"""Substitute constants in the prompt."""
5049
# Substitute constants by reading the constants file.
5150
# Regex to extract all occurrences of ${gr.<constant_name>}
52-
if self.uses_old_constant_schema(text):
53-
warnings.warn(
54-
"It appears that you are using an old schema for gaurdrails variables, "
55-
"follow the new namespaced convention "
56-
"documented here: https://docs.guardrailsai.com/0-2-migration/"
57-
)
58-
5951
matches = re.findall(r"\${gr\.(\w+)}", text)
6052

6153
# Substitute all occurrences of ${gr.<constant_name>}
@@ -67,13 +59,6 @@ def substitute_constants(self, text):
6759

6860
return text
6961

70-
def uses_old_constant_schema(self, text) -> bool:
71-
matches = re.findall(r"@(\w+)", text)
72-
if len(matches) == 0:
73-
return False
74-
else:
75-
return True
76-
7762
def get_prompt_variables(self):
7863
return self.variable_names
7964

guardrails/prompt/instructions.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
"""Instructions to the LLM, to be passed in the prompt."""
22
from string import Template
3-
from warnings import warn
43

54
from guardrails.utils.parsing_utils import get_template_variables
65

@@ -30,12 +29,6 @@ def format(self, **kwargs):
3029
# Only use the keyword arguments that are present in the prompt.
3130
vars = get_template_variables(self.source)
3231
filtered_kwargs = {k: v for k, v in kwargs.items() if k in vars}
33-
if len(filtered_kwargs) == 0:
34-
warn(
35-
"Instructions do not have any variables, "
36-
"if you are migrating follow the new variable convention "
37-
"documented here: https://docs.guardrailsai.com/0-2-migration/"
38-
)
3932

4033
# Return another instance of the class with the formatted prompt.
4134
formatted_instructions = Template(self.source).safe_substitute(

guardrails/prompt/prompt.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
"""The LLM prompt."""
2-
import warnings
32
from string import Template
43

54
from guardrails.utils.parsing_utils import get_template_variables
@@ -21,12 +20,6 @@ def format(self, **kwargs):
2120
# Only use the keyword arguments that are present in the prompt.
2221
vars = get_template_variables(self.source)
2322
filtered_kwargs = {k: v for k, v in kwargs.items() if k in vars}
24-
if len(filtered_kwargs) == 0:
25-
warnings.warn(
26-
"Prompt does not have any variables, "
27-
"if you are migrating follow the new variable convention "
28-
"documented here: https://docs.guardrailsai.com/0-2-migration/"
29-
)
3023

3124
# Return another instance of the class with the formatted prompt.
3225
formatted_prompt = Template(self.source).safe_substitute(**filtered_kwargs)

tests/unit_tests/test_prompt.py

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"""Unit tests for prompt and instructions parsing."""
22

33
from string import Template
4-
from unittest import mock
54

65
import pytest
76
from pydantic import BaseModel, Field
@@ -232,31 +231,6 @@ def test_substitute_constants(prompt_str, final_prompt):
232231
assert prompt.source == final_prompt
233232

234233

235-
# TODO: Deprecate when we can confirm migration off the old, non-namespaced standard
236-
@pytest.mark.parametrize(
237-
"text, is_old_schema",
238-
[
239-
(RAIL_WITH_OLD_CONSTANT_SCHEMA, True), # Test with a single match
240-
(
241-
RAIL_WITH_FORMAT_INSTRUCTIONS,
242-
False,
243-
), # Test with no matches/correct namespacing
244-
],
245-
)
246-
def test_uses_old_constant_schema(text, is_old_schema):
247-
with mock.patch("warnings.warn") as warn_mock:
248-
guard = gd.Guard.from_rail_string(text)
249-
assert guard.prompt.uses_old_constant_schema(text) == is_old_schema
250-
if is_old_schema:
251-
# we only check for the warning when we have an older schema
252-
warn_mock.assert_called_once_with(
253-
"""It appears that you are using an old schema for gaurdrails\
254-
variables, follow the new namespaced convention documented here:\
255-
https://docs.guardrailsai.com/0-2-migration/\
256-
"""
257-
)
258-
259-
260234
class TestResponse(BaseModel):
261235
grade: int = Field(description="The grade of the response")
262236

0 commit comments

Comments
 (0)