33package openai
44
55import (
6- "bytes"
7- "context"
86 "encoding/json"
9- "io"
10- "mime/multipart"
11- "net/http"
12- "slices"
137
14- "github.com/openai/openai-go/v3/internal/apiform"
158 "github.com/openai/openai-go/v3/internal/apijson"
16- "github.com/openai/openai-go/v3/internal/requestconfig"
179 "github.com/openai/openai-go/v3/option"
1810 "github.com/openai/openai-go/v3/packages/respjson"
19- "github.com/openai/openai-go/v3/shared/constant"
2011)
2112
2213// BetaChatKitService contains methods and other services that help with
@@ -42,15 +33,6 @@ func NewBetaChatKitService(opts ...option.RequestOption) (r BetaChatKitService)
4233 return
4334}
4435
45- // Upload a ChatKit file
46- func (r * BetaChatKitService ) UploadFile (ctx context.Context , body BetaChatKitUploadFileParams , opts ... option.RequestOption ) (res * BetaChatKitUploadFileResponseUnion , err error ) {
47- opts = slices .Concat (r .Options , opts )
48- opts = append ([]option.RequestOption {option .WithHeader ("OpenAI-Beta" , "chatkit_beta=v1" )}, opts ... )
49- path := "chatkit/files"
50- err = requestconfig .ExecuteNewRequest (ctx , http .MethodPost , path , body , & res , opts ... )
51- return
52- }
53-
5436// Workflow metadata and state returned for the session.
5537type ChatKitWorkflow struct {
5638 // Identifier of the workflow backing the session.
@@ -141,165 +123,3 @@ func (r ChatKitWorkflowTracing) RawJSON() string { return r.JSON.raw }
141123func (r * ChatKitWorkflowTracing ) UnmarshalJSON (data []byte ) error {
142124 return apijson .UnmarshalRoot (data , r )
143125}
144-
145- // Metadata for a non-image file uploaded through ChatKit.
146- type FilePart struct {
147- // Unique identifier for the uploaded file.
148- ID string `json:"id,required"`
149- // MIME type reported for the uploaded file. Defaults to null when unknown.
150- MimeType string `json:"mime_type,required"`
151- // Original filename supplied by the uploader. Defaults to null when unnamed.
152- Name string `json:"name,required"`
153- // Type discriminator that is always `file`.
154- Type constant.File `json:"type,required"`
155- // Signed URL for downloading the uploaded file. Defaults to null when no download
156- // link is available.
157- UploadURL string `json:"upload_url,required"`
158- // JSON contains metadata for fields, check presence with [respjson.Field.Valid].
159- JSON struct {
160- ID respjson.Field
161- MimeType respjson.Field
162- Name respjson.Field
163- Type respjson.Field
164- UploadURL respjson.Field
165- ExtraFields map [string ]respjson.Field
166- raw string
167- } `json:"-"`
168- }
169-
170- // Returns the unmodified JSON received from the API
171- func (r FilePart ) RawJSON () string { return r .JSON .raw }
172- func (r * FilePart ) UnmarshalJSON (data []byte ) error {
173- return apijson .UnmarshalRoot (data , r )
174- }
175-
176- // Metadata for an image uploaded through ChatKit.
177- type ImagePart struct {
178- // Unique identifier for the uploaded image.
179- ID string `json:"id,required"`
180- // MIME type of the uploaded image.
181- MimeType string `json:"mime_type,required"`
182- // Original filename for the uploaded image. Defaults to null when unnamed.
183- Name string `json:"name,required"`
184- // Preview URL that can be rendered inline for the image.
185- PreviewURL string `json:"preview_url,required"`
186- // Type discriminator that is always `image`.
187- Type constant.Image `json:"type,required"`
188- // Signed URL for downloading the uploaded image. Defaults to null when no download
189- // link is available.
190- UploadURL string `json:"upload_url,required"`
191- // JSON contains metadata for fields, check presence with [respjson.Field.Valid].
192- JSON struct {
193- ID respjson.Field
194- MimeType respjson.Field
195- Name respjson.Field
196- PreviewURL respjson.Field
197- Type respjson.Field
198- UploadURL respjson.Field
199- ExtraFields map [string ]respjson.Field
200- raw string
201- } `json:"-"`
202- }
203-
204- // Returns the unmodified JSON received from the API
205- func (r ImagePart ) RawJSON () string { return r .JSON .raw }
206- func (r * ImagePart ) UnmarshalJSON (data []byte ) error {
207- return apijson .UnmarshalRoot (data , r )
208- }
209-
210- // BetaChatKitUploadFileResponseUnion contains all possible properties and values
211- // from [FilePart], [ImagePart].
212- //
213- // Use the [BetaChatKitUploadFileResponseUnion.AsAny] method to switch on the
214- // variant.
215- //
216- // Use the methods beginning with 'As' to cast the union to one of its variants.
217- type BetaChatKitUploadFileResponseUnion struct {
218- ID string `json:"id"`
219- MimeType string `json:"mime_type"`
220- Name string `json:"name"`
221- // Any of "file", "image".
222- Type string `json:"type"`
223- UploadURL string `json:"upload_url"`
224- // This field is from variant [ImagePart].
225- PreviewURL string `json:"preview_url"`
226- JSON struct {
227- ID respjson.Field
228- MimeType respjson.Field
229- Name respjson.Field
230- Type respjson.Field
231- UploadURL respjson.Field
232- PreviewURL respjson.Field
233- raw string
234- } `json:"-"`
235- }
236-
237- // anyBetaChatKitUploadFileResponse is implemented by each variant of
238- // [BetaChatKitUploadFileResponseUnion] to add type safety for the return type of
239- // [BetaChatKitUploadFileResponseUnion.AsAny]
240- type anyBetaChatKitUploadFileResponse interface {
241- implBetaChatKitUploadFileResponseUnion ()
242- }
243-
244- func (FilePart ) implBetaChatKitUploadFileResponseUnion () {}
245- func (ImagePart ) implBetaChatKitUploadFileResponseUnion () {}
246-
247- // Use the following switch statement to find the correct variant
248- //
249- // switch variant := BetaChatKitUploadFileResponseUnion.AsAny().(type) {
250- // case openai.FilePart:
251- // case openai.ImagePart:
252- // default:
253- // fmt.Errorf("no variant present")
254- // }
255- func (u BetaChatKitUploadFileResponseUnion ) AsAny () anyBetaChatKitUploadFileResponse {
256- switch u .Type {
257- case "file" :
258- return u .AsFile ()
259- case "image" :
260- return u .AsImage ()
261- }
262- return nil
263- }
264-
265- func (u BetaChatKitUploadFileResponseUnion ) AsFile () (v FilePart ) {
266- apijson .UnmarshalRoot (json .RawMessage (u .JSON .raw ), & v )
267- return
268- }
269-
270- func (u BetaChatKitUploadFileResponseUnion ) AsImage () (v ImagePart ) {
271- apijson .UnmarshalRoot (json .RawMessage (u .JSON .raw ), & v )
272- return
273- }
274-
275- // Returns the unmodified JSON received from the API
276- func (u BetaChatKitUploadFileResponseUnion ) RawJSON () string { return u .JSON .raw }
277-
278- func (r * BetaChatKitUploadFileResponseUnion ) UnmarshalJSON (data []byte ) error {
279- return apijson .UnmarshalRoot (data , r )
280- }
281-
282- type BetaChatKitUploadFileParams struct {
283- // Binary file contents to store with the ChatKit session. Supports PDFs and PNG,
284- // JPG, JPEG, GIF, or WEBP images.
285- File io.Reader `json:"file,omitzero,required" format:"binary"`
286- paramObj
287- }
288-
289- func (r BetaChatKitUploadFileParams ) MarshalMultipart () (data []byte , contentType string , err error ) {
290- buf := bytes .NewBuffer (nil )
291- writer := multipart .NewWriter (buf )
292- err = apiform .MarshalRoot (r , writer )
293- if err == nil {
294- err = apiform .WriteExtras (writer , r .ExtraFields ())
295- }
296- if err != nil {
297- writer .Close ()
298- return nil , "" , err
299- }
300- err = writer .Close ()
301- if err != nil {
302- return nil , "" , err
303- }
304- return buf .Bytes (), writer .FormDataContentType (), nil
305- }
0 commit comments