-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathaimodelprices.go
More file actions
127 lines (113 loc) · 4.38 KB
/
Copy pathaimodelprices.go
File metadata and controls
127 lines (113 loc) · 4.38 KB
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
package codersdk
import (
"context"
"net/http"
"time"
)
// AIModelPrice is a per-model token price used by AI Gateway to compute the
// cost of an interception.
//
// Prices are integer micro-units per million tokens, so 1000000 is $1.00 per
// million tokens. A nil price means the price is not known, which the cost
// calculation treats the same as zero. Distinguish that from an explicit 0,
// which declares the model free of charge.
type AIModelPrice struct {
Provider string `json:"provider"`
Model string `json:"model"`
InputPrice *int64 `json:"input_price"`
OutputPrice *int64 `json:"output_price"`
CacheReadPrice *int64 `json:"cache_read_price"`
CacheWritePrice *int64 `json:"cache_write_price"`
Source AIModelPriceSource `json:"source"`
CreatedAt time.Time `json:"created_at" format:"date-time"`
UpdatedAt time.Time `json:"updated_at" format:"date-time"`
}
// AIModelPriceSource is where a model price came from.
type AIModelPriceSource string
const (
// AIModelPriceSourceDefault is a price from the embedded price book.
AIModelPriceSourceDefault AIModelPriceSource = "default"
// AIModelPriceSourceCustom is a price set through the API.
AIModelPriceSourceCustom AIModelPriceSource = "custom"
)
// AIModelPriceSourceFilter selects which prices a listing reports. It is
// distinct from AIModelPriceSource because no stored price is "all".
//
// @typescript-ignore AIModelPriceSourceFilter
type AIModelPriceSourceFilter string
const (
AIModelPriceSourceFilterDefault = AIModelPriceSourceFilter(AIModelPriceSourceDefault)
AIModelPriceSourceFilterCustom = AIModelPriceSourceFilter(AIModelPriceSourceCustom)
// AIModelPriceSourceFilterAll reports every price a model holds, so a model
// carrying both appears twice.
AIModelPriceSourceFilterAll AIModelPriceSourceFilter = "all"
)
// MaxAIModelPricesBytes bounds an upsert request body.
const MaxAIModelPricesBytes = 1 << 20 // 1 MiB
// UpsertAIModelPricesRequest sets prices for the listed models. Models absent
// from the request are left untouched.
type UpsertAIModelPricesRequest struct {
Prices []AIModelPriceUpsert `json:"prices"`
}
// AIModelPriceUpsert is one model's prices in an upsert request. It carries
// only the writable fields of AIModelPrice.
type AIModelPriceUpsert struct {
Provider string `json:"provider"`
Model string `json:"model"`
InputPrice *int64 `json:"input_price"`
OutputPrice *int64 `json:"output_price"`
CacheReadPrice *int64 `json:"cache_read_price"`
CacheWritePrice *int64 `json:"cache_write_price"`
}
// AIModelPricesFilter narrows the listed model prices. An empty field does not
// filter on that attribute.
//
// @typescript-ignore AIModelPricesFilter
type AIModelPricesFilter struct {
Provider string `json:"provider,omitempty"`
Model string `json:"model,omitempty"`
// Source narrows to prices from one source. A model with both reports only
// its custom price unless this is set.
Source AIModelPriceSourceFilter `json:"source,omitempty"`
}
func (f AIModelPricesFilter) asRequestOption() RequestOption {
return func(r *http.Request) {
query := r.URL.Query()
if f.Provider != "" {
query.Set("provider", f.Provider)
}
if f.Model != "" {
query.Set("model", f.Model)
}
if f.Source != "" {
query.Set("source", string(f.Source))
}
r.URL.RawQuery = query.Encode()
}
}
// ListAIModelPrices returns the AI model prices matching the filter.
func (c *ExperimentalClient) ListAIModelPrices(ctx context.Context, filter AIModelPricesFilter) ([]AIModelPrice, error) {
res, err := c.Request(ctx, http.MethodGet, "/api/experimental/ai/model-prices", nil, filter.asRequestOption())
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return nil, ReadBodyAsError(res)
}
var prices []AIModelPrice
return prices, ReadBodyAsJSON(res, &prices)
}
// UpsertAIModelPrices sets prices for the models in req. The request is
// rejected in full if any model fails validation.
func (c *ExperimentalClient) UpsertAIModelPrices(ctx context.Context, req UpsertAIModelPricesRequest) error {
res, err := c.Request(ctx, http.MethodPost, "/api/experimental/ai/model-prices", req)
if err != nil {
return err
}
defer res.Body.Close()
if res.StatusCode != http.StatusNoContent {
return ReadBodyAsError(res)
}
return nil
}