|
| 1 | +package llm_inference |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + llm_inference "github.com/scaleway/scaleway-sdk-go/api/llm_inference/v1beta1" |
| 6 | + "time" |
| 7 | + "github.com/scaleway/scaleway-cli/v2/internal/human" |
| 8 | + "github.com/scaleway/scaleway-cli/v2/internal/core" |
| 9 | + "reflect" |
| 10 | + "github.com/scaleway/scaleway-sdk-go/scw" |
| 11 | + "context" |
| 12 | + "github.com/fatih/color" |
| 13 | + "net/http" |
| 14 | + "errors" |
| 15 | +) |
| 16 | + |
| 17 | +const ( |
| 18 | + deploymentActionTimeout = 40 * time.Minute |
| 19 | +) |
| 20 | + |
| 21 | +var ( |
| 22 | + deployementStateMarshalSpecs = human.EnumMarshalSpecs{ |
| 23 | + llm_inference.DeploymentStatusCreating: &human.EnumMarshalSpec{Attribute: color.FgBlue}, |
| 24 | + llm_inference.DeploymentStatusDeploying: &human.EnumMarshalSpec{Attribute: color.FgBlue}, |
| 25 | + llm_inference.DeploymentStatusDeleting: &human.EnumMarshalSpec{Attribute: color.FgBlue}, |
| 26 | + llm_inference.DeploymentStatusError: &human.EnumMarshalSpec{Attribute: color.FgRed}, |
| 27 | + llm_inference.DeploymentStatusReady: &human.EnumMarshalSpec{Attribute: color.FgGreen}, |
| 28 | + llm_inference.DeploymentStatusLocked: &human.EnumMarshalSpec{Attribute: color.FgRed}, |
| 29 | + } |
| 30 | +) |
| 31 | + |
| 32 | +func DeploymentMarshalerFunc(i interface{}, opt *human.MarshalOpt) (string, error) { |
| 33 | + type tmp llm_inference.Deployment |
| 34 | + deployment := tmp(i.(llm_inference.Deployment)) |
| 35 | + opt.Sections = []*human.MarshalSection{ |
| 36 | + { |
| 37 | + FieldName: "Endpoints", |
| 38 | + Title: "Endpoints", |
| 39 | + }, |
| 40 | + } |
| 41 | + str, err := human.Marshal(deployment, opt) |
| 42 | + if err != nil { |
| 43 | + return "", err |
| 44 | + } |
| 45 | + return str, nil |
| 46 | +} |
| 47 | + |
| 48 | +func deploymentCreateBuilder(c *core.Command) *core.Command { |
| 49 | + c.ArgSpecs.GetByName("node-type").AutoCompleteFunc = autocompleteDeploymentNodeType |
| 50 | + type llmInferenceEndpointSpecCustom struct { |
| 51 | + *llm_inference.EndpointSpec |
| 52 | + IsPublic bool `json:"is-public"` |
| 53 | + } |
| 54 | + |
| 55 | + type llmInferenceCreateDeploymentRequestCustom struct { |
| 56 | + *llm_inference.CreateDeploymentRequest |
| 57 | + Endpoints []*llmInferenceEndpointSpecCustom `json:"endpoints"` |
| 58 | + } |
| 59 | + |
| 60 | + c.ArgSpecs.AddBefore("endpoints.{index}.private-network.private-network-id", &core.ArgSpec{ |
| 61 | + Name: "endpoints.{index}.is-public", |
| 62 | + Short: "Will configure your public endpoint if true", |
| 63 | + Required: false, |
| 64 | + Default: core.DefaultValueSetter("false"), |
| 65 | + }) |
| 66 | + |
| 67 | + c.ArgsType = reflect.TypeOf(llmInferenceCreateDeploymentRequestCustom{}) |
| 68 | + |
| 69 | + c.WaitFunc = func(ctx context.Context, argsI, respI interface{}) (interface{}, error) { |
| 70 | + api := llm_inference.NewAPI(core.ExtractClient(ctx)) |
| 71 | + return api.WaitForDeployment(&llm_inference.WaitForDeploymentRequest{ |
| 72 | + DeploymentId: respI.(*llm_inference.Deployment).ID, |
| 73 | + Region: respI.(*llm_inference.Deployment).Region, |
| 74 | + Status: respI.(*llm_inference.Deployment).Status, |
| 75 | + Timeout: scw.TimeDurationPtr(deploymentActionTimeout), |
| 76 | + RetryInterval: core.DefaultRetryInterval, |
| 77 | + }) |
| 78 | + } |
| 79 | + c.Interceptor = func(ctx context.Context, argsI interface{}, runner core.CommandRunner) (interface{}, error) { |
| 80 | + deploymentCreateCustomRequest := argsI.(*llmInferenceCreateDeploymentRequestCustom) |
| 81 | + deploymentRequest := deploymentCreateCustomRequest.CreateDeploymentRequest |
| 82 | + if deploymentCreateCustomRequest.Endpoints == nil { |
| 83 | + publicEndpoint := &llm_inference.EndpointSpecPublic{} |
| 84 | + endpoint := llm_inference.EndpointSpec{ |
| 85 | + Public: publicEndpoint, |
| 86 | + PrivateNetwork: nil, |
| 87 | + DisableAuth: false, |
| 88 | + } |
| 89 | + deploymentRequest.Endpoints = append(deploymentRequest.Endpoints, &endpoint) |
| 90 | + return runner(ctx, deploymentRequest) |
| 91 | + } |
| 92 | + for _, endpoint := range deploymentCreateCustomRequest.Endpoints { |
| 93 | + publicEndpoint := &llm_inference.EndpointSpecPublic{} |
| 94 | + if !endpoint.IsPublic { |
| 95 | + publicEndpoint = nil |
| 96 | + } |
| 97 | + privateNetwork := &llm_inference.EndpointSpecPrivateNetwork{} |
| 98 | + if endpoint.EndpointSpec == nil { |
| 99 | + privateNetwork = nil |
| 100 | + } else { |
| 101 | + privateNetwork.PrivateNetworkID = endpoint.PrivateNetwork.PrivateNetworkID |
| 102 | + } |
| 103 | + endpoint := llm_inference.EndpointSpec{ |
| 104 | + Public: publicEndpoint, |
| 105 | + PrivateNetwork: privateNetwork, |
| 106 | + DisableAuth: endpoint.DisableAuth, |
| 107 | + } |
| 108 | + deploymentRequest.Endpoints = append(deploymentRequest.Endpoints, &endpoint) |
| 109 | + } |
| 110 | + |
| 111 | + return runner(ctx, deploymentRequest) |
| 112 | + } |
| 113 | + |
| 114 | + return c |
| 115 | +} |
| 116 | + |
| 117 | +func deploymentDeleteBuilder(c *core.Command) *core.Command { |
| 118 | + c.WaitFunc = func(ctx context.Context, argsI, respI interface{}) (interface{}, error) { |
| 119 | + api := llm_inference.NewAPI(core.ExtractClient(ctx)) |
| 120 | + deployment, err := api.WaitForDeployment(&llm_inference.WaitForDeploymentRequest{ |
| 121 | + DeploymentId: respI.(*llm_inference.Deployment).ID, |
| 122 | + Region: respI.(*llm_inference.Deployment).Region, |
| 123 | + Status: respI.(*llm_inference.Deployment).Status, |
| 124 | + Timeout: scw.TimeDurationPtr(deploymentActionTimeout), |
| 125 | + RetryInterval: core.DefaultRetryInterval, |
| 126 | + }) |
| 127 | + if err != nil { |
| 128 | + notFoundError := &scw.ResourceNotFoundError{} |
| 129 | + responseError := &scw.ResponseError{} |
| 130 | + if errors.As(err, &responseError) && responseError.StatusCode == http.StatusNotFound || errors.As(err, ¬FoundError) { |
| 131 | + return &core.SuccessResult{ |
| 132 | + Resource: "deployment", |
| 133 | + Verb: "delete", |
| 134 | + }, nil |
| 135 | + } |
| 136 | + return nil, err |
| 137 | + } |
| 138 | + return deployment, nil |
| 139 | + } |
| 140 | + return c |
| 141 | +} |
| 142 | + |
| 143 | +var completeListNodeTypesCache *llm_inference.ListNodeTypesResponse |
| 144 | + |
| 145 | +func autocompleteDeploymentNodeType(ctx context.Context, prefix string, request any) core.AutocompleteSuggestions { |
| 146 | + req := request.(*llm_inference.CreateDeploymentRequest) |
| 147 | + suggestions := core.AutocompleteSuggestions(nil) |
| 148 | + |
| 149 | + client := core.ExtractClient(ctx) |
| 150 | + api := llm_inference.NewAPI(client) |
| 151 | + |
| 152 | + if completeListNodeTypesCache == nil { |
| 153 | + res, err := api.ListNodeTypes(&llm_inference.ListNodeTypesRequest{ |
| 154 | + Region: req.Region, |
| 155 | + }) |
| 156 | + if err != nil { |
| 157 | + return nil |
| 158 | + } |
| 159 | + completeListNodeTypesCache = res |
| 160 | + } |
| 161 | + |
| 162 | + for _, nodeType := range completeListNodeTypesCache.NodeTypes { |
| 163 | + if strings.HasPrefix(nodeType.Name, prefix) { |
| 164 | + suggestions = append(suggestions, nodeType.Name) |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + return suggestions |
| 169 | +} |
0 commit comments