Skip to content

Commit

Permalink
adding back get_fixed_answer_choices_list - revert 07688e5
Browse files Browse the repository at this point in the history
  • Loading branch information
VictorSanh committed Mar 1, 2022
1 parent 33029c3 commit d7ccd0a
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion promptsource/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import pandas as pd
import pkg_resources
import yaml
from jinja2 import BaseLoader, Environment
from jinja2 import BaseLoader, Environment, meta


# Truncation of jinja template variables
Expand Down Expand Up @@ -135,6 +135,24 @@ def get_answer_choices_list(self, example):
rendered_choices = rtemplate.render(**protected_example)
return [self._unescape_pipe(answer_choice.strip()) for answer_choice in rendered_choices.split("|||")]

def get_fixed_answer_choices_list(self):
"""
Returns a list of answer choices that is static across examples, if possible
:return: list of strings, or None if no static list exists
"""
jinja = self.get_answer_choices_expr()
if jinja is None:
return None

parse = env.parse(jinja)
variables = meta.find_undeclared_variables(parse)
if len(variables) == 0:
rtemplate = env.from_string(jinja)
rendered_choices = rtemplate.render()
return [answer_choice.strip() for answer_choice in rendered_choices.split("|||")]
else:
return None

def apply(self, example, truncate=True, highlight_variables=False):
"""
Creates a prompt by applying this template to an example
Expand Down

0 comments on commit d7ccd0a

Please sign in to comment.