Upload formats.py with huggingface_hub
Browse files12310 unmodified lines14151617181920212284 unmodified lines1071081091101111121138 unmodified lines12212312412512612712814 unmodified lines143144145146147from typing import (Any,Dict,10 unmodified linespassclass SystemFormat(Format):r"""Generates the whole input to the model, from constant strings that are given as args, and from values found in specified fields of the instance.SystemFormat expects the input instance to contain:1. A field named "system_prompt" whose value is a string (potentially empty) that delivers a task independent opening text.2. A field named "source" whose value is a string verbalizing the original values in the instance (as read84 unmodified linesinstance=instance, field_name="system_prompt")# pop "system_prompt", "instruction", and "target_prefix" from instanceif "target_prefix" in instance:instance.pop("target_prefix")if "instruction" in instance:8 unmodified linesdemos is not None and isoftype(demos, List[Dict[str, Any]])), f"A list of dict-s is expected in field '{self.demos_field}'. Received instance: {instance}"demo_instances = demos# pop demos from instanceinstance.pop(self.demos_field)demos_string = ""14 unmodified linestarget_prefix=target_prefix,**self.format_args,)instance["source"] = outputreturn instance123410 unmodified lines15161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646584 unmodified lines1501511521531541558 unmodified lines16416516616716816914 unmodified lines184185186187188189import refrom typing import (Any,Dict,10 unmodified linespassdef apply_capital_new_line_notation(text: str) -> str:r"""Transforms a given string by applying the Capital New Line Notation.The Capital New Line Notation (\N) is designed to manage newline behavior in a string efficiently.This custom notation aims to consolidate multiple newline characters (\n) into a single newline underspecific conditions, with tailored handling based on whether there's preceding text. The functiondistinguishes between two primary scenarios:1. If there's text (referred to as a prefix) followed by any number of \n characters and then one ormore \N, the entire sequence is replaced with a single \n. This effectively simplifies multiplenewlines and notation characters into a single newline when there's preceding text.2. If the string starts with \n characters followed by \N without any text before this sequence, or if\N is at the very beginning of the string, the sequence is completely removed. This case isapplicable when the notation should not introduce any newlines due to the absence of preceding text.Args:text (str): The input string to be transformed, potentially containing the Capital New Line Notation(\N) mixed with actual newline characters (\n).Returns:str: The string after applying the Capital New Line Notation rules, which either consolidates multiplenewlines and notation characters into a single newline when text precedes them, or removes thenotation and any preceding newlines entirely if no text is present before the notation.Examples:>>> apply_capital_new_line_notation("Hello World\\n\\n\N")'Hello World\\n'>>> apply_capital_new_line_notation("\\n\\n\NGoodbye World")'Goodbye World'>>> apply_capital_new_line_notation("\N")''"""# If sequence of \N or \n that ends with \N has no characters before delete ittext = re.sub(r"^(?:\n|\\N)*\\N", "", text)# Replace every sequence of \N or \n that ends with \N with \nreturn re.sub(r"[\n(\\N)]*(\\N)+", r"\n", text)class SystemFormat(Format):r"""Generates the whole input to the model, from constant strings that are given as args, and from values found in specified fields of the instance.Important: formats can use '\N' notations that means new-line if no new-line before and no empty string before.SystemFormat expects the input instance to contain:1. A field named "system_prompt" whose value is a string (potentially empty) that delivers a task independent opening text.2. A field named "source" whose value is a string verbalizing the original values in the instance (as read84 unmodified linesinstance=instance, field_name="system_prompt")if "target_prefix" in instance:instance.pop("target_prefix")if "instruction" in instance:8 unmodified linesdemos is not None and isoftype(demos, List[Dict[str, Any]])), f"A list of dict-s is expected in field '{self.demos_field}'. Received instance: {instance}"demo_instances = demosinstance.pop(self.demos_field)demos_string = ""14 unmodified linestarget_prefix=target_prefix,**self.format_args,)output = apply_capital_new_line_notation(output)instance["source"] = outputreturn instance
formats.py+44-2