-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathdeployment.go
135 lines (113 loc) · 4.45 KB
/
deployment.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package replicate
import (
"context"
"encoding/json"
"fmt"
"net/http"
)
type Deployment struct {
Owner string `json:"owner"`
Name string `json:"name"`
CurrentRelease DeploymentRelease `json:"current_release"`
rawJSON json.RawMessage `json:"-"`
}
type DeploymentRelease struct {
Number int `json:"number"`
Model string `json:"model"`
Version string `json:"version"`
CreatedAt string `json:"created_at"`
CreatedBy Account `json:"created_by"`
Configuration DeploymentConfiguration `json:"configuration"`
}
type DeploymentConfiguration struct {
Hardware string `json:"hardware"`
MinInstances int `json:"min_instances"`
MaxInstances int `json:"max_instances"`
}
func (d *Deployment) RawJSON() json.RawMessage {
return d.rawJSON
}
var _ json.Unmarshaler = (*Deployment)(nil)
func (d *Deployment) UnmarshalJSON(data []byte) error {
d.rawJSON = data
type Alias Deployment
alias := &struct{ *Alias }{Alias: (*Alias)(d)}
return json.Unmarshal(data, alias)
}
// CreateDeploymentPrediction sends a request to the Replicate API to create a prediction using the specified deployment.
func (c *Client) CreatePredictionWithDeployment(ctx context.Context, deploymentOwner string, deploymentName string, input PredictionInput, webhook *Webhook, stream bool) (*Prediction, error) {
path := fmt.Sprintf("/deployments/%s/%s/predictions", deploymentOwner, deploymentName)
req, err := c.createPredictionRequest(ctx, path, nil, input, webhook, stream)
if err != nil {
return nil, err
}
prediction := &Prediction{}
if err := c.do(req, prediction); err != nil {
return nil, fmt.Errorf("failed to create prediction with deployment: %w", err)
}
return prediction, nil
}
// GetDeployment retrieves the details of a specific deployment.
func (c *Client) GetDeployment(ctx context.Context, deploymentOwner string, deploymentName string) (*Deployment, error) {
deployment := &Deployment{}
path := fmt.Sprintf("/deployments/%s/%s", deploymentOwner, deploymentName)
err := c.fetch(ctx, http.MethodGet, path, nil, deployment)
if err != nil {
return nil, fmt.Errorf("failed to get deployment: %w", err)
}
return deployment, nil
}
// ListDeployments retrieves a list of deployments associated with the current account.
func (c *Client) ListDeployments(ctx context.Context) (*Page[Deployment], error) {
response := &Page[Deployment]{}
path := "/deployments"
err := c.fetch(ctx, http.MethodGet, path, nil, &response)
if err != nil {
return nil, fmt.Errorf("failed to list deployments: %w", err)
}
return response, nil
}
type CreateDeploymentOptions struct {
Name string `json:"name"`
Model string `json:"model"`
Version string `json:"version"`
Hardware string `json:"hardware"`
MinInstances int `json:"min_instances"`
MaxInstances int `json:"max_instances"`
}
// CreateDeployment creates a new deployment.
func (c *Client) CreateDeployment(ctx context.Context, options CreateDeploymentOptions) (*Deployment, error) {
deployment := &Deployment{}
path := "/deployments"
err := c.fetch(ctx, http.MethodPost, path, options, deployment)
if err != nil {
return nil, fmt.Errorf("failed to create deployment: %w", err)
}
return deployment, nil
}
type UpdateDeploymentOptions struct {
Model *string `json:"model,omitempty"`
Version *string `json:"version,omitempty"`
Hardware *string `json:"hardware,omitempty"`
MinInstances *int `json:"min_instances,omitempty"`
MaxInstances *int `json:"max_instances,omitempty"`
}
// UpdateDeployment updates an existing deployment.
func (c *Client) UpdateDeployment(ctx context.Context, deploymentOwner string, deploymentName string, options UpdateDeploymentOptions) (*Deployment, error) {
deployment := &Deployment{}
path := fmt.Sprintf("/deployments/%s/%s", deploymentOwner, deploymentName)
err := c.fetch(ctx, http.MethodPatch, path, options, deployment)
if err != nil {
return nil, fmt.Errorf("failed to update deployment: %w", err)
}
return deployment, nil
}
// DeleteDeployment deletes an existing deployment.
func (c *Client) DeleteDeployment(ctx context.Context, deploymentOwner string, deploymentName string) error {
path := fmt.Sprintf("/deployments/%s/%s", deploymentOwner, deploymentName)
err := c.fetch(ctx, http.MethodDelete, path, nil, nil)
if err != nil {
return fmt.Errorf("failed to delete deployment: %w", err)
}
return nil
}