inference.go raw
1 package govultr
2
3 import (
4 "context"
5 "fmt"
6 "net/http"
7 )
8
9 const inferencePath = "/v2/inference"
10
11 // InferenceService is the interface to interact with the Inference endpoints on the Vultr API
12 // Link: https://www.vultr.com/api/#tag/serverless-inference
13 type InferenceService interface {
14 List(ctx context.Context) ([]Inference, *http.Response, error)
15 Create(ctx context.Context, inferenceReq *InferenceCreateUpdateReq) (*Inference, *http.Response, error)
16 Get(ctx context.Context, inferenceID string) (*Inference, *http.Response, error)
17 Update(ctx context.Context, inferenceID string, inferenceReq *InferenceCreateUpdateReq) (*Inference, *http.Response, error)
18 Delete(ctx context.Context, inferenceID string) error
19 GetUsage(ctx context.Context, inferenceID string) (*InferenceUsage, *http.Response, error)
20 }
21
22 // InferenceServiceHandler handles interaction with the server methods for the Vultr API
23 type InferenceServiceHandler struct {
24 client *Client
25 }
26
27 // Inference represents a Serverless Inference subscription
28 type Inference struct {
29 ID string `json:"id"`
30 DateCreated string `json:"date_created"`
31 Label string `json:"label"`
32 APIKey string `json:"api_key"`
33 }
34
35 // inferenceSubsBase holds the entire List API response
36 type inferenceSubsBase struct {
37 Subscriptions []Inference `json:"subscriptions"`
38 }
39
40 // inferenceSubBase holds the entire Get API response
41 type inferenceSubBase struct {
42 Subscription *Inference `json:"subscription"`
43 }
44
45 // InferenceCreateUpdateReq struct used to create and update a Serverless Inference subscription
46 type InferenceCreateUpdateReq struct {
47 Label string `json:"label,omitempty"`
48 }
49
50 // InferenceChatUsage represents chat/embeddings usage details for a Serverless Inference subscription
51 type InferenceChatUsage struct {
52 CurrentTokens int `json:"current_tokens"`
53 MonthlyAllotment int `json:"monthly_allotment"`
54 Overage int `json:"overage"`
55 }
56
57 // InferenceAudioUsage represents audio generation details for a Serverless Inference subscription
58 type InferenceAudioUsage struct {
59 TTSCharacters int `json:"tts_characters"`
60 TTSSMCharacters int `json:"tts_sm_characters"`
61 }
62
63 // InferenceUsage represents chat/embeddings and audio usage for a Serverless Inference subscription
64 type InferenceUsage struct {
65 Chat InferenceChatUsage `json:"chat"`
66 Audio InferenceAudioUsage `json:"audio"`
67 }
68
69 // inferenceUsageBase represents a migration status object API response for a Serverless Inference subscription
70 type inferenceUsageBase struct {
71 Usage *InferenceUsage `json:"usage"`
72 }
73
74 // List retrieves all Serverless Inference subscriptions on your account
75 func (d *InferenceServiceHandler) List(ctx context.Context) ([]Inference, *http.Response, error) {
76 req, err := d.client.NewRequest(ctx, http.MethodGet, inferencePath, nil)
77 if err != nil {
78 return nil, nil, err
79 }
80
81 inferenceSubs := new(inferenceSubsBase)
82 resp, err := d.client.DoWithContext(ctx, req, inferenceSubs)
83 if err != nil {
84 return nil, nil, err
85 }
86
87 return inferenceSubs.Subscriptions, resp, nil
88 }
89
90 // Create will create the Serverless Inference subscription with the given parameters
91 func (d *InferenceServiceHandler) Create(ctx context.Context, inferenceReq *InferenceCreateUpdateReq) (*Inference, *http.Response, error) { //nolint:lll
92 req, err := d.client.NewRequest(ctx, http.MethodPost, inferencePath, inferenceReq)
93 if err != nil {
94 return nil, nil, err
95 }
96
97 inferenceSub := new(inferenceSubBase)
98 resp, err := d.client.DoWithContext(ctx, req, inferenceSub)
99 if err != nil {
100 return nil, nil, err
101 }
102
103 return inferenceSub.Subscription, resp, nil
104 }
105
106 // Get will get the Serverless Inference subscription with the given inferenceID
107 func (d *InferenceServiceHandler) Get(ctx context.Context, inferenceID string) (*Inference, *http.Response, error) {
108 uri := fmt.Sprintf("%s/%s", inferencePath, inferenceID)
109
110 req, err := d.client.NewRequest(ctx, http.MethodGet, uri, nil)
111 if err != nil {
112 return nil, nil, err
113 }
114
115 inferenceSub := new(inferenceSubBase)
116 resp, err := d.client.DoWithContext(ctx, req, inferenceSub)
117 if err != nil {
118 return nil, nil, err
119 }
120
121 return inferenceSub.Subscription, resp, nil
122 }
123
124 // Update will update the Serverless Inference subscription with the given parameters
125 func (d *InferenceServiceHandler) Update(ctx context.Context, inferenceID string, inferenceReq *InferenceCreateUpdateReq) (*Inference, *http.Response, error) { //nolint:lll
126 uri := fmt.Sprintf("%s/%s", inferencePath, inferenceID)
127
128 req, err := d.client.NewRequest(ctx, http.MethodPatch, uri, inferenceReq)
129 if err != nil {
130 return nil, nil, err
131 }
132
133 inferenceSub := new(inferenceSubBase)
134 resp, err := d.client.DoWithContext(ctx, req, inferenceSub)
135 if err != nil {
136 return nil, nil, err
137 }
138
139 return inferenceSub.Subscription, resp, nil
140 }
141
142 // Delete a Serverless Inference subscription. All data will be permanently lost
143 func (d *InferenceServiceHandler) Delete(ctx context.Context, inferenceID string) error {
144 uri := fmt.Sprintf("%s/%s", inferencePath, inferenceID)
145
146 req, err := d.client.NewRequest(ctx, http.MethodDelete, uri, nil)
147 if err != nil {
148 return err
149 }
150
151 _, err = d.client.DoWithContext(ctx, req, nil)
152 return err
153 }
154
155 // GetUsage retrieves chat/embeddings and audio generation usage information for your Serverless Inference subscription
156 func (d *InferenceServiceHandler) GetUsage(ctx context.Context, inferenceID string) (*InferenceUsage, *http.Response, error) {
157 uri := fmt.Sprintf("%s/%s/usage", inferencePath, inferenceID)
158
159 req, err := d.client.NewRequest(ctx, http.MethodGet, uri, nil)
160 if err != nil {
161 return nil, nil, err
162 }
163
164 inferenceUsage := new(inferenceUsageBase)
165 resp, err := d.client.DoWithContext(ctx, req, inferenceUsage)
166 if err != nil {
167 return nil, nil, err
168 }
169
170 return inferenceUsage.Usage, resp, nil
171 }
172