-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
25 lines (20 loc) · 875 Bytes
/
utils.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
import os
import csv
from datetime import datetime
def log_to_csv(question, answer):
log_dir, log_file = "local_chat_history", "qa_log.csv"
# Ensure log directory exists, create if not
if not os.path.exists(log_dir):
os.makedirs(log_dir)
# Construct the full file path
log_path = os.path.join(log_dir, log_file)
# Check if file exists, if not create and write headers
if not os.path.isfile(log_path):
with open(log_path, mode='w', newline='', encoding='utf-8') as file:
writer = csv.writer(file)
writer.writerow(["timestamp", "question", "answer"])
# Append the log entry
with open(log_path, mode='a', newline='', encoding='utf-8') as file:
writer = csv.writer(file)
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
writer.writerow([timestamp, question, answer])