-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprompt_manager.py
94 lines (82 loc) · 3.78 KB
/
prompt_manager.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import json
import hashlib
import os
class PromptManager:
def __init__(self, prompt_dir, logger, response_file="LLMs_responses.json", save_dir="prompts/prompt_evals"):
self.prompt_dir = prompt_dir
self.prompt_save_dir = save_dir
self.response_file = response_file
self.logger = logger
os.makedirs(self.prompt_dir, exist_ok=True)
if not os.path.exists(self.response_file):
with open(self.response_file, 'w') as f:
json.dump({}, f)
def save_prompt(self, prompt_id, prompt_content):
"""Save the static prompt content if it does not already exist."""
prompt_file = os.path.join(self.prompt_save_dir, f"{prompt_id}.json")
if os.path.exists(prompt_file):
pass
with open(prompt_file, 'w') as f:
json.dump(prompt_content, f)
def load_prompt(self, prompt_id):
"""Load a static prompt template."""
prompt_file = os.path.join(self.prompt_dir, f"{prompt_id}.json")
if not os.path.exists(prompt_file):
raise FileNotFoundError(f"Prompt file not found: {prompt_file}")
with open(prompt_file, 'r') as f:
return json.load(f)
def render_prompt(self, static_prompt, entire_doc, **dynamic_parts):
"""Render a dynamic prompt by replacing placeholders."""
if entire_doc:
# Handle the "parts" elements in the prompt
for item in static_prompt:
if "parts" in item:
item["parts"] = [
{
"text": part["text"].format(**dynamic_parts)
if "{" in part["text"] and "}" in part["text"]
else part["text"]
}
for part in item["parts"]
]
elif "content" in item:
if "{" in item["content"] and "}" in item["content"]:
item["content"] = item["content"].format(**dynamic_parts)
else:
item["content"]
return static_prompt
else:
return [
{**item, "content": item["content"].format(**dynamic_parts)}
for item in static_prompt
]
def save_response(self, prompt_id, response):
"""Save the response with prompt_id as the key. Skip if prompt_id is already responded."""
self.logger.info(f"Saving response for prompt_id: {prompt_id}")
# Load existing responses safely
with open(self.response_file, 'r') as f:
responses = json.load(f) # The file is assumed to be well-formed JSON
# Skip if the prompt_id already has a response
if prompt_id in responses:
self.logger.info(f"Prompt already responded: {prompt_id}")
return
# Add new response
responses[prompt_id] = response
# Write back safely, ensuring no leftover data
with open(self.response_file, 'w') as f:
json.dump(responses, f, indent=2)
f.truncate() # Ensure no extra data remains
def retrieve_response(self, prompt_id):
"""Retrieve a saved response based on the prompt_id."""
if not os.path.exists(self.response_file):
return None
with open(self.response_file, 'r') as f:
self.logger.debug(f"Retrieving response for prompt_id: {prompt_id}")
responses = json.load(f)
if prompt_id not in responses:
return None
return responses.get(prompt_id)
def _calculate_checksum(self, prompt):
"""Calculate checksum for a given content."""
self.logger.debug(f"Calculating checksum for content: prompt")
return hashlib.sha256(prompt.encode()).hexdigest()