1// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2
3package openai
4
5import (
6 "context"
7 "errors"
8 "fmt"
9 "net/http"
10 "net/url"
11 "reflect"
12
13 "github.com/openai/openai-go/internal/apijson"
14 "github.com/openai/openai-go/internal/apiquery"
15 "github.com/openai/openai-go/internal/requestconfig"
16 "github.com/openai/openai-go/option"
17 "github.com/openai/openai-go/packages/pagination"
18 "github.com/openai/openai-go/packages/param"
19 "github.com/openai/openai-go/packages/resp"
20 "github.com/openai/openai-go/packages/ssestream"
21 "github.com/openai/openai-go/shared"
22 "github.com/openai/openai-go/shared/constant"
23 "github.com/tidwall/gjson"
24)
25
26// ChatCompletionService contains methods and other services that help with
27// interacting with the openai API.
28//
29// Note, unlike clients, this service does not read variables from the environment
30// automatically. You should not instantiate this service directly, and instead use
31// the [NewChatCompletionService] method instead.
32type ChatCompletionService struct {
33 Options []option.RequestOption
34 Messages ChatCompletionMessageService
35}
36
37// NewChatCompletionService generates a new service that applies the given options
38// to each request. These options are applied after the parent client's options (if
39// there is one), and before any request-specific options.
40func NewChatCompletionService(opts ...option.RequestOption) (r ChatCompletionService) {
41 r = ChatCompletionService{}
42 r.Options = opts
43 r.Messages = NewChatCompletionMessageService(opts...)
44 return
45}
46
47// **Starting a new project?** We recommend trying
48// [Responses](https://platform.openai.com/docs/api-reference/responses) to take
49// advantage of the latest OpenAI platform features. Compare
50// [Chat Completions with Responses](https://platform.openai.com/docs/guides/responses-vs-chat-completions?api-mode=responses).
51//
52// ---
53//
54// Creates a model response for the given chat conversation. Learn more in the
55// [text generation](https://platform.openai.com/docs/guides/text-generation),
56// [vision](https://platform.openai.com/docs/guides/vision), and
57// [audio](https://platform.openai.com/docs/guides/audio) guides.
58//
59// Parameter support can differ depending on the model used to generate the
60// response, particularly for newer reasoning models. Parameters that are only
61// supported for reasoning models are noted below. For the current state of
62// unsupported parameters in reasoning models,
63// [refer to the reasoning guide](https://platform.openai.com/docs/guides/reasoning).
64func (r *ChatCompletionService) New(ctx context.Context, body ChatCompletionNewParams, opts ...option.RequestOption) (res *ChatCompletion, err error) {
65 opts = append(r.Options[:], opts...)
66 path := "chat/completions"
67 err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, body, &res, opts...)
68 return
69}
70
71// **Starting a new project?** We recommend trying
72// [Responses](https://platform.openai.com/docs/api-reference/responses) to take
73// advantage of the latest OpenAI platform features. Compare
74// [Chat Completions with Responses](https://platform.openai.com/docs/guides/responses-vs-chat-completions?api-mode=responses).
75//
76// ---
77//
78// Creates a model response for the given chat conversation. Learn more in the
79// [text generation](https://platform.openai.com/docs/guides/text-generation),
80// [vision](https://platform.openai.com/docs/guides/vision), and
81// [audio](https://platform.openai.com/docs/guides/audio) guides.
82//
83// Parameter support can differ depending on the model used to generate the
84// response, particularly for newer reasoning models. Parameters that are only
85// supported for reasoning models are noted below. For the current state of
86// unsupported parameters in reasoning models,
87// [refer to the reasoning guide](https://platform.openai.com/docs/guides/reasoning).
88func (r *ChatCompletionService) NewStreaming(ctx context.Context, body ChatCompletionNewParams, opts ...option.RequestOption) (stream *ssestream.Stream[ChatCompletionChunk]) {
89 var (
90 raw *http.Response
91 err error
92 )
93 opts = append(r.Options[:], opts...)
94 opts = append([]option.RequestOption{option.WithJSONSet("stream", true)}, opts...)
95 path := "chat/completions"
96 err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, body, &raw, opts...)
97 return ssestream.NewStream[ChatCompletionChunk](ssestream.NewDecoder(raw), err)
98}
99
100// Get a stored chat completion. Only Chat Completions that have been created with
101// the `store` parameter set to `true` will be returned.
102func (r *ChatCompletionService) Get(ctx context.Context, completionID string, opts ...option.RequestOption) (res *ChatCompletion, err error) {
103 opts = append(r.Options[:], opts...)
104 if completionID == "" {
105 err = errors.New("missing required completion_id parameter")
106 return
107 }
108 path := fmt.Sprintf("chat/completions/%s", completionID)
109 err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, nil, &res, opts...)
110 return
111}
112
113// Modify a stored chat completion. Only Chat Completions that have been created
114// with the `store` parameter set to `true` can be modified. Currently, the only
115// supported modification is to update the `metadata` field.
116func (r *ChatCompletionService) Update(ctx context.Context, completionID string, body ChatCompletionUpdateParams, opts ...option.RequestOption) (res *ChatCompletion, err error) {
117 opts = append(r.Options[:], opts...)
118 if completionID == "" {
119 err = errors.New("missing required completion_id parameter")
120 return
121 }
122 path := fmt.Sprintf("chat/completions/%s", completionID)
123 err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, body, &res, opts...)
124 return
125}
126
127// List stored Chat Completions. Only Chat Completions that have been stored with
128// the `store` parameter set to `true` will be returned.
129func (r *ChatCompletionService) List(ctx context.Context, query ChatCompletionListParams, opts ...option.RequestOption) (res *pagination.CursorPage[ChatCompletion], err error) {
130 var raw *http.Response
131 opts = append(r.Options[:], opts...)
132 opts = append([]option.RequestOption{option.WithResponseInto(&raw)}, opts...)
133 path := "chat/completions"
134 cfg, err := requestconfig.NewRequestConfig(ctx, http.MethodGet, path, query, &res, opts...)
135 if err != nil {
136 return nil, err
137 }
138 err = cfg.Execute()
139 if err != nil {
140 return nil, err
141 }
142 res.SetPageConfig(cfg, raw)
143 return res, nil
144}
145
146// List stored Chat Completions. Only Chat Completions that have been stored with
147// the `store` parameter set to `true` will be returned.
148func (r *ChatCompletionService) ListAutoPaging(ctx context.Context, query ChatCompletionListParams, opts ...option.RequestOption) *pagination.CursorPageAutoPager[ChatCompletion] {
149 return pagination.NewCursorPageAutoPager(r.List(ctx, query, opts...))
150}
151
152// Delete a stored chat completion. Only Chat Completions that have been created
153// with the `store` parameter set to `true` can be deleted.
154func (r *ChatCompletionService) Delete(ctx context.Context, completionID string, opts ...option.RequestOption) (res *ChatCompletionDeleted, err error) {
155 opts = append(r.Options[:], opts...)
156 if completionID == "" {
157 err = errors.New("missing required completion_id parameter")
158 return
159 }
160 path := fmt.Sprintf("chat/completions/%s", completionID)
161 err = requestconfig.ExecuteNewRequest(ctx, http.MethodDelete, path, nil, &res, opts...)
162 return
163}
164
165// Represents a chat completion response returned by model, based on the provided
166// input.
167type ChatCompletion struct {
168 // A unique identifier for the chat completion.
169 ID string `json:"id,required"`
170 // A list of chat completion choices. Can be more than one if `n` is greater
171 // than 1.
172 Choices []ChatCompletionChoice `json:"choices,required"`
173 // The Unix timestamp (in seconds) of when the chat completion was created.
174 Created int64 `json:"created,required"`
175 // The model used for the chat completion.
176 Model string `json:"model,required"`
177 // The object type, which is always `chat.completion`.
178 Object constant.ChatCompletion `json:"object,required"`
179 // The service tier used for processing the request.
180 //
181 // Any of "scale", "default".
182 ServiceTier ChatCompletionServiceTier `json:"service_tier,nullable"`
183 // This fingerprint represents the backend configuration that the model runs with.
184 //
185 // Can be used in conjunction with the `seed` request parameter to understand when
186 // backend changes have been made that might impact determinism.
187 SystemFingerprint string `json:"system_fingerprint"`
188 // Usage statistics for the completion request.
189 Usage CompletionUsage `json:"usage"`
190 // Metadata for the response, check the presence of optional fields with the
191 // [resp.Field.IsPresent] method.
192 JSON struct {
193 ID resp.Field
194 Choices resp.Field
195 Created resp.Field
196 Model resp.Field
197 Object resp.Field
198 ServiceTier resp.Field
199 SystemFingerprint resp.Field
200 Usage resp.Field
201 ExtraFields map[string]resp.Field
202 raw string
203 } `json:"-"`
204}
205
206// Returns the unmodified JSON received from the API
207func (r ChatCompletion) RawJSON() string { return r.JSON.raw }
208func (r *ChatCompletion) UnmarshalJSON(data []byte) error {
209 return apijson.UnmarshalRoot(data, r)
210}
211
212type ChatCompletionChoice struct {
213 // The reason the model stopped generating tokens. This will be `stop` if the model
214 // hit a natural stop point or a provided stop sequence, `length` if the maximum
215 // number of tokens specified in the request was reached, `content_filter` if
216 // content was omitted due to a flag from our content filters, `tool_calls` if the
217 // model called a tool, or `function_call` (deprecated) if the model called a
218 // function.
219 //
220 // Any of "stop", "length", "tool_calls", "content_filter", "function_call".
221 FinishReason string `json:"finish_reason,required"`
222 // The index of the choice in the list of choices.
223 Index int64 `json:"index,required"`
224 // Log probability information for the choice.
225 Logprobs ChatCompletionChoiceLogprobs `json:"logprobs,required"`
226 // A chat completion message generated by the model.
227 Message ChatCompletionMessage `json:"message,required"`
228 // Metadata for the response, check the presence of optional fields with the
229 // [resp.Field.IsPresent] method.
230 JSON struct {
231 FinishReason resp.Field
232 Index resp.Field
233 Logprobs resp.Field
234 Message resp.Field
235 ExtraFields map[string]resp.Field
236 raw string
237 } `json:"-"`
238}
239
240// Returns the unmodified JSON received from the API
241func (r ChatCompletionChoice) RawJSON() string { return r.JSON.raw }
242func (r *ChatCompletionChoice) UnmarshalJSON(data []byte) error {
243 return apijson.UnmarshalRoot(data, r)
244}
245
246// Log probability information for the choice.
247type ChatCompletionChoiceLogprobs struct {
248 // A list of message content tokens with log probability information.
249 Content []ChatCompletionTokenLogprob `json:"content,required"`
250 // A list of message refusal tokens with log probability information.
251 Refusal []ChatCompletionTokenLogprob `json:"refusal,required"`
252 // Metadata for the response, check the presence of optional fields with the
253 // [resp.Field.IsPresent] method.
254 JSON struct {
255 Content resp.Field
256 Refusal resp.Field
257 ExtraFields map[string]resp.Field
258 raw string
259 } `json:"-"`
260}
261
262// Returns the unmodified JSON received from the API
263func (r ChatCompletionChoiceLogprobs) RawJSON() string { return r.JSON.raw }
264func (r *ChatCompletionChoiceLogprobs) UnmarshalJSON(data []byte) error {
265 return apijson.UnmarshalRoot(data, r)
266}
267
268// The service tier used for processing the request.
269type ChatCompletionServiceTier string
270
271const (
272 ChatCompletionServiceTierScale ChatCompletionServiceTier = "scale"
273 ChatCompletionServiceTierDefault ChatCompletionServiceTier = "default"
274)
275
276// Messages sent by the model in response to user messages.
277//
278// The property Role is required.
279type ChatCompletionAssistantMessageParam struct {
280 // The refusal message by the assistant.
281 Refusal param.Opt[string] `json:"refusal,omitzero"`
282 // An optional name for the participant. Provides the model information to
283 // differentiate between participants of the same role.
284 Name param.Opt[string] `json:"name,omitzero"`
285 // Data about a previous audio response from the model.
286 // [Learn more](https://platform.openai.com/docs/guides/audio).
287 Audio ChatCompletionAssistantMessageParamAudio `json:"audio,omitzero"`
288 // The contents of the assistant message. Required unless `tool_calls` or
289 // `function_call` is specified.
290 Content ChatCompletionAssistantMessageParamContentUnion `json:"content,omitzero"`
291 // Deprecated and replaced by `tool_calls`. The name and arguments of a function
292 // that should be called, as generated by the model.
293 //
294 // Deprecated: deprecated
295 FunctionCall ChatCompletionAssistantMessageParamFunctionCall `json:"function_call,omitzero"`
296 // The tool calls generated by the model, such as function calls.
297 ToolCalls []ChatCompletionMessageToolCallParam `json:"tool_calls,omitzero"`
298 // The role of the messages author, in this case `assistant`.
299 //
300 // This field can be elided, and will marshal its zero value as "assistant".
301 Role constant.Assistant `json:"role,required"`
302 paramObj
303}
304
305// IsPresent returns true if the field's value is not omitted and not the JSON
306// "null". To check if this field is omitted, use [param.IsOmitted].
307func (f ChatCompletionAssistantMessageParam) IsPresent() bool {
308 return !param.IsOmitted(f) && !f.IsNull()
309}
310func (r ChatCompletionAssistantMessageParam) MarshalJSON() (data []byte, err error) {
311 type shadow ChatCompletionAssistantMessageParam
312 return param.MarshalObject(r, (*shadow)(&r))
313}
314
315// Data about a previous audio response from the model.
316// [Learn more](https://platform.openai.com/docs/guides/audio).
317//
318// The property ID is required.
319type ChatCompletionAssistantMessageParamAudio struct {
320 // Unique identifier for a previous audio response from the model.
321 ID string `json:"id,required"`
322 paramObj
323}
324
325// IsPresent returns true if the field's value is not omitted and not the JSON
326// "null". To check if this field is omitted, use [param.IsOmitted].
327func (f ChatCompletionAssistantMessageParamAudio) IsPresent() bool {
328 return !param.IsOmitted(f) && !f.IsNull()
329}
330func (r ChatCompletionAssistantMessageParamAudio) MarshalJSON() (data []byte, err error) {
331 type shadow ChatCompletionAssistantMessageParamAudio
332 return param.MarshalObject(r, (*shadow)(&r))
333}
334
335// Only one field can be non-zero.
336//
337// Use [param.IsOmitted] to confirm if a field is set.
338type ChatCompletionAssistantMessageParamContentUnion struct {
339 OfString param.Opt[string] `json:",omitzero,inline"`
340 OfArrayOfContentParts []ChatCompletionAssistantMessageParamContentArrayOfContentPartUnion `json:",omitzero,inline"`
341 paramUnion
342}
343
344// IsPresent returns true if the field's value is not omitted and not the JSON
345// "null". To check if this field is omitted, use [param.IsOmitted].
346func (u ChatCompletionAssistantMessageParamContentUnion) IsPresent() bool {
347 return !param.IsOmitted(u) && !u.IsNull()
348}
349func (u ChatCompletionAssistantMessageParamContentUnion) MarshalJSON() ([]byte, error) {
350 return param.MarshalUnion[ChatCompletionAssistantMessageParamContentUnion](u.OfString, u.OfArrayOfContentParts)
351}
352
353func (u *ChatCompletionAssistantMessageParamContentUnion) asAny() any {
354 if !param.IsOmitted(u.OfString) {
355 return &u.OfString.Value
356 } else if !param.IsOmitted(u.OfArrayOfContentParts) {
357 return &u.OfArrayOfContentParts
358 }
359 return nil
360}
361
362// Only one field can be non-zero.
363//
364// Use [param.IsOmitted] to confirm if a field is set.
365type ChatCompletionAssistantMessageParamContentArrayOfContentPartUnion struct {
366 OfText *ChatCompletionContentPartTextParam `json:",omitzero,inline"`
367 OfRefusal *ChatCompletionContentPartRefusalParam `json:",omitzero,inline"`
368 paramUnion
369}
370
371// IsPresent returns true if the field's value is not omitted and not the JSON
372// "null". To check if this field is omitted, use [param.IsOmitted].
373func (u ChatCompletionAssistantMessageParamContentArrayOfContentPartUnion) IsPresent() bool {
374 return !param.IsOmitted(u) && !u.IsNull()
375}
376func (u ChatCompletionAssistantMessageParamContentArrayOfContentPartUnion) MarshalJSON() ([]byte, error) {
377 return param.MarshalUnion[ChatCompletionAssistantMessageParamContentArrayOfContentPartUnion](u.OfText, u.OfRefusal)
378}
379
380func (u *ChatCompletionAssistantMessageParamContentArrayOfContentPartUnion) asAny() any {
381 if !param.IsOmitted(u.OfText) {
382 return u.OfText
383 } else if !param.IsOmitted(u.OfRefusal) {
384 return u.OfRefusal
385 }
386 return nil
387}
388
389// Returns a pointer to the underlying variant's property, if present.
390func (u ChatCompletionAssistantMessageParamContentArrayOfContentPartUnion) GetText() *string {
391 if vt := u.OfText; vt != nil {
392 return &vt.Text
393 }
394 return nil
395}
396
397// Returns a pointer to the underlying variant's property, if present.
398func (u ChatCompletionAssistantMessageParamContentArrayOfContentPartUnion) GetRefusal() *string {
399 if vt := u.OfRefusal; vt != nil {
400 return &vt.Refusal
401 }
402 return nil
403}
404
405// Returns a pointer to the underlying variant's property, if present.
406func (u ChatCompletionAssistantMessageParamContentArrayOfContentPartUnion) GetType() *string {
407 if vt := u.OfText; vt != nil {
408 return (*string)(&vt.Type)
409 } else if vt := u.OfRefusal; vt != nil {
410 return (*string)(&vt.Type)
411 }
412 return nil
413}
414
415func init() {
416 apijson.RegisterUnion[ChatCompletionAssistantMessageParamContentArrayOfContentPartUnion](
417 "type",
418 apijson.UnionVariant{
419 TypeFilter: gjson.JSON,
420 Type: reflect.TypeOf(ChatCompletionContentPartTextParam{}),
421 DiscriminatorValue: "text",
422 },
423 apijson.UnionVariant{
424 TypeFilter: gjson.JSON,
425 Type: reflect.TypeOf(ChatCompletionContentPartRefusalParam{}),
426 DiscriminatorValue: "refusal",
427 },
428 )
429}
430
431// Deprecated and replaced by `tool_calls`. The name and arguments of a function
432// that should be called, as generated by the model.
433//
434// Deprecated: deprecated
435//
436// The properties Arguments, Name are required.
437type ChatCompletionAssistantMessageParamFunctionCall struct {
438 // The arguments to call the function with, as generated by the model in JSON
439 // format. Note that the model does not always generate valid JSON, and may
440 // hallucinate parameters not defined by your function schema. Validate the
441 // arguments in your code before calling your function.
442 Arguments string `json:"arguments,required"`
443 // The name of the function to call.
444 Name string `json:"name,required"`
445 paramObj
446}
447
448// IsPresent returns true if the field's value is not omitted and not the JSON
449// "null". To check if this field is omitted, use [param.IsOmitted].
450func (f ChatCompletionAssistantMessageParamFunctionCall) IsPresent() bool {
451 return !param.IsOmitted(f) && !f.IsNull()
452}
453func (r ChatCompletionAssistantMessageParamFunctionCall) MarshalJSON() (data []byte, err error) {
454 type shadow ChatCompletionAssistantMessageParamFunctionCall
455 return param.MarshalObject(r, (*shadow)(&r))
456}
457
458// If the audio output modality is requested, this object contains data about the
459// audio response from the model.
460// [Learn more](https://platform.openai.com/docs/guides/audio).
461type ChatCompletionAudio struct {
462 // Unique identifier for this audio response.
463 ID string `json:"id,required"`
464 // Base64 encoded audio bytes generated by the model, in the format specified in
465 // the request.
466 Data string `json:"data,required"`
467 // The Unix timestamp (in seconds) for when this audio response will no longer be
468 // accessible on the server for use in multi-turn conversations.
469 ExpiresAt int64 `json:"expires_at,required"`
470 // Transcript of the audio generated by the model.
471 Transcript string `json:"transcript,required"`
472 // Metadata for the response, check the presence of optional fields with the
473 // [resp.Field.IsPresent] method.
474 JSON struct {
475 ID resp.Field
476 Data resp.Field
477 ExpiresAt resp.Field
478 Transcript resp.Field
479 ExtraFields map[string]resp.Field
480 raw string
481 } `json:"-"`
482}
483
484// Returns the unmodified JSON received from the API
485func (r ChatCompletionAudio) RawJSON() string { return r.JSON.raw }
486func (r *ChatCompletionAudio) UnmarshalJSON(data []byte) error {
487 return apijson.UnmarshalRoot(data, r)
488}
489
490// Parameters for audio output. Required when audio output is requested with
491// `modalities: ["audio"]`.
492// [Learn more](https://platform.openai.com/docs/guides/audio).
493//
494// The properties Format, Voice are required.
495type ChatCompletionAudioParam struct {
496 // Specifies the output audio format. Must be one of `wav`, `mp3`, `flac`, `opus`,
497 // or `pcm16`.
498 //
499 // Any of "wav", "mp3", "flac", "opus", "pcm16".
500 Format ChatCompletionAudioParamFormat `json:"format,omitzero,required"`
501 // The voice the model uses to respond. Supported voices are `alloy`, `ash`,
502 // `ballad`, `coral`, `echo`, `sage`, and `shimmer`.
503 //
504 // Any of "alloy", "ash", "ballad", "coral", "echo", "sage", "shimmer", "verse".
505 Voice ChatCompletionAudioParamVoice `json:"voice,omitzero,required"`
506 paramObj
507}
508
509// IsPresent returns true if the field's value is not omitted and not the JSON
510// "null". To check if this field is omitted, use [param.IsOmitted].
511func (f ChatCompletionAudioParam) IsPresent() bool { return !param.IsOmitted(f) && !f.IsNull() }
512func (r ChatCompletionAudioParam) MarshalJSON() (data []byte, err error) {
513 type shadow ChatCompletionAudioParam
514 return param.MarshalObject(r, (*shadow)(&r))
515}
516
517// Specifies the output audio format. Must be one of `wav`, `mp3`, `flac`, `opus`,
518// or `pcm16`.
519type ChatCompletionAudioParamFormat string
520
521const (
522 ChatCompletionAudioParamFormatWAV ChatCompletionAudioParamFormat = "wav"
523 ChatCompletionAudioParamFormatMP3 ChatCompletionAudioParamFormat = "mp3"
524 ChatCompletionAudioParamFormatFLAC ChatCompletionAudioParamFormat = "flac"
525 ChatCompletionAudioParamFormatOpus ChatCompletionAudioParamFormat = "opus"
526 ChatCompletionAudioParamFormatPcm16 ChatCompletionAudioParamFormat = "pcm16"
527)
528
529// The voice the model uses to respond. Supported voices are `alloy`, `ash`,
530// `ballad`, `coral`, `echo`, `sage`, and `shimmer`.
531type ChatCompletionAudioParamVoice string
532
533const (
534 ChatCompletionAudioParamVoiceAlloy ChatCompletionAudioParamVoice = "alloy"
535 ChatCompletionAudioParamVoiceAsh ChatCompletionAudioParamVoice = "ash"
536 ChatCompletionAudioParamVoiceBallad ChatCompletionAudioParamVoice = "ballad"
537 ChatCompletionAudioParamVoiceCoral ChatCompletionAudioParamVoice = "coral"
538 ChatCompletionAudioParamVoiceEcho ChatCompletionAudioParamVoice = "echo"
539 ChatCompletionAudioParamVoiceSage ChatCompletionAudioParamVoice = "sage"
540 ChatCompletionAudioParamVoiceShimmer ChatCompletionAudioParamVoice = "shimmer"
541 ChatCompletionAudioParamVoiceVerse ChatCompletionAudioParamVoice = "verse"
542)
543
544// Represents a streamed chunk of a chat completion response returned by the model,
545// based on the provided input.
546// [Learn more](https://platform.openai.com/docs/guides/streaming-responses).
547type ChatCompletionChunk struct {
548 // A unique identifier for the chat completion. Each chunk has the same ID.
549 ID string `json:"id,required"`
550 // A list of chat completion choices. Can contain more than one elements if `n` is
551 // greater than 1. Can also be empty for the last chunk if you set
552 // `stream_options: {"include_usage": true}`.
553 Choices []ChatCompletionChunkChoice `json:"choices,required"`
554 // The Unix timestamp (in seconds) of when the chat completion was created. Each
555 // chunk has the same timestamp.
556 Created int64 `json:"created,required"`
557 // The model to generate the completion.
558 Model string `json:"model,required"`
559 // The object type, which is always `chat.completion.chunk`.
560 Object constant.ChatCompletionChunk `json:"object,required"`
561 // The service tier used for processing the request.
562 //
563 // Any of "scale", "default".
564 ServiceTier ChatCompletionChunkServiceTier `json:"service_tier,nullable"`
565 // This fingerprint represents the backend configuration that the model runs with.
566 // Can be used in conjunction with the `seed` request parameter to understand when
567 // backend changes have been made that might impact determinism.
568 SystemFingerprint string `json:"system_fingerprint"`
569 // An optional field that will only be present when you set
570 // `stream_options: {"include_usage": true}` in your request. When present, it
571 // contains a null value **except for the last chunk** which contains the token
572 // usage statistics for the entire request.
573 //
574 // **NOTE:** If the stream is interrupted or cancelled, you may not receive the
575 // final usage chunk which contains the total token usage for the request.
576 Usage CompletionUsage `json:"usage,nullable"`
577 // Metadata for the response, check the presence of optional fields with the
578 // [resp.Field.IsPresent] method.
579 JSON struct {
580 ID resp.Field
581 Choices resp.Field
582 Created resp.Field
583 Model resp.Field
584 Object resp.Field
585 ServiceTier resp.Field
586 SystemFingerprint resp.Field
587 Usage resp.Field
588 ExtraFields map[string]resp.Field
589 raw string
590 } `json:"-"`
591}
592
593// Returns the unmodified JSON received from the API
594func (r ChatCompletionChunk) RawJSON() string { return r.JSON.raw }
595func (r *ChatCompletionChunk) UnmarshalJSON(data []byte) error {
596 return apijson.UnmarshalRoot(data, r)
597}
598
599type ChatCompletionChunkChoice struct {
600 // A chat completion delta generated by streamed model responses.
601 Delta ChatCompletionChunkChoiceDelta `json:"delta,required"`
602 // The reason the model stopped generating tokens. This will be `stop` if the model
603 // hit a natural stop point or a provided stop sequence, `length` if the maximum
604 // number of tokens specified in the request was reached, `content_filter` if
605 // content was omitted due to a flag from our content filters, `tool_calls` if the
606 // model called a tool, or `function_call` (deprecated) if the model called a
607 // function.
608 //
609 // Any of "stop", "length", "tool_calls", "content_filter", "function_call".
610 FinishReason string `json:"finish_reason,required"`
611 // The index of the choice in the list of choices.
612 Index int64 `json:"index,required"`
613 // Log probability information for the choice.
614 Logprobs ChatCompletionChunkChoiceLogprobs `json:"logprobs,nullable"`
615 // Metadata for the response, check the presence of optional fields with the
616 // [resp.Field.IsPresent] method.
617 JSON struct {
618 Delta resp.Field
619 FinishReason resp.Field
620 Index resp.Field
621 Logprobs resp.Field
622 ExtraFields map[string]resp.Field
623 raw string
624 } `json:"-"`
625}
626
627// Returns the unmodified JSON received from the API
628func (r ChatCompletionChunkChoice) RawJSON() string { return r.JSON.raw }
629func (r *ChatCompletionChunkChoice) UnmarshalJSON(data []byte) error {
630 return apijson.UnmarshalRoot(data, r)
631}
632
633// A chat completion delta generated by streamed model responses.
634type ChatCompletionChunkChoiceDelta struct {
635 // The contents of the chunk message.
636 Content string `json:"content,nullable"`
637 // Deprecated and replaced by `tool_calls`. The name and arguments of a function
638 // that should be called, as generated by the model.
639 //
640 // Deprecated: deprecated
641 FunctionCall ChatCompletionChunkChoiceDeltaFunctionCall `json:"function_call"`
642 // The refusal message generated by the model.
643 Refusal string `json:"refusal,nullable"`
644 // The role of the author of this message.
645 //
646 // Any of "developer", "system", "user", "assistant", "tool".
647 Role string `json:"role"`
648 ToolCalls []ChatCompletionChunkChoiceDeltaToolCall `json:"tool_calls"`
649 // Metadata for the response, check the presence of optional fields with the
650 // [resp.Field.IsPresent] method.
651 JSON struct {
652 Content resp.Field
653 FunctionCall resp.Field
654 Refusal resp.Field
655 Role resp.Field
656 ToolCalls resp.Field
657 ExtraFields map[string]resp.Field
658 raw string
659 } `json:"-"`
660}
661
662// Returns the unmodified JSON received from the API
663func (r ChatCompletionChunkChoiceDelta) RawJSON() string { return r.JSON.raw }
664func (r *ChatCompletionChunkChoiceDelta) UnmarshalJSON(data []byte) error {
665 return apijson.UnmarshalRoot(data, r)
666}
667
668// Deprecated and replaced by `tool_calls`. The name and arguments of a function
669// that should be called, as generated by the model.
670//
671// Deprecated: deprecated
672type ChatCompletionChunkChoiceDeltaFunctionCall struct {
673 // The arguments to call the function with, as generated by the model in JSON
674 // format. Note that the model does not always generate valid JSON, and may
675 // hallucinate parameters not defined by your function schema. Validate the
676 // arguments in your code before calling your function.
677 Arguments string `json:"arguments"`
678 // The name of the function to call.
679 Name string `json:"name"`
680 // Metadata for the response, check the presence of optional fields with the
681 // [resp.Field.IsPresent] method.
682 JSON struct {
683 Arguments resp.Field
684 Name resp.Field
685 ExtraFields map[string]resp.Field
686 raw string
687 } `json:"-"`
688}
689
690// Returns the unmodified JSON received from the API
691func (r ChatCompletionChunkChoiceDeltaFunctionCall) RawJSON() string { return r.JSON.raw }
692func (r *ChatCompletionChunkChoiceDeltaFunctionCall) UnmarshalJSON(data []byte) error {
693 return apijson.UnmarshalRoot(data, r)
694}
695
696type ChatCompletionChunkChoiceDeltaToolCall struct {
697 Index int64 `json:"index,required"`
698 // The ID of the tool call.
699 ID string `json:"id"`
700 Function ChatCompletionChunkChoiceDeltaToolCallFunction `json:"function"`
701 // The type of the tool. Currently, only `function` is supported.
702 //
703 // Any of "function".
704 Type string `json:"type"`
705 // Metadata for the response, check the presence of optional fields with the
706 // [resp.Field.IsPresent] method.
707 JSON struct {
708 Index resp.Field
709 ID resp.Field
710 Function resp.Field
711 Type resp.Field
712 ExtraFields map[string]resp.Field
713 raw string
714 } `json:"-"`
715}
716
717// Returns the unmodified JSON received from the API
718func (r ChatCompletionChunkChoiceDeltaToolCall) RawJSON() string { return r.JSON.raw }
719func (r *ChatCompletionChunkChoiceDeltaToolCall) UnmarshalJSON(data []byte) error {
720 return apijson.UnmarshalRoot(data, r)
721}
722
723type ChatCompletionChunkChoiceDeltaToolCallFunction struct {
724 // The arguments to call the function with, as generated by the model in JSON
725 // format. Note that the model does not always generate valid JSON, and may
726 // hallucinate parameters not defined by your function schema. Validate the
727 // arguments in your code before calling your function.
728 Arguments string `json:"arguments"`
729 // The name of the function to call.
730 Name string `json:"name"`
731 // Metadata for the response, check the presence of optional fields with the
732 // [resp.Field.IsPresent] method.
733 JSON struct {
734 Arguments resp.Field
735 Name resp.Field
736 ExtraFields map[string]resp.Field
737 raw string
738 } `json:"-"`
739}
740
741// Returns the unmodified JSON received from the API
742func (r ChatCompletionChunkChoiceDeltaToolCallFunction) RawJSON() string { return r.JSON.raw }
743func (r *ChatCompletionChunkChoiceDeltaToolCallFunction) UnmarshalJSON(data []byte) error {
744 return apijson.UnmarshalRoot(data, r)
745}
746
747// Log probability information for the choice.
748type ChatCompletionChunkChoiceLogprobs struct {
749 // A list of message content tokens with log probability information.
750 Content []ChatCompletionTokenLogprob `json:"content,required"`
751 // A list of message refusal tokens with log probability information.
752 Refusal []ChatCompletionTokenLogprob `json:"refusal,required"`
753 // Metadata for the response, check the presence of optional fields with the
754 // [resp.Field.IsPresent] method.
755 JSON struct {
756 Content resp.Field
757 Refusal resp.Field
758 ExtraFields map[string]resp.Field
759 raw string
760 } `json:"-"`
761}
762
763// Returns the unmodified JSON received from the API
764func (r ChatCompletionChunkChoiceLogprobs) RawJSON() string { return r.JSON.raw }
765func (r *ChatCompletionChunkChoiceLogprobs) UnmarshalJSON(data []byte) error {
766 return apijson.UnmarshalRoot(data, r)
767}
768
769// The service tier used for processing the request.
770type ChatCompletionChunkServiceTier string
771
772const (
773 ChatCompletionChunkServiceTierScale ChatCompletionChunkServiceTier = "scale"
774 ChatCompletionChunkServiceTierDefault ChatCompletionChunkServiceTier = "default"
775)
776
777func TextContentPart(text string) ChatCompletionContentPartUnionParam {
778 var variant ChatCompletionContentPartTextParam
779 variant.Text = text
780 return ChatCompletionContentPartUnionParam{OfText: &variant}
781}
782
783func ImageContentPart(imageURL ChatCompletionContentPartImageImageURLParam) ChatCompletionContentPartUnionParam {
784 var variant ChatCompletionContentPartImageParam
785 variant.ImageURL = imageURL
786 return ChatCompletionContentPartUnionParam{OfImageURL: &variant}
787}
788
789func InputAudioContentPart(inputAudio ChatCompletionContentPartInputAudioInputAudioParam) ChatCompletionContentPartUnionParam {
790 var variant ChatCompletionContentPartInputAudioParam
791 variant.InputAudio = inputAudio
792 return ChatCompletionContentPartUnionParam{OfInputAudio: &variant}
793}
794
795func FileContentPart(file ChatCompletionContentPartFileFileParam) ChatCompletionContentPartUnionParam {
796 var variant ChatCompletionContentPartFileParam
797 variant.File = file
798 return ChatCompletionContentPartUnionParam{OfFile: &variant}
799}
800
801// Only one field can be non-zero.
802//
803// Use [param.IsOmitted] to confirm if a field is set.
804type ChatCompletionContentPartUnionParam struct {
805 OfText *ChatCompletionContentPartTextParam `json:",omitzero,inline"`
806 OfImageURL *ChatCompletionContentPartImageParam `json:",omitzero,inline"`
807 OfInputAudio *ChatCompletionContentPartInputAudioParam `json:",omitzero,inline"`
808 OfFile *ChatCompletionContentPartFileParam `json:",omitzero,inline"`
809 paramUnion
810}
811
812// IsPresent returns true if the field's value is not omitted and not the JSON
813// "null". To check if this field is omitted, use [param.IsOmitted].
814func (u ChatCompletionContentPartUnionParam) IsPresent() bool {
815 return !param.IsOmitted(u) && !u.IsNull()
816}
817func (u ChatCompletionContentPartUnionParam) MarshalJSON() ([]byte, error) {
818 return param.MarshalUnion[ChatCompletionContentPartUnionParam](u.OfText, u.OfImageURL, u.OfInputAudio, u.OfFile)
819}
820
821func (u *ChatCompletionContentPartUnionParam) asAny() any {
822 if !param.IsOmitted(u.OfText) {
823 return u.OfText
824 } else if !param.IsOmitted(u.OfImageURL) {
825 return u.OfImageURL
826 } else if !param.IsOmitted(u.OfInputAudio) {
827 return u.OfInputAudio
828 } else if !param.IsOmitted(u.OfFile) {
829 return u.OfFile
830 }
831 return nil
832}
833
834// Returns a pointer to the underlying variant's property, if present.
835func (u ChatCompletionContentPartUnionParam) GetText() *string {
836 if vt := u.OfText; vt != nil {
837 return &vt.Text
838 }
839 return nil
840}
841
842// Returns a pointer to the underlying variant's property, if present.
843func (u ChatCompletionContentPartUnionParam) GetImageURL() *ChatCompletionContentPartImageImageURLParam {
844 if vt := u.OfImageURL; vt != nil {
845 return &vt.ImageURL
846 }
847 return nil
848}
849
850// Returns a pointer to the underlying variant's property, if present.
851func (u ChatCompletionContentPartUnionParam) GetInputAudio() *ChatCompletionContentPartInputAudioInputAudioParam {
852 if vt := u.OfInputAudio; vt != nil {
853 return &vt.InputAudio
854 }
855 return nil
856}
857
858// Returns a pointer to the underlying variant's property, if present.
859func (u ChatCompletionContentPartUnionParam) GetFile() *ChatCompletionContentPartFileFileParam {
860 if vt := u.OfFile; vt != nil {
861 return &vt.File
862 }
863 return nil
864}
865
866// Returns a pointer to the underlying variant's property, if present.
867func (u ChatCompletionContentPartUnionParam) GetType() *string {
868 if vt := u.OfText; vt != nil {
869 return (*string)(&vt.Type)
870 } else if vt := u.OfImageURL; vt != nil {
871 return (*string)(&vt.Type)
872 } else if vt := u.OfInputAudio; vt != nil {
873 return (*string)(&vt.Type)
874 } else if vt := u.OfFile; vt != nil {
875 return (*string)(&vt.Type)
876 }
877 return nil
878}
879
880func init() {
881 apijson.RegisterUnion[ChatCompletionContentPartUnionParam](
882 "type",
883 apijson.UnionVariant{
884 TypeFilter: gjson.JSON,
885 Type: reflect.TypeOf(ChatCompletionContentPartTextParam{}),
886 DiscriminatorValue: "text",
887 },
888 apijson.UnionVariant{
889 TypeFilter: gjson.JSON,
890 Type: reflect.TypeOf(ChatCompletionContentPartImageParam{}),
891 DiscriminatorValue: "image_url",
892 },
893 apijson.UnionVariant{
894 TypeFilter: gjson.JSON,
895 Type: reflect.TypeOf(ChatCompletionContentPartInputAudioParam{}),
896 DiscriminatorValue: "input_audio",
897 },
898 apijson.UnionVariant{
899 TypeFilter: gjson.JSON,
900 Type: reflect.TypeOf(ChatCompletionContentPartFileParam{}),
901 DiscriminatorValue: "file",
902 },
903 )
904}
905
906// Learn about [file inputs](https://platform.openai.com/docs/guides/text) for text
907// generation.
908//
909// The properties File, Type are required.
910type ChatCompletionContentPartFileParam struct {
911 File ChatCompletionContentPartFileFileParam `json:"file,omitzero,required"`
912 // The type of the content part. Always `file`.
913 //
914 // This field can be elided, and will marshal its zero value as "file".
915 Type constant.File `json:"type,required"`
916 paramObj
917}
918
919// IsPresent returns true if the field's value is not omitted and not the JSON
920// "null". To check if this field is omitted, use [param.IsOmitted].
921func (f ChatCompletionContentPartFileParam) IsPresent() bool {
922 return !param.IsOmitted(f) && !f.IsNull()
923}
924func (r ChatCompletionContentPartFileParam) MarshalJSON() (data []byte, err error) {
925 type shadow ChatCompletionContentPartFileParam
926 return param.MarshalObject(r, (*shadow)(&r))
927}
928
929type ChatCompletionContentPartFileFileParam struct {
930 // The base64 encoded file data, used when passing the file to the model as a
931 // string.
932 FileData param.Opt[string] `json:"file_data,omitzero"`
933 // The ID of an uploaded file to use as input.
934 FileID param.Opt[string] `json:"file_id,omitzero"`
935 // The name of the file, used when passing the file to the model as a string.
936 Filename param.Opt[string] `json:"filename,omitzero"`
937 paramObj
938}
939
940// IsPresent returns true if the field's value is not omitted and not the JSON
941// "null". To check if this field is omitted, use [param.IsOmitted].
942func (f ChatCompletionContentPartFileFileParam) IsPresent() bool {
943 return !param.IsOmitted(f) && !f.IsNull()
944}
945func (r ChatCompletionContentPartFileFileParam) MarshalJSON() (data []byte, err error) {
946 type shadow ChatCompletionContentPartFileFileParam
947 return param.MarshalObject(r, (*shadow)(&r))
948}
949
950// Learn about [image inputs](https://platform.openai.com/docs/guides/vision).
951//
952// The properties ImageURL, Type are required.
953type ChatCompletionContentPartImageParam struct {
954 ImageURL ChatCompletionContentPartImageImageURLParam `json:"image_url,omitzero,required"`
955 // The type of the content part.
956 //
957 // This field can be elided, and will marshal its zero value as "image_url".
958 Type constant.ImageURL `json:"type,required"`
959 paramObj
960}
961
962// IsPresent returns true if the field's value is not omitted and not the JSON
963// "null". To check if this field is omitted, use [param.IsOmitted].
964func (f ChatCompletionContentPartImageParam) IsPresent() bool {
965 return !param.IsOmitted(f) && !f.IsNull()
966}
967func (r ChatCompletionContentPartImageParam) MarshalJSON() (data []byte, err error) {
968 type shadow ChatCompletionContentPartImageParam
969 return param.MarshalObject(r, (*shadow)(&r))
970}
971
972// The property URL is required.
973type ChatCompletionContentPartImageImageURLParam struct {
974 // Either a URL of the image or the base64 encoded image data.
975 URL string `json:"url,required" format:"uri"`
976 // Specifies the detail level of the image. Learn more in the
977 // [Vision guide](https://platform.openai.com/docs/guides/vision#low-or-high-fidelity-image-understanding).
978 //
979 // Any of "auto", "low", "high".
980 Detail string `json:"detail,omitzero"`
981 paramObj
982}
983
984// IsPresent returns true if the field's value is not omitted and not the JSON
985// "null". To check if this field is omitted, use [param.IsOmitted].
986func (f ChatCompletionContentPartImageImageURLParam) IsPresent() bool {
987 return !param.IsOmitted(f) && !f.IsNull()
988}
989func (r ChatCompletionContentPartImageImageURLParam) MarshalJSON() (data []byte, err error) {
990 type shadow ChatCompletionContentPartImageImageURLParam
991 return param.MarshalObject(r, (*shadow)(&r))
992}
993
994func init() {
995 apijson.RegisterFieldValidator[ChatCompletionContentPartImageImageURLParam](
996 "Detail", false, "auto", "low", "high",
997 )
998}
999
1000// Learn about [audio inputs](https://platform.openai.com/docs/guides/audio).
1001//
1002// The properties InputAudio, Type are required.
1003type ChatCompletionContentPartInputAudioParam struct {
1004 InputAudio ChatCompletionContentPartInputAudioInputAudioParam `json:"input_audio,omitzero,required"`
1005 // The type of the content part. Always `input_audio`.
1006 //
1007 // This field can be elided, and will marshal its zero value as "input_audio".
1008 Type constant.InputAudio `json:"type,required"`
1009 paramObj
1010}
1011
1012// IsPresent returns true if the field's value is not omitted and not the JSON
1013// "null". To check if this field is omitted, use [param.IsOmitted].
1014func (f ChatCompletionContentPartInputAudioParam) IsPresent() bool {
1015 return !param.IsOmitted(f) && !f.IsNull()
1016}
1017func (r ChatCompletionContentPartInputAudioParam) MarshalJSON() (data []byte, err error) {
1018 type shadow ChatCompletionContentPartInputAudioParam
1019 return param.MarshalObject(r, (*shadow)(&r))
1020}
1021
1022// The properties Data, Format are required.
1023type ChatCompletionContentPartInputAudioInputAudioParam struct {
1024 // Base64 encoded audio data.
1025 Data string `json:"data,required"`
1026 // The format of the encoded audio data. Currently supports "wav" and "mp3".
1027 //
1028 // Any of "wav", "mp3".
1029 Format string `json:"format,omitzero,required"`
1030 paramObj
1031}
1032
1033// IsPresent returns true if the field's value is not omitted and not the JSON
1034// "null". To check if this field is omitted, use [param.IsOmitted].
1035func (f ChatCompletionContentPartInputAudioInputAudioParam) IsPresent() bool {
1036 return !param.IsOmitted(f) && !f.IsNull()
1037}
1038func (r ChatCompletionContentPartInputAudioInputAudioParam) MarshalJSON() (data []byte, err error) {
1039 type shadow ChatCompletionContentPartInputAudioInputAudioParam
1040 return param.MarshalObject(r, (*shadow)(&r))
1041}
1042
1043func init() {
1044 apijson.RegisterFieldValidator[ChatCompletionContentPartInputAudioInputAudioParam](
1045 "Format", false, "wav", "mp3",
1046 )
1047}
1048
1049// The properties Refusal, Type are required.
1050type ChatCompletionContentPartRefusalParam struct {
1051 // The refusal message generated by the model.
1052 Refusal string `json:"refusal,required"`
1053 // The type of the content part.
1054 //
1055 // This field can be elided, and will marshal its zero value as "refusal".
1056 Type constant.Refusal `json:"type,required"`
1057 paramObj
1058}
1059
1060// IsPresent returns true if the field's value is not omitted and not the JSON
1061// "null". To check if this field is omitted, use [param.IsOmitted].
1062func (f ChatCompletionContentPartRefusalParam) IsPresent() bool {
1063 return !param.IsOmitted(f) && !f.IsNull()
1064}
1065func (r ChatCompletionContentPartRefusalParam) MarshalJSON() (data []byte, err error) {
1066 type shadow ChatCompletionContentPartRefusalParam
1067 return param.MarshalObject(r, (*shadow)(&r))
1068}
1069
1070// Learn about
1071// [text inputs](https://platform.openai.com/docs/guides/text-generation).
1072//
1073// The properties Text, Type are required.
1074type ChatCompletionContentPartTextParam struct {
1075 // The text content.
1076 Text string `json:"text,required"`
1077 // The type of the content part.
1078 //
1079 // This field can be elided, and will marshal its zero value as "text".
1080 Type constant.Text `json:"type,required"`
1081 paramObj
1082}
1083
1084// IsPresent returns true if the field's value is not omitted and not the JSON
1085// "null". To check if this field is omitted, use [param.IsOmitted].
1086func (f ChatCompletionContentPartTextParam) IsPresent() bool {
1087 return !param.IsOmitted(f) && !f.IsNull()
1088}
1089func (r ChatCompletionContentPartTextParam) MarshalJSON() (data []byte, err error) {
1090 type shadow ChatCompletionContentPartTextParam
1091 return param.MarshalObject(r, (*shadow)(&r))
1092}
1093
1094type ChatCompletionDeleted struct {
1095 // The ID of the chat completion that was deleted.
1096 ID string `json:"id,required"`
1097 // Whether the chat completion was deleted.
1098 Deleted bool `json:"deleted,required"`
1099 // The type of object being deleted.
1100 Object constant.ChatCompletionDeleted `json:"object,required"`
1101 // Metadata for the response, check the presence of optional fields with the
1102 // [resp.Field.IsPresent] method.
1103 JSON struct {
1104 ID resp.Field
1105 Deleted resp.Field
1106 Object resp.Field
1107 ExtraFields map[string]resp.Field
1108 raw string
1109 } `json:"-"`
1110}
1111
1112// Returns the unmodified JSON received from the API
1113func (r ChatCompletionDeleted) RawJSON() string { return r.JSON.raw }
1114func (r *ChatCompletionDeleted) UnmarshalJSON(data []byte) error {
1115 return apijson.UnmarshalRoot(data, r)
1116}
1117
1118// Developer-provided instructions that the model should follow, regardless of
1119// messages sent by the user. With o1 models and newer, `developer` messages
1120// replace the previous `system` messages.
1121//
1122// The properties Content, Role are required.
1123type ChatCompletionDeveloperMessageParam struct {
1124 // The contents of the developer message.
1125 Content ChatCompletionDeveloperMessageParamContentUnion `json:"content,omitzero,required"`
1126 // An optional name for the participant. Provides the model information to
1127 // differentiate between participants of the same role.
1128 Name param.Opt[string] `json:"name,omitzero"`
1129 // The role of the messages author, in this case `developer`.
1130 //
1131 // This field can be elided, and will marshal its zero value as "developer".
1132 Role constant.Developer `json:"role,required"`
1133 paramObj
1134}
1135
1136// IsPresent returns true if the field's value is not omitted and not the JSON
1137// "null". To check if this field is omitted, use [param.IsOmitted].
1138func (f ChatCompletionDeveloperMessageParam) IsPresent() bool {
1139 return !param.IsOmitted(f) && !f.IsNull()
1140}
1141func (r ChatCompletionDeveloperMessageParam) MarshalJSON() (data []byte, err error) {
1142 type shadow ChatCompletionDeveloperMessageParam
1143 return param.MarshalObject(r, (*shadow)(&r))
1144}
1145
1146// Only one field can be non-zero.
1147//
1148// Use [param.IsOmitted] to confirm if a field is set.
1149type ChatCompletionDeveloperMessageParamContentUnion struct {
1150 OfString param.Opt[string] `json:",omitzero,inline"`
1151 OfArrayOfContentParts []ChatCompletionContentPartTextParam `json:",omitzero,inline"`
1152 paramUnion
1153}
1154
1155// IsPresent returns true if the field's value is not omitted and not the JSON
1156// "null". To check if this field is omitted, use [param.IsOmitted].
1157func (u ChatCompletionDeveloperMessageParamContentUnion) IsPresent() bool {
1158 return !param.IsOmitted(u) && !u.IsNull()
1159}
1160func (u ChatCompletionDeveloperMessageParamContentUnion) MarshalJSON() ([]byte, error) {
1161 return param.MarshalUnion[ChatCompletionDeveloperMessageParamContentUnion](u.OfString, u.OfArrayOfContentParts)
1162}
1163
1164func (u *ChatCompletionDeveloperMessageParamContentUnion) asAny() any {
1165 if !param.IsOmitted(u.OfString) {
1166 return &u.OfString.Value
1167 } else if !param.IsOmitted(u.OfArrayOfContentParts) {
1168 return &u.OfArrayOfContentParts
1169 }
1170 return nil
1171}
1172
1173// Specifying a particular function via `{"name": "my_function"}` forces the model
1174// to call that function.
1175//
1176// The property Name is required.
1177type ChatCompletionFunctionCallOptionParam struct {
1178 // The name of the function to call.
1179 Name string `json:"name,required"`
1180 paramObj
1181}
1182
1183// IsPresent returns true if the field's value is not omitted and not the JSON
1184// "null". To check if this field is omitted, use [param.IsOmitted].
1185func (f ChatCompletionFunctionCallOptionParam) IsPresent() bool {
1186 return !param.IsOmitted(f) && !f.IsNull()
1187}
1188func (r ChatCompletionFunctionCallOptionParam) MarshalJSON() (data []byte, err error) {
1189 type shadow ChatCompletionFunctionCallOptionParam
1190 return param.MarshalObject(r, (*shadow)(&r))
1191}
1192
1193// Deprecated: deprecated
1194//
1195// The properties Content, Name, Role are required.
1196type ChatCompletionFunctionMessageParam struct {
1197 // The contents of the function message.
1198 Content param.Opt[string] `json:"content,omitzero,required"`
1199 // The name of the function to call.
1200 Name string `json:"name,required"`
1201 // The role of the messages author, in this case `function`.
1202 //
1203 // This field can be elided, and will marshal its zero value as "function".
1204 Role constant.Function `json:"role,required"`
1205 paramObj
1206}
1207
1208// IsPresent returns true if the field's value is not omitted and not the JSON
1209// "null". To check if this field is omitted, use [param.IsOmitted].
1210func (f ChatCompletionFunctionMessageParam) IsPresent() bool {
1211 return !param.IsOmitted(f) && !f.IsNull()
1212}
1213func (r ChatCompletionFunctionMessageParam) MarshalJSON() (data []byte, err error) {
1214 type shadow ChatCompletionFunctionMessageParam
1215 return param.MarshalObject(r, (*shadow)(&r))
1216}
1217
1218// A chat completion message generated by the model.
1219type ChatCompletionMessage struct {
1220 // The contents of the message.
1221 Content string `json:"content,required"`
1222 // The refusal message generated by the model.
1223 Refusal string `json:"refusal,required"`
1224 // The role of the author of this message.
1225 Role constant.Assistant `json:"role,required"`
1226 // Annotations for the message, when applicable, as when using the
1227 // [web search tool](https://platform.openai.com/docs/guides/tools-web-search?api-mode=chat).
1228 Annotations []ChatCompletionMessageAnnotation `json:"annotations"`
1229 // If the audio output modality is requested, this object contains data about the
1230 // audio response from the model.
1231 // [Learn more](https://platform.openai.com/docs/guides/audio).
1232 Audio ChatCompletionAudio `json:"audio,nullable"`
1233 // Deprecated and replaced by `tool_calls`. The name and arguments of a function
1234 // that should be called, as generated by the model.
1235 //
1236 // Deprecated: deprecated
1237 FunctionCall ChatCompletionMessageFunctionCall `json:"function_call"`
1238 // The tool calls generated by the model, such as function calls.
1239 ToolCalls []ChatCompletionMessageToolCall `json:"tool_calls"`
1240 // Metadata for the response, check the presence of optional fields with the
1241 // [resp.Field.IsPresent] method.
1242 JSON struct {
1243 Content resp.Field
1244 Refusal resp.Field
1245 Role resp.Field
1246 Annotations resp.Field
1247 Audio resp.Field
1248 FunctionCall resp.Field
1249 ToolCalls resp.Field
1250 ExtraFields map[string]resp.Field
1251 raw string
1252 } `json:"-"`
1253}
1254
1255// Returns the unmodified JSON received from the API
1256func (r ChatCompletionMessage) RawJSON() string { return r.JSON.raw }
1257func (r *ChatCompletionMessage) UnmarshalJSON(data []byte) error {
1258 return apijson.UnmarshalRoot(data, r)
1259}
1260
1261func (r ChatCompletionMessage) ToParam() ChatCompletionMessageParamUnion {
1262 asst := r.ToAssistantMessageParam()
1263 return ChatCompletionMessageParamUnion{OfAssistant: &asst}
1264}
1265
1266func (r ChatCompletionMessage) ToAssistantMessageParam() ChatCompletionAssistantMessageParam {
1267 var p ChatCompletionAssistantMessageParam
1268
1269 // It is important to not rely on the JSON metadata property
1270 // here, it may be unset if the receiver was generated via a
1271 // [ChatCompletionAccumulator].
1272 //
1273 // Explicit null is intentionally elided from the response.
1274 if r.Content != "" {
1275 p.Content.OfString = String(r.Content)
1276 }
1277 if r.Refusal != "" {
1278 p.Refusal = String(r.Refusal)
1279 }
1280
1281 p.Audio.ID = r.Audio.ID
1282 p.Role = r.Role
1283 p.FunctionCall.Arguments = r.FunctionCall.Arguments
1284 p.FunctionCall.Name = r.FunctionCall.Name
1285
1286 if len(r.ToolCalls) > 0 {
1287 p.ToolCalls = make([]ChatCompletionMessageToolCallParam, len(r.ToolCalls))
1288 for i, v := range r.ToolCalls {
1289 p.ToolCalls[i].ID = v.ID
1290 p.ToolCalls[i].Function.Arguments = v.Function.Arguments
1291 p.ToolCalls[i].Function.Name = v.Function.Name
1292 }
1293 }
1294 return p
1295}
1296
1297// A URL citation when using web search.
1298type ChatCompletionMessageAnnotation struct {
1299 // The type of the URL citation. Always `url_citation`.
1300 Type constant.URLCitation `json:"type,required"`
1301 // A URL citation when using web search.
1302 URLCitation ChatCompletionMessageAnnotationURLCitation `json:"url_citation,required"`
1303 // Metadata for the response, check the presence of optional fields with the
1304 // [resp.Field.IsPresent] method.
1305 JSON struct {
1306 Type resp.Field
1307 URLCitation resp.Field
1308 ExtraFields map[string]resp.Field
1309 raw string
1310 } `json:"-"`
1311}
1312
1313// Returns the unmodified JSON received from the API
1314func (r ChatCompletionMessageAnnotation) RawJSON() string { return r.JSON.raw }
1315func (r *ChatCompletionMessageAnnotation) UnmarshalJSON(data []byte) error {
1316 return apijson.UnmarshalRoot(data, r)
1317}
1318
1319// A URL citation when using web search.
1320type ChatCompletionMessageAnnotationURLCitation struct {
1321 // The index of the last character of the URL citation in the message.
1322 EndIndex int64 `json:"end_index,required"`
1323 // The index of the first character of the URL citation in the message.
1324 StartIndex int64 `json:"start_index,required"`
1325 // The title of the web resource.
1326 Title string `json:"title,required"`
1327 // The URL of the web resource.
1328 URL string `json:"url,required"`
1329 // Metadata for the response, check the presence of optional fields with the
1330 // [resp.Field.IsPresent] method.
1331 JSON struct {
1332 EndIndex resp.Field
1333 StartIndex resp.Field
1334 Title resp.Field
1335 URL resp.Field
1336 ExtraFields map[string]resp.Field
1337 raw string
1338 } `json:"-"`
1339}
1340
1341// Returns the unmodified JSON received from the API
1342func (r ChatCompletionMessageAnnotationURLCitation) RawJSON() string { return r.JSON.raw }
1343func (r *ChatCompletionMessageAnnotationURLCitation) UnmarshalJSON(data []byte) error {
1344 return apijson.UnmarshalRoot(data, r)
1345}
1346
1347// Deprecated and replaced by `tool_calls`. The name and arguments of a function
1348// that should be called, as generated by the model.
1349//
1350// Deprecated: deprecated
1351type ChatCompletionMessageFunctionCall struct {
1352 // The arguments to call the function with, as generated by the model in JSON
1353 // format. Note that the model does not always generate valid JSON, and may
1354 // hallucinate parameters not defined by your function schema. Validate the
1355 // arguments in your code before calling your function.
1356 Arguments string `json:"arguments,required"`
1357 // The name of the function to call.
1358 Name string `json:"name,required"`
1359 // Metadata for the response, check the presence of optional fields with the
1360 // [resp.Field.IsPresent] method.
1361 JSON struct {
1362 Arguments resp.Field
1363 Name resp.Field
1364 ExtraFields map[string]resp.Field
1365 raw string
1366 } `json:"-"`
1367}
1368
1369// Returns the unmodified JSON received from the API
1370func (r ChatCompletionMessageFunctionCall) RawJSON() string { return r.JSON.raw }
1371func (r *ChatCompletionMessageFunctionCall) UnmarshalJSON(data []byte) error {
1372 return apijson.UnmarshalRoot(data, r)
1373}
1374
1375func AssistantMessage[T string | []ChatCompletionAssistantMessageParamContentArrayOfContentPartUnion](content T) ChatCompletionMessageParamUnion {
1376 var assistant ChatCompletionAssistantMessageParam
1377 switch v := any(content).(type) {
1378 case string:
1379 assistant.Content.OfString = param.NewOpt(v)
1380 case []ChatCompletionAssistantMessageParamContentArrayOfContentPartUnion:
1381 assistant.Content.OfArrayOfContentParts = v
1382 }
1383 return ChatCompletionMessageParamUnion{OfAssistant: &assistant}
1384}
1385
1386func DeveloperMessage[T string | []ChatCompletionContentPartTextParam](content T) ChatCompletionMessageParamUnion {
1387 var developer ChatCompletionDeveloperMessageParam
1388 switch v := any(content).(type) {
1389 case string:
1390 developer.Content.OfString = param.NewOpt(v)
1391 case []ChatCompletionContentPartTextParam:
1392 developer.Content.OfArrayOfContentParts = v
1393 }
1394 return ChatCompletionMessageParamUnion{OfDeveloper: &developer}
1395}
1396
1397func SystemMessage[T string | []ChatCompletionContentPartTextParam](content T) ChatCompletionMessageParamUnion {
1398 var system ChatCompletionSystemMessageParam
1399 switch v := any(content).(type) {
1400 case string:
1401 system.Content.OfString = param.NewOpt(v)
1402 case []ChatCompletionContentPartTextParam:
1403 system.Content.OfArrayOfContentParts = v
1404 }
1405 return ChatCompletionMessageParamUnion{OfSystem: &system}
1406}
1407
1408func UserMessage[T string | []ChatCompletionContentPartUnionParam](content T) ChatCompletionMessageParamUnion {
1409 var user ChatCompletionUserMessageParam
1410 switch v := any(content).(type) {
1411 case string:
1412 user.Content.OfString = param.NewOpt(v)
1413 case []ChatCompletionContentPartUnionParam:
1414 user.Content.OfArrayOfContentParts = v
1415 }
1416 return ChatCompletionMessageParamUnion{OfUser: &user}
1417}
1418
1419func ToolMessage[T string | []ChatCompletionContentPartTextParam](content T, toolCallID string) ChatCompletionMessageParamUnion {
1420 var tool ChatCompletionToolMessageParam
1421 switch v := any(content).(type) {
1422 case string:
1423 tool.Content.OfString = param.NewOpt(v)
1424 case []ChatCompletionContentPartTextParam:
1425 tool.Content.OfArrayOfContentParts = v
1426 }
1427 tool.ToolCallID = toolCallID
1428 return ChatCompletionMessageParamUnion{OfTool: &tool}
1429}
1430
1431func ChatCompletionMessageParamOfFunction(content string, name string) ChatCompletionMessageParamUnion {
1432 var function ChatCompletionFunctionMessageParam
1433 function.Content = param.NewOpt(content)
1434 function.Name = name
1435 return ChatCompletionMessageParamUnion{OfFunction: &function}
1436}
1437
1438// Only one field can be non-zero.
1439//
1440// Use [param.IsOmitted] to confirm if a field is set.
1441type ChatCompletionMessageParamUnion struct {
1442 OfDeveloper *ChatCompletionDeveloperMessageParam `json:",omitzero,inline"`
1443 OfSystem *ChatCompletionSystemMessageParam `json:",omitzero,inline"`
1444 OfUser *ChatCompletionUserMessageParam `json:",omitzero,inline"`
1445 OfAssistant *ChatCompletionAssistantMessageParam `json:",omitzero,inline"`
1446 OfTool *ChatCompletionToolMessageParam `json:",omitzero,inline"`
1447 OfFunction *ChatCompletionFunctionMessageParam `json:",omitzero,inline"`
1448 paramUnion
1449}
1450
1451// IsPresent returns true if the field's value is not omitted and not the JSON
1452// "null". To check if this field is omitted, use [param.IsOmitted].
1453func (u ChatCompletionMessageParamUnion) IsPresent() bool { return !param.IsOmitted(u) && !u.IsNull() }
1454func (u ChatCompletionMessageParamUnion) MarshalJSON() ([]byte, error) {
1455 return param.MarshalUnion[ChatCompletionMessageParamUnion](u.OfDeveloper,
1456 u.OfSystem,
1457 u.OfUser,
1458 u.OfAssistant,
1459 u.OfTool,
1460 u.OfFunction)
1461}
1462
1463func (u *ChatCompletionMessageParamUnion) asAny() any {
1464 if !param.IsOmitted(u.OfDeveloper) {
1465 return u.OfDeveloper
1466 } else if !param.IsOmitted(u.OfSystem) {
1467 return u.OfSystem
1468 } else if !param.IsOmitted(u.OfUser) {
1469 return u.OfUser
1470 } else if !param.IsOmitted(u.OfAssistant) {
1471 return u.OfAssistant
1472 } else if !param.IsOmitted(u.OfTool) {
1473 return u.OfTool
1474 } else if !param.IsOmitted(u.OfFunction) {
1475 return u.OfFunction
1476 }
1477 return nil
1478}
1479
1480// Returns a pointer to the underlying variant's property, if present.
1481func (u ChatCompletionMessageParamUnion) GetAudio() *ChatCompletionAssistantMessageParamAudio {
1482 if vt := u.OfAssistant; vt != nil {
1483 return &vt.Audio
1484 }
1485 return nil
1486}
1487
1488// Returns a pointer to the underlying variant's property, if present.
1489func (u ChatCompletionMessageParamUnion) GetFunctionCall() *ChatCompletionAssistantMessageParamFunctionCall {
1490 if vt := u.OfAssistant; vt != nil {
1491 return &vt.FunctionCall
1492 }
1493 return nil
1494}
1495
1496// Returns a pointer to the underlying variant's property, if present.
1497func (u ChatCompletionMessageParamUnion) GetRefusal() *string {
1498 if vt := u.OfAssistant; vt != nil && vt.Refusal.IsPresent() {
1499 return &vt.Refusal.Value
1500 }
1501 return nil
1502}
1503
1504// Returns a pointer to the underlying variant's property, if present.
1505func (u ChatCompletionMessageParamUnion) GetToolCalls() []ChatCompletionMessageToolCallParam {
1506 if vt := u.OfAssistant; vt != nil {
1507 return vt.ToolCalls
1508 }
1509 return nil
1510}
1511
1512// Returns a pointer to the underlying variant's property, if present.
1513func (u ChatCompletionMessageParamUnion) GetToolCallID() *string {
1514 if vt := u.OfTool; vt != nil {
1515 return &vt.ToolCallID
1516 }
1517 return nil
1518}
1519
1520// Returns a pointer to the underlying variant's property, if present.
1521func (u ChatCompletionMessageParamUnion) GetRole() *string {
1522 if vt := u.OfDeveloper; vt != nil {
1523 return (*string)(&vt.Role)
1524 } else if vt := u.OfSystem; vt != nil {
1525 return (*string)(&vt.Role)
1526 } else if vt := u.OfUser; vt != nil {
1527 return (*string)(&vt.Role)
1528 } else if vt := u.OfAssistant; vt != nil {
1529 return (*string)(&vt.Role)
1530 } else if vt := u.OfTool; vt != nil {
1531 return (*string)(&vt.Role)
1532 } else if vt := u.OfFunction; vt != nil {
1533 return (*string)(&vt.Role)
1534 }
1535 return nil
1536}
1537
1538// Returns a pointer to the underlying variant's property, if present.
1539func (u ChatCompletionMessageParamUnion) GetName() *string {
1540 if vt := u.OfDeveloper; vt != nil && vt.Name.IsPresent() {
1541 return &vt.Name.Value
1542 } else if vt := u.OfSystem; vt != nil && vt.Name.IsPresent() {
1543 return &vt.Name.Value
1544 } else if vt := u.OfUser; vt != nil && vt.Name.IsPresent() {
1545 return &vt.Name.Value
1546 } else if vt := u.OfAssistant; vt != nil && vt.Name.IsPresent() {
1547 return &vt.Name.Value
1548 } else if vt := u.OfFunction; vt != nil {
1549 return (*string)(&vt.Name)
1550 }
1551 return nil
1552}
1553
1554// Returns a subunion which exports methods to access subproperties
1555//
1556// Or use AsAny() to get the underlying value
1557func (u ChatCompletionMessageParamUnion) GetContent() (res chatCompletionMessageParamUnionContent) {
1558 if vt := u.OfDeveloper; vt != nil {
1559 res.ofChatCompletionDeveloperMessageContent = &vt.Content
1560 } else if vt := u.OfSystem; vt != nil {
1561 res.ofChatCompletionSystemMessageContent = &vt.Content
1562 } else if vt := u.OfUser; vt != nil {
1563 res.ofChatCompletionUserMessageContent = &vt.Content
1564 } else if vt := u.OfAssistant; vt != nil {
1565 res.ofChatCompletionAssistantMessageContent = &vt.Content
1566 } else if vt := u.OfTool; vt != nil {
1567 res.ofChatCompletionToolMessageContent = &vt.Content
1568 } else if vt := u.OfFunction; vt != nil && vt.Content.IsPresent() {
1569 res.ofString = &vt.Content.Value
1570 }
1571 return
1572}
1573
1574// Only one field can be non-zero.
1575//
1576// Use [param.IsOmitted] to confirm if a field is set.
1577type chatCompletionMessageParamUnionContent struct {
1578 ofChatCompletionDeveloperMessageContent *ChatCompletionDeveloperMessageParamContentUnion
1579 ofChatCompletionSystemMessageContent *ChatCompletionSystemMessageParamContentUnion
1580 ofChatCompletionUserMessageContent *ChatCompletionUserMessageParamContentUnion
1581 ofChatCompletionAssistantMessageContent *ChatCompletionAssistantMessageParamContentUnion
1582 ofChatCompletionToolMessageContent *ChatCompletionToolMessageParamContentUnion
1583 ofString *string
1584}
1585
1586// Use the following switch statement to get the type of the union:
1587//
1588// switch u.AsAny().(type) {
1589// case *string:
1590// case *[]openai.ChatCompletionContentPartTextParam:
1591// case *[]openai.ChatCompletionContentPartUnionParam:
1592// case *[]openai.ChatCompletionAssistantMessageParamContentArrayOfContentPartUnion:
1593// default:
1594// fmt.Errorf("not present")
1595// }
1596func (u chatCompletionMessageParamUnionContent) AsAny() any {
1597 if !param.IsOmitted(u.ofChatCompletionDeveloperMessageContent) {
1598 return u.ofChatCompletionDeveloperMessageContent.asAny()
1599 } else if !param.IsOmitted(u.ofChatCompletionSystemMessageContent) {
1600 return u.ofChatCompletionSystemMessageContent.asAny()
1601 } else if !param.IsOmitted(u.ofChatCompletionUserMessageContent) {
1602 return u.ofChatCompletionUserMessageContent.asAny()
1603 } else if !param.IsOmitted(u.ofChatCompletionAssistantMessageContent) {
1604 return u.ofChatCompletionAssistantMessageContent.asAny()
1605 } else if !param.IsOmitted(u.ofChatCompletionToolMessageContent) {
1606 return u.ofChatCompletionToolMessageContent.asAny()
1607 } else if !param.IsOmitted(u.ofString) {
1608 return u.ofString
1609 }
1610 return nil
1611}
1612
1613func init() {
1614 apijson.RegisterUnion[ChatCompletionMessageParamUnion](
1615 "role",
1616 apijson.UnionVariant{
1617 TypeFilter: gjson.JSON,
1618 Type: reflect.TypeOf(ChatCompletionDeveloperMessageParam{}),
1619 DiscriminatorValue: "developer",
1620 },
1621 apijson.UnionVariant{
1622 TypeFilter: gjson.JSON,
1623 Type: reflect.TypeOf(ChatCompletionSystemMessageParam{}),
1624 DiscriminatorValue: "system",
1625 },
1626 apijson.UnionVariant{
1627 TypeFilter: gjson.JSON,
1628 Type: reflect.TypeOf(ChatCompletionUserMessageParam{}),
1629 DiscriminatorValue: "user",
1630 },
1631 apijson.UnionVariant{
1632 TypeFilter: gjson.JSON,
1633 Type: reflect.TypeOf(ChatCompletionAssistantMessageParam{}),
1634 DiscriminatorValue: "assistant",
1635 },
1636 apijson.UnionVariant{
1637 TypeFilter: gjson.JSON,
1638 Type: reflect.TypeOf(ChatCompletionToolMessageParam{}),
1639 DiscriminatorValue: "tool",
1640 },
1641 apijson.UnionVariant{
1642 TypeFilter: gjson.JSON,
1643 Type: reflect.TypeOf(ChatCompletionFunctionMessageParam{}),
1644 DiscriminatorValue: "function",
1645 },
1646 )
1647}
1648
1649type ChatCompletionMessageToolCall struct {
1650 // The ID of the tool call.
1651 ID string `json:"id,required"`
1652 // The function that the model called.
1653 Function ChatCompletionMessageToolCallFunction `json:"function,required"`
1654 // The type of the tool. Currently, only `function` is supported.
1655 Type constant.Function `json:"type,required"`
1656 // Metadata for the response, check the presence of optional fields with the
1657 // [resp.Field.IsPresent] method.
1658 JSON struct {
1659 ID resp.Field
1660 Function resp.Field
1661 Type resp.Field
1662 ExtraFields map[string]resp.Field
1663 raw string
1664 } `json:"-"`
1665}
1666
1667// Returns the unmodified JSON received from the API
1668func (r ChatCompletionMessageToolCall) RawJSON() string { return r.JSON.raw }
1669func (r *ChatCompletionMessageToolCall) UnmarshalJSON(data []byte) error {
1670 return apijson.UnmarshalRoot(data, r)
1671}
1672
1673// ToParam converts this ChatCompletionMessageToolCall to a
1674// ChatCompletionMessageToolCallParam.
1675//
1676// Warning: the fields of the param type will not be present. ToParam should only
1677// be used at the last possible moment before sending a request. Test for this with
1678// ChatCompletionMessageToolCallParam.IsOverridden()
1679func (r ChatCompletionMessageToolCall) ToParam() ChatCompletionMessageToolCallParam {
1680 return param.OverrideObj[ChatCompletionMessageToolCallParam](r.RawJSON())
1681}
1682
1683// The function that the model called.
1684type ChatCompletionMessageToolCallFunction struct {
1685 // The arguments to call the function with, as generated by the model in JSON
1686 // format. Note that the model does not always generate valid JSON, and may
1687 // hallucinate parameters not defined by your function schema. Validate the
1688 // arguments in your code before calling your function.
1689 Arguments string `json:"arguments,required"`
1690 // The name of the function to call.
1691 Name string `json:"name,required"`
1692 // Metadata for the response, check the presence of optional fields with the
1693 // [resp.Field.IsPresent] method.
1694 JSON struct {
1695 Arguments resp.Field
1696 Name resp.Field
1697 ExtraFields map[string]resp.Field
1698 raw string
1699 } `json:"-"`
1700}
1701
1702// Returns the unmodified JSON received from the API
1703func (r ChatCompletionMessageToolCallFunction) RawJSON() string { return r.JSON.raw }
1704func (r *ChatCompletionMessageToolCallFunction) UnmarshalJSON(data []byte) error {
1705 return apijson.UnmarshalRoot(data, r)
1706}
1707
1708// The properties ID, Function, Type are required.
1709type ChatCompletionMessageToolCallParam struct {
1710 // The ID of the tool call.
1711 ID string `json:"id,required"`
1712 // The function that the model called.
1713 Function ChatCompletionMessageToolCallFunctionParam `json:"function,omitzero,required"`
1714 // The type of the tool. Currently, only `function` is supported.
1715 //
1716 // This field can be elided, and will marshal its zero value as "function".
1717 Type constant.Function `json:"type,required"`
1718 paramObj
1719}
1720
1721// IsPresent returns true if the field's value is not omitted and not the JSON
1722// "null". To check if this field is omitted, use [param.IsOmitted].
1723func (f ChatCompletionMessageToolCallParam) IsPresent() bool {
1724 return !param.IsOmitted(f) && !f.IsNull()
1725}
1726func (r ChatCompletionMessageToolCallParam) MarshalJSON() (data []byte, err error) {
1727 type shadow ChatCompletionMessageToolCallParam
1728 return param.MarshalObject(r, (*shadow)(&r))
1729}
1730
1731// The function that the model called.
1732//
1733// The properties Arguments, Name are required.
1734type ChatCompletionMessageToolCallFunctionParam struct {
1735 // The arguments to call the function with, as generated by the model in JSON
1736 // format. Note that the model does not always generate valid JSON, and may
1737 // hallucinate parameters not defined by your function schema. Validate the
1738 // arguments in your code before calling your function.
1739 Arguments string `json:"arguments,required"`
1740 // The name of the function to call.
1741 Name string `json:"name,required"`
1742 paramObj
1743}
1744
1745// IsPresent returns true if the field's value is not omitted and not the JSON
1746// "null". To check if this field is omitted, use [param.IsOmitted].
1747func (f ChatCompletionMessageToolCallFunctionParam) IsPresent() bool {
1748 return !param.IsOmitted(f) && !f.IsNull()
1749}
1750func (r ChatCompletionMessageToolCallFunctionParam) MarshalJSON() (data []byte, err error) {
1751 type shadow ChatCompletionMessageToolCallFunctionParam
1752 return param.MarshalObject(r, (*shadow)(&r))
1753}
1754
1755// Specifies a tool the model should use. Use to force the model to call a specific
1756// function.
1757//
1758// The properties Function, Type are required.
1759type ChatCompletionNamedToolChoiceParam struct {
1760 Function ChatCompletionNamedToolChoiceFunctionParam `json:"function,omitzero,required"`
1761 // The type of the tool. Currently, only `function` is supported.
1762 //
1763 // This field can be elided, and will marshal its zero value as "function".
1764 Type constant.Function `json:"type,required"`
1765 paramObj
1766}
1767
1768// IsPresent returns true if the field's value is not omitted and not the JSON
1769// "null". To check if this field is omitted, use [param.IsOmitted].
1770func (f ChatCompletionNamedToolChoiceParam) IsPresent() bool {
1771 return !param.IsOmitted(f) && !f.IsNull()
1772}
1773func (r ChatCompletionNamedToolChoiceParam) MarshalJSON() (data []byte, err error) {
1774 type shadow ChatCompletionNamedToolChoiceParam
1775 return param.MarshalObject(r, (*shadow)(&r))
1776}
1777
1778// The property Name is required.
1779type ChatCompletionNamedToolChoiceFunctionParam struct {
1780 // The name of the function to call.
1781 Name string `json:"name,required"`
1782 paramObj
1783}
1784
1785// IsPresent returns true if the field's value is not omitted and not the JSON
1786// "null". To check if this field is omitted, use [param.IsOmitted].
1787func (f ChatCompletionNamedToolChoiceFunctionParam) IsPresent() bool {
1788 return !param.IsOmitted(f) && !f.IsNull()
1789}
1790func (r ChatCompletionNamedToolChoiceFunctionParam) MarshalJSON() (data []byte, err error) {
1791 type shadow ChatCompletionNamedToolChoiceFunctionParam
1792 return param.MarshalObject(r, (*shadow)(&r))
1793}
1794
1795// Static predicted output content, such as the content of a text file that is
1796// being regenerated.
1797//
1798// The properties Content, Type are required.
1799type ChatCompletionPredictionContentParam struct {
1800 // The content that should be matched when generating a model response. If
1801 // generated tokens would match this content, the entire model response can be
1802 // returned much more quickly.
1803 Content ChatCompletionPredictionContentContentUnionParam `json:"content,omitzero,required"`
1804 // The type of the predicted content you want to provide. This type is currently
1805 // always `content`.
1806 //
1807 // This field can be elided, and will marshal its zero value as "content".
1808 Type constant.Content `json:"type,required"`
1809 paramObj
1810}
1811
1812// IsPresent returns true if the field's value is not omitted and not the JSON
1813// "null". To check if this field is omitted, use [param.IsOmitted].
1814func (f ChatCompletionPredictionContentParam) IsPresent() bool {
1815 return !param.IsOmitted(f) && !f.IsNull()
1816}
1817func (r ChatCompletionPredictionContentParam) MarshalJSON() (data []byte, err error) {
1818 type shadow ChatCompletionPredictionContentParam
1819 return param.MarshalObject(r, (*shadow)(&r))
1820}
1821
1822// Only one field can be non-zero.
1823//
1824// Use [param.IsOmitted] to confirm if a field is set.
1825type ChatCompletionPredictionContentContentUnionParam struct {
1826 OfString param.Opt[string] `json:",omitzero,inline"`
1827 OfArrayOfContentParts []ChatCompletionContentPartTextParam `json:",omitzero,inline"`
1828 paramUnion
1829}
1830
1831// IsPresent returns true if the field's value is not omitted and not the JSON
1832// "null". To check if this field is omitted, use [param.IsOmitted].
1833func (u ChatCompletionPredictionContentContentUnionParam) IsPresent() bool {
1834 return !param.IsOmitted(u) && !u.IsNull()
1835}
1836func (u ChatCompletionPredictionContentContentUnionParam) MarshalJSON() ([]byte, error) {
1837 return param.MarshalUnion[ChatCompletionPredictionContentContentUnionParam](u.OfString, u.OfArrayOfContentParts)
1838}
1839
1840func (u *ChatCompletionPredictionContentContentUnionParam) asAny() any {
1841 if !param.IsOmitted(u.OfString) {
1842 return &u.OfString.Value
1843 } else if !param.IsOmitted(u.OfArrayOfContentParts) {
1844 return &u.OfArrayOfContentParts
1845 }
1846 return nil
1847}
1848
1849// A chat completion message generated by the model.
1850type ChatCompletionStoreMessage struct {
1851 // The identifier of the chat message.
1852 ID string `json:"id,required"`
1853 // Metadata for the response, check the presence of optional fields with the
1854 // [resp.Field.IsPresent] method.
1855 JSON struct {
1856 ID resp.Field
1857 ExtraFields map[string]resp.Field
1858 raw string
1859 } `json:"-"`
1860 ChatCompletionMessage
1861}
1862
1863// Returns the unmodified JSON received from the API
1864func (r ChatCompletionStoreMessage) RawJSON() string { return r.JSON.raw }
1865func (r *ChatCompletionStoreMessage) UnmarshalJSON(data []byte) error {
1866 return apijson.UnmarshalRoot(data, r)
1867}
1868
1869// Options for streaming response. Only set this when you set `stream: true`.
1870type ChatCompletionStreamOptionsParam struct {
1871 // If set, an additional chunk will be streamed before the `data: [DONE]` message.
1872 // The `usage` field on this chunk shows the token usage statistics for the entire
1873 // request, and the `choices` field will always be an empty array.
1874 //
1875 // All other chunks will also include a `usage` field, but with a null value.
1876 // **NOTE:** If the stream is interrupted, you may not receive the final usage
1877 // chunk which contains the total token usage for the request.
1878 IncludeUsage param.Opt[bool] `json:"include_usage,omitzero"`
1879 paramObj
1880}
1881
1882// IsPresent returns true if the field's value is not omitted and not the JSON
1883// "null". To check if this field is omitted, use [param.IsOmitted].
1884func (f ChatCompletionStreamOptionsParam) IsPresent() bool { return !param.IsOmitted(f) && !f.IsNull() }
1885func (r ChatCompletionStreamOptionsParam) MarshalJSON() (data []byte, err error) {
1886 type shadow ChatCompletionStreamOptionsParam
1887 return param.MarshalObject(r, (*shadow)(&r))
1888}
1889
1890// Developer-provided instructions that the model should follow, regardless of
1891// messages sent by the user. With o1 models and newer, use `developer` messages
1892// for this purpose instead.
1893//
1894// The properties Content, Role are required.
1895type ChatCompletionSystemMessageParam struct {
1896 // The contents of the system message.
1897 Content ChatCompletionSystemMessageParamContentUnion `json:"content,omitzero,required"`
1898 // An optional name for the participant. Provides the model information to
1899 // differentiate between participants of the same role.
1900 Name param.Opt[string] `json:"name,omitzero"`
1901 // The role of the messages author, in this case `system`.
1902 //
1903 // This field can be elided, and will marshal its zero value as "system".
1904 Role constant.System `json:"role,required"`
1905 paramObj
1906}
1907
1908// IsPresent returns true if the field's value is not omitted and not the JSON
1909// "null". To check if this field is omitted, use [param.IsOmitted].
1910func (f ChatCompletionSystemMessageParam) IsPresent() bool { return !param.IsOmitted(f) && !f.IsNull() }
1911func (r ChatCompletionSystemMessageParam) MarshalJSON() (data []byte, err error) {
1912 type shadow ChatCompletionSystemMessageParam
1913 return param.MarshalObject(r, (*shadow)(&r))
1914}
1915
1916// Only one field can be non-zero.
1917//
1918// Use [param.IsOmitted] to confirm if a field is set.
1919type ChatCompletionSystemMessageParamContentUnion struct {
1920 OfString param.Opt[string] `json:",omitzero,inline"`
1921 OfArrayOfContentParts []ChatCompletionContentPartTextParam `json:",omitzero,inline"`
1922 paramUnion
1923}
1924
1925// IsPresent returns true if the field's value is not omitted and not the JSON
1926// "null". To check if this field is omitted, use [param.IsOmitted].
1927func (u ChatCompletionSystemMessageParamContentUnion) IsPresent() bool {
1928 return !param.IsOmitted(u) && !u.IsNull()
1929}
1930func (u ChatCompletionSystemMessageParamContentUnion) MarshalJSON() ([]byte, error) {
1931 return param.MarshalUnion[ChatCompletionSystemMessageParamContentUnion](u.OfString, u.OfArrayOfContentParts)
1932}
1933
1934func (u *ChatCompletionSystemMessageParamContentUnion) asAny() any {
1935 if !param.IsOmitted(u.OfString) {
1936 return &u.OfString.Value
1937 } else if !param.IsOmitted(u.OfArrayOfContentParts) {
1938 return &u.OfArrayOfContentParts
1939 }
1940 return nil
1941}
1942
1943type ChatCompletionTokenLogprob struct {
1944 // The token.
1945 Token string `json:"token,required"`
1946 // A list of integers representing the UTF-8 bytes representation of the token.
1947 // Useful in instances where characters are represented by multiple tokens and
1948 // their byte representations must be combined to generate the correct text
1949 // representation. Can be `null` if there is no bytes representation for the token.
1950 Bytes []int64 `json:"bytes,required"`
1951 // The log probability of this token, if it is within the top 20 most likely
1952 // tokens. Otherwise, the value `-9999.0` is used to signify that the token is very
1953 // unlikely.
1954 Logprob float64 `json:"logprob,required"`
1955 // List of the most likely tokens and their log probability, at this token
1956 // position. In rare cases, there may be fewer than the number of requested
1957 // `top_logprobs` returned.
1958 TopLogprobs []ChatCompletionTokenLogprobTopLogprob `json:"top_logprobs,required"`
1959 // Metadata for the response, check the presence of optional fields with the
1960 // [resp.Field.IsPresent] method.
1961 JSON struct {
1962 Token resp.Field
1963 Bytes resp.Field
1964 Logprob resp.Field
1965 TopLogprobs resp.Field
1966 ExtraFields map[string]resp.Field
1967 raw string
1968 } `json:"-"`
1969}
1970
1971// Returns the unmodified JSON received from the API
1972func (r ChatCompletionTokenLogprob) RawJSON() string { return r.JSON.raw }
1973func (r *ChatCompletionTokenLogprob) UnmarshalJSON(data []byte) error {
1974 return apijson.UnmarshalRoot(data, r)
1975}
1976
1977type ChatCompletionTokenLogprobTopLogprob struct {
1978 // The token.
1979 Token string `json:"token,required"`
1980 // A list of integers representing the UTF-8 bytes representation of the token.
1981 // Useful in instances where characters are represented by multiple tokens and
1982 // their byte representations must be combined to generate the correct text
1983 // representation. Can be `null` if there is no bytes representation for the token.
1984 Bytes []int64 `json:"bytes,required"`
1985 // The log probability of this token, if it is within the top 20 most likely
1986 // tokens. Otherwise, the value `-9999.0` is used to signify that the token is very
1987 // unlikely.
1988 Logprob float64 `json:"logprob,required"`
1989 // Metadata for the response, check the presence of optional fields with the
1990 // [resp.Field.IsPresent] method.
1991 JSON struct {
1992 Token resp.Field
1993 Bytes resp.Field
1994 Logprob resp.Field
1995 ExtraFields map[string]resp.Field
1996 raw string
1997 } `json:"-"`
1998}
1999
2000// Returns the unmodified JSON received from the API
2001func (r ChatCompletionTokenLogprobTopLogprob) RawJSON() string { return r.JSON.raw }
2002func (r *ChatCompletionTokenLogprobTopLogprob) UnmarshalJSON(data []byte) error {
2003 return apijson.UnmarshalRoot(data, r)
2004}
2005
2006// The properties Function, Type are required.
2007type ChatCompletionToolParam struct {
2008 Function shared.FunctionDefinitionParam `json:"function,omitzero,required"`
2009 // The type of the tool. Currently, only `function` is supported.
2010 //
2011 // This field can be elided, and will marshal its zero value as "function".
2012 Type constant.Function `json:"type,required"`
2013 paramObj
2014}
2015
2016// IsPresent returns true if the field's value is not omitted and not the JSON
2017// "null". To check if this field is omitted, use [param.IsOmitted].
2018func (f ChatCompletionToolParam) IsPresent() bool { return !param.IsOmitted(f) && !f.IsNull() }
2019func (r ChatCompletionToolParam) MarshalJSON() (data []byte, err error) {
2020 type shadow ChatCompletionToolParam
2021 return param.MarshalObject(r, (*shadow)(&r))
2022}
2023
2024func ChatCompletionToolChoiceOptionParamOfChatCompletionNamedToolChoice(function ChatCompletionNamedToolChoiceFunctionParam) ChatCompletionToolChoiceOptionUnionParam {
2025 var variant ChatCompletionNamedToolChoiceParam
2026 variant.Function = function
2027 return ChatCompletionToolChoiceOptionUnionParam{OfChatCompletionNamedToolChoice: &variant}
2028}
2029
2030// Only one field can be non-zero.
2031//
2032// Use [param.IsOmitted] to confirm if a field is set.
2033type ChatCompletionToolChoiceOptionUnionParam struct {
2034 // Check if union is this variant with !param.IsOmitted(union.OfAuto)
2035 OfAuto param.Opt[string] `json:",omitzero,inline"`
2036 OfChatCompletionNamedToolChoice *ChatCompletionNamedToolChoiceParam `json:",omitzero,inline"`
2037 paramUnion
2038}
2039
2040// IsPresent returns true if the field's value is not omitted and not the JSON
2041// "null". To check if this field is omitted, use [param.IsOmitted].
2042func (u ChatCompletionToolChoiceOptionUnionParam) IsPresent() bool {
2043 return !param.IsOmitted(u) && !u.IsNull()
2044}
2045func (u ChatCompletionToolChoiceOptionUnionParam) MarshalJSON() ([]byte, error) {
2046 return param.MarshalUnion[ChatCompletionToolChoiceOptionUnionParam](u.OfAuto, u.OfChatCompletionNamedToolChoice)
2047}
2048
2049func (u *ChatCompletionToolChoiceOptionUnionParam) asAny() any {
2050 if !param.IsOmitted(u.OfAuto) {
2051 return &u.OfAuto
2052 } else if !param.IsOmitted(u.OfChatCompletionNamedToolChoice) {
2053 return u.OfChatCompletionNamedToolChoice
2054 }
2055 return nil
2056}
2057
2058// Returns a pointer to the underlying variant's property, if present.
2059func (u ChatCompletionToolChoiceOptionUnionParam) GetFunction() *ChatCompletionNamedToolChoiceFunctionParam {
2060 if vt := u.OfChatCompletionNamedToolChoice; vt != nil {
2061 return &vt.Function
2062 }
2063 return nil
2064}
2065
2066// Returns a pointer to the underlying variant's property, if present.
2067func (u ChatCompletionToolChoiceOptionUnionParam) GetType() *string {
2068 if vt := u.OfChatCompletionNamedToolChoice; vt != nil {
2069 return (*string)(&vt.Type)
2070 }
2071 return nil
2072}
2073
2074// The properties Content, Role, ToolCallID are required.
2075type ChatCompletionToolMessageParam struct {
2076 // The contents of the tool message.
2077 Content ChatCompletionToolMessageParamContentUnion `json:"content,omitzero,required"`
2078 // Tool call that this message is responding to.
2079 ToolCallID string `json:"tool_call_id,required"`
2080 // The role of the messages author, in this case `tool`.
2081 //
2082 // This field can be elided, and will marshal its zero value as "tool".
2083 Role constant.Tool `json:"role,required"`
2084 paramObj
2085}
2086
2087// IsPresent returns true if the field's value is not omitted and not the JSON
2088// "null". To check if this field is omitted, use [param.IsOmitted].
2089func (f ChatCompletionToolMessageParam) IsPresent() bool { return !param.IsOmitted(f) && !f.IsNull() }
2090func (r ChatCompletionToolMessageParam) MarshalJSON() (data []byte, err error) {
2091 type shadow ChatCompletionToolMessageParam
2092 return param.MarshalObject(r, (*shadow)(&r))
2093}
2094
2095// Only one field can be non-zero.
2096//
2097// Use [param.IsOmitted] to confirm if a field is set.
2098type ChatCompletionToolMessageParamContentUnion struct {
2099 OfString param.Opt[string] `json:",omitzero,inline"`
2100 OfArrayOfContentParts []ChatCompletionContentPartTextParam `json:",omitzero,inline"`
2101 paramUnion
2102}
2103
2104// IsPresent returns true if the field's value is not omitted and not the JSON
2105// "null". To check if this field is omitted, use [param.IsOmitted].
2106func (u ChatCompletionToolMessageParamContentUnion) IsPresent() bool {
2107 return !param.IsOmitted(u) && !u.IsNull()
2108}
2109func (u ChatCompletionToolMessageParamContentUnion) MarshalJSON() ([]byte, error) {
2110 return param.MarshalUnion[ChatCompletionToolMessageParamContentUnion](u.OfString, u.OfArrayOfContentParts)
2111}
2112
2113func (u *ChatCompletionToolMessageParamContentUnion) asAny() any {
2114 if !param.IsOmitted(u.OfString) {
2115 return &u.OfString.Value
2116 } else if !param.IsOmitted(u.OfArrayOfContentParts) {
2117 return &u.OfArrayOfContentParts
2118 }
2119 return nil
2120}
2121
2122// Messages sent by an end user, containing prompts or additional context
2123// information.
2124//
2125// The properties Content, Role are required.
2126type ChatCompletionUserMessageParam struct {
2127 // The contents of the user message.
2128 Content ChatCompletionUserMessageParamContentUnion `json:"content,omitzero,required"`
2129 // An optional name for the participant. Provides the model information to
2130 // differentiate between participants of the same role.
2131 Name param.Opt[string] `json:"name,omitzero"`
2132 // The role of the messages author, in this case `user`.
2133 //
2134 // This field can be elided, and will marshal its zero value as "user".
2135 Role constant.User `json:"role,required"`
2136 paramObj
2137}
2138
2139// IsPresent returns true if the field's value is not omitted and not the JSON
2140// "null". To check if this field is omitted, use [param.IsOmitted].
2141func (f ChatCompletionUserMessageParam) IsPresent() bool { return !param.IsOmitted(f) && !f.IsNull() }
2142func (r ChatCompletionUserMessageParam) MarshalJSON() (data []byte, err error) {
2143 type shadow ChatCompletionUserMessageParam
2144 return param.MarshalObject(r, (*shadow)(&r))
2145}
2146
2147// Only one field can be non-zero.
2148//
2149// Use [param.IsOmitted] to confirm if a field is set.
2150type ChatCompletionUserMessageParamContentUnion struct {
2151 OfString param.Opt[string] `json:",omitzero,inline"`
2152 OfArrayOfContentParts []ChatCompletionContentPartUnionParam `json:",omitzero,inline"`
2153 paramUnion
2154}
2155
2156// IsPresent returns true if the field's value is not omitted and not the JSON
2157// "null". To check if this field is omitted, use [param.IsOmitted].
2158func (u ChatCompletionUserMessageParamContentUnion) IsPresent() bool {
2159 return !param.IsOmitted(u) && !u.IsNull()
2160}
2161func (u ChatCompletionUserMessageParamContentUnion) MarshalJSON() ([]byte, error) {
2162 return param.MarshalUnion[ChatCompletionUserMessageParamContentUnion](u.OfString, u.OfArrayOfContentParts)
2163}
2164
2165func (u *ChatCompletionUserMessageParamContentUnion) asAny() any {
2166 if !param.IsOmitted(u.OfString) {
2167 return &u.OfString.Value
2168 } else if !param.IsOmitted(u.OfArrayOfContentParts) {
2169 return &u.OfArrayOfContentParts
2170 }
2171 return nil
2172}
2173
2174type ChatCompletionNewParams struct {
2175 // A list of messages comprising the conversation so far. Depending on the
2176 // [model](https://platform.openai.com/docs/models) you use, different message
2177 // types (modalities) are supported, like
2178 // [text](https://platform.openai.com/docs/guides/text-generation),
2179 // [images](https://platform.openai.com/docs/guides/vision), and
2180 // [audio](https://platform.openai.com/docs/guides/audio).
2181 Messages []ChatCompletionMessageParamUnion `json:"messages,omitzero,required"`
2182 // Model ID used to generate the response, like `gpt-4o` or `o1`. OpenAI offers a
2183 // wide range of models with different capabilities, performance characteristics,
2184 // and price points. Refer to the
2185 // [model guide](https://platform.openai.com/docs/models) to browse and compare
2186 // available models.
2187 Model shared.ChatModel `json:"model,omitzero,required"`
2188 // Number between -2.0 and 2.0. Positive values penalize new tokens based on their
2189 // existing frequency in the text so far, decreasing the model's likelihood to
2190 // repeat the same line verbatim.
2191 FrequencyPenalty param.Opt[float64] `json:"frequency_penalty,omitzero"`
2192 // Whether to return log probabilities of the output tokens or not. If true,
2193 // returns the log probabilities of each output token returned in the `content` of
2194 // `message`.
2195 Logprobs param.Opt[bool] `json:"logprobs,omitzero"`
2196 // An upper bound for the number of tokens that can be generated for a completion,
2197 // including visible output tokens and
2198 // [reasoning tokens](https://platform.openai.com/docs/guides/reasoning).
2199 MaxCompletionTokens param.Opt[int64] `json:"max_completion_tokens,omitzero"`
2200 // The maximum number of [tokens](/tokenizer) that can be generated in the chat
2201 // completion. This value can be used to control
2202 // [costs](https://openai.com/api/pricing/) for text generated via API.
2203 //
2204 // This value is now deprecated in favor of `max_completion_tokens`, and is not
2205 // compatible with
2206 // [o1 series models](https://platform.openai.com/docs/guides/reasoning).
2207 MaxTokens param.Opt[int64] `json:"max_tokens,omitzero"`
2208 // How many chat completion choices to generate for each input message. Note that
2209 // you will be charged based on the number of generated tokens across all of the
2210 // choices. Keep `n` as `1` to minimize costs.
2211 N param.Opt[int64] `json:"n,omitzero"`
2212 // Number between -2.0 and 2.0. Positive values penalize new tokens based on
2213 // whether they appear in the text so far, increasing the model's likelihood to
2214 // talk about new topics.
2215 PresencePenalty param.Opt[float64] `json:"presence_penalty,omitzero"`
2216 // This feature is in Beta. If specified, our system will make a best effort to
2217 // sample deterministically, such that repeated requests with the same `seed` and
2218 // parameters should return the same result. Determinism is not guaranteed, and you
2219 // should refer to the `system_fingerprint` response parameter to monitor changes
2220 // in the backend.
2221 Seed param.Opt[int64] `json:"seed,omitzero"`
2222 // Whether or not to store the output of this chat completion request for use in
2223 // our [model distillation](https://platform.openai.com/docs/guides/distillation)
2224 // or [evals](https://platform.openai.com/docs/guides/evals) products.
2225 Store param.Opt[bool] `json:"store,omitzero"`
2226 // What sampling temperature to use, between 0 and 2. Higher values like 0.8 will
2227 // make the output more random, while lower values like 0.2 will make it more
2228 // focused and deterministic. We generally recommend altering this or `top_p` but
2229 // not both.
2230 Temperature param.Opt[float64] `json:"temperature,omitzero"`
2231 // An integer between 0 and 20 specifying the number of most likely tokens to
2232 // return at each token position, each with an associated log probability.
2233 // `logprobs` must be set to `true` if this parameter is used.
2234 TopLogprobs param.Opt[int64] `json:"top_logprobs,omitzero"`
2235 // An alternative to sampling with temperature, called nucleus sampling, where the
2236 // model considers the results of the tokens with top_p probability mass. So 0.1
2237 // means only the tokens comprising the top 10% probability mass are considered.
2238 //
2239 // We generally recommend altering this or `temperature` but not both.
2240 TopP param.Opt[float64] `json:"top_p,omitzero"`
2241 // Whether to enable
2242 // [parallel function calling](https://platform.openai.com/docs/guides/function-calling#configuring-parallel-function-calling)
2243 // during tool use.
2244 ParallelToolCalls param.Opt[bool] `json:"parallel_tool_calls,omitzero"`
2245 // A unique identifier representing your end-user, which can help OpenAI to monitor
2246 // and detect abuse.
2247 // [Learn more](https://platform.openai.com/docs/guides/safety-best-practices#end-user-ids).
2248 User param.Opt[string] `json:"user,omitzero"`
2249 // Parameters for audio output. Required when audio output is requested with
2250 // `modalities: ["audio"]`.
2251 // [Learn more](https://platform.openai.com/docs/guides/audio).
2252 Audio ChatCompletionAudioParam `json:"audio,omitzero"`
2253 // Modify the likelihood of specified tokens appearing in the completion.
2254 //
2255 // Accepts a JSON object that maps tokens (specified by their token ID in the
2256 // tokenizer) to an associated bias value from -100 to 100. Mathematically, the
2257 // bias is added to the logits generated by the model prior to sampling. The exact
2258 // effect will vary per model, but values between -1 and 1 should decrease or
2259 // increase likelihood of selection; values like -100 or 100 should result in a ban
2260 // or exclusive selection of the relevant token.
2261 LogitBias map[string]int64 `json:"logit_bias,omitzero"`
2262 // Set of 16 key-value pairs that can be attached to an object. This can be useful
2263 // for storing additional information about the object in a structured format, and
2264 // querying for objects via API or the dashboard.
2265 //
2266 // Keys are strings with a maximum length of 64 characters. Values are strings with
2267 // a maximum length of 512 characters.
2268 Metadata shared.MetadataParam `json:"metadata,omitzero"`
2269 // Output types that you would like the model to generate. Most models are capable
2270 // of generating text, which is the default:
2271 //
2272 // `["text"]`
2273 //
2274 // The `gpt-4o-audio-preview` model can also be used to
2275 // [generate audio](https://platform.openai.com/docs/guides/audio). To request that
2276 // this model generate both text and audio responses, you can use:
2277 //
2278 // `["text", "audio"]`
2279 Modalities []string `json:"modalities,omitzero"`
2280 // **o-series models only**
2281 //
2282 // Constrains effort on reasoning for
2283 // [reasoning models](https://platform.openai.com/docs/guides/reasoning). Currently
2284 // supported values are `low`, `medium`, and `high`. Reducing reasoning effort can
2285 // result in faster responses and fewer tokens used on reasoning in a response.
2286 //
2287 // Any of "low", "medium", "high".
2288 ReasoningEffort shared.ReasoningEffort `json:"reasoning_effort,omitzero"`
2289 // Specifies the latency tier to use for processing the request. This parameter is
2290 // relevant for customers subscribed to the scale tier service:
2291 //
2292 // - If set to 'auto', and the Project is Scale tier enabled, the system will
2293 // utilize scale tier credits until they are exhausted.
2294 // - If set to 'auto', and the Project is not Scale tier enabled, the request will
2295 // be processed using the default service tier with a lower uptime SLA and no
2296 // latency guarentee.
2297 // - If set to 'default', the request will be processed using the default service
2298 // tier with a lower uptime SLA and no latency guarentee.
2299 // - When not set, the default behavior is 'auto'.
2300 //
2301 // When this parameter is set, the response body will include the `service_tier`
2302 // utilized.
2303 //
2304 // Any of "auto", "default".
2305 ServiceTier ChatCompletionNewParamsServiceTier `json:"service_tier,omitzero"`
2306 // Up to 4 sequences where the API will stop generating further tokens. The
2307 // returned text will not contain the stop sequence.
2308 Stop ChatCompletionNewParamsStopUnion `json:"stop,omitzero"`
2309 // Options for streaming response. Only set this when you set `stream: true`.
2310 StreamOptions ChatCompletionStreamOptionsParam `json:"stream_options,omitzero"`
2311 // Deprecated in favor of `tool_choice`.
2312 //
2313 // Controls which (if any) function is called by the model.
2314 //
2315 // `none` means the model will not call a function and instead generates a message.
2316 //
2317 // `auto` means the model can pick between generating a message or calling a
2318 // function.
2319 //
2320 // Specifying a particular function via `{"name": "my_function"}` forces the model
2321 // to call that function.
2322 //
2323 // `none` is the default when no functions are present. `auto` is the default if
2324 // functions are present.
2325 FunctionCall ChatCompletionNewParamsFunctionCallUnion `json:"function_call,omitzero"`
2326 // Deprecated in favor of `tools`.
2327 //
2328 // A list of functions the model may generate JSON inputs for.
2329 Functions []ChatCompletionNewParamsFunction `json:"functions,omitzero"`
2330 // Static predicted output content, such as the content of a text file that is
2331 // being regenerated.
2332 Prediction ChatCompletionPredictionContentParam `json:"prediction,omitzero"`
2333 // An object specifying the format that the model must output.
2334 //
2335 // Setting to `{ "type": "json_schema", "json_schema": {...} }` enables Structured
2336 // Outputs which ensures the model will match your supplied JSON schema. Learn more
2337 // in the
2338 // [Structured Outputs guide](https://platform.openai.com/docs/guides/structured-outputs).
2339 //
2340 // Setting to `{ "type": "json_object" }` enables the older JSON mode, which
2341 // ensures the message the model generates is valid JSON. Using `json_schema` is
2342 // preferred for models that support it.
2343 ResponseFormat ChatCompletionNewParamsResponseFormatUnion `json:"response_format,omitzero"`
2344 // Controls which (if any) tool is called by the model. `none` means the model will
2345 // not call any tool and instead generates a message. `auto` means the model can
2346 // pick between generating a message or calling one or more tools. `required` means
2347 // the model must call one or more tools. Specifying a particular tool via
2348 // `{"type": "function", "function": {"name": "my_function"}}` forces the model to
2349 // call that tool.
2350 //
2351 // `none` is the default when no tools are present. `auto` is the default if tools
2352 // are present.
2353 ToolChoice ChatCompletionToolChoiceOptionUnionParam `json:"tool_choice,omitzero"`
2354 // A list of tools the model may call. Currently, only functions are supported as a
2355 // tool. Use this to provide a list of functions the model may generate JSON inputs
2356 // for. A max of 128 functions are supported.
2357 Tools []ChatCompletionToolParam `json:"tools,omitzero"`
2358 // This tool searches the web for relevant results to use in a response. Learn more
2359 // about the
2360 // [web search tool](https://platform.openai.com/docs/guides/tools-web-search?api-mode=chat).
2361 WebSearchOptions ChatCompletionNewParamsWebSearchOptions `json:"web_search_options,omitzero"`
2362 paramObj
2363}
2364
2365// IsPresent returns true if the field's value is not omitted and not the JSON
2366// "null". To check if this field is omitted, use [param.IsOmitted].
2367func (f ChatCompletionNewParams) IsPresent() bool { return !param.IsOmitted(f) && !f.IsNull() }
2368
2369func (r ChatCompletionNewParams) MarshalJSON() (data []byte, err error) {
2370 type shadow ChatCompletionNewParams
2371 return param.MarshalObject(r, (*shadow)(&r))
2372}
2373
2374// Only one field can be non-zero.
2375//
2376// Use [param.IsOmitted] to confirm if a field is set.
2377type ChatCompletionNewParamsFunctionCallUnion struct {
2378 // Check if union is this variant with !param.IsOmitted(union.OfFunctionCallMode)
2379 OfFunctionCallMode param.Opt[string] `json:",omitzero,inline"`
2380 OfFunctionCallOption *ChatCompletionFunctionCallOptionParam `json:",omitzero,inline"`
2381 paramUnion
2382}
2383
2384// IsPresent returns true if the field's value is not omitted and not the JSON
2385// "null". To check if this field is omitted, use [param.IsOmitted].
2386func (u ChatCompletionNewParamsFunctionCallUnion) IsPresent() bool {
2387 return !param.IsOmitted(u) && !u.IsNull()
2388}
2389func (u ChatCompletionNewParamsFunctionCallUnion) MarshalJSON() ([]byte, error) {
2390 return param.MarshalUnion[ChatCompletionNewParamsFunctionCallUnion](u.OfFunctionCallMode, u.OfFunctionCallOption)
2391}
2392
2393func (u *ChatCompletionNewParamsFunctionCallUnion) asAny() any {
2394 if !param.IsOmitted(u.OfFunctionCallMode) {
2395 return &u.OfFunctionCallMode
2396 } else if !param.IsOmitted(u.OfFunctionCallOption) {
2397 return u.OfFunctionCallOption
2398 }
2399 return nil
2400}
2401
2402// Returns a pointer to the underlying variant's property, if present.
2403func (u ChatCompletionNewParamsFunctionCallUnion) GetName() *string {
2404 if vt := u.OfFunctionCallOption; vt != nil {
2405 return &vt.Name
2406 }
2407 return nil
2408}
2409
2410// Deprecated: deprecated
2411//
2412// The property Name is required.
2413type ChatCompletionNewParamsFunction struct {
2414 // The name of the function to be called. Must be a-z, A-Z, 0-9, or contain
2415 // underscores and dashes, with a maximum length of 64.
2416 Name string `json:"name,required"`
2417 // A description of what the function does, used by the model to choose when and
2418 // how to call the function.
2419 Description param.Opt[string] `json:"description,omitzero"`
2420 // The parameters the functions accepts, described as a JSON Schema object. See the
2421 // [guide](https://platform.openai.com/docs/guides/function-calling) for examples,
2422 // and the
2423 // [JSON Schema reference](https://json-schema.org/understanding-json-schema/) for
2424 // documentation about the format.
2425 //
2426 // Omitting `parameters` defines a function with an empty parameter list.
2427 Parameters shared.FunctionParameters `json:"parameters,omitzero"`
2428 paramObj
2429}
2430
2431// IsPresent returns true if the field's value is not omitted and not the JSON
2432// "null". To check if this field is omitted, use [param.IsOmitted].
2433func (f ChatCompletionNewParamsFunction) IsPresent() bool { return !param.IsOmitted(f) && !f.IsNull() }
2434func (r ChatCompletionNewParamsFunction) MarshalJSON() (data []byte, err error) {
2435 type shadow ChatCompletionNewParamsFunction
2436 return param.MarshalObject(r, (*shadow)(&r))
2437}
2438
2439// Only one field can be non-zero.
2440//
2441// Use [param.IsOmitted] to confirm if a field is set.
2442type ChatCompletionNewParamsResponseFormatUnion struct {
2443 OfText *shared.ResponseFormatTextParam `json:",omitzero,inline"`
2444 OfJSONSchema *shared.ResponseFormatJSONSchemaParam `json:",omitzero,inline"`
2445 OfJSONObject *shared.ResponseFormatJSONObjectParam `json:",omitzero,inline"`
2446 paramUnion
2447}
2448
2449// IsPresent returns true if the field's value is not omitted and not the JSON
2450// "null". To check if this field is omitted, use [param.IsOmitted].
2451func (u ChatCompletionNewParamsResponseFormatUnion) IsPresent() bool {
2452 return !param.IsOmitted(u) && !u.IsNull()
2453}
2454func (u ChatCompletionNewParamsResponseFormatUnion) MarshalJSON() ([]byte, error) {
2455 return param.MarshalUnion[ChatCompletionNewParamsResponseFormatUnion](u.OfText, u.OfJSONSchema, u.OfJSONObject)
2456}
2457
2458func (u *ChatCompletionNewParamsResponseFormatUnion) asAny() any {
2459 if !param.IsOmitted(u.OfText) {
2460 return u.OfText
2461 } else if !param.IsOmitted(u.OfJSONSchema) {
2462 return u.OfJSONSchema
2463 } else if !param.IsOmitted(u.OfJSONObject) {
2464 return u.OfJSONObject
2465 }
2466 return nil
2467}
2468
2469// Returns a pointer to the underlying variant's property, if present.
2470func (u ChatCompletionNewParamsResponseFormatUnion) GetJSONSchema() *shared.ResponseFormatJSONSchemaJSONSchemaParam {
2471 if vt := u.OfJSONSchema; vt != nil {
2472 return &vt.JSONSchema
2473 }
2474 return nil
2475}
2476
2477// Returns a pointer to the underlying variant's property, if present.
2478func (u ChatCompletionNewParamsResponseFormatUnion) GetType() *string {
2479 if vt := u.OfText; vt != nil {
2480 return (*string)(&vt.Type)
2481 } else if vt := u.OfJSONSchema; vt != nil {
2482 return (*string)(&vt.Type)
2483 } else if vt := u.OfJSONObject; vt != nil {
2484 return (*string)(&vt.Type)
2485 }
2486 return nil
2487}
2488
2489// Specifies the latency tier to use for processing the request. This parameter is
2490// relevant for customers subscribed to the scale tier service:
2491//
2492// - If set to 'auto', and the Project is Scale tier enabled, the system will
2493// utilize scale tier credits until they are exhausted.
2494// - If set to 'auto', and the Project is not Scale tier enabled, the request will
2495// be processed using the default service tier with a lower uptime SLA and no
2496// latency guarentee.
2497// - If set to 'default', the request will be processed using the default service
2498// tier with a lower uptime SLA and no latency guarentee.
2499// - When not set, the default behavior is 'auto'.
2500//
2501// When this parameter is set, the response body will include the `service_tier`
2502// utilized.
2503type ChatCompletionNewParamsServiceTier string
2504
2505const (
2506 ChatCompletionNewParamsServiceTierAuto ChatCompletionNewParamsServiceTier = "auto"
2507 ChatCompletionNewParamsServiceTierDefault ChatCompletionNewParamsServiceTier = "default"
2508)
2509
2510// Only one field can be non-zero.
2511//
2512// Use [param.IsOmitted] to confirm if a field is set.
2513type ChatCompletionNewParamsStopUnion struct {
2514 OfString param.Opt[string] `json:",omitzero,inline"`
2515 OfChatCompletionNewsStopArray []string `json:",omitzero,inline"`
2516 paramUnion
2517}
2518
2519// IsPresent returns true if the field's value is not omitted and not the JSON
2520// "null". To check if this field is omitted, use [param.IsOmitted].
2521func (u ChatCompletionNewParamsStopUnion) IsPresent() bool { return !param.IsOmitted(u) && !u.IsNull() }
2522func (u ChatCompletionNewParamsStopUnion) MarshalJSON() ([]byte, error) {
2523 return param.MarshalUnion[ChatCompletionNewParamsStopUnion](u.OfString, u.OfChatCompletionNewsStopArray)
2524}
2525
2526func (u *ChatCompletionNewParamsStopUnion) asAny() any {
2527 if !param.IsOmitted(u.OfString) {
2528 return &u.OfString.Value
2529 } else if !param.IsOmitted(u.OfChatCompletionNewsStopArray) {
2530 return &u.OfChatCompletionNewsStopArray
2531 }
2532 return nil
2533}
2534
2535// This tool searches the web for relevant results to use in a response. Learn more
2536// about the
2537// [web search tool](https://platform.openai.com/docs/guides/tools-web-search?api-mode=chat).
2538type ChatCompletionNewParamsWebSearchOptions struct {
2539 // Approximate location parameters for the search.
2540 UserLocation ChatCompletionNewParamsWebSearchOptionsUserLocation `json:"user_location,omitzero"`
2541 // High level guidance for the amount of context window space to use for the
2542 // search. One of `low`, `medium`, or `high`. `medium` is the default.
2543 //
2544 // Any of "low", "medium", "high".
2545 SearchContextSize string `json:"search_context_size,omitzero"`
2546 paramObj
2547}
2548
2549// IsPresent returns true if the field's value is not omitted and not the JSON
2550// "null". To check if this field is omitted, use [param.IsOmitted].
2551func (f ChatCompletionNewParamsWebSearchOptions) IsPresent() bool {
2552 return !param.IsOmitted(f) && !f.IsNull()
2553}
2554func (r ChatCompletionNewParamsWebSearchOptions) MarshalJSON() (data []byte, err error) {
2555 type shadow ChatCompletionNewParamsWebSearchOptions
2556 return param.MarshalObject(r, (*shadow)(&r))
2557}
2558
2559func init() {
2560 apijson.RegisterFieldValidator[ChatCompletionNewParamsWebSearchOptions](
2561 "SearchContextSize", false, "low", "medium", "high",
2562 )
2563}
2564
2565// Approximate location parameters for the search.
2566//
2567// The properties Approximate, Type are required.
2568type ChatCompletionNewParamsWebSearchOptionsUserLocation struct {
2569 // Approximate location parameters for the search.
2570 Approximate ChatCompletionNewParamsWebSearchOptionsUserLocationApproximate `json:"approximate,omitzero,required"`
2571 // The type of location approximation. Always `approximate`.
2572 //
2573 // This field can be elided, and will marshal its zero value as "approximate".
2574 Type constant.Approximate `json:"type,required"`
2575 paramObj
2576}
2577
2578// IsPresent returns true if the field's value is not omitted and not the JSON
2579// "null". To check if this field is omitted, use [param.IsOmitted].
2580func (f ChatCompletionNewParamsWebSearchOptionsUserLocation) IsPresent() bool {
2581 return !param.IsOmitted(f) && !f.IsNull()
2582}
2583func (r ChatCompletionNewParamsWebSearchOptionsUserLocation) MarshalJSON() (data []byte, err error) {
2584 type shadow ChatCompletionNewParamsWebSearchOptionsUserLocation
2585 return param.MarshalObject(r, (*shadow)(&r))
2586}
2587
2588// Approximate location parameters for the search.
2589type ChatCompletionNewParamsWebSearchOptionsUserLocationApproximate struct {
2590 // Free text input for the city of the user, e.g. `San Francisco`.
2591 City param.Opt[string] `json:"city,omitzero"`
2592 // The two-letter [ISO country code](https://en.wikipedia.org/wiki/ISO_3166-1) of
2593 // the user, e.g. `US`.
2594 Country param.Opt[string] `json:"country,omitzero"`
2595 // Free text input for the region of the user, e.g. `California`.
2596 Region param.Opt[string] `json:"region,omitzero"`
2597 // The [IANA timezone](https://timeapi.io/documentation/iana-timezones) of the
2598 // user, e.g. `America/Los_Angeles`.
2599 Timezone param.Opt[string] `json:"timezone,omitzero"`
2600 paramObj
2601}
2602
2603// IsPresent returns true if the field's value is not omitted and not the JSON
2604// "null". To check if this field is omitted, use [param.IsOmitted].
2605func (f ChatCompletionNewParamsWebSearchOptionsUserLocationApproximate) IsPresent() bool {
2606 return !param.IsOmitted(f) && !f.IsNull()
2607}
2608func (r ChatCompletionNewParamsWebSearchOptionsUserLocationApproximate) MarshalJSON() (data []byte, err error) {
2609 type shadow ChatCompletionNewParamsWebSearchOptionsUserLocationApproximate
2610 return param.MarshalObject(r, (*shadow)(&r))
2611}
2612
2613type ChatCompletionUpdateParams struct {
2614 // Set of 16 key-value pairs that can be attached to an object. This can be useful
2615 // for storing additional information about the object in a structured format, and
2616 // querying for objects via API or the dashboard.
2617 //
2618 // Keys are strings with a maximum length of 64 characters. Values are strings with
2619 // a maximum length of 512 characters.
2620 Metadata shared.MetadataParam `json:"metadata,omitzero,required"`
2621 paramObj
2622}
2623
2624// IsPresent returns true if the field's value is not omitted and not the JSON
2625// "null". To check if this field is omitted, use [param.IsOmitted].
2626func (f ChatCompletionUpdateParams) IsPresent() bool { return !param.IsOmitted(f) && !f.IsNull() }
2627
2628func (r ChatCompletionUpdateParams) MarshalJSON() (data []byte, err error) {
2629 type shadow ChatCompletionUpdateParams
2630 return param.MarshalObject(r, (*shadow)(&r))
2631}
2632
2633type ChatCompletionListParams struct {
2634 // Identifier for the last chat completion from the previous pagination request.
2635 After param.Opt[string] `query:"after,omitzero" json:"-"`
2636 // Number of Chat Completions to retrieve.
2637 Limit param.Opt[int64] `query:"limit,omitzero" json:"-"`
2638 // The model used to generate the Chat Completions.
2639 Model param.Opt[string] `query:"model,omitzero" json:"-"`
2640 // A list of metadata keys to filter the Chat Completions by. Example:
2641 //
2642 // `metadata[key1]=value1&metadata[key2]=value2`
2643 Metadata shared.MetadataParam `query:"metadata,omitzero" json:"-"`
2644 // Sort order for Chat Completions by timestamp. Use `asc` for ascending order or
2645 // `desc` for descending order. Defaults to `asc`.
2646 //
2647 // Any of "asc", "desc".
2648 Order ChatCompletionListParamsOrder `query:"order,omitzero" json:"-"`
2649 paramObj
2650}
2651
2652// IsPresent returns true if the field's value is not omitted and not the JSON
2653// "null". To check if this field is omitted, use [param.IsOmitted].
2654func (f ChatCompletionListParams) IsPresent() bool { return !param.IsOmitted(f) && !f.IsNull() }
2655
2656// URLQuery serializes [ChatCompletionListParams]'s query parameters as
2657// `url.Values`.
2658func (r ChatCompletionListParams) URLQuery() (v url.Values) {
2659 return apiquery.MarshalWithSettings(r, apiquery.QuerySettings{
2660 ArrayFormat: apiquery.ArrayQueryFormatBrackets,
2661 NestedFormat: apiquery.NestedQueryFormatBrackets,
2662 })
2663}
2664
2665// Sort order for Chat Completions by timestamp. Use `asc` for ascending order or
2666// `desc` for descending order. Defaults to `asc`.
2667type ChatCompletionListParamsOrder string
2668
2669const (
2670 ChatCompletionListParamsOrderAsc ChatCompletionListParamsOrder = "asc"
2671 ChatCompletionListParamsOrderDesc ChatCompletionListParamsOrder = "desc"
2672)