6
6
7
7
import regex
8
8
9
- from guardrails .namespace_template import NamespaceTemplate
10
9
from guardrails .utils .constants import constants
11
10
from guardrails .utils .parsing_utils import get_template_variables
12
11
@@ -17,8 +16,13 @@ class BasePrompt:
17
16
def __init__ (self , source : str , output_schema : Optional [str ] = None ):
18
17
self .format_instructions_start = self .get_format_instructions_idx (source )
19
18
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
+ )
22
26
23
27
# If an output schema is provided, substitute it in the prompt.
24
28
if output_schema :
@@ -44,28 +48,6 @@ def variable_names(self):
44
48
def format_instructions (self ):
45
49
return self .source [self .format_instructions_start :]
46
50
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
-
69
51
def uses_old_constant_schema (self , text ) -> bool :
70
52
matches = re .findall (r"@(\w+)" , text )
71
53
if len (matches ) == 0 :
0 commit comments