Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

base_prompt: Stop warning on unsupported variables notation #444

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 0 additions & 15 deletions guardrails/prompt/base_prompt.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""Class for representing a prompt entry."""
import re
import warnings
from string import Template
from typing import Optional

Expand Down Expand Up @@ -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.<constant_name>}
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.<constant_name>}
Expand All @@ -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

Expand Down
7 changes: 0 additions & 7 deletions guardrails/prompt/instructions.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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(
Expand Down
7 changes: 0 additions & 7 deletions guardrails/prompt/prompt.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""The LLM prompt."""
import warnings
from string import Template

from guardrails.utils.parsing_utils import get_template_variables
Expand All @@ -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)
Expand Down
26 changes: 0 additions & 26 deletions tests/unit_tests/test_prompt.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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")

Expand Down
Loading