|
3 | 3 | import signal
|
4 | 4 | import sys
|
5 | 5 | import warnings
|
| 6 | +from openai.validators import ( |
| 7 | + write_out_file, |
| 8 | + apply_necessary_remediation, |
| 9 | + apply_optional_remediation, |
| 10 | + read_any_format, |
| 11 | + get_validators, |
| 12 | +) |
6 | 13 |
|
7 | 14 | import openai
|
8 | 15 |
|
@@ -393,8 +400,69 @@ def cancel(cls, args):
|
393 | 400 | resp = openai.FineTune.cancel(id=args.id)
|
394 | 401 | print(resp)
|
395 | 402 |
|
| 403 | + @classmethod |
| 404 | + def prepare_data(cls, args): |
| 405 | + |
| 406 | + sys.stdout.write("Analyzing...\n") |
| 407 | + fname = args.file |
| 408 | + df, remediation = read_any_format(fname) |
| 409 | + apply_necessary_remediation(None, remediation) |
| 410 | + |
| 411 | + validators = get_validators() |
| 412 | + |
| 413 | + optional_remediations = [] |
| 414 | + if remediation is not None: |
| 415 | + optional_remediations.append(remediation) |
| 416 | + for validator in validators: |
| 417 | + remediation = validator(df) |
| 418 | + if remediation is not None: |
| 419 | + optional_remediations.append(remediation) |
| 420 | + df = apply_necessary_remediation(df, remediation) |
| 421 | + |
| 422 | + any_optional_or_necessary_remediations = any( |
| 423 | + [ |
| 424 | + remediation |
| 425 | + for remediation in optional_remediations |
| 426 | + if remediation.optional_msg is not None |
| 427 | + or remediation.necessary_msg is not None |
| 428 | + ] |
| 429 | + ) |
| 430 | + |
| 431 | + if any_optional_or_necessary_remediations: |
| 432 | + sys.stdout.write( |
| 433 | + "\n\nBased on the analysis we will perform the following actions:\n" |
| 434 | + ) |
| 435 | + |
| 436 | + for remediation in optional_remediations: |
| 437 | + df = apply_optional_remediation(df, remediation) |
| 438 | + else: |
| 439 | + sys.stdout.write("\n\nNo remediations found.\n") |
| 440 | + |
| 441 | + write_out_file(df, fname, any_optional_or_necessary_remediations) |
| 442 | + |
| 443 | + |
| 444 | +def tools_register(parser): |
| 445 | + subparsers = parser.add_subparsers( |
| 446 | + title="Tools", help="Convenience client side tools" |
| 447 | + ) |
| 448 | + |
| 449 | + def help(args): |
| 450 | + parser.print_help() |
| 451 | + |
| 452 | + parser.set_defaults(func=help) |
| 453 | + |
| 454 | + sub = subparsers.add_parser("fine_tunes.prepare_data") |
| 455 | + sub.add_argument( |
| 456 | + "-f", |
| 457 | + "--file", |
| 458 | + required=True, |
| 459 | + help="JSONL, JSON, CSV, TSV, TXT or XLSX file containing prompt-completion examples to be analyzed." |
| 460 | + "This should be the local file path.", |
| 461 | + ) |
| 462 | + sub.set_defaults(func=FineTune.prepare_data) |
| 463 | + |
396 | 464 |
|
397 |
| -def register(parser): |
| 465 | +def api_register(parser): |
398 | 466 | # Engine management
|
399 | 467 | subparsers = parser.add_subparsers(help="All API subcommands")
|
400 | 468 |
|
|
0 commit comments