@@ -703,6 +703,147 @@ def test_inference_with_multimodal_content(
703703 )
704704
705705
706+ class TestMetricPromptBuilder :
707+ """Unit tests for the MetricPromptBuilder class."""
708+
709+ def test_metric_prompt_builder_minimal_fields (self ):
710+ criteria = {"criterion1" : "definition1" }
711+ rating_scores = {"score1" : "description1" }
712+ builder = vertexai_genai_types .MetricPromptBuilder (
713+ criteria = criteria ,
714+ rating_scores = rating_scores ,
715+ metric_definition = None ,
716+ few_shot_examples = None ,
717+ )
718+ assert builder .criteria == criteria
719+ assert builder .rating_scores == rating_scores
720+ expected_instruction = (
721+ "You are an expert evaluator. Your task is to evaluate the quality"
722+ " of the responses generated by AI models. We will provide you with"
723+ " the user prompt and an AI-generated responses.\n You should first"
724+ " read the user input carefully for analyzing the task, and then"
725+ " evaluate the quality of the responses based on the Criteria"
726+ " provided in the Evaluation section below.\n You will assign the"
727+ " response a rating following the Rating Scores and Evaluation"
728+ " Steps. Give step by step explanations for your rating, and only"
729+ " choose ratings from the Rating Scores."
730+ )
731+ expected_evaluation_steps = {
732+ "Step 1" : (
733+ "Assess the response in aspects of all criteria provided. Provide"
734+ " assessment according to each criterion."
735+ ),
736+ "Step 2" : (
737+ "Score based on the Rating Scores. Give a brief rationale to"
738+ " explain your evaluation considering each individual criterion."
739+ ),
740+ }
741+ assert builder .instruction == expected_instruction
742+ assert builder .evaluation_steps == expected_evaluation_steps
743+ assert "{response}" in builder .text
744+
745+ def test_metric_prompt_builder_all_fields (self ):
746+ criteria = {"criterion1" : "definition1" }
747+ rating_scores = {"score1" : "description1" }
748+ instruction = "Custom instruction."
749+ metric_definition = "Custom metric definition."
750+ evaluation_steps = {"step1" : "custom step 1" }
751+ few_shot_examples = ["example1" , "example2" ]
752+ builder = vertexai_genai_types .MetricPromptBuilder (
753+ criteria = criteria ,
754+ rating_scores = rating_scores ,
755+ instruction = instruction ,
756+ metric_definition = metric_definition ,
757+ evaluation_steps = evaluation_steps ,
758+ few_shot_examples = few_shot_examples ,
759+ )
760+ assert builder .criteria == criteria
761+ assert builder .rating_scores == rating_scores
762+ assert builder .instruction == instruction
763+ assert builder .metric_definition == metric_definition
764+ assert builder .evaluation_steps == evaluation_steps
765+ assert builder .few_shot_examples == few_shot_examples
766+ assert instruction in builder .text
767+ assert metric_definition in builder .text
768+ assert "custom step 1" in builder .text
769+ assert "example1" in builder .text
770+
771+ def test_metric_prompt_builder_default_instruction_and_steps_in_text (self ):
772+ criteria = {"c1" : "v1" }
773+ rating_scores = {"s1" : "d1" }
774+ builder = vertexai_genai_types .MetricPromptBuilder (
775+ criteria = criteria ,
776+ rating_scores = rating_scores ,
777+ metric_definition = None ,
778+ few_shot_examples = None ,
779+ )
780+ expected_instruction = (
781+ "You are an expert evaluator. Your task is to evaluate the quality"
782+ " of the responses generated by AI models. We will provide you with"
783+ " the user prompt and an AI-generated responses.\n You should first"
784+ " read the user input carefully for analyzing the task, and then"
785+ " evaluate the quality of the responses based on the Criteria"
786+ " provided in the Evaluation section below.\n You will assign the"
787+ " response a rating following the Rating Scores and Evaluation"
788+ " Steps. Give step by step explanations for your rating, and only"
789+ " choose ratings from the Rating Scores."
790+ )
791+ expected_evaluation_steps = {
792+ "Step 1" : (
793+ "Assess the response in aspects of all criteria provided. Provide"
794+ " assessment according to each criterion."
795+ ),
796+ "Step 2" : (
797+ "Score based on the Rating Scores. Give a brief rationale to"
798+ " explain your evaluation considering each individual criterion."
799+ ),
800+ }
801+ assert expected_instruction in builder .text
802+ for step_desc in expected_evaluation_steps .values ():
803+ assert step_desc in builder .text
804+
805+ def test_metric_prompt_builder_custom_instruction_and_steps_in_text (self ):
806+ criteria = {"c1" : "v1" }
807+ rating_scores = {"s1" : "d1" }
808+ custom_instruction = "My custom instructions."
809+ custom_steps = {"Step 1" : "Do this first." , "Step 2" : "Then do that." }
810+ builder = vertexai_genai_types .MetricPromptBuilder (
811+ criteria = criteria ,
812+ rating_scores = rating_scores ,
813+ instruction = custom_instruction ,
814+ evaluation_steps = custom_steps ,
815+ metric_definition = None ,
816+ few_shot_examples = None ,
817+ )
818+ assert custom_instruction in builder .text
819+ for step_desc in custom_steps .values ():
820+ assert step_desc in builder .text
821+
822+ def test_metric_prompt_builder_missing_criteria_raises_error (self ):
823+ with pytest .raises (
824+ ValueError ,
825+ match = "Both 'criteria' and 'rating_scores' are required" ,
826+ ):
827+ vertexai_genai_types .MetricPromptBuilder (
828+ rating_scores = {"score1" : "description1" },
829+ criteria = None ,
830+ metric_definition = None ,
831+ few_shot_examples = None ,
832+ )
833+
834+ def test_metric_prompt_builder_missing_rating_scores_raises_error (self ):
835+ with pytest .raises (
836+ ValueError ,
837+ match = "Both 'criteria' and 'rating_scores' are required" ,
838+ ):
839+ vertexai_genai_types .MetricPromptBuilder (
840+ criteria = {"criterion1" : "definition1" },
841+ rating_scores = None ,
842+ metric_definition = None ,
843+ few_shot_examples = None ,
844+ )
845+
846+
706847class TestPromptTemplate :
707848 """Unit tests for the PromptTemplate class."""
708849
0 commit comments