From 6a8d6e69856d11aa5d6cc45ca1430effbb7234f6 Mon Sep 17 00:00:00 2001 From: Brendan Martin Date: Sun, 10 Sep 2023 12:41:51 -0400 Subject: [PATCH] fine tuning job listing (#489) * fine tuning job listing * client test --- client_test.go | 3 ++ fine_tuning_job.go | 64 +++++++++++++++++++++++++++++++++++++++++ fine_tuning_job_test.go | 22 ++++++++++++++ 3 files changed, 89 insertions(+) diff --git a/client_test.go b/client_test.go index 9b5046899..d1975536a 100644 --- a/client_test.go +++ b/client_test.go @@ -235,6 +235,9 @@ func TestClientReturnsRequestBuilderErrors(t *testing.T) { {"ListFineTuningJobEvents", func() (any, error) { return client.ListFineTuningJobEvents(ctx, "") }}, + {"ListFineTuningJobs", func() (any, error) { + return client.ListFineTuningJobs(ctx) + }}, {"Moderations", func() (any, error) { return client.Moderations(ctx, ModerationRequest{}) }}, diff --git a/fine_tuning_job.go b/fine_tuning_job.go index a840b7ec3..0325b5232 100644 --- a/fine_tuning_job.go +++ b/fine_tuning_job.go @@ -51,6 +51,12 @@ type FineTuningJobEvent struct { Type string `json:"type"` } +type FineTuningJobsList struct { + Object string `json:"object"` + Data []FineTuningJob `json:"data"` + HasMore bool `json:"has_more"` +} + // CreateFineTuningJob create a fine tuning job. func (c *Client) CreateFineTuningJob( ctx context.Context, @@ -151,3 +157,61 @@ func (c *Client) ListFineTuningJobEvents( err = c.sendRequest(req, &response) return } + +type listFineTuningJobsParameters struct { + after *string + limit *int +} + +type ListFineTuningJobsParameter func(*listFineTuningJobsParameters) + +func ListFineTuningJobsWithAfter(after string) ListFineTuningJobsParameter { + return func(args *listFineTuningJobsParameters) { + args.after = &after + } +} + +func ListFineTuningJobsWithLimit(limit int) ListFineTuningJobsParameter { + return func(args *listFineTuningJobsParameters) { + args.limit = &limit + } +} + +func (c *Client) ListFineTuningJobs( + ctx context.Context, + setters ...ListFineTuningJobsParameter, +) (response FineTuningJobsList, err error) { + parameters := &listFineTuningJobsParameters{ + after: nil, + limit: nil, + } + + for _, setter := range setters { + setter(parameters) + } + + urlValues := url.Values{} + if parameters.after != nil { + urlValues.Add("after", *parameters.after) + } + if parameters.limit != nil { + urlValues.Add("limit", fmt.Sprintf("%d", *parameters.limit)) + } + + encodedValues := "" + if len(urlValues) > 0 { + encodedValues = "?" + urlValues.Encode() + } + + req, err := c.newRequest( + ctx, + http.MethodGet, + c.fullURL("/fine_tuning/jobs"+encodedValues), + ) + if err != nil { + return + } + + err = c.sendRequest(req, &response) + return +} diff --git a/fine_tuning_job_test.go b/fine_tuning_job_test.go index 519c6cd2d..914125cc1 100644 --- a/fine_tuning_job_test.go +++ b/fine_tuning_job_test.go @@ -87,4 +87,26 @@ func TestFineTuningJob(t *testing.T) { ListFineTuningJobEventsWithLimit(10), ) checks.NoError(t, err, "ListFineTuningJobEvents error") + + _, err = client.ListFineTuningJobs(ctx) + checks.NoError(t, err, "ListFineTuningJobs error") + + _, err = client.ListFineTuningJobs( + ctx, + ListFineTuningJobsWithAfter("last-event-id"), + ) + checks.NoError(t, err, "ListFineTuningJobs error") + + _, err = client.ListFineTuningJobs( + ctx, + ListFineTuningJobsWithLimit(10), + ) + checks.NoError(t, err, "ListFineTuningJobs error") + + _, err = client.ListFineTuningJobs( + ctx, + ListFineTuningJobsWithAfter("last-event-id"), + ListFineTuningJobsWithLimit(10), + ) + checks.NoError(t, err, "ListFineTuningJobs error") }