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 9
9
import pandas as pd
10
10
import pkg_resources
11
11
import yaml
12
- from jinja2 import BaseLoader , Environment
12
+ from jinja2 import BaseLoader , Environment , meta
13
13
14
14
15
15
# Truncation of jinja template variables
@@ -135,6 +135,24 @@ def get_answer_choices_list(self, example):
135
135
rendered_choices = rtemplate .render (** protected_example )
136
136
return [self ._unescape_pipe (answer_choice .strip ()) for answer_choice in rendered_choices .split ("|||" )]
137
137
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
+
138
156
def apply (self , example , truncate = True , highlight_variables = False ):
139
157
"""
140
158
Creates a prompt by applying this template to an example
You can’t perform that action at this time.
0 commit comments