Skip to content

Commit

Permalink
fixup delete resource
Browse files Browse the repository at this point in the history
  • Loading branch information
skyscrapr committed Jun 19, 2023
1 parent ab20e55 commit c6f7ba0
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 13 deletions.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ require (

require (
github.com/hashicorp/terraform-plugin-log v0.9.0
github.com/skyscrapr/openai-sdk-go v0.0.0-20230618031714-fbf8a7352691
github.com/skyscrapr/openai-sdk-go v0.0.0-20230619065909-d1e3156ff50a
)

require (
Expand Down Expand Up @@ -63,6 +63,7 @@ require (
github.com/posener/complete v1.2.3 // indirect
github.com/russross/blackfriday v1.6.0 // indirect
github.com/shopspring/decimal v1.3.1 // indirect
github.com/skyscrapr/openai-sseclient-go v0.0.0-20230619011341-1f8774816d56 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/vmihailenco/msgpack v4.0.4+incompatible // indirect
github.com/vmihailenco/msgpack/v5 v5.3.5 // indirect
Expand Down
8 changes: 6 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,12 @@ github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFR
github.com/shopspring/decimal v1.3.1 h1:2Usl1nmF/WZucqkFZhnfFYxxxu8LG21F6nPQBE5gKV8=
github.com/shopspring/decimal v1.3.1/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
github.com/skeema/knownhosts v1.1.0 h1:Wvr9V0MxhjRbl3f9nMnKnFfiWTJmtECJ9Njkea3ysW0=
github.com/skyscrapr/openai-sdk-go v0.0.0-20230618031714-fbf8a7352691 h1:w/oaAf1AgY4I2PU+4s5i6muO3Qt2QxnVZnHaDuleTBI=
github.com/skyscrapr/openai-sdk-go v0.0.0-20230618031714-fbf8a7352691/go.mod h1:FsqF4u4OHs6BFjFuQavyHuqI8eWDt68fdLvfP/gXns8=
github.com/skyscrapr/openai-sdk-go v0.0.0-20230619044807-8443be5141a6 h1:sg/ddQKByiG+BhgJ0R6u2TUhTB6dOlZJxCiQVCmfE5E=
github.com/skyscrapr/openai-sdk-go v0.0.0-20230619044807-8443be5141a6/go.mod h1:ZN3ESLlPXOb2XsQIg8Eq5EUN0kTg6WB3CiyYzBdxtDw=
github.com/skyscrapr/openai-sdk-go v0.0.0-20230619065909-d1e3156ff50a h1:kknsOelLXV5UTxWs3JPnkAkYTV7xljKNtM5dxE03I8g=
github.com/skyscrapr/openai-sdk-go v0.0.0-20230619065909-d1e3156ff50a/go.mod h1:FsqF4u4OHs6BFjFuQavyHuqI8eWDt68fdLvfP/gXns8=
github.com/skyscrapr/openai-sseclient-go v0.0.0-20230619011341-1f8774816d56 h1:7gsW0+rg49Ir6HyVBgCnxC41+9kyDO7qNQuUmyEb3I0=
github.com/skyscrapr/openai-sseclient-go v0.0.0-20230619011341-1f8774816d56/go.mod h1:Bixu/oBLy9NIqktbwaRoix92tOE1OkR7B0sjhJkGA24=
github.com/spf13/cast v1.3.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
github.com/spf13/cast v1.5.0 h1:rj3WzYc11XZaIZMPKmwP96zkFEnnAmV8s6XbB2aY32w=
github.com/spf13/cast v1.5.0/go.mod h1:SpXXQ5YoyJw6s3/6cMTQuxvgRl3PCJiyaX9p6b155UU=
Expand Down
56 changes: 51 additions & 5 deletions openai/finetune_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,15 +307,61 @@ func (r *FineTuneResource) Delete(ctx context.Context, req resource.DeleteReques
return
}

bDeleted, err := r.client.FineTunes().DeleteFineTuneModel(data.Id.ValueString())
tflog.Info(ctx, "Get existing Fine-Tune...")
fineTune, err := r.client.FineTunes().GetFineTune(data.Id.ValueString())
if err != nil {
resp.Diagnostics.AddError("OpenAI Client Error", fmt.Sprintf("Unable to delete Fime Tune Model, got error: %s", err))
resp.Diagnostics.AddError("OpenAI Client Error", fmt.Sprintf("Unable to read Fine Tune, got error: %s", err))
return
}
if bDeleted {
// need to ignore is fine tune is already delete. - Missing test

// Cancel fine tune
tflog.Info(ctx, fmt.Sprintf("Fine-Tune.Status: %s", fineTune.Status))
if fineTune.Status != "succeeded" {
tflog.Info(ctx, "Cancelling Fine-Tune")
_, err = r.client.FineTunes().CancelFineTune(data.Id.ValueString())
if err != nil {
resp.Diagnostics.AddError("OpenAI Client Error", fmt.Sprintf("Unable to cancel fine tune %s, got error: %s", fineTune.Id, err))
return
}
}

// Delete result files
for _, file := range fineTune.ResultFiles {
tflog.Info(ctx, fmt.Sprintf("Deleting Fine-Tune Result File: %s", file.Id))
_, err := r.client.Files().DeleteFile(file.Id)
if err != nil {
if openaierr, ok := err.(*openai.APIError); ok {
if openaierr.HTTPStatusCode == 404 {
tflog.Info(ctx, "Fine-Tune Result File does not exist")
} else {
resp.Diagnostics.AddError("OpenAI Client Error", fmt.Sprintf("Unable to delete Result File %s, got error: %s", file.Id, err))
return
}
} else {
resp.Diagnostics.AddError("OpenAI Client Error", fmt.Sprintf("Unable to delete Result File %s, got error: %s", file.Id, err))
return
}
}
}

// Delete the fine tuned model
if fineTune.FineTunedModel != "" {
tflog.Info(ctx, fmt.Sprintf("Deleting Fine-Tune Model: %s", fineTune.FineTunedModel))
bDeleted, err := r.client.FineTunes().DeleteFineTuneModel(fineTune.FineTunedModel)
if err != nil {
if err, ok := err.(*openai.APIError); ok {
fmt.Println("openai error:", err.Code)
// Or whatever other field(s) you need
}

resp.Diagnostics.AddError("OpenAI Client Error", fmt.Sprintf("Unable to delete Fime Tune Model, got error: %s", err))
return
}
if !bDeleted {
tflog.Trace(ctx, "Fine Tune Model not deleted")
}
tflog.Trace(ctx, "Fine Tune Model deleted successfully")
} else {
tflog.Trace(ctx, "Fine Tune Model not deleted")
}
}

Expand Down
10 changes: 5 additions & 5 deletions openai/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ func (e OpenAIFineTuneModel) AttrTypes() map[string]attr.Type {
"object": types.StringType,
"model": types.StringType,
"created": types.Int64Type,
"events": types.ListType{},
"events": types.ListType{ElemType: types.ObjectType{AttrTypes: OpenAIFineTuneEventModel{}.AttrTypes()}},
"fine_tuned_model": types.StringType,
"hyperparams": types.ObjectType{},
"hyperparams": types.ObjectType{AttrTypes: OpenAIFineTuneHyperparamsModel{}.AttrTypes()},
"organization_id": types.StringType,
"status": types.StringType,
"result_files": types.ListType{},
"training_files": types.ListType{},
"validation_files": types.ListType{},
"result_files": types.ListType{ElemType: types.ObjectType{AttrTypes: OpenAIFileModel{}.AttrTypes()}},
"training_files": types.ListType{ElemType: types.ObjectType{AttrTypes: OpenAIFileModel{}.AttrTypes()}},
"validation_files": types.ListType{ElemType: types.ObjectType{AttrTypes: OpenAIFileModel{}.AttrTypes()}},
"updated_at": types.Int64Type,
}
}
Expand Down

0 comments on commit c6f7ba0

Please sign in to comment.