From 05304b2f01dbb506804df49e13d93c062f0d4266 Mon Sep 17 00:00:00 2001 From: Jason Dai Date: Thu, 20 Mar 2025 18:04:26 -0700 Subject: [PATCH] No public description PiperOrigin-RevId: 739002065 --- vertexai/evaluation/prompt_template.py | 15 ++++++++++----- vertexai/preview/evaluation/prompt_template.py | 15 ++++++++++----- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/vertexai/evaluation/prompt_template.py b/vertexai/evaluation/prompt_template.py index 473293d20f..256a19d845 100644 --- a/vertexai/evaluation/prompt_template.py +++ b/vertexai/evaluation/prompt_template.py @@ -66,11 +66,16 @@ def assemble(self, **kwargs) -> "PromptTemplate": Returns: A new PromptTemplate instance with the updated template string. """ - replaced_values = { - key: kwargs.get(key, "{" + key + "}") for key in self.variables - } - new_template = self.template.format(**replaced_values) - return PromptTemplate(new_template) + assembled_string = self.template + for variable_name, value in kwargs.items(): + if variable_name not in self.variables: + raise ValueError( + f"Invalid variable name '{variable_name}'. " + f"Valid variables are: {self.variables}" + ) + placeholder = "{" + variable_name + "}" + assembled_string = assembled_string.replace(placeholder, str(value)) + return PromptTemplate(assembled_string) def __str__(self) -> str: """Returns the template string.""" diff --git a/vertexai/preview/evaluation/prompt_template.py b/vertexai/preview/evaluation/prompt_template.py index 473293d20f..256a19d845 100644 --- a/vertexai/preview/evaluation/prompt_template.py +++ b/vertexai/preview/evaluation/prompt_template.py @@ -66,11 +66,16 @@ def assemble(self, **kwargs) -> "PromptTemplate": Returns: A new PromptTemplate instance with the updated template string. """ - replaced_values = { - key: kwargs.get(key, "{" + key + "}") for key in self.variables - } - new_template = self.template.format(**replaced_values) - return PromptTemplate(new_template) + assembled_string = self.template + for variable_name, value in kwargs.items(): + if variable_name not in self.variables: + raise ValueError( + f"Invalid variable name '{variable_name}'. " + f"Valid variables are: {self.variables}" + ) + placeholder = "{" + variable_name + "}" + assembled_string = assembled_string.replace(placeholder, str(value)) + return PromptTemplate(assembled_string) def __str__(self) -> str: """Returns the template string."""