-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlogger.py
73 lines (49 loc) · 2.26 KB
/
logger.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
import os
def create_log_file(log_path: str, user_id: int) -> None:
'''
Creates a log file for a user if it does not already exist.
Args:
* log_path (str): The path to the directory where the log files are stored.
* user (int): The ID of the user.
'''
if not os.path.exists(f'{log_path}/{user_id}'):
print(f'User with ID: {user_id} does not have a log file created. Creating.')
os.makedirs(f'{log_path}/{user_id}')
def append_log(log_path: str, user_id: int, prompt: str) -> None:
'''
Appends a log entry to the log file.
Args:
* log_path (str): The path to the directory where the log files are stored.
* user (int): The ID of the user.
* prompt (str): The user's prompt to be written to the log file.
'''
with open(f'{log_path}/{user_id}/{user_id}.txt', 'a') as f:
f.write(f'{user_id} wrote: "{prompt}"')
def append_token_log(log_path: str, user_id: int, n_token: int) -> None:
'''
Appends number of tokens being used to the log file.
Args:
* log_path (str): The path to the directory where the log files are stored.
* user_id (int): The ID of the user.
* token (int): The number of the token being used.
'''
with open(f'{log_path}/{user_id}/{user_id}.txt', 'a') as f:
f.write(f' using {n_token} tokens.')
def append_warning_log(log_path: str, user_id: int) -> None:
'''
Appends a warning message to the log file indicating that the user's content was flagged by the OpenAI Moderation API.
Args:
* log_path (str): The path to the directory where the log files are stored.
* user_id (int): The ID of the user.
'''
with open(f'{log_path}/{user_id}/{user_id}.txt', 'a') as f:
f.write(f' and was flagged by the OpenAI Moderation API!')
def append_newline_log(log_path: str, user_id: int) -> None:
'''
Appends a newline character to the log file.
Args:
* log_path (str): The path to the directory where the log files are stored.
* user_id (int): The ID of the user.
'''
with open(f'{log_path}/{user_id}/{user_id}.txt', 'a') as f:
f.write(f'\n')