Repository navigation
Expand file tree
/
Copy pathLLM_00_module_2_Peripheral.py
More file actions
412 lines (343 loc) · 16.7 KB
/
Copy pathLLM_00_module_2_Peripheral.py
File metadata and controls
412 lines (343 loc) · 16.7 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
"""
[OpenAi-API-0_FuncBank.py]
Purpose: Correct Ask Directory or select file.
Author: Meng-Chi Ed Chen
Date: 2023-07-07
Updates:
1. 2024-03-11: Upgrade interation question to allow new topic, follow up, or the original select file.
2.
Status: Complete.
"""
import sys, os, base64, tkinter, datetime, json, requests, subprocess, re, shutil, time
from tkinter import filedialog
from openai import OpenAI
from datetime import datetime, timedelta
import pandas as pd
from tabulate import tabulate
#[1] Basic functions.
def datetime_stamp():
now = datetime.now()
return now.strftime("%Y-%m-%d_%H-%M-%S")
def datetime_filename():
now = datetime.now()
return now.strftime("%Y-%m%d-%H%M")
def read_excel_as_df(path_excel, sheet_name=None):
print('\n[read_excel_as_df]')
if sheet_name is None:
sheet_name = 0 # Assuming 0 is the index of the first sheet
df = pd.read_excel(path_excel, sheet_name=sheet_name)
print('--[dataframe headers]', df.head())
return df
#[2] Cool interaction questions.
def q0_choose_provider_and_model(path_model_excel):
print('\n[q0_choose_provider_and_model]')
#[1] Read excel as dataframe
try:
df = pd.read_excel(path_model_excel, '01_Pricing')
df['model_name'] = df['model_name'].str.strip()
truncate_api_key = lambda x: x[:10] + '...' if len(x) > 10 else x
df_cover = df.copy()
df_cover = df_cover.drop(columns=['Link'])
df_cover['api_key'] = df_cover['api_key'].apply(truncate_api_key)
print(tabulate(df_cover, headers='keys', tablefmt='orgtbl'))
except:
text_please_create_excel = \
f'\n[Create Excel with Model Information]\n'\
f'Please create an excel at the following path: {path_model_excel}\n'\
f'Name the first sheet as "01_Pricing", and create six columns with the follow name: '\
f'llm_provider, model_name, Input (USD/Mtok), Output (USD/Mtok), Link, api_key\n'\
f'Column names with underscore need to be exactly the same, and those contents must be filled, '\
f'because they will be read by code.\n\n'
print(text_please_create_excel)
message = \
f'\n[Select model]\n\
Type the index of the model that fits your purpose today.\n'
ans_model = int(input(message))
len_model = len(df)
if 0 <= ans_model or ans_model <= len_model:
llm_provider = df['llm_provider'].iloc[ans_model]
model_name = df['model_name'].iloc[ans_model]
api_key = df['api_key'].iloc[ans_model]
else:
raise ValueError('Please select valid model. System exit.')
sys.exit()
return llm_provider, model_name, api_key
def q1_new_file_or_followup(dir_questions, llm_provider, model_name, path_prompt=None):
"""
[Opening question]
"""
print('\n\n[q1_new_file_or_followup]')
dt_file = datetime_filename()
message = \
f'\n[{dt_file}] Hello, this is {model_name} from {llm_provider}. How can I help you today? \n\
(a) Type a new name for the new topic. \n\
This will create and open a new txt file named as "{dt_file}_Topic-1".\n\
(b) Type "+" to followup with the latest question.\n\
This will copy the latest txt file and create a "_n+1" version for it.\n\
(c) Type nothing (just press enter) to choose an existing text file.\n\
(d) Type "0" to start a new round.\n\n'
ans_task = input(message)
if ans_task == '+':
path_latest_txt = find_latest_txt(dir_questions)
path_txt = create_plus_one_copy_of_txt_file(path_latest_txt)
basename_txt = os.path.basename(path_txt)
print(f'--> (b) Followup the latest question: {basename_txt}')
open_txt(path_txt)
q2_let_me_know_when_question_is_ready(model_name, path_txt, path_prompt)
elif ans_task == '':
print('--> (c) Choose an existing question file.')
path_txt, _ = tkinter_select_file(dialog_title = 'Choose an existing question file')
elif ans_task == '0':
print('--> (d) Start a new round.')
script_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'LLM_07_All_Models.py')
os.system(f'start cmd /k python "{script_path}"')
sys.exit()
else:
print(f"--> (a) Let's start a new topic of {ans_task}.")
filename = dt_file + '_' + ans_task + '_1.txt'
path_txt = create_txt_file(filename, dir_questions)
open_txt(path_txt)
q2_let_me_know_when_question_is_ready(model_name, path_txt, path_prompt)
return path_txt
def q2_let_me_know_when_question_is_ready(model_name, path_txt="", path_prompt=None):
print('\n\n[q2_let_me_know_when_question_is_ready]')
#print(f'[path_prompt]: {path_prompt}. Is the path_prompt Noen? empty?{path_prompt is not None} {path_prompt != ""} ')
basename_txt = os.path.basename(path_txt)
message = f"The file {basename_txt} has been created and is now open for you.\n"
#[3] Check if path_prompt is not None
if path_prompt is not None and path_prompt != "":
basename_prompt = os.path.basename(path_prompt)
prompt_text = f"\n{read_text_from_txt(path_prompt, char_preview=0)}\n"
message += f"Here are some common prompts from {basename_prompt} that you might want to use.\n{prompt_text}"
message += "Please save and close the program once you finish compiling the question.\nPress Enter to continue.\n"
_ = input(message) # The line is used to wait for enter pressed. Do not delete.
print("The question is ready. Let's proceed.")
#[3] Peripheral functions
def remove_older_asked_questions(dir_questions, ext, name_dump_folder, day_lmt=7):
print('\n[remove_older_asked_questions]')
fc_trg, fc_top, fc_rmv = 5, 20, 10
list_file_paths = [os.path.join(dir_questions, file) for file in os.listdir(dir_questions) if file.endswith(ext)]
dir_dump = os.path.join(dir_questions, name_dump_folder)
if not os.path.exists(dir_dump):
os.makedirs(dir_dump)
cnt_ext_file = len(list_file_paths)
print(f'--There are {cnt_ext_file} files with extension {ext}.')
#[2] Trigger file removal.
if cnt_ext_file > fc_trg:
#[3] Sort the files by date
file_date_map = {}
for file_path in list_file_paths:
file_date = os.path.getmtime(file_path)
file_date_map[file_path] = datetime.fromtimestamp(file_date)
sorted_files = sorted(file_date_map.items(), key=lambda x: x[1])
#[4] If there's more than "fc_top" files, remove oldest "fc_rmv" files at once.
if cnt_ext_file > fc_top:
num_files_to_remove = cnt_ext_file - fc_rmv
for i in range(num_files_to_remove):
file_path, _ = sorted_files[i]
shutil.move(file_path, dir_dump)
print(f'--File removed: {os.path.basename(file_path)}')
#[5] Or, just remove files older than "day_lmt" days
else:
current_date = datetime.now()
for file_path, file_date in sorted_files:
if current_date - file_date > timedelta(days=day_lmt):
final_path = os.path.join(dir_dump, os.path.basename(file_path))
if os.path.exists(final_path):
os.remove(final_path)
shutil.move(file_path, dir_dump)
print(f'--File removed: {os.path.basename(file_path)}')
#[6] Get the size of a single directory dir_dump
dump_size_kB = round(get_directory_size(dir_dump)/1024)
print(f'--Current size of the dump folder {name_dump_folder} in kB: {dump_size_kB}.\n\n')
def get_directory_size(directory):
total_size = 0
for dirpath, _, filenames in os.walk(directory):
for filename in filenames:
filepath = os.path.join(dirpath, filename)
total_size += os.path.getsize(filepath)
return total_size
def filter_file_with_keywords(dir_folder, list_keywords_select=None, list_keywords_discard=None):
print('\n[filter_file_with_keywords]')
if list_keywords_select is None:
list_keywords_select = []
if list_keywords_discard is None:
list_keywords_discard = []
# Check if directory exists
if not os.path.isdir(dir_folder):
raise FileNotFoundError(f"The directory '{dir_folder}' was not found.")
sys.exit(1)
# Get list of files in the directory
list_file_names = [file for file in os.listdir(dir_folder)
if os.path.isfile(os.path.join(dir_folder, file))]
# Filter names by keywords to select
if list_keywords_select:
list_file_names = [file for file in list_file_names
if any(keyword in file for keyword in list_keywords_select)]
# Filter names by keywords to discard
if list_keywords_discard:
list_file_names = [file for file in list_file_names
if not any(keyword in file for keyword in list_keywords_discard)]
# Generate file paths
list_file_paths = [os.path.join(dir_folder, file) for file in list_file_names]
# Print result
print('--list_file_names:', list_file_names, end='\n\n')
return list_file_paths, list_file_names
def find_latest_txt(dir=None):
print('\n[find_latest_txt]')
if dir is None:
dir = os.path.dirname(os.path.abspath(__file__))
os.makedirs(dir, exist_ok=True) # Ensure the directory exists.
# Assuming filter_file_with_keywords returns a tuple of (list_of_full_paths, list_of_filenames)
list_file_paths, list_file_names = filter_file_with_keywords(dir, list_keywords_select='.txt')
# Now work with the full paths directly
# Sort full paths by creation time, newest first
list_file_paths.sort(key=lambda file_path: os.path.getctime(file_path), reverse=True)
# Check if there are any .txt files
if list_file_paths:
# Get the most recently created file's path
path_latest_txt = list_file_paths[0] # Take the first element as it's the newest.
print('--Latest .txt file: ', os.path.basename(path_latest_txt))
else:
print('--No .txt files found. Program exit.')
path_latest_txt = None
sys.esit()
return path_latest_txt
def create_plus_one_copy_of_txt_file(path_txt):
"""
Increments the numerical part of a filename before the '.txt' extension.
If the file is named 'example_1.txt', it will be changed to 'example_2.txt'.
If no numerical part is found, '_1' will be added before the extension.
Save a new cope of path_txt with a new filename in the same dir.
"""
print('\n[create_plus_one_copy_of_txt_file]')
# Regular expression to find the "_{n}.txt" part of the filename
pattern = re.compile(r"(_(\d+))\.txt$")
# Search for the pattern in the filename
match = pattern.search(path_txt)
if match:
# Extract the full matched pattern (_{n}) and the numerical part ({n})
full_match, num_part = match.groups()
# Increment the number
num_incremented = int(num_part) + 1
# Replace the original numerical part with the incremented value
path_new_txt = path_txt.replace(full_match, f"_{num_incremented}")
else:
# If no numerical part is found, add '_1' before the '.txt' extension
path_new_txt = path_txt.replace(".txt", "_1.txt")
#[] Copy and append opening.
shutil.copy(path_txt, path_new_txt)
text_to_append = 'User:\n\n'
append_text_to_txt(path_new_txt, text_to_append, 0)
print(f'--File created at: {path_new_txt}')
return path_new_txt
def create_txt_file(path_txt, dir=None):
"""
Create an empty .txt file at the specified directory (or in the current directory if not specified).
Then open the txt file with the default system application for .txt files, and return the path to that file.
"""
print('\n[create_txt_file]')
if dir is None:
dir = os.path.dirname(os.path.abspath(__file__))
os.makedirs(dir, exist_ok=True) # Ensure the directory exists.
path_txt = os.path.join(dir, path_txt) # Create the full path for the file.
# Create the file if it doesn't exist.
with open(path_txt, 'w') as file:
pass # Just create the file, no need to write anything.
print(f'--File created at: {path_txt}')
return path_txt
def open_txt(path_txt):
print('\n[open_txt]')
# Open the file with the default application.
if os.name == 'nt': # For Windows
os.startfile(path_txt)
elif os.name == 'posix': # For MacOS
subprocess.run(['open', path_txt])
else: # For Linux (xdg-open should work for most environments)
subprocess.run(['xdg-open', path_txt])
return path_txt
def tkinter_select_file(dialog_title='Select a file'):
print('\n[tkinter_select_file]')
root = tkinter.Tk() # Creat a window (or container) for tkinter GUI (Graphical User Interface) toolkit
root.withdraw() # Hide the main window since we don't need to see it.
try:
file_path = filedialog.askopenfilename(title=dialog_title)
if file_path:
file_basename = os.path.basename(file_path)
file_path = os.path.normpath(file_path) # Clean the path
print(f'--file_path: {file_path}\n\n')
return file_path, file_basename
else:
print('--No file selected. Exit code.')
sys.exit()
except Exception as e:
print(f'--An error occurred: {e}')
return None, None
finally:
root.destroy() # Release resources.
#[4] Reading, saving, and handling txt files.
#[4-1] Read question from .txt file.
def read_text_from_txt(path_txt, char_preview=0):
time.sleep(0.5)
try:
with open(path_txt, 'r') as file:
content = file.read()
if char_preview > 0:
preview = content[:char_preview].replace('\n', '\n >> ')
print(f'[read_text_from_txt] content quick view:\n >> {preview}')
word_count = len(content.replace('\n', ' ').replace('\t', ' ').split())
print(f'--Approximate word count: {word_count}\n')
return content
except FileNotFoundError:
print(f'--File not found:\n {path_txt}')
sys.exit()
except Exception as e:
print(f'--An error occurred while reading the file:\n {str(e)}')
sys.exit()
#[4-2] Append reply to .txt file and save.
def append_text_to_txt(path_txt, text_to_append, char_preview): # This code will not rename the file.
#[] Check if the path_txt exists
if not os.path.exists(path_txt):
print(f'[append_text_to_txt] File not found:\n {path_txt}')
sys.exit()
#[] Get current datetime and create opening_of_append.
current_datetime = datetime_stamp()
opening_of_append = '\n\n--\n[' + current_datetime + ']\n'
try:
with open(path_txt, 'a') as file: #a = append, w = write
file.write(opening_of_append)
file.write(text_to_append)
preview = text_to_append[:char_preview].replace('\n', '\n >> ')
print(f'[read_text_from_txt] content quick view:\n >> {preview}')
word_count = len(text_to_append.replace('\n', ' ').replace('\t', ' ').split())
print(f'--Approximate word count: {word_count}\n')
file.close()
except Exception as e:
print(f'[append_text_to_txt] An error occurred while reading the file:\n {str(e)}')
sys.exit()
# This function is no longer in use.
def do_you_want_to_open_txt(message, path_txt):
message = message + ' [Y/N]'
ans_open = input(message)
ans_open = ans_open.lower()
if ('yes' in ans_open) or ('ye' in ans_open) or ('y' in ans_open) or ('ok' in ans_open) \
or ('sure' in ans_open) or ('go' in ans_open):
print('User agreed to proceed.', ans_open)
try:
# Attempt to open the text file
with open(path_txt, 'r') as file:
print(f"Contents of {path_txt}:\n")
print(file.read())
# Exit the program after displaying the file content
sys.exit()
except FileNotFoundError:
print(f"The file at {path_txt} was not found.")
sys.exit(1)
except Exception as e:
print(f"An error occurred: {e}")
sys.exit(1)
elif ('no' in ans_open) or ('n' in ans_open):
print('User chose to stop.', ans_open)
sys.exit()
else:
print('[Invalid Input] I will just keep moving on.\n', ans_open)