-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcli.py
More file actions
executable file
·544 lines (491 loc) · 16.8 KB
/
Copy pathcli.py
File metadata and controls
executable file
·544 lines (491 loc) · 16.8 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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
#!/usr/bin/env python3
import argparse
from datetime import datetime, timezone
from functools import partial
import sys
# importing readline adds history and navigation to input builtin
import readline # noqa F401
from rich.console import Console
from rich.markdown import Markdown
from libopenai import core
from libopenai.pricing import KNOWN_MODELS
from libopenai.constants import DEFAULT_MODEL
from libopenai.files import Files
from libopenai.vectors import Vectors
from libopenai.validation import (
IMAGE_FORMAT_DEFAULT,
IMAGE_FORMATS,
IMAGE_MODEL_DEFAULT,
IMAGE_MODELS,
IMAGE_QUALITIES,
IMAGE_QUALITY_DEFAULT,
IMAGE_SIZE_DEFAULT,
validate_image_size,
)
def _argparse_type(validator):
"""Wrap a validator so argparse shows its message instead of a generic one."""
def _wrapped(value):
try:
return validator(value)
except ValueError as e:
raise argparse.ArgumentTypeError(str(e)) from e
_wrapped.__name__ = validator.__name__
return _wrapped
console = Console()
def check_exit(user_input):
return user_input in ("q", "x", "quit", "exit")
def cli_input():
try:
user_input = input("> ")
except (KeyboardInterrupt, EOFError):
print()
return None
else:
if check_exit(user_input):
return None
return user_input
def cli_input_multiline():
user_input = []
while True:
try:
line = input("> ")
except (KeyboardInterrupt, EOFError):
print()
return None
else:
if line == "SEND":
break
user_input.append(line)
user_input = "\n".join(user_input)
if check_exit(user_input):
return None
return user_input
def fmt_ts(ts):
if ts is None:
return ""
return datetime.fromtimestamp(ts, tz=timezone.utc).strftime("%Y-%m-%dT%H:%M:%S")
def print_table(headers, rows, right_align=()):
widths = [
max(len(headers[i]), max(len(r[i]) for r in rows)) for i in range(len(headers))
]
def fmt_row(row):
parts = [
f"{val:>{widths[i]}}" if i in right_align else f"{val:<{widths[i]}}"
for i, val in enumerate(row)
]
return " ".join(parts)
print(fmt_row(headers))
for row in rows:
print(fmt_row(row))
def cli_output(msg, info, rich=False):
if rich and sys.stdout.isatty():
console.print(Markdown(msg))
else:
print(msg)
print(info, file=sys.stderr)
def main():
parser = argparse.ArgumentParser(description="Interact with OpenAI's LLMs.")
subparsers = parser.add_subparsers(dest="command")
files_parser = subparsers.add_parser("files", help="Manage uploaded files.")
files_sub = files_parser.add_subparsers(dest="files_command")
files_sub.add_parser("list", help="List uploaded files.")
files_add_parser = files_sub.add_parser("add", help="Upload file(s).")
files_add_parser.add_argument(
"files", nargs="+", metavar="FILE", help="File(s) to upload."
)
files_del_parser = files_sub.add_parser("delete", help="Delete file(s) by ID.")
files_del_parser.add_argument(
"ids", nargs="+", metavar="FILE_ID", help="File ID(s) to delete."
)
files_sub.add_parser("purge", help="Delete all uploaded files.")
vectors_parser = subparsers.add_parser("vectors", help="Manage vector stores.")
vectors_sub = vectors_parser.add_subparsers(dest="vectors_command")
vectors_sub.add_parser("list", help="List vector stores.")
vectors_create_parser = vectors_sub.add_parser(
"create", help="Create a vector store."
)
vectors_create_parser.add_argument(
"name", metavar="NAME", help="Name for the vector store."
)
vectors_create_parser.add_argument(
"files", nargs="*", metavar="FILE", help="File(s) to upload and add."
)
vectors_create_parser.add_argument(
"--no-wait",
action="store_true",
help="Return immediately without waiting for indexing to complete.",
)
vectors_del_parser = vectors_sub.add_parser(
"delete", help="Delete a vector store by ID."
)
vectors_del_parser.add_argument(
"id", metavar="VECTOR_STORE_ID", help="Vector store ID to delete."
)
vectors_sub.add_parser("purge", help="Delete all vector stores.")
vectors_files_parser = vectors_sub.add_parser(
"files", help="Manage files in a vector store."
)
vectors_files_sub = vectors_files_parser.add_subparsers(
dest="vectors_files_command"
)
vectors_files_list_parser = vectors_files_sub.add_parser(
"list", help="List files in a vector store."
)
vectors_files_list_parser.add_argument(
"id", metavar="VECTOR_STORE_ID", help="Vector store ID."
)
vectors_files_add_parser = vectors_files_sub.add_parser(
"add", help="Upload file(s) and add to a vector store."
)
vectors_files_add_parser.add_argument(
"id", metavar="VECTOR_STORE_ID", help="Vector store ID."
)
vectors_files_add_parser.add_argument(
"files", nargs="+", metavar="FILE", help="File path(s) to upload and add."
)
vectors_files_add_parser.add_argument(
"--no-wait",
action="store_true",
help="Return immediately without waiting for indexing to complete.",
)
vectors_files_add_id_parser = vectors_files_sub.add_parser(
"add-id", help="Add already-uploaded file(s) to a vector store by ID."
)
vectors_files_add_id_parser.add_argument(
"id", metavar="VECTOR_STORE_ID", help="Vector store ID."
)
vectors_files_add_id_parser.add_argument(
"file_ids", nargs="+", metavar="FILE_ID", help="File ID(s) to add."
)
vectors_files_add_id_parser.add_argument(
"--no-wait",
action="store_true",
help="Return immediately without waiting for indexing to complete.",
)
vectors_files_del_parser = vectors_files_sub.add_parser(
"delete", help="Remove file(s) from a vector store."
)
vectors_files_del_parser.add_argument(
"id", metavar="VECTOR_STORE_ID", help="Vector store ID."
)
vectors_files_del_parser.add_argument(
"file_ids", nargs="+", metavar="FILE_ID", help="File ID(s) to remove."
)
parser.add_argument(
"-m",
"--multiline",
action="store_true",
help='Enable multiline input mode. Input "SEND" when you are done.',
)
parser.add_argument(
"-M",
"--model",
default=DEFAULT_MODEL,
help=f"Use different model than {DEFAULT_MODEL}",
)
parser.add_argument(
"-b",
"--batch-mode",
action="store_true",
help="No prompt, quit after first response. For use with pipes/redirection.",
)
parser.add_argument(
"-p",
"--prepend",
metavar="STRING",
help="String to prepend to the first user message, followed by two newlines.",
)
parser.add_argument(
"-pf",
"--prepend-file",
metavar="FILE",
help="Plain text file whose contents will be prepended to the first user message.",
)
parser.add_argument(
"-i",
"--image",
nargs="+",
metavar="FILE",
help="Image file(s) to include.",
)
parser.add_argument(
"-f",
"--file",
nargs="+",
metavar="FILE",
help="Document(s) to include.",
)
parser.add_argument(
"-vf",
"--vectorize-file",
nargs="+",
metavar="FILE",
help="Document(s) to upload to a vector store for semantic file search.",
)
parser.add_argument(
"-vs",
"--vector-store",
metavar="ID",
help="Use a pre-existing vector store by ID for semantic file search.",
)
parser.add_argument(
"-r",
"--rich",
action="store_true",
help="Render Markdown with rich text formatting in the terminal.",
)
parser.add_argument(
"-w",
"--web-search",
action="store_true",
help="Enable web search tool.",
)
parser.add_argument(
"-ig",
"--image-generation",
action="store_true",
help="Enable image generation tool. Generated images are saved to the data directory.",
)
parser.add_argument(
"--image-size",
type=_argparse_type(validate_image_size),
default=IMAGE_SIZE_DEFAULT,
metavar="SIZE",
help=(
f"Generated image size (auto or WIDTHxHEIGHT, e.g. 1024x1024). "
f"Default: {IMAGE_SIZE_DEFAULT}."
),
)
parser.add_argument(
"--image-quality",
choices=IMAGE_QUALITIES,
default=IMAGE_QUALITY_DEFAULT,
help=f"Generated image quality. Default: {IMAGE_QUALITY_DEFAULT}.",
)
parser.add_argument(
"--image-format",
choices=IMAGE_FORMATS,
default=IMAGE_FORMAT_DEFAULT,
help=f"Generated image format. Default: {IMAGE_FORMAT_DEFAULT}.",
)
parser.add_argument(
"--image-model",
choices=IMAGE_MODELS,
default=IMAGE_MODEL_DEFAULT,
help=f"Image generation model. Default: {IMAGE_MODEL_DEFAULT}.",
)
parser.add_argument(
"-d",
"--debug",
action="store_true",
help="Pretty print raw OpenAI responses to stderr.",
)
parser.add_argument(
"-l",
"--list-known",
action="store_true",
help="List models with pricing records available.",
)
parser.add_argument(
"-L",
"--list-all",
action="store_true",
help="List all models available.",
)
args = parser.parse_args()
if args.command == "files":
files_api = Files()
if args.files_command == "list":
files = files_api.list_files()
if not files:
return
rows = [
(fid, name, str(size), purpose, fmt_ts(created_at), fmt_ts(expires_at))
for fid, name, size, purpose, created_at, expires_at in files
]
print_table(
("ID", "FILENAME", "SIZE", "PURPOSE", "CREATED_AT", "EXPIRES_AT"),
rows,
right_align=(2,),
)
elif args.files_command == "add":
for path in args.files:
file_id = files_api.upload_file(path, "user_data")
print(file_id)
elif args.files_command == "delete":
for file_id in args.ids:
files_api.delete_file(file_id)
elif args.files_command == "purge":
for file_id, *_ in files_api.list_files():
files_api.delete_file(file_id)
else:
files_parser.print_help()
return
if args.command == "vectors":
vectors_api = Vectors()
if args.vectors_command == "list":
stores = vectors_api.list_vector_stores()
if not stores:
return
rows = [
(vsid, name, status, fmt_ts(created_at))
for vsid, name, status, created_at in stores
]
print_table(("ID", "NAME", "STATUS", "CREATED_AT"), rows)
elif args.vectors_command == "create":
vs_id = vectors_api.create_vector_store(args.name)
if args.files:
files_api = Files(vectors_api.client)
for path in args.files:
file_id = files_api.upload_file(path, "assistants")
vectors_api.add_vector_store_file(vs_id, file_id)
if args.files and not args.no_wait:
vectors_api.wait_for_vector_store(vs_id)
print(vs_id)
elif args.vectors_command == "delete":
vectors_api.delete_vector_store(args.id)
elif args.vectors_command == "purge":
for vsid, *_ in vectors_api.list_vector_stores():
vectors_api.delete_vector_store(vsid)
elif args.vectors_command == "files":
if args.vectors_files_command == "list":
files = vectors_api.list_vector_store_files(args.id)
if not files:
return
rows = [
(fid, status, fmt_ts(created_at))
for fid, status, created_at in files
]
print_table(("ID", "STATUS", "CREATED_AT"), rows)
elif args.vectors_files_command == "add":
if args.files:
files_api = Files(vectors_api.client)
for path in args.files:
file_id = files_api.upload_file(path, "assistants")
vectors_api.add_vector_store_file(args.id, file_id)
if not args.no_wait:
vectors_api.wait_for_vector_store(args.id)
elif args.vectors_files_command == "add-id":
for file_id in args.file_ids:
vectors_api.add_vector_store_file(args.id, file_id)
if not args.no_wait:
vectors_api.wait_for_vector_store(args.id)
elif args.vectors_files_command == "delete":
for file_id in args.file_ids:
vectors_api.delete_vector_store_file(args.id, file_id)
else:
vectors_files_parser.print_help()
else:
vectors_parser.print_help()
return
list_opts = [args.list_known, args.list_all]
if (
any(list_opts)
and (
args.multiline
or args.model != DEFAULT_MODEL
or args.batch_mode
or args.prepend
or args.prepend_file
or args.image
or args.file
or args.vectorize_file
or args.vector_store
or args.rich
or args.web_search
or args.image_generation
or args.image_size != IMAGE_SIZE_DEFAULT
or args.image_quality != IMAGE_QUALITY_DEFAULT
or args.image_format != IMAGE_FORMAT_DEFAULT
or args.image_model != IMAGE_MODEL_DEFAULT
or args.debug
)
) or sum(list_opts) > 1:
parser.error(
"-l/--list-known and -L/--list-all "
"cannot be combined with each other or other options."
)
if args.list_known:
for m in KNOWN_MODELS:
print(m)
return
if args.list_all:
all_models = core.GptCore().list_models()
for m in all_models:
print(m)
return
if args.batch_mode:
prompt = sys.stdin.read().strip()
if args.prepend:
prompt = args.prepend + "\n\n" + prompt
if args.prepend_file:
with open(args.prepend_file, "r") as f:
prompt = f.read().strip() + "\n\n" + prompt
def batch_input():
return prompt
def batch_output(msg, info):
print(msg)
print(info, file=sys.stderr)
core.GptCore(
batch_input,
batch_output,
args.model,
web_search=args.web_search,
image_generation=args.image_generation,
image_size=args.image_size,
image_quality=args.image_quality,
image_format=args.image_format,
image_model=args.image_model,
debug=args.debug,
).main(
image_paths=args.image,
file_paths=args.file,
vectorize_file_paths=args.vectorize_file,
vector_store_id=args.vector_store,
one_shot=True,
)
return
if args.multiline:
input_f = cli_input_multiline
else:
input_f = cli_input
prefix = ""
if args.prepend:
prefix += args.prepend + "\n\n"
if args.prepend_file:
with open(args.prepend_file, "r") as f:
prefix += f.read().strip() + "\n\n"
if prefix:
original_input_f = input_f
def input_f_with_prepend():
msg = original_input_f()
if msg is not None:
msg = prefix + msg
return msg
# Replace input_f for the first call only
first_call = [True]
def input_f():
if first_call[0]:
first_call[0] = False
return input_f_with_prepend()
return original_input_f()
output_f = partial(cli_output, rich=args.rich)
core.GptCore(
input_f,
output_f,
model=args.model,
web_search=args.web_search,
image_generation=args.image_generation,
image_size=args.image_size,
image_quality=args.image_quality,
image_format=args.image_format,
image_model=args.image_model,
debug=args.debug,
).main(
image_paths=args.image,
file_paths=args.file,
vectorize_file_paths=args.vectorize_file,
vector_store_id=args.vector_store,
)
if __name__ == "__main__":
main()