Skip to content

Commit 43ebe97

Browse files
jsondaicopybara-github
authored andcommitted
feat: change format string assembly logic for prompt templates.
PiperOrigin-RevId: 739307798
1 parent 03d9bf7 commit 43ebe97

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

vertexai/evaluation/prompt_template.py

+10-5
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,16 @@ def assemble(self, **kwargs) -> "PromptTemplate":
6666
Returns:
6767
A new PromptTemplate instance with the updated template string.
6868
"""
69-
replaced_values = {
70-
key: kwargs.get(key, "{" + key + "}") for key in self.variables
71-
}
72-
new_template = self.template.format(**replaced_values)
73-
return PromptTemplate(new_template)
69+
assembled_string = self.template
70+
for variable_name, value in kwargs.items():
71+
if variable_name not in self.variables:
72+
raise ValueError(
73+
f"Invalid variable name '{variable_name}'. "
74+
f"Valid variables are: {self.variables}"
75+
)
76+
placeholder = "{" + variable_name + "}"
77+
assembled_string = assembled_string.replace(placeholder, str(value))
78+
return PromptTemplate(assembled_string)
7479

7580
def __str__(self) -> str:
7681
"""Returns the template string."""

vertexai/preview/evaluation/prompt_template.py

+10-5
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,16 @@ def assemble(self, **kwargs) -> "PromptTemplate":
6666
Returns:
6767
A new PromptTemplate instance with the updated template string.
6868
"""
69-
replaced_values = {
70-
key: kwargs.get(key, "{" + key + "}") for key in self.variables
71-
}
72-
new_template = self.template.format(**replaced_values)
73-
return PromptTemplate(new_template)
69+
assembled_string = self.template
70+
for variable_name, value in kwargs.items():
71+
if variable_name not in self.variables:
72+
raise ValueError(
73+
f"Invalid variable name '{variable_name}'. "
74+
f"Valid variables are: {self.variables}"
75+
)
76+
placeholder = "{" + variable_name + "}"
77+
assembled_string = assembled_string.replace(placeholder, str(value))
78+
return PromptTemplate(assembled_string)
7479

7580
def __str__(self) -> str:
7681
"""Returns the template string."""

0 commit comments

Comments
 (0)