Skip to content

Commit

Permalink
fine tuning job listing (#489)
Browse files Browse the repository at this point in the history
* fine tuning job listing

* client test
  • Loading branch information
BrendanMartin authored Sep 10, 2023
1 parent 3589837 commit 6a8d6e6
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
3 changes: 3 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})
}},
Expand Down
64 changes: 64 additions & 0 deletions fine_tuning_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
}
22 changes: 22 additions & 0 deletions fine_tuning_job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

0 comments on commit 6a8d6e6

Please sign in to comment.