File tree Expand file tree Collapse file tree 1 file changed +19
-1
lines changed Expand file tree Collapse file tree 1 file changed +19
-1
lines changed Original file line number Diff line number Diff line change 99import pandas as pd
1010import pkg_resources
1111import yaml
12- from jinja2 import BaseLoader , Environment
12+ from jinja2 import BaseLoader , Environment , meta
1313
1414
1515# Truncation of jinja template variables
@@ -135,6 +135,24 @@ def get_answer_choices_list(self, example):
135135 rendered_choices = rtemplate .render (** protected_example )
136136 return [self ._unescape_pipe (answer_choice .strip ()) for answer_choice in rendered_choices .split ("|||" )]
137137
138+ def get_fixed_answer_choices_list (self ):
139+ """
140+ Returns a list of answer choices that is static across examples, if possible
141+ :return: list of strings, or None if no static list exists
142+ """
143+ jinja = self .get_answer_choices_expr ()
144+ if jinja is None :
145+ return None
146+
147+ parse = env .parse (jinja )
148+ variables = meta .find_undeclared_variables (parse )
149+ if len (variables ) == 0 :
150+ rtemplate = env .from_string (jinja )
151+ rendered_choices = rtemplate .render ()
152+ return [answer_choice .strip () for answer_choice in rendered_choices .split ("|||" )]
153+ else :
154+ return None
155+
138156 def apply (self , example , truncate = True , highlight_variables = False ):
139157 """
140158 Creates a prompt by applying this template to an example
You can’t perform that action at this time.
0 commit comments