| 10 | |
| 11 | |
| 12 | class Preformatter(object): |
| 13 | __slots__ = ("template", "name", "_verbose") |
| 14 | |
| 15 | def __init__(self, template_path: str = "", verbose: bool = False): |
| 16 | self._verbose = verbose |
| 17 | self.name = os.path.basename(template_path).rsplit(".", 1)[0] |
| 18 | if not os.path.exists(template_path): |
| 19 | raise ValueError(f"Can't read preformatter template json: {template_path}") |
| 20 | with open(template_path) as fp: |
| 21 | self.template = json.load(fp) |
| 22 | if self._verbose: |
| 23 | print( |
| 24 | f"Using prompt template {template_path}: {self.template['description']}" |
| 25 | ) |
| 26 | |
| 27 | def generate_prompt( |
| 28 | self, |
| 29 | instruction: str, |
| 30 | input: Union[None, str] = None, |
| 31 | label: Union[None, str] = None, |
| 32 | ) -> str: |
| 33 | # returns the full prompt from instruction and optional input |
| 34 | # if a label (=response, =output) is provided, it's also appended. |
| 35 | if input is not None: |
| 36 | res = self.template["prompt_input"].format( |
| 37 | instruction=instruction, input=input |
| 38 | ) |
| 39 | else: |
| 40 | res = self.template["prompt_no_input"].format(instruction=instruction) |
| 41 | if label: |
| 42 | res = f"{res}{label}" |
| 43 | if self._verbose: |
| 44 | print(res) |
| 45 | return res |
| 46 | |
| 47 | def get_response(self, output: str) -> str: |
| 48 | return output.split(self.template["response_split"])[1].strip() |