Skip to content

Commit d7ccd0a

Browse files
committed
adding back get_fixed_answer_choices_list - revert 07688e5
1 parent 33029c3 commit d7ccd0a

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

promptsource/templates.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import pandas as pd
1010
import pkg_resources
1111
import 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

0 commit comments

Comments
 (0)