-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage.go
More file actions
288 lines (254 loc) · 10 KB
/
image.go
File metadata and controls
288 lines (254 loc) · 10 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
package together
import (
"context"
"encoding/json"
"net/http"
"slices"
"github.com/togethercomputer/together-go/internal/apijson"
"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"
)
// ImageService 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 [NewImageService] method instead.
type ImageService struct {
Options []option.RequestOption
}
// NewImageService 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 NewImageService(opts ...option.RequestOption) (r ImageService) {
r = ImageService{}
r.Options = opts
return
}
// Use an image model to generate an image for a given prompt.
func (r *ImageService) Generate(ctx context.Context, body ImageGenerateParams, opts ...option.RequestOption) (res *ImageFile, err error) {
opts = slices.Concat(r.Options, opts)
path := "images/generations"
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, body, &res, opts...)
return res, err
}
type ImageDataB64 struct {
B64Json string `json:"b64_json" api:"required"`
Index int64 `json:"index" api:"required"`
// Any of "b64_json".
Type ImageDataB64Type `json:"type" api:"required"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
B64Json respjson.Field
Index respjson.Field
Type respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r ImageDataB64) RawJSON() string { return r.JSON.raw }
func (r *ImageDataB64) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
type ImageDataB64Type string
const (
ImageDataB64TypeB64Json ImageDataB64Type = "b64_json"
)
type ImageDataURL struct {
Index int64 `json:"index" api:"required"`
// Any of "url".
Type ImageDataURLType `json:"type" api:"required"`
URL string `json:"url" api:"required"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
Index respjson.Field
Type respjson.Field
URL respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r ImageDataURL) RawJSON() string { return r.JSON.raw }
func (r *ImageDataURL) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
type ImageDataURLType string
const (
ImageDataURLTypeURL ImageDataURLType = "url"
)
type ImageFile struct {
ID string `json:"id" api:"required"`
Data []ImageFileDataUnion `json:"data" api:"required"`
Model string `json:"model" api:"required"`
// The object type, which is always `list`.
Object constant.List `json:"object" default:"list"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
ID respjson.Field
Data respjson.Field
Model respjson.Field
Object respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r ImageFile) RawJSON() string { return r.JSON.raw }
func (r *ImageFile) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
// ImageFileDataUnion contains all possible properties and values from
// [ImageDataB64], [ImageDataURL].
//
// Use the [ImageFileDataUnion.AsAny] method to switch on the variant.
//
// Use the methods beginning with 'As' to cast the union to one of its variants.
type ImageFileDataUnion struct {
// This field is from variant [ImageDataB64].
B64Json string `json:"b64_json"`
Index int64 `json:"index"`
// Any of "b64_json", "url".
Type string `json:"type"`
// This field is from variant [ImageDataURL].
URL string `json:"url"`
JSON struct {
B64Json respjson.Field
Index respjson.Field
Type respjson.Field
URL respjson.Field
raw string
} `json:"-"`
}
// anyImageFileData is implemented by each variant of [ImageFileDataUnion] to add
// type safety for the return type of [ImageFileDataUnion.AsAny]
type anyImageFileData interface {
implImageFileDataUnion()
}
func (ImageDataB64) implImageFileDataUnion() {}
func (ImageDataURL) implImageFileDataUnion() {}
// Use the following switch statement to find the correct variant
//
// switch variant := ImageFileDataUnion.AsAny().(type) {
// case together.ImageDataB64:
// case together.ImageDataURL:
// default:
// fmt.Errorf("no variant present")
// }
func (u ImageFileDataUnion) AsAny() anyImageFileData {
switch u.Type {
case "b64_json":
return u.AsB64Json()
case "url":
return u.AsURL()
}
return nil
}
func (u ImageFileDataUnion) AsB64Json() (v ImageDataB64) {
apijson.UnmarshalRoot(json.RawMessage(u.JSON.raw), &v)
return
}
func (u ImageFileDataUnion) AsURL() (v ImageDataURL) {
apijson.UnmarshalRoot(json.RawMessage(u.JSON.raw), &v)
return
}
// Returns the unmodified JSON received from the API
func (u ImageFileDataUnion) RawJSON() string { return u.JSON.raw }
func (r *ImageFileDataUnion) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
type ImageGenerateParams struct {
// The model to use for image generation.
//
// [See all of Together AI's image models](https://docs.together.ai/docs/serverless-models#image-models)
Model ImageGenerateParamsModel `json:"model,omitzero" api:"required"`
// A description of the desired images. Maximum length varies by model.
Prompt string `json:"prompt" api:"required"`
// If true, disables the safety checker for image generation.
DisableSafetyChecker param.Opt[bool] `json:"disable_safety_checker,omitzero"`
// Adjusts the alignment of the generated image with the input prompt. Higher
// values (e.g., 8-10) make the output more faithful to the prompt, while lower
// values (e.g., 1-5) encourage more creative freedom.
GuidanceScale param.Opt[float64] `json:"guidance_scale,omitzero"`
// Height of the image to generate in number of pixels.
Height param.Opt[int64] `json:"height,omitzero"`
// URL of an image to use for image models that support it.
ImageURL param.Opt[string] `json:"image_url,omitzero"`
// Number of image results to generate.
N param.Opt[int64] `json:"n,omitzero"`
// The prompt or prompts not to guide the image generation.
NegativePrompt param.Opt[string] `json:"negative_prompt,omitzero"`
// Seed used for generation. Can be used to reproduce image generations.
Seed param.Opt[int64] `json:"seed,omitzero"`
// Number of generation steps.
Steps param.Opt[int64] `json:"steps,omitzero"`
// Width of the image to generate in number of pixels.
Width param.Opt[int64] `json:"width,omitzero"`
// An array of objects that define LoRAs (Low-Rank Adaptations) to influence the
// generated image.
ImageLoras []ImageGenerateParamsImageLora `json:"image_loras,omitzero"`
// The format of the image response. Can be either be `jpeg` or `png`. Defaults to
// `jpeg`.
//
// Any of "jpeg", "png".
OutputFormat ImageGenerateParamsOutputFormat `json:"output_format,omitzero"`
// An array of image URLs that guide the overall appearance and style of the
// generated image. These reference images influence the visual characteristics
// consistently across the generation.
ReferenceImages []string `json:"reference_images,omitzero"`
// Format of the image response. Can be either a base64 string or a URL.
//
// Any of "base64", "url".
ResponseFormat ImageGenerateParamsResponseFormat `json:"response_format,omitzero"`
paramObj
}
func (r ImageGenerateParams) MarshalJSON() (data []byte, err error) {
type shadow ImageGenerateParams
return param.MarshalObject(r, (*shadow)(&r))
}
func (r *ImageGenerateParams) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
// The model to use for image generation.
//
// [See all of Together AI's image models](https://docs.together.ai/docs/serverless-models#image-models)
type ImageGenerateParamsModel string
const (
ImageGenerateParamsModelBlackForestLabsFlux1SchnellFree ImageGenerateParamsModel = "black-forest-labs/FLUX.1-schnell-Free"
ImageGenerateParamsModelBlackForestLabsFlux1Schnell ImageGenerateParamsModel = "black-forest-labs/FLUX.1-schnell"
ImageGenerateParamsModelBlackForestLabsFlux1_1Pro ImageGenerateParamsModel = "black-forest-labs/FLUX.1.1-pro"
)
// The properties Path, Scale are required.
type ImageGenerateParamsImageLora struct {
// The URL of the LoRA to apply (e.g.
// https://huggingface.co/strangerzonehf/Flux-Midjourney-Mix2-LoRA).
Path string `json:"path" api:"required"`
// The strength of the LoRA's influence. Most LoRA's recommend a value of 1.
Scale float64 `json:"scale" api:"required"`
paramObj
}
func (r ImageGenerateParamsImageLora) MarshalJSON() (data []byte, err error) {
type shadow ImageGenerateParamsImageLora
return param.MarshalObject(r, (*shadow)(&r))
}
func (r *ImageGenerateParamsImageLora) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
// The format of the image response. Can be either be `jpeg` or `png`. Defaults to
// `jpeg`.
type ImageGenerateParamsOutputFormat string
const (
ImageGenerateParamsOutputFormatJpeg ImageGenerateParamsOutputFormat = "jpeg"
ImageGenerateParamsOutputFormatPng ImageGenerateParamsOutputFormat = "png"
)
// Format of the image response. Can be either a base64 string or a URL.
type ImageGenerateParamsResponseFormat string
const (
ImageGenerateParamsResponseFormatBase64 ImageGenerateParamsResponseFormat = "base64"
ImageGenerateParamsResponseFormatURL ImageGenerateParamsResponseFormat = "url"
)