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/respjson"
 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	// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
 86	JSON struct {
 87		ID                       respjson.Field
 88		CreatedAt                respjson.Field
 89		FineTunedModelCheckpoint respjson.Field
 90		FineTuningJobID          respjson.Field
 91		Metrics                  respjson.Field
 92		Object                   respjson.Field
 93		StepNumber               respjson.Field
 94		ExtraFields              map[string]respjson.Field
 95		raw                      string
 96	} `json:"-"`
 97}
 98
 99// Returns the unmodified JSON received from the API
100func (r FineTuningJobCheckpoint) RawJSON() string { return r.JSON.raw }
101func (r *FineTuningJobCheckpoint) UnmarshalJSON(data []byte) error {
102	return apijson.UnmarshalRoot(data, r)
103}
104
105// Metrics at the step number during the fine-tuning job.
106type FineTuningJobCheckpointMetrics struct {
107	FullValidLoss              float64 `json:"full_valid_loss"`
108	FullValidMeanTokenAccuracy float64 `json:"full_valid_mean_token_accuracy"`
109	Step                       float64 `json:"step"`
110	TrainLoss                  float64 `json:"train_loss"`
111	TrainMeanTokenAccuracy     float64 `json:"train_mean_token_accuracy"`
112	ValidLoss                  float64 `json:"valid_loss"`
113	ValidMeanTokenAccuracy     float64 `json:"valid_mean_token_accuracy"`
114	// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
115	JSON struct {
116		FullValidLoss              respjson.Field
117		FullValidMeanTokenAccuracy respjson.Field
118		Step                       respjson.Field
119		TrainLoss                  respjson.Field
120		TrainMeanTokenAccuracy     respjson.Field
121		ValidLoss                  respjson.Field
122		ValidMeanTokenAccuracy     respjson.Field
123		ExtraFields                map[string]respjson.Field
124		raw                        string
125	} `json:"-"`
126}
127
128// Returns the unmodified JSON received from the API
129func (r FineTuningJobCheckpointMetrics) RawJSON() string { return r.JSON.raw }
130func (r *FineTuningJobCheckpointMetrics) UnmarshalJSON(data []byte) error {
131	return apijson.UnmarshalRoot(data, r)
132}
133
134type FineTuningJobCheckpointListParams struct {
135	// Identifier for the last checkpoint ID from the previous pagination request.
136	After param.Opt[string] `query:"after,omitzero" json:"-"`
137	// Number of checkpoints to retrieve.
138	Limit param.Opt[int64] `query:"limit,omitzero" json:"-"`
139	paramObj
140}
141
142// URLQuery serializes [FineTuningJobCheckpointListParams]'s query parameters as
143// `url.Values`.
144func (r FineTuningJobCheckpointListParams) URLQuery() (v url.Values, err error) {
145	return apiquery.MarshalWithSettings(r, apiquery.QuerySettings{
146		ArrayFormat:  apiquery.ArrayQueryFormatBrackets,
147		NestedFormat: apiquery.NestedQueryFormatBrackets,
148	})
149}