-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.go
More file actions
222 lines (199 loc) · 8.05 KB
/
model.go
File metadata and controls
222 lines (199 loc) · 8.05 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
package together
import (
"context"
"net/http"
"net/url"
"slices"
"github.com/togethercomputer/together-go/internal/apijson"
"github.com/togethercomputer/together-go/internal/apiquery"
"github.com/togethercomputer/together-go/internal/requestconfig"
"github.com/togethercomputer/together-go/option"
"github.com/togethercomputer/together-go/packages/param"
"github.com/togethercomputer/together-go/packages/respjson"
"github.com/togethercomputer/together-go/shared/constant"
)
// ModelService contains methods and other services that help with interacting with
// the together API.
//
// Note, unlike clients, this service does not read variables from the environment
// automatically. You should not instantiate this service directly, and instead use
// the [NewModelService] method instead.
type ModelService struct {
Options []option.RequestOption
Uploads ModelUploadService
}
// NewModelService generates a new service that applies the given options to each
// request. These options are applied after the parent client's options (if there
// is one), and before any request-specific options.
func NewModelService(opts ...option.RequestOption) (r ModelService) {
r = ModelService{}
r.Options = opts
r.Uploads = NewModelUploadService(opts...)
return
}
// Lists all of Together's open-source models and metadata including pricing, chat
// template, and context.
func (r *ModelService) List(ctx context.Context, query ModelListParams, opts ...option.RequestOption) (res *[]ModelObject, err error) {
opts = slices.Concat(r.Options, opts)
path := "models"
err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, query, &res, opts...)
return res, err
}
// Upload a custom model or adapter from Hugging Face or S3
func (r *ModelService) Upload(ctx context.Context, body ModelUploadParams, opts ...option.RequestOption) (res *ModelUploadResponse, err error) {
opts = slices.Concat(r.Options, opts)
path := "models"
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, body, &res, opts...)
return res, err
}
type ModelObject struct {
ID string `json:"id" api:"required"`
Created int64 `json:"created" api:"required"`
// The object type, which is always `model`.
Object constant.Model `json:"object" default:"model"`
// Any of "chat", "language", "code", "image", "embedding", "moderation", "rerank".
Type ModelObjectType `json:"type" api:"required"`
ContextLength int64 `json:"context_length"`
DisplayName string `json:"display_name"`
License string `json:"license"`
Link string `json:"link"`
Organization string `json:"organization"`
Pricing ModelObjectPricing `json:"pricing"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
ID respjson.Field
Created respjson.Field
Object respjson.Field
Type respjson.Field
ContextLength respjson.Field
DisplayName respjson.Field
License respjson.Field
Link respjson.Field
Organization respjson.Field
Pricing respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r ModelObject) RawJSON() string { return r.JSON.raw }
func (r *ModelObject) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
type ModelObjectType string
const (
ModelObjectTypeChat ModelObjectType = "chat"
ModelObjectTypeLanguage ModelObjectType = "language"
ModelObjectTypeCode ModelObjectType = "code"
ModelObjectTypeImage ModelObjectType = "image"
ModelObjectTypeEmbedding ModelObjectType = "embedding"
ModelObjectTypeModeration ModelObjectType = "moderation"
ModelObjectTypeRerank ModelObjectType = "rerank"
)
type ModelObjectPricing struct {
Base float64 `json:"base" api:"required"`
Finetune float64 `json:"finetune" api:"required"`
Hourly float64 `json:"hourly" api:"required"`
Input float64 `json:"input" api:"required"`
Output float64 `json:"output" api:"required"`
CachedInput float64 `json:"cached_input"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
Base respjson.Field
Finetune respjson.Field
Hourly respjson.Field
Input respjson.Field
Output respjson.Field
CachedInput respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r ModelObjectPricing) RawJSON() string { return r.JSON.raw }
func (r *ModelObjectPricing) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
type ModelUploadResponse struct {
Data ModelUploadResponseData `json:"data" api:"required"`
Message string `json:"message" api:"required"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
Data respjson.Field
Message respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r ModelUploadResponse) RawJSON() string { return r.JSON.raw }
func (r *ModelUploadResponse) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
type ModelUploadResponseData struct {
JobID string `json:"job_id" api:"required"`
ModelID string `json:"model_id" api:"required"`
ModelName string `json:"model_name" api:"required"`
ModelSource string `json:"model_source" api:"required"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
JobID respjson.Field
ModelID respjson.Field
ModelName respjson.Field
ModelSource respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r ModelUploadResponseData) RawJSON() string { return r.JSON.raw }
func (r *ModelUploadResponseData) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
type ModelListParams struct {
// Filter models to only return dedicated models
Dedicated param.Opt[bool] `query:"dedicated,omitzero" json:"-"`
paramObj
}
// URLQuery serializes [ModelListParams]'s query parameters as `url.Values`.
func (r ModelListParams) URLQuery() (v url.Values, err error) {
return apiquery.MarshalWithSettings(r, apiquery.QuerySettings{
ArrayFormat: apiquery.ArrayQueryFormatComma,
NestedFormat: apiquery.NestedQueryFormatBrackets,
})
}
type ModelUploadParams struct {
// The name to give to your uploaded model
ModelName string `json:"model_name" api:"required"`
// The source location of the model (Hugging Face repo or S3 path)
ModelSource string `json:"model_source" api:"required"`
// The base model to use for an adapter if setting it to run against a serverless
// pool. Only used for model_type `adapter`.
BaseModel param.Opt[string] `json:"base_model,omitzero"`
// A description of your model
Description param.Opt[string] `json:"description,omitzero"`
// Hugging Face token (if uploading from Hugging Face)
HfToken param.Opt[string] `json:"hf_token,omitzero"`
// The lora pool to use for an adapter if setting it to run against, say, a
// dedicated pool. Only used for model_type `adapter`.
LoraModel param.Opt[string] `json:"lora_model,omitzero"`
// Whether the model is a full model or an adapter
//
// Any of "model", "adapter".
ModelType ModelUploadParamsModelType `json:"model_type,omitzero"`
paramObj
}
func (r ModelUploadParams) MarshalJSON() (data []byte, err error) {
type shadow ModelUploadParams
return param.MarshalObject(r, (*shadow)(&r))
}
func (r *ModelUploadParams) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
// Whether the model is a full model or an adapter
type ModelUploadParamsModelType string
const (
ModelUploadParamsModelTypeModel ModelUploadParamsModelType = "model"
ModelUploadParamsModelTypeAdapter ModelUploadParamsModelType = "adapter"
)