paper-llm / logging.py
Brett Fiedler
initial test commit
bfb63a8
#@title JSON log system prompt management
class LLMPrompts:
def __init__(self, log_file):
self.prompts = []
self.log_file = log_file
self.load_prompts_from_log()
def add_prompt(self, identity, prompt, temperature):
self.prompts.append({'identity': identity, 'prompt': prompt, 'temperature': temperature})
self.save_prompts_to_log()
def remove_prompt(self, identity):
self.prompts = [p for p in self.prompts if p['identity'] != identity]
self.save_prompts_to_log()
def get_prompt(self, identity):
for prompt in self.prompts:
if prompt['identity'] == identity:
return prompt['prompt']
return None
def get_temperature(self, identity):
for prompt in self.prompts:
if prompt['identity'] == identity:
return prompt['temperature']
return None
def load_prompts_from_log(self):
try:
with open(self.log_file, 'r') as file:
self.prompts = json.load(file)
except FileNotFoundError:
print("No file found. Starting from scratch.")
pass # Ignore if the log file is not found or cannot be read
def save_prompts_to_log(self):
with open(self.log_file, 'w') as file:
json.dump(self.prompts, file)
# Accessing prompts and temperatures
# print(llm_prompts.get_prompt("LLM_2")) # Output: Write a short story about a detective solving a mysterious murder case.
# print(llm_prompts.get_temperature("LLM_2")) # Output: 0.7
# Adding a new prompt with temperature
# llm_prompts.add_prompt("LLM_5", "Explain the concept of artificial intelligence.", 0.5)
# Set the filepath for the log
log_file_path = '/drive/My Drive/Colab Notebooks/llm_prompts_log.json'
sys_prompt = LLMPrompts(log_file_path)
# Print all prompts and temperatures
for prompt in sys_prompt.prompts:
print(prompt['identity'], prompt['prompt'], prompt['temperature'])
# Want to add personas? Do that here!
# sys_prompt.add_prompt('LLM_2', "")
# Log the last conversation
convo_log_file_path = '/drive/My Drive/Colab Notebooks/last_conversation.json'
# Convert list to JSON object
json_object = json.dumps(context)
# Save JSON object to a file
with open('output.json', 'w') as file:
file.write(json_object)
# Save list items as separate lines in a JSON
# with open(convo_log_file_path, 'w') as file:
# for item in context:
# file.write(json.dumps(item) + '\n')