-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathprompt_mgr.py
39 lines (30 loc) · 1.25 KB
/
prompt_mgr.py
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
class Prompt:
def __init__(self, file_path = None, hot_reload = True):
self.file_path = file_path
self.cached = None
self.hot_reload = hot_reload
def render(self, **kwargs) -> str:
if (self.cached is None) or (self.hot_reload):
with open(self.file_path, 'r') as f:
self.cached = f.read()
return self.cached.format(**kwargs)
def pd_render(self, row) -> str:
"""
Render into a template directly from a Pandas row.
"""
if (self.cached is None) or (self.hot_reload):
with open(self.file_path, 'r') as f:
self.cached = f.read()
return self.cached.format_map(row.to_dict())
class PromptMgr:
def __init__(self, hot_reload: bool = True, src_dir: str = 'resources/prompts'):
""" Creates a prompt manager.
Args:
hot_reload: If true, reloads the prompt every time it is called.
src_dir: The directory where the prompts are stored.
"""
self.hot_reload = hot_reload
self.src_dir = src_dir
def bind(self, prompt_id: str) -> Prompt:
return Prompt(file_path = f'{self.src_dir}/{prompt_id}.txt',
hot_reload=self.hot_reload)