google-research
176 строк · 7.1 Кб
1# coding=utf-8
2# Copyright 2024 The Google Research Authors.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16"""Registry of all instructions."""
17from instruction_following_eval import instructions18
19_KEYWORD = "keywords:"20
21_LANGUAGE = "language:"22
23_LENGTH = "length_constraints:"24
25_CONTENT = "detectable_content:"26
27_FORMAT = "detectable_format:"28
29_MULTITURN = "multi-turn:"30
31_COMBINATION = "combination:"32
33_STARTEND = "startend:"34
35_CHANGE_CASES = "change_case:"36
37_PUNCTUATION = "punctuation:"38
39INSTRUCTION_DICT = {40_KEYWORD + "existence": instructions.KeywordChecker,41_KEYWORD + "frequency": instructions.KeywordFrequencyChecker,42# TODO(jeffreyzhou): make a proper set of sentences to choose from43# _KEYWORD + "key_sentences": instructions.KeySentenceChecker,44_KEYWORD + "forbidden_words": instructions.ForbiddenWords,45_KEYWORD + "letter_frequency": instructions.LetterFrequencyChecker,46_LANGUAGE + "response_language": instructions.ResponseLanguageChecker,47_LENGTH + "number_sentences": instructions.NumberOfSentences,48_LENGTH + "number_paragraphs": instructions.ParagraphChecker,49_LENGTH + "number_words": instructions.NumberOfWords,50_LENGTH + "nth_paragraph_first_word": instructions.ParagraphFirstWordCheck,51_CONTENT + "number_placeholders": instructions.PlaceholderChecker,52_CONTENT + "postscript": instructions.PostscriptChecker,53_FORMAT + "number_bullet_lists": instructions.BulletListChecker,54# TODO(jeffreyzhou): Pre-create paragraph or use prompt to replace55# _CONTENT + "rephrase_paragraph": instructions.RephraseParagraph,56_FORMAT + "constrained_response": instructions.ConstrainedResponseChecker,57_FORMAT + "number_highlighted_sections": (58instructions.HighlightSectionChecker),59_FORMAT + "multiple_sections": instructions.SectionChecker,60# TODO(tianjianlu): Re-enable rephrasing with preprocessing the message.61# _FORMAT + "rephrase": instructions.RephraseChecker,62_FORMAT + "json_format": instructions.JsonFormat,63_FORMAT + "title": instructions.TitleChecker,64# TODO(tianjianlu): Re-enable with specific prompts.65# _MULTITURN + "constrained_start": instructions.ConstrainedStartChecker,66_COMBINATION + "two_responses": instructions.TwoResponsesChecker,67_COMBINATION + "repeat_prompt": instructions.RepeatPromptThenAnswer,68_STARTEND + "end_checker": instructions.EndChecker,69_CHANGE_CASES
70+ "capital_word_frequency": instructions.CapitalWordFrequencyChecker,71_CHANGE_CASES
72+ "english_capital": instructions.CapitalLettersEnglishChecker,73_CHANGE_CASES
74+ "english_lowercase": instructions.LowercaseLettersEnglishChecker,75_PUNCTUATION + "no_comma": instructions.CommaChecker,76_STARTEND + "quotation": instructions.QuotationChecker,77}
78
79INSTRUCTION_CONFLICTS = {80_KEYWORD + "existence": {_KEYWORD + "existence"},81_KEYWORD + "frequency": {_KEYWORD + "frequency"},82# TODO(jeffreyzhou): make a proper set of sentences to choose from83# _KEYWORD + "key_sentences": instructions.KeySentenceChecker,84_KEYWORD + "forbidden_words": {_KEYWORD + "forbidden_words"},85_KEYWORD + "letter_frequency": {_KEYWORD + "letter_frequency"},86_LANGUAGE
87+ "response_language": {88_LANGUAGE + "response_language",89_FORMAT + "multiple_sections",90_KEYWORD + "existence",91_KEYWORD + "frequency",92_KEYWORD + "forbidden_words",93_STARTEND + "end_checker",94_CHANGE_CASES + "english_capital",95_CHANGE_CASES + "english_lowercase",96},97_LENGTH + "number_sentences": {_LENGTH + "number_sentences"},98_LENGTH + "number_paragraphs": {99_LENGTH + "number_paragraphs",100_LENGTH + "nth_paragraph_first_word",101_LENGTH + "number_sentences",102_LENGTH + "nth_paragraph_first_word",103},104_LENGTH + "number_words": {_LENGTH + "number_words"},105_LENGTH + "nth_paragraph_first_word": {106_LENGTH + "nth_paragraph_first_word",107_LENGTH + "number_paragraphs",108},109_CONTENT + "number_placeholders": {_CONTENT + "number_placeholders"},110_CONTENT + "postscript": {_CONTENT + "postscript"},111_FORMAT + "number_bullet_lists": {_FORMAT + "number_bullet_lists"},112# TODO(jeffreyzhou): Pre-create paragraph or use prompt to replace113# _CONTENT + "rephrase_paragraph": instructions.RephraseParagraph,114_FORMAT + "constrained_response": set(INSTRUCTION_DICT.keys()),115_FORMAT
116+ "number_highlighted_sections": {_FORMAT + "number_highlighted_sections"},117_FORMAT
118+ "multiple_sections": {119_FORMAT + "multiple_sections",120_LANGUAGE + "response_language",121_FORMAT + "number_highlighted_sections",122},123# TODO(tianjianlu): Re-enable rephrasing with preprocessing the message.124# _FORMAT + "rephrase": instructions.RephraseChecker,125_FORMAT
126+ "json_format": set(INSTRUCTION_DICT.keys()).difference(127{_KEYWORD + "forbidden_words", _KEYWORD + "existence"}128),129_FORMAT + "title": {_FORMAT + "title"},130# TODO(tianjianlu): Re-enable with specific prompts.131# _MULTITURN + "constrained_start": instructions.ConstrainedStartChecker,132_COMBINATION
133+ "two_responses": set(INSTRUCTION_DICT.keys()).difference({134_KEYWORD + "forbidden_words",135_KEYWORD + "existence",136_LANGUAGE + "response_language",137_FORMAT + "title",138_PUNCTUATION + "no_comma"139}),140_COMBINATION + "repeat_prompt": set(INSTRUCTION_DICT.keys()).difference({141_KEYWORD + "existence",142_FORMAT + "title",143_PUNCTUATION + "no_comma"144}),145_STARTEND + "end_checker": {_STARTEND + "end_checker"},146_CHANGE_CASES + "capital_word_frequency": {147_CHANGE_CASES + "capital_word_frequency",148_CHANGE_CASES + "english_lowercase",149_CHANGE_CASES + "english_capital",150},151_CHANGE_CASES + "english_capital": {_CHANGE_CASES + "english_capital"},152_CHANGE_CASES + "english_lowercase": {153_CHANGE_CASES + "english_lowercase",154_CHANGE_CASES + "english_capital",155},156_PUNCTUATION + "no_comma": {_PUNCTUATION + "no_comma"},157_STARTEND + "quotation": {_STARTEND + "quotation", _FORMAT + "title"},158}
159
160
161def conflict_make(conflicts):162"""Makes sure if A conflicts with B, B will conflict with A.163
164Args:
165conflicts: Dictionary of potential conflicts where key is instruction id
166and value is set of instruction ids that it conflicts with.
167
168Returns:
169Revised version of the dictionary. All instructions conflict with
170themselves. If A conflicts with B, B will conflict with A.
171"""
172for key in conflicts:173for k in conflicts[key]:174conflicts[k].add(key)175conflicts[key].add(key)176return conflicts177