-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprompter.py
More file actions
52 lines (44 loc) · 1.49 KB
/
prompter.py
File metadata and controls
52 lines (44 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import json
import os
from typing import Union, Optional
class Prompter(object):
def __init__(self, template_name: Optional[str] = None, verbose: bool = False) -> None:
self._verbose = verbose
if not template_name:
template_name = "alpaca"
file_name = os.path.join("tools/prompt_template", f"{template_name}.json")
if os.path.exists(file_name):
with open(file=file_name) as json_file:
self.template = json.load(json_file)
else:
raise FileNotFoundError(f"Can't open {file_name}")
if self._verbose:
print(
f'Using prompt template {template_name}: {self.template["description"]}'
)
def generate_prompt(
self,
instruction: str,
input: Optional[str] = None,
label: Optional[str] = None
):
if input:
res = self.template["prompt_input"].format(
instruction = instruction,
input = input
)
else:
res = self.template["prompt_no_input"].format(
instruction = instruction
)
if label:
res = f"{res}{label}"
if self._verbose:
print(res)
return res
def get_response(self, output: str):
'''
**__Args:__**
- output: output response from LLMs
'''
return output.split(self.template["response_split"])[1].strip()