1
+ from __future__ import annotations
2
+
3
+ import json
4
+ from typing import TYPE_CHECKING
5
+ from argparse import ArgumentParser
6
+
7
+ from ..._utils import get_client , print_model
8
+ from ...._types import Omittable , omit
9
+ from ...._utils import is_given
10
+ from ..._models import BaseModel
11
+ from ....pagination import SyncCursorPage
12
+ from ....types .fine_tuning import (
13
+ FineTuningJob ,
14
+ FineTuningJobEvent ,
15
+ )
16
+
17
+ if TYPE_CHECKING :
18
+ from argparse import _SubParsersAction
19
+
20
+
21
+ def register (subparser : _SubParsersAction [ArgumentParser ]) -> None :
22
+ sub = subparser .add_parser ("chat_fine_tunes.create" )
23
+ sub .add_argument (
24
+ "-m" ,
25
+ "--model" ,
26
+ help = "The model to fine-tune." ,
27
+ required = True ,
28
+ )
29
+ sub .add_argument (
30
+ "-t" ,
31
+ "--training-file" ,
32
+ help = "The training file to fine-tune the model on." ,
33
+ required = True ,
34
+ )
35
+ sub .add_argument (
36
+ "-H" ,
37
+ "--hyperparameters" ,
38
+ help = "JSON string of hyperparameters to use for fine-tuning." ,
39
+ type = str ,
40
+ )
41
+ sub .add_argument (
42
+ "-s" ,
43
+ "--suffix" ,
44
+ help = "A suffix to add to the fine-tuned model name." ,
45
+ )
46
+ sub .add_argument (
47
+ "-v" ,
48
+ "--validation-file" ,
49
+ help = "The validation file to use for fine-tuning." ,
50
+ )
51
+ sub .set_defaults (func = CLIChatFineTunes .create , args_model = CLIChatFineTunesCreateArgs )
52
+
53
+ sub = subparser .add_parser ("chat_fine_tunes.get" )
54
+ sub .add_argument (
55
+ "-i" ,
56
+ "--id" ,
57
+ help = "The ID of the fine-tuning job to retrieve." ,
58
+ required = True ,
59
+ )
60
+ sub .set_defaults (func = CLIChatFineTunes .retrieve , args_model = CLIChatFineTunesRetrieveArgs )
61
+
62
+ sub = subparser .add_parser ("chat_fine_tunes.list" )
63
+ sub .add_argument (
64
+ "-a" ,
65
+ "--after" ,
66
+ help = "Identifier for the last job from the previous pagination request. If provided, only jobs created after this job will be returned." ,
67
+ )
68
+ sub .add_argument (
69
+ "-l" ,
70
+ "--limit" ,
71
+ help = "Number of fine-tuning jobs to retrieve." ,
72
+ type = int ,
73
+ )
74
+ sub .set_defaults (func = CLIChatFineTunes .list , args_model = CLIChatFineTunesListArgs )
75
+
76
+ sub = subparser .add_parser ("chat_fine_tunes.cancel" )
77
+ sub .add_argument (
78
+ "-i" ,
79
+ "--id" ,
80
+ help = "The ID of the fine-tuning job to cancel." ,
81
+ required = True ,
82
+ )
83
+ sub .set_defaults (func = CLIChatFineTunes .cancel , args_model = CLIChatFineTunesCancelArgs )
84
+
85
+
86
+ class CLIChatFineTunesCreateArgs (BaseModel ):
87
+ model : str
88
+ training_file : str
89
+ hyperparameters : Omittable [str ] = omit
90
+ suffix : Omittable [str ] = omit
91
+ validation_file : Omittable [str ] = omit
92
+
93
+
94
+ class CLIChatFineTunesRetrieveArgs (BaseModel ):
95
+ id : str
96
+
97
+
98
+ class CLIChatFineTunesListArgs (BaseModel ):
99
+ after : Omittable [str ] = omit
100
+ limit : Omittable [int ] = omit
101
+
102
+
103
+ class CLIChatFineTunesCancelArgs (BaseModel ):
104
+ id : str
105
+
106
+
107
+ class CLIChatFineTunes :
108
+ @staticmethod
109
+ def create (args : CLIChatFineTunesCreateArgs ) -> None :
110
+ hyperparameters = json .loads (str (args .hyperparameters )) if is_given (args .hyperparameters ) else omit
111
+ fine_tuning_job : FineTuningJob = get_client ().fine_tuning .jobs .create (
112
+ model = args .model ,
113
+ training_file = args .training_file ,
114
+ hyperparameters = hyperparameters ,
115
+ suffix = args .suffix ,
116
+ validation_file = args .validation_file ,
117
+ )
118
+ print_model (fine_tuning_job )
119
+
120
+ @staticmethod
121
+ def retrieve (args : CLIChatFineTunesRetrieveArgs ) -> None :
122
+ fine_tuning_job : FineTuningJob = get_client ().fine_tuning .jobs .retrieve (fine_tuning_job_id = args .id )
123
+ print_model (fine_tuning_job )
124
+
125
+ @staticmethod
126
+ def list (args : CLIChatFineTunesListArgs ) -> None :
127
+ fine_tuning_jobs : SyncCursorPage [FineTuningJob ] = get_client ().fine_tuning .jobs .list (
128
+ after = args .after or omit , limit = args .limit or omit
129
+ )
130
+ print_model (fine_tuning_jobs )
131
+
132
+ @staticmethod
133
+ def cancel (args : CLIChatFineTunesCancelArgs ) -> None :
134
+ fine_tuning_job : FineTuningJob = get_client ().fine_tuning .jobs .cancel (fine_tuning_job_id = args .id )
135
+ print_model (fine_tuning_job )
0 commit comments