1// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2
3package openai
4
5import (
6 "context"
7 "errors"
8 "fmt"
9 "net/http"
10 "net/url"
11
12 "github.com/openai/openai-go/internal/apijson"
13 "github.com/openai/openai-go/internal/apiquery"
14 "github.com/openai/openai-go/internal/requestconfig"
15 "github.com/openai/openai-go/option"
16 "github.com/openai/openai-go/packages/pagination"
17 "github.com/openai/openai-go/packages/param"
18 "github.com/openai/openai-go/packages/resp"
19 "github.com/openai/openai-go/shared/constant"
20)
21
22// FineTuningJobCheckpointService contains methods and other services that help
23// with interacting with the openai API.
24//
25// Note, unlike clients, this service does not read variables from the environment
26// automatically. You should not instantiate this service directly, and instead use
27// the [NewFineTuningJobCheckpointService] method instead.
28type FineTuningJobCheckpointService struct {
29 Options []option.RequestOption
30}
31
32// NewFineTuningJobCheckpointService generates a new service that applies the given
33// options to each request. These options are applied after the parent client's
34// options (if there is one), and before any request-specific options.
35func NewFineTuningJobCheckpointService(opts ...option.RequestOption) (r FineTuningJobCheckpointService) {
36 r = FineTuningJobCheckpointService{}
37 r.Options = opts
38 return
39}
40
41// List checkpoints for a fine-tuning job.
42func (r *FineTuningJobCheckpointService) List(ctx context.Context, fineTuningJobID string, query FineTuningJobCheckpointListParams, opts ...option.RequestOption) (res *pagination.CursorPage[FineTuningJobCheckpoint], err error) {
43 var raw *http.Response
44 opts = append(r.Options[:], opts...)
45 opts = append([]option.RequestOption{option.WithResponseInto(&raw)}, opts...)
46 if fineTuningJobID == "" {
47 err = errors.New("missing required fine_tuning_job_id parameter")
48 return
49 }
50 path := fmt.Sprintf("fine_tuning/jobs/%s/checkpoints", fineTuningJobID)
51 cfg, err := requestconfig.NewRequestConfig(ctx, http.MethodGet, path, query, &res, opts...)
52 if err != nil {
53 return nil, err
54 }
55 err = cfg.Execute()
56 if err != nil {
57 return nil, err
58 }
59 res.SetPageConfig(cfg, raw)
60 return res, nil
61}
62
63// List checkpoints for a fine-tuning job.
64func (r *FineTuningJobCheckpointService) ListAutoPaging(ctx context.Context, fineTuningJobID string, query FineTuningJobCheckpointListParams, opts ...option.RequestOption) *pagination.CursorPageAutoPager[FineTuningJobCheckpoint] {
65 return pagination.NewCursorPageAutoPager(r.List(ctx, fineTuningJobID, query, opts...))
66}
67
68// The `fine_tuning.job.checkpoint` object represents a model checkpoint for a
69// fine-tuning job that is ready to use.
70type FineTuningJobCheckpoint struct {
71 // The checkpoint identifier, which can be referenced in the API endpoints.
72 ID string `json:"id,required"`
73 // The Unix timestamp (in seconds) for when the checkpoint was created.
74 CreatedAt int64 `json:"created_at,required"`
75 // The name of the fine-tuned checkpoint model that is created.
76 FineTunedModelCheckpoint string `json:"fine_tuned_model_checkpoint,required"`
77 // The name of the fine-tuning job that this checkpoint was created from.
78 FineTuningJobID string `json:"fine_tuning_job_id,required"`
79 // Metrics at the step number during the fine-tuning job.
80 Metrics FineTuningJobCheckpointMetrics `json:"metrics,required"`
81 // The object type, which is always "fine_tuning.job.checkpoint".
82 Object constant.FineTuningJobCheckpoint `json:"object,required"`
83 // The step number that the checkpoint was created at.
84 StepNumber int64 `json:"step_number,required"`
85 // Metadata for the response, check the presence of optional fields with the
86 // [resp.Field.IsPresent] method.
87 JSON struct {
88 ID resp.Field
89 CreatedAt resp.Field
90 FineTunedModelCheckpoint resp.Field
91 FineTuningJobID resp.Field
92 Metrics resp.Field
93 Object resp.Field
94 StepNumber resp.Field
95 ExtraFields map[string]resp.Field
96 raw string
97 } `json:"-"`
98}
99
100// Returns the unmodified JSON received from the API
101func (r FineTuningJobCheckpoint) RawJSON() string { return r.JSON.raw }
102func (r *FineTuningJobCheckpoint) UnmarshalJSON(data []byte) error {
103 return apijson.UnmarshalRoot(data, r)
104}
105
106// Metrics at the step number during the fine-tuning job.
107type FineTuningJobCheckpointMetrics struct {
108 FullValidLoss float64 `json:"full_valid_loss"`
109 FullValidMeanTokenAccuracy float64 `json:"full_valid_mean_token_accuracy"`
110 Step float64 `json:"step"`
111 TrainLoss float64 `json:"train_loss"`
112 TrainMeanTokenAccuracy float64 `json:"train_mean_token_accuracy"`
113 ValidLoss float64 `json:"valid_loss"`
114 ValidMeanTokenAccuracy float64 `json:"valid_mean_token_accuracy"`
115 // Metadata for the response, check the presence of optional fields with the
116 // [resp.Field.IsPresent] method.
117 JSON struct {
118 FullValidLoss resp.Field
119 FullValidMeanTokenAccuracy resp.Field
120 Step resp.Field
121 TrainLoss resp.Field
122 TrainMeanTokenAccuracy resp.Field
123 ValidLoss resp.Field
124 ValidMeanTokenAccuracy resp.Field
125 ExtraFields map[string]resp.Field
126 raw string
127 } `json:"-"`
128}
129
130// Returns the unmodified JSON received from the API
131func (r FineTuningJobCheckpointMetrics) RawJSON() string { return r.JSON.raw }
132func (r *FineTuningJobCheckpointMetrics) UnmarshalJSON(data []byte) error {
133 return apijson.UnmarshalRoot(data, r)
134}
135
136type FineTuningJobCheckpointListParams struct {
137 // Identifier for the last checkpoint ID from the previous pagination request.
138 After param.Opt[string] `query:"after,omitzero" json:"-"`
139 // Number of checkpoints to retrieve.
140 Limit param.Opt[int64] `query:"limit,omitzero" json:"-"`
141 paramObj
142}
143
144// IsPresent returns true if the field's value is not omitted and not the JSON
145// "null". To check if this field is omitted, use [param.IsOmitted].
146func (f FineTuningJobCheckpointListParams) IsPresent() bool {
147 return !param.IsOmitted(f) && !f.IsNull()
148}
149
150// URLQuery serializes [FineTuningJobCheckpointListParams]'s query parameters as
151// `url.Values`.
152func (r FineTuningJobCheckpointListParams) URLQuery() (v url.Values) {
153 return apiquery.MarshalWithSettings(r, apiquery.QuerySettings{
154 ArrayFormat: apiquery.ArrayQueryFormatBrackets,
155 NestedFormat: apiquery.NestedQueryFormatBrackets,
156 })
157}