Skip to content
Open
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
7 changes: 4 additions & 3 deletions guardrails/prompt/base_prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ def __init__(
):
"""Initialize and substitute constants in the prompt."""
self._source = source
# Cache variable names once for efficiency
self.variable_names: List[str] = get_template_variables(source)
self.format_instructions_start = self.get_format_instructions_idx(source)

# FIXME: Why is this happening on init instead of on format?
# Substitute constants in the prompt.
source = self.substitute_constants(source)

# FIXME: Why is this happening on init instead of on format?
# If an output schema is provided, substitute it in the prompt.
# Substitute output schemas if provided.
if output_schema or xml_output_schema:
self.source = Template(source).safe_substitute(
output_schema=output_schema, xml_output_schema=xml_output_schema
Expand Down Expand Up @@ -72,6 +72,7 @@ def substitute_constants(self, text: str) -> str:
return text

def get_prompt_variables(self) -> List[str]:
# Return cached variable names for efficiency
return self.variable_names

def format(self, **kwargs) -> "BasePrompt":
Expand Down
5 changes: 2 additions & 3 deletions guardrails/prompt/prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from string import Template

from guardrails.utils.templating_utils import get_template_variables

from .base_prompt import BasePrompt

Expand All @@ -18,8 +17,8 @@ def __eq__(self, __value: object) -> bool:

def format(self, **kwargs) -> "Prompt":
"""Format the prompt using the given keyword arguments."""
# Only use the keyword arguments that are present in the prompt.
vars = get_template_variables(self.source)
# Only use the keyword arguments that are present in the prompt (cached from BasePrompt).
vars = self.variable_names
filtered_kwargs = {k: v for k, v in kwargs.items() if k in vars}

# Return another instance of the class with the formatted prompt.
Expand Down