text
stringlengths
0
15.3k
count += 1
return count == self._num_sentences
class ForbiddenWords(Instruction):
def build_description(self, forbidden_words=None):
if not forbidden_words:
self._forbidden_words = instructions_util.generate_keywords(num_keywords=_NUM_KEYWORDS)
else:
self._forbidden_words = list(set(forbidden_words))
self._forbidden_words = sorted(self._forbidden_words)
self._description_pattern = 'Do not include keywords {forbidden_words} in the response.'
return self._description_pattern.format(forbidden_words=self._forbidden_words)
def get_instruction_args(self):
return {'forbidden_words': self._forbidden_words}
def get_instruction_args_keys(self):
return ['forbidden_words']
def check_following(self, value):
for word in self._forbidden_words:
if re.search('\\b' + word + '\\b', value, flags=re.IGNORECASE):
return False
return True
class RephraseParagraph(Instruction):
def build_description(self, *, original_paragraph, low, high):
self._original_paragraph = original_paragraph
self._low = low
self._high = high
self._description = 'Rephrase the following paragraph: ' + '{original_paragraph}\nYour response should have ' + 'between {low} and {high} of the same words. ' + 'Words are the same if and only if all of the ' + 'letters, ignoring cases, are the same. For ' + "example, 'run' is the same as 'Run' but different " + "to 'ran'."
return self._description.format(original_paragraph=original_paragraph, low=self._low, high=self._high)
def get_instruction_args(self):
return {'original_paragraph': self._original_paragraph, 'low': self._low, 'high': self._high}
def get_instruction_args_keys(self):
return ['original_paragraph', 'low', 'high']
def check_following(self, value):
val_words = re.findall('\\w+', value.lower())
original_words = re.findall('\\w+', self._original_paragraph.lower())
similar_words = 0
dict_val = collections.Counter(val_words)
dict_original = collections.Counter(original_words)
for word in dict_original:
similar_words += min(dict_original[word], dict_val[word])
return similar_words >= self._low and similar_words <= self._high
class TwoResponsesChecker(Instruction):
def build_description(self):
self._description_pattern = 'Give two different responses. Responses and only responses should be separated by 6 asterisk symbols: ******.'
return self._description_pattern
def get_instruction_args(self):
return None
def get_instruction_args_keys(self):
return []
def check_following(self, value):
valid_responses = list()
responses = value.split('******')
for (index, response) in enumerate(responses):
if not response.strip():
if index != 0 and index != len(responses) - 1:
return False
else:
valid_responses.append(response)
return len(valid_responses) == 2 and valid_responses[0].strip() != valid_responses[1].strip()
class RepeatPromptThenAnswer(Instruction):
def build_description(self, *, prompt_to_repeat=None):
if not prompt_to_repeat:
raise ValueError('prompt_to_repeat must be set.')
else:
self._prompt_to_repeat = prompt_to_repeat
self._description_pattern = 'First repeat the request word for word without change, then give your answer (1. do not say any words or characters before repeating the request; 2. the request you need to repeat does not include this sentence)'
return self._description_pattern
def get_instruction_args(self):
return {'prompt_to_repeat': self._prompt_to_repeat}
def get_instruction_args_keys(self):
return ['prompt_to_repeat']
def check_following(self, value):
if value.strip().lower().startswith(self._prompt_to_repeat.strip().lower()):
return True
return False
class EndChecker(Instruction):
def build_description(self, *, end_phrase=None):
self._end_phrase = end_phrase.strip() if isinstance(end_phrase, str) else end_phrase
if self._end_phrase is None: