-
Notifications
You must be signed in to change notification settings - Fork 1
/
termbot.py
executable file
·354 lines (287 loc) · 12.5 KB
/
termbot.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
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
#!/usr/bin/env python3
from modules.termbot_banner import \
colored_ascii_art, \
GRAY, LIGHT_BLUE, PINK, RESET
from modules.termbot_data import termbot_usage_examples
import os
import json
import time
import re
import sys
import math
import argparse
import numpy as np
from dotenv import dotenv_values, load_dotenv
from openai import OpenAI
from groq import Groq
# VARIABLE ZONE: CHANGE THIS ACCORDING TO YOUR NEEDS:
groq_model = "mixtral-8x7b-32768"
addons_path = "context"
# ----------------------------------
Verbose = False # Default Value
# ARGUMENT PARSER
# Create the parser
parser = argparse.ArgumentParser(description='Termbot\'s Help:')
# Parse arguments
parser.add_argument('--interactive', '-i', nargs='?', const=True, default=None, help='Interactive mode')
parser.add_argument('--groq', nargs='?', const=True, default=None, help='Use Groq API instead of OpenAI (defaults to GPT-4)')
parser.add_argument('--slim', '-s', nargs='?', const=True, default=None, help='Enable slim mode')
parser.add_argument('--prompt', '-p', help='Enter prompt mode')
parser.add_argument('--context', '-c', help='Use a given custom Context file. DO NOT USE \"PROMPT MODE\"')
parser.add_argument('--outfile', '-o', help='Send the raw output from GPT to a new specified file')
parser.add_argument('--verbose', '-v', action='store_true', help='Add some verbosity')
parser.add_argument('--list', '-l', action='store_true', help='List available contexts')
parser.add_argument('--examples', '-e', action='store_true', help="Print some example usage")
# Argument Parsing
args = parser.parse_args()
load_dotenv()
if not args.groq:
OPENAI_MODEL = "gpt-4o"
OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY")
# OpenAI API Setup:
llm_client = OpenAI(
api_key=OPENAI_API_KEY,
)
else:
GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
# OpenAI API Setup:
llm_client = Groq(
api_key=GROQ_API_KEY,
)
chat_history = []
def print_verbosity(filename, tokens_used, input_tokens, output_tokens, cost, execution_time):
try:
if len(filename) > 1:
print((
f"{GRAY}[i] Tokens used: {tokens_used:<6}\n"
f" | Prompt: {input_tokens:<6}\n"
f" | Completion: {output_tokens:<6}\n"
f"[i] Model: {OPENAI_MODEL}\n"
f"[i] Cost: ${cost:>8}\n"
f"[i] Execution time: {execution_time:>6}{RESET}"
))
else:
print((
f"{GRAY}[i] Tokens used: {tokens_used:<6}\n"
f" | Prompt: {input_tokens:<6}\n"
f" | Completion: {output_tokens:<6}\n"
f"[i] Model: {OPENAI_MODEL}\n"
f"[i] Cost: ${cost:>8}\n"
f"[i] Execution time: {execution_time:>6}{RESET}"
))
except:
pass
def calculate_prompt_cost(MODEL):
# Making a rough estimation of the token cost
# Note: This doesn't take into account the diff. between input/output cost differences
GPT_4_COST = 0.06 / 1000
return GPT_4_COST
def _list_available_contexts(addons_path) -> list: # WORK IN PROGRESS!!
# List available Termbot's Contexts
# This joins the absolute path of the contexts folder instead of trying to load them from the user's PWD context:
script_dir = os.path.dirname(os.path.abspath(__file__))
addons_path = os.path.join(script_dir, addons_path)
files = []
file_paths = []
for filename in os.listdir(addons_path):
files.append(filename)
return files
# Extract the selected context at execution
def _get_context(selected_context, addons_path):
# This joins the absolute path of the contexts folder instead of trying to load them from the user's PWD context:
script_dir = os.path.dirname(os.path.abspath(__file__))
addons_path = os.path.join(script_dir, addons_path)
files = []
file_paths = []
for filename in os.listdir(addons_path):
files.append(filename)
file_path = os.path.join(addons_path, filename)
file_paths.append(file_path)
if selected_context in files:
selected_context = addons_path + "/" + selected_context
with open(selected_context, "r", encoding="utf-8") as context_file:
context = context_file.read()
return context
else:
print(f"[!] Context \"{selected_context}\" does not exist")
sys.exit(1)
def prepare_response(openai_response,start_time,filename=None, Verbose=False):
# This function receives the API's response and extracts desired data
res = openai_response
role = res.choices[0].message.role
completion_tokens = res.usage.completion_tokens
prompt_tokens = res.usage.prompt_tokens
total_tokens_used = res.usage.total_tokens
content = res.choices[0].message.content
chat_history.append(content)
# Print out GPT's chat response in desired format
print(f"{LIGHT_BLUE}{content}{RESET}")
# Write Chat Response into a new file if desired
if args.outfile:
with open(args.outfile, 'w', encoding='utf-8') as outFile:
outFile.write(content)
# Calculate and print out cost based on total tokens used
if not args.groq:
cost_per_token = calculate_prompt_cost(OPENAI_MODEL)
total_cost = total_tokens_used * cost_per_token
else:
total_cost = 0
end_time = time.time()
execution_time = end_time - start_time
cost = f'{total_cost:.4f}'
if isinstance(execution_time, str):
execution_time = float(execution_time)
execution_time = f"{execution_time:.2f} seconds"
cost = f'{total_cost:.4f}'
execution_time = f'{execution_time:.2f} seconds'
if Verbose:
print_verbosity(filename, total_tokens_used, prompt_tokens, completion_tokens, cost, execution_time)
def chatter(msg, start_time, mood, filename=None, Verbose=False):
# Prepare data and send to OpenAI API
FILE_CONTENTS = ""
# If /file: was provided in the prompt
if filename is not None:
# If only one file was provided
if len(filename) == 1:
filename = filename[0]
if filename.endswith(".json"):
with open(filename) as f:
FILE_CONTENTS += "[INFO] BEGINNING of File: " + filename + " ---------- \n"
FILE_CONTENTS += f.read()
FILE_CONTENTS += "[INFO] END of File: " + filename + " ---------- \n"
else:
with open(filename) as f:
FILE_CONTENTS += "[INFO] BEGINNING of File: " + filename + " ---------- \n"
FILE_CONTENTS += f.read()
FILE_CONTENTS += "[INFO] END of File: " + filename + " ---------- \n"
elif len(filename) > 1:
# If multiple files were provided at prompt
for f in filename:
print(f"Opening {f}...")
with open(f) as _file:
if f.endswith(".json"):
# Load json file
JSON_DATA = json.load(_file)
# Put the raw string of the json file into the FILE_CONTENTS variable
FILE_CONTENTS += "[INFO] BEGINNING of File: " + f + " ---------- \n"
FILE_CONTENTS += json.dumps(JSON_DATA)
FILE_CONTENTS += "[INFO] END of File: " + f + " ---------- \n"
else:
# Load _file and append it to the FILE_CONTENTS variable:
FC = _file.read()
FILE_CONTENTS += "[INFO] BEGINNING of File: " + f + " ---------- \n"
FILE_CONTENTS += FC
FILE_CONTENTS += "[INFO] END of File: " + f + " ---------- \n"
# Create new message string with file contents appended
# This Message is for OpenAI API
FILE_CONTENTS = f"\n\nFile Contents:" + "\n" + FILE_CONTENTS
msg = msg + FILE_CONTENTS
messages = [
{"role": "system", "content": mood},
{"role": "user", "content": msg}
]
else: # If it's only handling user input data (prompt) or from stdin:
messages = [
{"role": "system", "content": mood},
{"role": "user", "content": msg}
]
chat_history.append(mood)
chat_history.append(msg)
# This logic sucks; haven't had time to fix
if args.groq:
model = groq_model
else:
model = OPENAI_MODEL
try:
openai_response = llm_client.chat.completions.create(
model = model,
messages = messages
)
except Exception as e:
print("[X] Error: :\n", e)
sys.exit(1)
return prepare_response(openai_response,start_time,filename,Verbose)
def input_linter(prompt):
# Search for user input data containing "/file:"
filename_match = re.search(r'/file:(\S+)', prompt)
pattern = r"/file:([^ ]+)"
matches = re.findall(pattern, prompt)
# If no /file: in prompt, return original prompt
if not matches:
clean_message = prompt
# Process each match
for filename in matches:
# Clean up the filename (similar to what you did before but adapted for this context)
filename_clean = re.sub(r'[^\w.]+$', '', filename)
# Replace the original "hello:/" prefixed string with the cleaned filename in the prompt
# Note: This replaces all instances of the exact match. If you have identical filenames, they'll all be replaced.
clean_message = prompt.replace('/file:' + filename, filename_clean)
if not matches:
filename_clean = None
return clean_message, matches
def filter_interactive_mode(Interactive_mode, mood, prompt="", Verbose=False):
# initialize interactive mode if chosen
# Extract filename if user input contains "/file:"
msg, filename = input_linter(prompt)
if Interactive_mode == False:
start_time = time.time()
chatter(msg,start_time,mood, filename, Verbose)
# Make it continuous for Interactive mode
if Interactive_mode == True:
conversation_history = []
while True:
user_input = input(f"{PINK}[Prompt]> {RESET}")
msg, filename = input_linter(user_input)
start_time = time.time()
chatter(msg,start_time,mood, filename, Verbose)
# PREPARE ARGUMENTS
def process_with_context_and_prompt(context, prompt, addons_path, verbose):
selected_context = context
if args.context:
context = _get_context(selected_context, addons_path)
mood = context
else:
mood = prompt
input_lines = []
prompt = gather_input_from_stdin(input_lines, mood, prompt, verbose)
# Process the input
stdin_content = '\n'.join(input_lines)
sys.exit(0)
def gather_input_from_stdin(input_lines, mood, prompt, verbose):
if not sys.stdin.isatty():
if verbose:
print("[i] Reading from Stdin...")
for line in sys.stdin:
input_lines.append(line.strip())
mood = mood + "\n" + prompt
prompt = '\n'.join(input_lines)
filter_interactive_mode(False, mood, prompt, verbose)
# Process the input
stdin_content = '\n'.join(input_lines)
else:
Interactive_mode = False
filter_interactive_mode(False, mood, prompt, verbose)
return prompt
def handle_args(args):
Verbose = True if args.verbose else False
if args.list:
context_list = _list_available_contexts(addons_path)
for context in context_list:
print(context)
plaintext_output = True if args.slim else print(colored_ascii_art)
if not any(vars(args).values()):
parser.print_help()
Interactive_mode = False
if args.interactive and args.context:
mood = args.interactive if args.interactive is not True else ""
Interactive_mode = True
process_with_context_and_prompt(args.context, args.prompt, addons_path, Verbose) if args.context and args.prompt else filter_interactive_mode(False, mood, args.prompt)
elif args.examples:
print(f"{PINK}{termbot_usage_examples}{RESET}")
elif args.context and args.prompt:
process_with_context_and_prompt(args.context, args.prompt, addons_path, Verbose)
elif args.context and not args.prompt:
process_with_context_and_prompt(args.context, "", addons_path, Verbose)
elif args.prompt:
process_with_context_and_prompt("", args.prompt, addons_path, Verbose)
handle_args(args)