euroeval.metrics.ifeval.constraints¶
source module euroeval.metrics.ifeval.constraints
Constraint functions used for the IFEval metric.
Classes
-
Constraint — An instruction-following constraint.
Functions
-
register — Decorator that registers a function under the given name.
-
check_keyword_existence — Check that all keywords exist in the response.
-
check_keyword_frequency — Check keyword appears with required frequency.
-
check_forbidden_words — Check that forbidden words don't appear.
-
check_letter_frequency — Check letter appears with required frequency.
-
check_number_sentences — Check number of sentences.
-
check_number_paragraphs — Check number of paragraphs (separated by ***).
-
check_number_words — Check number of words.
-
check_nth_paragraph_first_word — Check paragraph count and first word of nth paragraph.
-
check_number_placeholders — Check minimum number of [placeholder] brackets.
-
check_postscript — Check for postscript marker.
-
check_number_bullet_lists — Check exact number of bullet points.
-
check_constrained_response_english — Check response contains one of the constrained options.
-
check_constrained_response_spanish — Check response contains one of the constrained options.
-
check_constrained_response_catalan — Check response contains one of the constrained options.
-
check_constrained_response_french — Check response contains one of the constrained options.
-
check_constrained_response_with_argument — Check response contains one of the constrained options.
-
check_number_highlighted_sections — Check minimum highlighted sections.
-
check_multiple_sections — Check for Section X markers.
-
check_json_format — Check response is valid JSON.
-
check_title — Check for <
> format. -
check_two_responses — Check for two different responses separated by **.
-
check_repeat_prompt — Check response starts with the prompt.
-
check_end_phrase — Check response ends with exact phrase.
-
check_capital_word_frequency — Check frequency of ALL CAPS words.
-
check_english_capital — Check response is English and all caps.
-
check_spanish_capital — Check response is Spanish and all caps.
-
check_french_capital — Check response is French and all caps.
-
check_catalan_capital — Check response is Catalan and all caps.
-
check_english_lowercase — Check response is English and all lowercase.
-
check_spanish_lowercase — Check response is Spanish and all lowercase.
-
check_french_lowercase — Check response is French and all lowercase.
-
check_catalan_lowercase — Check response is Catalan and all lowercase.
-
check_no_comma — Check response has no commas.
-
check_quotation — Check response is wrapped in double quotes.
-
check_response_language — Check response is in specified language.
-
check_lowercase_letters — Check response has no uppercase letters.
-
check_capital_letters — Check response has no lowercase letters.
-
check_no_digits — Check response contains no digits.
-
check_ethel_or_cedilla_not_present — Check response contains no forbidden character.
-
check_no_accents — Check response contains no accents.
-
check_accents — Check response contains accents.
-
check_informal_address — Check response contains informal address.
source class Constraint()
Bases : t.Protocol
An instruction-following constraint.
source register(name: str, **desired_keys_and_types) → c.Callable[[Constraint], Constraint]
Decorator that registers a function under the given name.
Parameters
-
name : str — The name under which to register the function.
-
**desired_keys_and_types — The keyword arguments and their types that the function expects.
Returns
-
c.Callable[[Constraint], Constraint] — The decorator function.
Raises
source check_keyword_existence(response: str, **constraint_kwargs) → bool
Check that all keywords exist in the response.
Parameters
-
response : str — The response string to be checked.
-
**constraint_kwargs — Keyword arguments containing
keywords– a list of keyword patterns (case‑insensitive) to search for.
Returns
-
bool — True if all keywords are found in the response, False otherwise.
source check_keyword_frequency(response: str, **constraint_kwargs) → bool
Check keyword appears with required frequency.
Parameters
-
response : str — The response string to check.
-
**constraint_kwargs — Keyword arguments containing
keyword,frequencyandrelation– the keyword pattern (case‑insensitive), the required frequency, and the comparison operator ("less than" or "at least").
Returns
-
bool — True if the keyword appears with the required frequency, False otherwise.
source check_forbidden_words(response: str, **constraint_kwargs) → bool
Check that forbidden words don't appear.
Parameters
-
response : str — The response string to check.
-
**constraint_kwargs — Keyword arguments containing
forbidden_words– a list of words that must not appear (case‑insensitive, whole‑word match).
Returns
-
bool — True if none of the forbidden words are found, False otherwise.
source check_letter_frequency(response: str, **constraint_kwargs) → bool
Check letter appears with required frequency.
Parameters
-
response : str — The response string to check.
-
**constraint_kwargs — Keyword arguments containing
letter,let_frequencyandlet_relation– the single character to count (case‑insensitive), the frequency threshold, and the comparison operator ("less than" or "at least").
Returns
-
bool — True if the letter frequency satisfies the relation, False otherwise.
Raises
-
InvalidBenchmark — If letter is not a single character.
source check_number_sentences(response: str, **constraint_kwargs) → bool
Check number of sentences.
Parameters
-
response : str — The response string to check.
-
**constraint_kwargs — Keyword arguments containing
num_sentencesandrelation.
Returns
-
bool — True if the sentence count satisfies the relation, False otherwise.
source check_number_paragraphs(response: str, **constraint_kwargs) → bool
Check number of paragraphs (separated by ***).
Parameters
-
response : str — The response string to check.
-
**constraint_kwargs — Keyword arguments containing
num_paragraphs– the exact number of paragraphs expected.
Returns
-
bool — True if the response contains exactly num_paragraphs non‑empty paragraphs, False otherwise.
source check_number_words(response: str, **constraint_kwargs) → bool
Check number of words.
Parameters
-
response : str — The response string to check.
-
**constraint_kwargs — Keyword arguments containing
num_wordsandrelation.
Returns
-
bool — True if the word count satisfies the relation, False otherwise.
source check_nth_paragraph_first_word(response: str, **constraint_kwargs) → bool
Check paragraph count and first word of nth paragraph.
Parameters
-
response : str — The response string to check.
-
**constraint_kwargs — Keyword arguments containing
num_paragraphs,nth_paragraph, andfirst_word– the expected first word of the nth paragraph (case‑insensitive).
Returns
-
bool — True if the response has exactly num_paragraphs paragraphs and the nth paragraph starts with first_word, False otherwise.
Raises
-
InvalidBenchmark — If the n'th paragraph is greater than the number of paragraphs in the response.
source check_number_placeholders(response: str, **constraint_kwargs) → bool
Check minimum number of [placeholder] brackets.
Parameters
-
response : str — The response string to check.
-
**constraint_kwargs — Keyword arguments containing
num_placeholders– the minimum number of placeholder brackets expected.
Returns
-
bool — True if the response contains at least num_placeholders placeholders of the form [placeholder], False otherwise.
source check_postscript(response: str, **constraint_kwargs) → bool
Check for postscript marker.
Parameters
-
response : str — The response string to check.
-
**constraint_kwargs — Keyword arguments containing
postscript_marker– the postscript label to look for (e.g. "P.S.", "P.P.S").
Returns
-
bool — True if the postscript marker is found in the response, False otherwise.
source check_number_bullet_lists(response: str, **constraint_kwargs) → bool
Check exact number of bullet points.
Parameters
-
response : str — The response string to check.
-
**constraint_kwargs — Keyword arguments containing
num_bullets– the exact number of bullet points expected.
Returns
-
bool — True if the response contains exactly num_bullets bullet points, where bullet points are lines starting with
*or-, False otherwise.
source check_constrained_response_english(response: str, **_) → bool
Check response contains one of the constrained options.
Parameters
-
response : str — The response string to check.
Returns
-
bool — True if the response contains exactly one of "My answer is yes.", "My answer is no.", or "My answer is maybe.", False otherwise.
source check_constrained_response_spanish(response: str, **_) → bool
Check response contains one of the constrained options.
Parameters
-
response : str — The response string to check.
Returns
-
bool — True if the response contains exactly one of "Mi respuesta es sí" or "Mi respuesta es no" or "Mi respuesta es tal vez", False otherwise.
source check_constrained_response_catalan(response: str, **_) → bool
Check response contains one of the constrained options.
Parameters
-
response : str — The response string to check.
Returns
-
bool — True if the response contains exactly one of "La meva resposta és sí.", "La meva resposta és no.", or "La meva resposta és potser.", False otherwise.
source check_constrained_response_french(response: str, **_) → bool
Check response contains one of the constrained options.
Parameters
-
response : str — The response string to check.
Returns
-
bool — True if the response contains exactly one of "Oui.", "Non.", or "Peut-être.", False otherwise.
source check_constrained_response_with_argument(response: str, **constraint_kwargs) → bool
Check response contains one of the constrained options.
Parameters
-
response : str — The response string to check.
-
**constraint_kwargs — Keyword arguments containing
options– a list of strings to check for.
Returns
-
bool — True if the response contains exactly one of the options, False otherwise.
source check_number_highlighted_sections(response: str, **constraint_kwargs) → bool
Check minimum highlighted sections.
Parameters
-
response : str — The response string to check.
-
**constraint_kwargs — Keyword arguments containing
num_highlights– the minimum number of highlighted sections expected.
Returns
-
bool — True if the response contains at least num_highlights non‑empty highlighted sections, where highlights are text wrapped in single (text) or double (text) asterisks, False otherwise.
source check_multiple_sections(response: str, **constraint_kwargs) → bool
Check for Section X markers.
Parameters
-
response : str — The response string to check.
-
**constraint_kwargs — Keyword arguments containing
section_spliterandnum_sections.
Returns
-
bool — True if the response contains at least num_sections sections delimited by markers of the form "
", False otherwise.
source check_json_format(response: str, **_) → bool
Check response is valid JSON.
Parameters
-
response : str — The response string to check.
Returns
-
bool — True if the response (after stripping optional
jsonfences) is valid JSON, False otherwise.
source check_title(response: str, **_) → bool
Check for <
Parameters
-
response : str — The response string to check.
Returns
-
bool — True if the response contains at least one non‑empty title wrapped in double angle brackets (e.g. <
>), False otherwise.
source check_two_responses(response: str, **_) → bool
Check for two different responses separated by **.
Parameters
-
response : str — The response string to check.
Returns
-
bool — True if the response contains exactly two non‑empty, non‑identical sections separated by "**", False otherwise.
source check_repeat_prompt(response: str, **constraint_kwargs) → bool
Check response starts with the prompt.
Parameters
-
response : str — The response string to check.
-
**constraint_kwargs — Keyword arguments containing
prompt_to_repeat– the exact prompt text the response must begin with (case‑insensitive).
Returns
-
bool — True if the response starts with prompt_to_repeat, False otherwise.
source check_end_phrase(response: str, **constraint_kwargs) → bool
Check response ends with exact phrase.
Parameters
-
response : str — The response string to check.
-
**constraint_kwargs — Keyword arguments containing
end_phrase– the exact phrase the response must end with (case‑insensitive).
Returns
-
bool — True if the response ends with end_phrase, False otherwise.
source check_capital_word_frequency(response: str, **constraint_kwargs) → bool
Check frequency of ALL CAPS words.
Parameters
-
response : str — The response string to check.
-
**constraint_kwargs — Keyword arguments containing
capital_frequencyandcapital_relation.
Returns
-
bool — True if the count of fully uppercased words satisfies the relation, False otherwise.
source check_english_capital(response: str, **_) → bool
Check response is English and all caps.
Parameters
-
response : str — The response string to check.
Returns
-
bool — True if the response is entirely uppercase and detected as English, False otherwise. Returns True if language detection fails.
source check_spanish_capital(response: str, **_) → bool
Check response is Spanish and all caps.
Parameters
-
response : str — The response string to check.
Returns
-
bool — True if the response is entirely uppercase and detected as Spanish, False otherwise. Returns True if language detection fails.
source check_french_capital(response: str, **_) → bool
Check response is French and all caps.
Parameters
-
response : str — The response string to check.
Returns
-
bool — True if the response is entirely uppercase and detected as French, False otherwise. Returns True if language detection fails.
source check_catalan_capital(response: str, **_) → bool
Check response is Catalan and all caps.
Parameters
-
response : str — The response string to check.
Returns
-
bool — True if the response is entirely uppercase and detected as Catalan, False otherwise. Returns True if language detection fails.
source check_english_lowercase(response: str, **_) → bool
Check response is English and all lowercase.
Parameters
-
response : str — The response string to check.
Returns
-
bool — True if the response is entirely lowercase and detected as English, False otherwise. Returns True if language detection fails.
source check_spanish_lowercase(response: str, **_) → bool
Check response is Spanish and all lowercase.
Parameters
-
response : str — The response string to check.
Returns
-
bool — True if the response is entirely lowercase and detected as Spanish, False otherwise. Returns True if language detection fails.
source check_french_lowercase(response: str, **_) → bool
Check response is French and all lowercase.
Parameters
-
response : str — The response string to check.
Returns
-
bool — True if the response is entirely lowercase and detected as French, False otherwise. Returns True if language detection fails.
source check_catalan_lowercase(response: str, **_) → bool
Check response is Catalan and all lowercase.
Parameters
-
response : str — The response string to check.
Returns
-
bool — True if the response is entirely lowercase and detected as Catalan, False otherwise. Returns True if language detection fails.
source check_no_comma(response: str, **_) → bool
Check response has no commas.
Parameters
-
response : str — The response string to check.
Returns
-
bool — True if the response contains no comma characters, False otherwise.
source check_quotation(response: str, **_) → bool
Check response is wrapped in double quotes.
Parameters
-
response : str — The response string to check.
Returns
-
bool — True if the response (after stripping whitespace) begins and ends with a double quote character, False otherwise.
source check_response_language(response: str, **constraint_kwargs) → bool
Check response is in specified language.
Parameters
-
response : str — The response string to check.
-
**constraint_kwargs — Keyword arguments containing
language– the language code of the language the response must be in.
Returns
-
bool — True if the response is detected as the specified language, False otherwise. Returns True if language detection fails.
source check_lowercase_letters(response: str, **_) → bool
Check response has no uppercase letters.
Parameters
-
response : str — The response string to check.
Returns
-
bool — True if the response contains no uppercase letters, False otherwise.
source check_capital_letters(response: str, **_) → bool
Check response has no lowercase letters.
Parameters
-
response : str — The response string to check.
Returns
-
bool — True if the response contains no lowercase letters, False otherwise.
source check_no_digits(response: str, **_) → bool
Check response contains no digits.
Parameters
-
response : str — The response string to check.
Returns
-
bool — True if the response contains no digits, False otherwise.
source check_ethel_or_cedilla_not_present(response: str, **constraint_kwargs) → bool
Check response contains no forbidden character.
Parameters
-
response : str — The response string to check.
-
**constraint_kwargs — Keyword arguments containing
forbidden_char– the character that is forbidden in the response (must not be present).
Returns
-
bool — True if the forbidden character is not present in the response, False otherwise.
source check_no_accents(response: str, **_) → bool
Check response contains no accents.
Parameters
-
response : str — The response string to check.
Returns
-
bool — True if the response contains no accents, False otherwise.
source check_accents(response: str, **_) → bool
Check response contains accents.
Parameters
-
response : str — The response string to check.
Returns
-
bool — True if the response contains accents, False otherwise.
source check_informal_address(response: str, **_) → bool
Check response contains informal address.
Parameters
-
response : str — The response string to check.
Returns
-
bool — True if the response contains informal address, False otherwise.