Upload 2 files
Browse files- utils/__init__.py +0 -0
- utils/prompter.py +51 -0
utils/__init__.py
ADDED
|
File without changes
|
utils/prompter.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
A dedicated helper to manage templates and prompt building.
|
| 3 |
+
"""
|
| 4 |
+
|
| 5 |
+
import json
|
| 6 |
+
import os.path as osp
|
| 7 |
+
from typing import Union
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
class Prompter(object):
|
| 11 |
+
__slots__ = ("template", "_verbose")
|
| 12 |
+
|
| 13 |
+
def __init__(self, template_name: str = "", verbose: bool = False):
|
| 14 |
+
self._verbose = verbose
|
| 15 |
+
if not template_name:
|
| 16 |
+
# Enforce the default here, so the constructor can be called with '' and will not break.
|
| 17 |
+
template_name = "alpaca"
|
| 18 |
+
file_name = osp.join("templates", f"{template_name}.json")
|
| 19 |
+
if not osp.exists(file_name):
|
| 20 |
+
raise ValueError(f"Can't read {file_name}")
|
| 21 |
+
with open(file_name) as fp:
|
| 22 |
+
self.template = json.load(fp)
|
| 23 |
+
if self._verbose:
|
| 24 |
+
print(
|
| 25 |
+
f"Using prompt template {template_name}: {self.template['description']}"
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
def generate_prompt(
|
| 29 |
+
self,
|
| 30 |
+
instruction: str,
|
| 31 |
+
input: Union[None, str] = None,
|
| 32 |
+
label: Union[None, str] = None,
|
| 33 |
+
) -> str:
|
| 34 |
+
# returns the full prompt from instruction and optional input
|
| 35 |
+
# if a label (=response, =output) is provided, it's also appended.
|
| 36 |
+
if input:
|
| 37 |
+
res = self.template["prompt_input"].format(
|
| 38 |
+
instruction=instruction, input=input
|
| 39 |
+
)
|
| 40 |
+
else:
|
| 41 |
+
res = self.template["prompt_no_input"].format(
|
| 42 |
+
instruction=instruction
|
| 43 |
+
)
|
| 44 |
+
if label:
|
| 45 |
+
res = f"{res}{label}"
|
| 46 |
+
if self._verbose:
|
| 47 |
+
print(res)
|
| 48 |
+
return res
|
| 49 |
+
|
| 50 |
+
def get_response(self, output: str) -> str:
|
| 51 |
+
return output.split(self.template["response_split"])[1].strip()
|