Repository: sashabaranov/go-openai
Pull request #497 (2023-09-23 01:13:24)

added delete fine tune model endpoint
**Describe the change**
With the deprecation of the fine-tunes endpoint, OpenAI moved the deletion of fine-tuned models to `DELETE /models/{model}`. This PR adds this new endpoint in `models.go`.

**Describe your solution**
I reworked the code present in `fine_tunes.go` that was already written for deleting fine-tuned models. Because OpenAI organized the new delete under the "models" prefix, this code is now located in the `models.go` file.

**Tests**
The tests used for the previous fine-tune model delete were reworked to use the new endpoint.


codecov[bot]: ## [Codecov](https://app.codecov.io/gh/sashabaranov/go-openai/pull/497?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) Report
> Merging [#497](https://app.codecov.io/gh/sashabaranov/go-openai/pull/497?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) (ac003ce) into [master](https://app.codecov.io/gh/sashabaranov/go-openai/commit/3589837b229aeace205f312aa839bf73154e2820?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) (3589837) will **increase** coverage by `0.15%`.
> Report is 1 commits behind head on master.
> The diff coverage is `100.00%`.

```diff
@@            Coverage Diff             @@
##           master     #497      +/-   ##
==========================================
+ Coverage   97.30%   97.46%   +0.15%     
==========================================
  Files          18       18              
  Lines         780      827      +47     
==========================================
+ Hits          759      806      +47     
  Misses         15       15              
  Partials        6        6              
```


| [Files Changed](https://app.codecov.io/gh/sashabaranov/go-openai/pull/497?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) | Coverage Δ | |
|---|---|---|
| [embeddings.go](https://app.codecov.io/gh/sashabaranov/go-openai/pull/497?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-ZW1iZWRkaW5ncy5nbw==) | `100.00% <100.00%> (ø)` | |
| [models.go](https://app.codecov.io/gh/sashabaranov/go-openai/pull/497?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-bW9kZWxzLmdv) | `100.00% <100.00%> (ø)` | |



Patch:
diff --git a/models.go b/models.go
index 560402e..c207f0a 100644
--- a/models.go
+++ b/models.go
@@ -33,6 +33,13 @@ type Permission struct {
 	IsBlocking         bool        `json:"is_blocking"`
 }
 
+// FineTuneModelDeleteResponse represents the deletion status of a fine-tuned model.
+type FineTuneModelDeleteResponse struct {
+	ID      string `json:"id"`
+	Object  string `json:"object"`
+	Deleted bool   `json:"deleted"`
+}
+
 // ModelsList is a list of models, including those that belong to the user or organization.
 type ModelsList struct {
 	Models []Model `json:"data"`
@@ -62,3 +69,16 @@ func (c *Client) GetModel(ctx context.Context, modelID string) (model Model, err
 	err = c.sendRequest(req, &model)
 	return
 }
+
+// DeleteFineTuneModel Deletes a fine-tune model. You must have the Owner
+// role in your organization to delete a model.
+func (c *Client) DeleteFineTuneModel(ctx context.Context, modelID string) (
+	response FineTuneModelDeleteResponse, err error) {
+	req, err := c.newRequest(ctx, http.MethodDelete, c.fullURL("/models/"+modelID))
+	if err != nil {
+		return
+	}
+
+	err = c.sendRequest(req, &response)
+	return
+}


Test patch:
diff --git a/client_test.go b/client_test.go
index 9b50468..2c1d749 100644
--- a/client_test.go
+++ b/client_test.go
@@ -271,6 +271,9 @@ func TestClientReturnsRequestBuilderErrors(t *testing.T) {
 		{"GetModel", func() (any, error) {
 			return client.GetModel(ctx, "text-davinci-003")
 		}},
+		{"DeleteFineTuneModel", func() (any, error) {
+			return client.DeleteFineTuneModel(ctx, "")
+		}},
 	}
 
 	for _, testCase := range testCases {
diff --git a/models_test.go b/models_test.go
index 59b4f5e..9ff7304 100644
--- a/models_test.go
+++ b/models_test.go
@@ -14,6 +14,8 @@ import (
 	"testing"
 )
 
+const testFineTuneModelID = "fine-tune-model-id"
+
 // TestListModels Tests the list models endpoint of the API using the mocked server.
 func TestListModels(t *testing.T) {
 	client, server, teardown := setupOpenAITestServer()
@@ -78,3 +80,16 @@ func TestGetModelReturnTimeoutError(t *testing.T) {
 		t.Fatal("Did not return timeout error")
 	}
 }
+
+func TestDeleteFineTuneModel(t *testing.T) {
+	client, server, teardown := setupOpenAITestServer()
+	defer teardown()
+	server.RegisterHandler("/v1/models/"+testFineTuneModelID, handleDeleteFineTuneModelEndpoint)
+	_, err := client.DeleteFineTuneModel(context.Background(), testFineTuneModelID)
+	checks.NoError(t, err, "DeleteFineTuneModel error")
+}
+
+func handleDeleteFineTuneModelEndpoint(w http.ResponseWriter, _ *http.Request) {
+	resBytes, _ := json.Marshal(FineTuneModelDeleteResponse{})
+	fmt.Fprintln(w, string(resBytes))
+}