Skip to content

Commit a6fbb35

Browse files
committed
base_prompt: Stop supporting deprecated variables notation
1 parent 58d2316 commit a6fbb35

File tree

1 file changed

+7
-25
lines changed

1 file changed

+7
-25
lines changed

guardrails/prompt/base_prompt.py

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
import regex
88

9-
from guardrails.namespace_template import NamespaceTemplate
109
from guardrails.utils.constants import constants
1110
from guardrails.utils.parsing_utils import get_template_variables
1211

@@ -17,8 +16,13 @@ class BasePrompt:
1716
def __init__(self, source: str, output_schema: Optional[str] = None):
1817
self.format_instructions_start = self.get_format_instructions_idx(source)
1918

20-
# Substitute constants in the prompt.
21-
source = self.substitute_constants(source)
19+
# Warn if the prompt uses the old constant schema.
20+
if self.uses_old_constant_schema(source):
21+
warnings.warn(
22+
"You may be using an an unsupported convention for specifying "
23+
"guardrails variables. Follow the new namespaced convention "
24+
"documented here: https://docs.guardrailsai.com/0-2-migration/"
25+
)
2226

2327
# If an output schema is provided, substitute it in the prompt.
2428
if output_schema:
@@ -44,28 +48,6 @@ def variable_names(self):
4448
def format_instructions(self):
4549
return self.source[self.format_instructions_start :]
4650

47-
def substitute_constants(self, text):
48-
"""Substitute constants in the prompt."""
49-
# Substitute constants by reading the constants file.
50-
# Regex to extract all occurrences of ${gr.<constant_name>}
51-
if self.uses_old_constant_schema(text):
52-
warnings.warn(
53-
"It appears that you are using an old schema for gaurdrails variables, "
54-
"follow the new namespaced convention "
55-
"documented here: https://docs.guardrailsai.com/0-2-migration/"
56-
)
57-
58-
matches = re.findall(r"\${gr\.(\w+)}", text)
59-
60-
# Substitute all occurrences of ${gr.<constant_name>}
61-
# with the value of the constant.
62-
for match in matches:
63-
template = NamespaceTemplate(text)
64-
mapping = {f"gr.{match}": constants[match]}
65-
text = template.safe_substitute(**mapping)
66-
67-
return text
68-
6951
def uses_old_constant_schema(self, text) -> bool:
7052
matches = re.findall(r"@(\w+)", text)
7153
if len(matches) == 0:

0 commit comments

Comments
 (0)