-
Notifications
You must be signed in to change notification settings - Fork 37
/
betathreadrun.go
1004 lines (892 loc) · 40.7 KB
/
betathreadrun.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
package openai
import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
"github.com/openai/openai-go/internal/apijson"
"github.com/openai/openai-go/internal/apiquery"
"github.com/openai/openai-go/internal/param"
"github.com/openai/openai-go/internal/requestconfig"
"github.com/openai/openai-go/option"
"github.com/openai/openai-go/packages/pagination"
"github.com/openai/openai-go/packages/ssestream"
)
// BetaThreadRunService contains methods and other services that help with
// interacting with the openai API.
//
// Note, unlike clients, this service does not read variables from the environment
// automatically. You should not instantiate this service directly, and instead use
// the [NewBetaThreadRunService] method instead.
type BetaThreadRunService struct {
Options []option.RequestOption
Steps *BetaThreadRunStepService
}
// NewBetaThreadRunService generates a new service that applies the given options
// to each request. These options are applied after the parent client's options (if
// there is one), and before any request-specific options.
func NewBetaThreadRunService(opts ...option.RequestOption) (r *BetaThreadRunService) {
r = &BetaThreadRunService{}
r.Options = opts
r.Steps = NewBetaThreadRunStepService(opts...)
return
}
// Create a run.
func (r *BetaThreadRunService) New(ctx context.Context, threadID string, params BetaThreadRunNewParams, opts ...option.RequestOption) (res *Run, err error) {
opts = append(r.Options[:], opts...)
opts = append([]option.RequestOption{option.WithHeader("OpenAI-Beta", "assistants=v2")}, opts...)
if threadID == "" {
err = errors.New("missing required thread_id parameter")
return
}
path := fmt.Sprintf("threads/%s/runs", threadID)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, params, &res, opts...)
return
}
// Create a run.
func (r *BetaThreadRunService) NewStreaming(ctx context.Context, threadID string, params BetaThreadRunNewParams, opts ...option.RequestOption) (stream *ssestream.Stream[AssistantStreamEvent]) {
var (
raw *http.Response
err error
)
opts = append(r.Options[:], opts...)
opts = append([]option.RequestOption{option.WithHeader("OpenAI-Beta", "assistants=v2"), option.WithJSONSet("stream", true)}, opts...)
if threadID == "" {
err = errors.New("missing required thread_id parameter")
return
}
path := fmt.Sprintf("threads/%s/runs", threadID)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, params, &raw, opts...)
return ssestream.NewStream[AssistantStreamEvent](ssestream.NewDecoder(raw), err)
}
// Create a run and poll until task is completed.
// Pass 0 to pollIntervalMs to use the default polling interval.
func (r *BetaThreadRunService) NewAndPoll(ctx context.Context, threadID string, params BetaThreadRunNewParams, pollIntervalMs int, opts ...option.RequestOption) (res *Run, err error) {
run, err := r.New(ctx, threadID, params, opts...)
if err != nil {
return nil, err
}
return r.PollStatus(ctx, threadID, run.ID, pollIntervalMs, opts...)
}
// Retrieves a run.
func (r *BetaThreadRunService) Get(ctx context.Context, threadID string, runID string, opts ...option.RequestOption) (res *Run, err error) {
opts = append(r.Options[:], opts...)
opts = append([]option.RequestOption{option.WithHeader("OpenAI-Beta", "assistants=v2")}, opts...)
if threadID == "" {
err = errors.New("missing required thread_id parameter")
return
}
if runID == "" {
err = errors.New("missing required run_id parameter")
return
}
path := fmt.Sprintf("threads/%s/runs/%s", threadID, runID)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, nil, &res, opts...)
return
}
// Modifies a run.
func (r *BetaThreadRunService) Update(ctx context.Context, threadID string, runID string, body BetaThreadRunUpdateParams, opts ...option.RequestOption) (res *Run, err error) {
opts = append(r.Options[:], opts...)
opts = append([]option.RequestOption{option.WithHeader("OpenAI-Beta", "assistants=v2")}, opts...)
if threadID == "" {
err = errors.New("missing required thread_id parameter")
return
}
if runID == "" {
err = errors.New("missing required run_id parameter")
return
}
path := fmt.Sprintf("threads/%s/runs/%s", threadID, runID)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, body, &res, opts...)
return
}
// Returns a list of runs belonging to a thread.
func (r *BetaThreadRunService) List(ctx context.Context, threadID string, query BetaThreadRunListParams, opts ...option.RequestOption) (res *pagination.CursorPage[Run], err error) {
var raw *http.Response
opts = append(r.Options[:], opts...)
opts = append([]option.RequestOption{option.WithHeader("OpenAI-Beta", "assistants=v2"), option.WithResponseInto(&raw)}, opts...)
if threadID == "" {
err = errors.New("missing required thread_id parameter")
return
}
path := fmt.Sprintf("threads/%s/runs", threadID)
cfg, err := requestconfig.NewRequestConfig(ctx, http.MethodGet, path, query, &res, opts...)
if err != nil {
return nil, err
}
err = cfg.Execute()
if err != nil {
return nil, err
}
res.SetPageConfig(cfg, raw)
return res, nil
}
// Returns a list of runs belonging to a thread.
func (r *BetaThreadRunService) ListAutoPaging(ctx context.Context, threadID string, query BetaThreadRunListParams, opts ...option.RequestOption) *pagination.CursorPageAutoPager[Run] {
return pagination.NewCursorPageAutoPager(r.List(ctx, threadID, query, opts...))
}
// Cancels a run that is `in_progress`.
func (r *BetaThreadRunService) Cancel(ctx context.Context, threadID string, runID string, opts ...option.RequestOption) (res *Run, err error) {
opts = append(r.Options[:], opts...)
opts = append([]option.RequestOption{option.WithHeader("OpenAI-Beta", "assistants=v2")}, opts...)
if threadID == "" {
err = errors.New("missing required thread_id parameter")
return
}
if runID == "" {
err = errors.New("missing required run_id parameter")
return
}
path := fmt.Sprintf("threads/%s/runs/%s/cancel", threadID, runID)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, nil, &res, opts...)
return
}
// When a run has the `status: "requires_action"` and `required_action.type` is
// `submit_tool_outputs`, this endpoint can be used to submit the outputs from the
// tool calls once they're all completed. All outputs must be submitted in a single
// request.
func (r *BetaThreadRunService) SubmitToolOutputs(ctx context.Context, threadID string, runID string, body BetaThreadRunSubmitToolOutputsParams, opts ...option.RequestOption) (res *Run, err error) {
opts = append(r.Options[:], opts...)
opts = append([]option.RequestOption{option.WithHeader("OpenAI-Beta", "assistants=v2")}, opts...)
if threadID == "" {
err = errors.New("missing required thread_id parameter")
return
}
if runID == "" {
err = errors.New("missing required run_id parameter")
return
}
path := fmt.Sprintf("threads/%s/runs/%s/submit_tool_outputs", threadID, runID)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, body, &res, opts...)
return
}
// A helper to submit a tool output to a run and poll for a terminal run state.
// Pass 0 to pollIntervalMs to use the default polling interval.
// More information on Run lifecycles can be found here:
// https://platform.openai.com/docs/assistants/how-it-works/runs-and-run-steps
func (r *BetaThreadRunService) SubmitToolOutputsAndPoll(ctx context.Context, threadID string, runID string, body BetaThreadRunSubmitToolOutputsParams, pollIntervalMs int, opts ...option.RequestOption) (*Run, error) {
run, err := r.SubmitToolOutputs(ctx, threadID, runID, body, opts...)
if err != nil {
return nil, err
}
return r.PollStatus(ctx, threadID, run.ID, pollIntervalMs, opts...)
}
// When a run has the `status: "requires_action"` and `required_action.type` is
// `submit_tool_outputs`, this endpoint can be used to submit the outputs from the
// tool calls once they're all completed. All outputs must be submitted in a single
// request.
func (r *BetaThreadRunService) SubmitToolOutputsStreaming(ctx context.Context, threadID string, runID string, body BetaThreadRunSubmitToolOutputsParams, opts ...option.RequestOption) (stream *ssestream.Stream[AssistantStreamEvent]) {
var (
raw *http.Response
err error
)
opts = append(r.Options[:], opts...)
opts = append([]option.RequestOption{option.WithHeader("OpenAI-Beta", "assistants=v2"), option.WithJSONSet("stream", true)}, opts...)
if threadID == "" {
err = errors.New("missing required thread_id parameter")
return
}
if runID == "" {
err = errors.New("missing required run_id parameter")
return
}
path := fmt.Sprintf("threads/%s/runs/%s/submit_tool_outputs", threadID, runID)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, body, &raw, opts...)
return ssestream.NewStream[AssistantStreamEvent](ssestream.NewDecoder(raw), err)
}
// Tool call objects
type RequiredActionFunctionToolCall struct {
// The ID of the tool call. This ID must be referenced when you submit the tool
// outputs in using the
// [Submit tool outputs to run](https://platform.openai.com/docs/api-reference/runs/submitToolOutputs)
// endpoint.
ID string `json:"id,required"`
// The function definition.
Function RequiredActionFunctionToolCallFunction `json:"function,required"`
// The type of tool call the output is required for. For now, this is always
// `function`.
Type RequiredActionFunctionToolCallType `json:"type,required"`
JSON requiredActionFunctionToolCallJSON `json:"-"`
}
// requiredActionFunctionToolCallJSON contains the JSON metadata for the struct
// [RequiredActionFunctionToolCall]
type requiredActionFunctionToolCallJSON struct {
ID apijson.Field
Function apijson.Field
Type apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *RequiredActionFunctionToolCall) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r requiredActionFunctionToolCallJSON) RawJSON() string {
return r.raw
}
// The function definition.
type RequiredActionFunctionToolCallFunction struct {
// The arguments that the model expects you to pass to the function.
Arguments string `json:"arguments,required"`
// The name of the function.
Name string `json:"name,required"`
JSON requiredActionFunctionToolCallFunctionJSON `json:"-"`
}
// requiredActionFunctionToolCallFunctionJSON contains the JSON metadata for the
// struct [RequiredActionFunctionToolCallFunction]
type requiredActionFunctionToolCallFunctionJSON struct {
Arguments apijson.Field
Name apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *RequiredActionFunctionToolCallFunction) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r requiredActionFunctionToolCallFunctionJSON) RawJSON() string {
return r.raw
}
// The type of tool call the output is required for. For now, this is always
// `function`.
type RequiredActionFunctionToolCallType string
const (
RequiredActionFunctionToolCallTypeFunction RequiredActionFunctionToolCallType = "function"
)
func (r RequiredActionFunctionToolCallType) IsKnown() bool {
switch r {
case RequiredActionFunctionToolCallTypeFunction:
return true
}
return false
}
// Represents an execution run on a
// [thread](https://platform.openai.com/docs/api-reference/threads).
type Run struct {
// The identifier, which can be referenced in API endpoints.
ID string `json:"id,required"`
// The ID of the
// [assistant](https://platform.openai.com/docs/api-reference/assistants) used for
// execution of this run.
AssistantID string `json:"assistant_id,required"`
// The Unix timestamp (in seconds) for when the run was cancelled.
CancelledAt int64 `json:"cancelled_at,required,nullable"`
// The Unix timestamp (in seconds) for when the run was completed.
CompletedAt int64 `json:"completed_at,required,nullable"`
// The Unix timestamp (in seconds) for when the run was created.
CreatedAt int64 `json:"created_at,required"`
// The Unix timestamp (in seconds) for when the run will expire.
ExpiresAt int64 `json:"expires_at,required,nullable"`
// The Unix timestamp (in seconds) for when the run failed.
FailedAt int64 `json:"failed_at,required,nullable"`
// Details on why the run is incomplete. Will be `null` if the run is not
// incomplete.
IncompleteDetails RunIncompleteDetails `json:"incomplete_details,required,nullable"`
// The instructions that the
// [assistant](https://platform.openai.com/docs/api-reference/assistants) used for
// this run.
Instructions string `json:"instructions,required"`
// The last error associated with this run. Will be `null` if there are no errors.
LastError RunLastError `json:"last_error,required,nullable"`
// The maximum number of completion tokens specified to have been used over the
// course of the run.
MaxCompletionTokens int64 `json:"max_completion_tokens,required,nullable"`
// The maximum number of prompt tokens specified to have been used over the course
// of the run.
MaxPromptTokens int64 `json:"max_prompt_tokens,required,nullable"`
// Set of 16 key-value pairs that can be attached to an object. This can be useful
// for storing additional information about the object in a structured format. Keys
// can be a maximum of 64 characters long and values can be a maximum of 512
// characters long.
Metadata interface{} `json:"metadata,required,nullable"`
// The model that the
// [assistant](https://platform.openai.com/docs/api-reference/assistants) used for
// this run.
Model string `json:"model,required"`
// The object type, which is always `thread.run`.
Object RunObject `json:"object,required"`
// Whether to enable
// [parallel function calling](https://platform.openai.com/docs/guides/function-calling#configuring-parallel-function-calling)
// during tool use.
ParallelToolCalls bool `json:"parallel_tool_calls,required"`
// Details on the action required to continue the run. Will be `null` if no action
// is required.
RequiredAction RunRequiredAction `json:"required_action,required,nullable"`
// The Unix timestamp (in seconds) for when the run was started.
StartedAt int64 `json:"started_at,required,nullable"`
// The status of the run, which can be either `queued`, `in_progress`,
// `requires_action`, `cancelling`, `cancelled`, `failed`, `completed`,
// `incomplete`, or `expired`.
Status RunStatus `json:"status,required"`
// The ID of the [thread](https://platform.openai.com/docs/api-reference/threads)
// that was executed on as a part of this run.
ThreadID string `json:"thread_id,required"`
// Controls which (if any) tool is called by the model. `none` means the model will
// not call any tools and instead generates a message. `auto` is the default value
// and means the model can pick between generating a message or calling one or more
// tools. `required` means the model must call one or more tools before responding
// to the user. Specifying a particular tool like `{"type": "file_search"}` or
// `{"type": "function", "function": {"name": "my_function"}}` forces the model to
// call that tool.
ToolChoice AssistantToolChoiceOptionUnion `json:"tool_choice,required,nullable"`
// The list of tools that the
// [assistant](https://platform.openai.com/docs/api-reference/assistants) used for
// this run.
Tools []AssistantTool `json:"tools,required"`
// Controls for how a thread will be truncated prior to the run. Use this to
// control the intial context window of the run.
TruncationStrategy RunTruncationStrategy `json:"truncation_strategy,required,nullable"`
// Usage statistics related to the run. This value will be `null` if the run is not
// in a terminal state (i.e. `in_progress`, `queued`, etc.).
Usage RunUsage `json:"usage,required,nullable"`
// The sampling temperature used for this run. If not set, defaults to 1.
Temperature float64 `json:"temperature,nullable"`
// The nucleus sampling value used for this run. If not set, defaults to 1.
TopP float64 `json:"top_p,nullable"`
JSON runJSON `json:"-"`
}
// runJSON contains the JSON metadata for the struct [Run]
type runJSON struct {
ID apijson.Field
AssistantID apijson.Field
CancelledAt apijson.Field
CompletedAt apijson.Field
CreatedAt apijson.Field
ExpiresAt apijson.Field
FailedAt apijson.Field
IncompleteDetails apijson.Field
Instructions apijson.Field
LastError apijson.Field
MaxCompletionTokens apijson.Field
MaxPromptTokens apijson.Field
Metadata apijson.Field
Model apijson.Field
Object apijson.Field
ParallelToolCalls apijson.Field
RequiredAction apijson.Field
StartedAt apijson.Field
Status apijson.Field
ThreadID apijson.Field
ToolChoice apijson.Field
Tools apijson.Field
TruncationStrategy apijson.Field
Usage apijson.Field
Temperature apijson.Field
TopP apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *Run) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r runJSON) RawJSON() string {
return r.raw
}
// Details on why the run is incomplete. Will be `null` if the run is not
// incomplete.
type RunIncompleteDetails struct {
// The reason why the run is incomplete. This will point to which specific token
// limit was reached over the course of the run.
Reason RunIncompleteDetailsReason `json:"reason"`
JSON runIncompleteDetailsJSON `json:"-"`
}
// runIncompleteDetailsJSON contains the JSON metadata for the struct
// [RunIncompleteDetails]
type runIncompleteDetailsJSON struct {
Reason apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *RunIncompleteDetails) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r runIncompleteDetailsJSON) RawJSON() string {
return r.raw
}
// The reason why the run is incomplete. This will point to which specific token
// limit was reached over the course of the run.
type RunIncompleteDetailsReason string
const (
RunIncompleteDetailsReasonMaxCompletionTokens RunIncompleteDetailsReason = "max_completion_tokens"
RunIncompleteDetailsReasonMaxPromptTokens RunIncompleteDetailsReason = "max_prompt_tokens"
)
func (r RunIncompleteDetailsReason) IsKnown() bool {
switch r {
case RunIncompleteDetailsReasonMaxCompletionTokens, RunIncompleteDetailsReasonMaxPromptTokens:
return true
}
return false
}
// The last error associated with this run. Will be `null` if there are no errors.
type RunLastError struct {
// One of `server_error`, `rate_limit_exceeded`, or `invalid_prompt`.
Code RunLastErrorCode `json:"code,required"`
// A human-readable description of the error.
Message string `json:"message,required"`
JSON runLastErrorJSON `json:"-"`
}
// runLastErrorJSON contains the JSON metadata for the struct [RunLastError]
type runLastErrorJSON struct {
Code apijson.Field
Message apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *RunLastError) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r runLastErrorJSON) RawJSON() string {
return r.raw
}
// One of `server_error`, `rate_limit_exceeded`, or `invalid_prompt`.
type RunLastErrorCode string
const (
RunLastErrorCodeServerError RunLastErrorCode = "server_error"
RunLastErrorCodeRateLimitExceeded RunLastErrorCode = "rate_limit_exceeded"
RunLastErrorCodeInvalidPrompt RunLastErrorCode = "invalid_prompt"
)
func (r RunLastErrorCode) IsKnown() bool {
switch r {
case RunLastErrorCodeServerError, RunLastErrorCodeRateLimitExceeded, RunLastErrorCodeInvalidPrompt:
return true
}
return false
}
// The object type, which is always `thread.run`.
type RunObject string
const (
RunObjectThreadRun RunObject = "thread.run"
)
func (r RunObject) IsKnown() bool {
switch r {
case RunObjectThreadRun:
return true
}
return false
}
// Details on the action required to continue the run. Will be `null` if no action
// is required.
type RunRequiredAction struct {
// Details on the tool outputs needed for this run to continue.
SubmitToolOutputs RunRequiredActionSubmitToolOutputs `json:"submit_tool_outputs,required"`
// For now, this is always `submit_tool_outputs`.
Type RunRequiredActionType `json:"type,required"`
JSON runRequiredActionJSON `json:"-"`
}
// runRequiredActionJSON contains the JSON metadata for the struct
// [RunRequiredAction]
type runRequiredActionJSON struct {
SubmitToolOutputs apijson.Field
Type apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *RunRequiredAction) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r runRequiredActionJSON) RawJSON() string {
return r.raw
}
// Details on the tool outputs needed for this run to continue.
type RunRequiredActionSubmitToolOutputs struct {
// A list of the relevant tool calls.
ToolCalls []RequiredActionFunctionToolCall `json:"tool_calls,required"`
JSON runRequiredActionSubmitToolOutputsJSON `json:"-"`
}
// runRequiredActionSubmitToolOutputsJSON contains the JSON metadata for the struct
// [RunRequiredActionSubmitToolOutputs]
type runRequiredActionSubmitToolOutputsJSON struct {
ToolCalls apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *RunRequiredActionSubmitToolOutputs) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r runRequiredActionSubmitToolOutputsJSON) RawJSON() string {
return r.raw
}
// For now, this is always `submit_tool_outputs`.
type RunRequiredActionType string
const (
RunRequiredActionTypeSubmitToolOutputs RunRequiredActionType = "submit_tool_outputs"
)
func (r RunRequiredActionType) IsKnown() bool {
switch r {
case RunRequiredActionTypeSubmitToolOutputs:
return true
}
return false
}
// Controls for how a thread will be truncated prior to the run. Use this to
// control the intial context window of the run.
type RunTruncationStrategy struct {
// The truncation strategy to use for the thread. The default is `auto`. If set to
// `last_messages`, the thread will be truncated to the n most recent messages in
// the thread. When set to `auto`, messages in the middle of the thread will be
// dropped to fit the context length of the model, `max_prompt_tokens`.
Type RunTruncationStrategyType `json:"type,required"`
// The number of most recent messages from the thread when constructing the context
// for the run.
LastMessages int64 `json:"last_messages,nullable"`
JSON runTruncationStrategyJSON `json:"-"`
}
// runTruncationStrategyJSON contains the JSON metadata for the struct
// [RunTruncationStrategy]
type runTruncationStrategyJSON struct {
Type apijson.Field
LastMessages apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *RunTruncationStrategy) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r runTruncationStrategyJSON) RawJSON() string {
return r.raw
}
// The truncation strategy to use for the thread. The default is `auto`. If set to
// `last_messages`, the thread will be truncated to the n most recent messages in
// the thread. When set to `auto`, messages in the middle of the thread will be
// dropped to fit the context length of the model, `max_prompt_tokens`.
type RunTruncationStrategyType string
const (
RunTruncationStrategyTypeAuto RunTruncationStrategyType = "auto"
RunTruncationStrategyTypeLastMessages RunTruncationStrategyType = "last_messages"
)
func (r RunTruncationStrategyType) IsKnown() bool {
switch r {
case RunTruncationStrategyTypeAuto, RunTruncationStrategyTypeLastMessages:
return true
}
return false
}
// Usage statistics related to the run. This value will be `null` if the run is not
// in a terminal state (i.e. `in_progress`, `queued`, etc.).
type RunUsage struct {
// Number of completion tokens used over the course of the run.
CompletionTokens int64 `json:"completion_tokens,required"`
// Number of prompt tokens used over the course of the run.
PromptTokens int64 `json:"prompt_tokens,required"`
// Total number of tokens used (prompt + completion).
TotalTokens int64 `json:"total_tokens,required"`
JSON runUsageJSON `json:"-"`
}
// runUsageJSON contains the JSON metadata for the struct [RunUsage]
type runUsageJSON struct {
CompletionTokens apijson.Field
PromptTokens apijson.Field
TotalTokens apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *RunUsage) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r runUsageJSON) RawJSON() string {
return r.raw
}
// The status of the run, which can be either `queued`, `in_progress`,
// `requires_action`, `cancelling`, `cancelled`, `failed`, `completed`,
// `incomplete`, or `expired`.
type RunStatus string
const (
RunStatusQueued RunStatus = "queued"
RunStatusInProgress RunStatus = "in_progress"
RunStatusRequiresAction RunStatus = "requires_action"
RunStatusCancelling RunStatus = "cancelling"
RunStatusCancelled RunStatus = "cancelled"
RunStatusFailed RunStatus = "failed"
RunStatusCompleted RunStatus = "completed"
RunStatusIncomplete RunStatus = "incomplete"
RunStatusExpired RunStatus = "expired"
)
func (r RunStatus) IsKnown() bool {
switch r {
case RunStatusQueued, RunStatusInProgress, RunStatusRequiresAction, RunStatusCancelling, RunStatusCancelled, RunStatusFailed, RunStatusCompleted, RunStatusIncomplete, RunStatusExpired:
return true
}
return false
}
type BetaThreadRunNewParams struct {
// The ID of the
// [assistant](https://platform.openai.com/docs/api-reference/assistants) to use to
// execute this run.
AssistantID param.Field[string] `json:"assistant_id,required"`
// A list of additional fields to include in the response. Currently the only
// supported value is `step_details.tool_calls[*].file_search.results[*].content`
// to fetch the file search result content.
//
// See the
// [file search tool documentation](https://platform.openai.com/docs/assistants/tools/file-search#customizing-file-search-settings)
// for more information.
Include param.Field[[]RunStepInclude] `query:"include"`
// Appends additional instructions at the end of the instructions for the run. This
// is useful for modifying the behavior on a per-run basis without overriding other
// instructions.
AdditionalInstructions param.Field[string] `json:"additional_instructions"`
// Adds additional messages to the thread before creating the run.
AdditionalMessages param.Field[[]BetaThreadRunNewParamsAdditionalMessage] `json:"additional_messages"`
// Overrides the
// [instructions](https://platform.openai.com/docs/api-reference/assistants/createAssistant)
// of the assistant. This is useful for modifying the behavior on a per-run basis.
Instructions param.Field[string] `json:"instructions"`
// The maximum number of completion tokens that may be used over the course of the
// run. The run will make a best effort to use only the number of completion tokens
// specified, across multiple turns of the run. If the run exceeds the number of
// completion tokens specified, the run will end with status `incomplete`. See
// `incomplete_details` for more info.
MaxCompletionTokens param.Field[int64] `json:"max_completion_tokens"`
// The maximum number of prompt tokens that may be used over the course of the run.
// The run will make a best effort to use only the number of prompt tokens
// specified, across multiple turns of the run. If the run exceeds the number of
// prompt tokens specified, the run will end with status `incomplete`. See
// `incomplete_details` for more info.
MaxPromptTokens param.Field[int64] `json:"max_prompt_tokens"`
// Set of 16 key-value pairs that can be attached to an object. This can be useful
// for storing additional information about the object in a structured format. Keys
// can be a maximum of 64 characters long and values can be a maximum of 512
// characters long.
Metadata param.Field[interface{}] `json:"metadata"`
// The ID of the [Model](https://platform.openai.com/docs/api-reference/models) to
// be used to execute this run. If a value is provided here, it will override the
// model associated with the assistant. If not, the model associated with the
// assistant will be used.
Model param.Field[ChatModel] `json:"model"`
// Whether to enable
// [parallel function calling](https://platform.openai.com/docs/guides/function-calling#configuring-parallel-function-calling)
// during tool use.
ParallelToolCalls param.Field[bool] `json:"parallel_tool_calls"`
// What sampling temperature to use, between 0 and 2. Higher values like 0.8 will
// make the output more random, while lower values like 0.2 will make it more
// focused and deterministic.
Temperature param.Field[float64] `json:"temperature"`
// Controls which (if any) tool is called by the model. `none` means the model will
// not call any tools and instead generates a message. `auto` is the default value
// and means the model can pick between generating a message or calling one or more
// tools. `required` means the model must call one or more tools before responding
// to the user. Specifying a particular tool like `{"type": "file_search"}` or
// `{"type": "function", "function": {"name": "my_function"}}` forces the model to
// call that tool.
ToolChoice param.Field[AssistantToolChoiceOptionUnionParam] `json:"tool_choice"`
// Override the tools the assistant can use for this run. This is useful for
// modifying the behavior on a per-run basis.
Tools param.Field[[]AssistantToolUnionParam] `json:"tools"`
// An alternative to sampling with temperature, called nucleus sampling, where the
// model considers the results of the tokens with top_p probability mass. So 0.1
// means only the tokens comprising the top 10% probability mass are considered.
//
// We generally recommend altering this or temperature but not both.
TopP param.Field[float64] `json:"top_p"`
// Controls for how a thread will be truncated prior to the run. Use this to
// control the intial context window of the run.
TruncationStrategy param.Field[BetaThreadRunNewParamsTruncationStrategy] `json:"truncation_strategy"`
}
func (r BetaThreadRunNewParams) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
// URLQuery serializes [BetaThreadRunNewParams]'s query parameters as `url.Values`.
func (r BetaThreadRunNewParams) URLQuery() (v url.Values) {
return apiquery.MarshalWithSettings(r, apiquery.QuerySettings{
ArrayFormat: apiquery.ArrayQueryFormatBrackets,
NestedFormat: apiquery.NestedQueryFormatBrackets,
})
}
type BetaThreadRunNewParamsAdditionalMessage struct {
// An array of content parts with a defined type, each can be of type `text` or
// images can be passed with `image_url` or `image_file`. Image types are only
// supported on
// [Vision-compatible models](https://platform.openai.com/docs/models).
Content param.Field[[]MessageContentPartParamUnion] `json:"content,required"`
// The role of the entity that is creating the message. Allowed values include:
//
// - `user`: Indicates the message is sent by an actual user and should be used in
// most cases to represent user-generated messages.
// - `assistant`: Indicates the message is generated by the assistant. Use this
// value to insert messages from the assistant into the conversation.
Role param.Field[BetaThreadRunNewParamsAdditionalMessagesRole] `json:"role,required"`
// A list of files attached to the message, and the tools they should be added to.
Attachments param.Field[[]BetaThreadRunNewParamsAdditionalMessagesAttachment] `json:"attachments"`
// Set of 16 key-value pairs that can be attached to an object. This can be useful
// for storing additional information about the object in a structured format. Keys
// can be a maximum of 64 characters long and values can be a maximum of 512
// characters long.
Metadata param.Field[interface{}] `json:"metadata"`
}
func (r BetaThreadRunNewParamsAdditionalMessage) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
// The role of the entity that is creating the message. Allowed values include:
//
// - `user`: Indicates the message is sent by an actual user and should be used in
// most cases to represent user-generated messages.
// - `assistant`: Indicates the message is generated by the assistant. Use this
// value to insert messages from the assistant into the conversation.
type BetaThreadRunNewParamsAdditionalMessagesRole string
const (
BetaThreadRunNewParamsAdditionalMessagesRoleUser BetaThreadRunNewParamsAdditionalMessagesRole = "user"
BetaThreadRunNewParamsAdditionalMessagesRoleAssistant BetaThreadRunNewParamsAdditionalMessagesRole = "assistant"
)
func (r BetaThreadRunNewParamsAdditionalMessagesRole) IsKnown() bool {
switch r {
case BetaThreadRunNewParamsAdditionalMessagesRoleUser, BetaThreadRunNewParamsAdditionalMessagesRoleAssistant:
return true
}
return false
}
type BetaThreadRunNewParamsAdditionalMessagesAttachment struct {
// The ID of the file to attach to the message.
FileID param.Field[string] `json:"file_id"`
// The tools to add this file to.
Tools param.Field[[]BetaThreadRunNewParamsAdditionalMessagesAttachmentsToolUnion] `json:"tools"`
}
func (r BetaThreadRunNewParamsAdditionalMessagesAttachment) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
type BetaThreadRunNewParamsAdditionalMessagesAttachmentsTool struct {
// The type of tool being defined: `code_interpreter`
Type param.Field[BetaThreadRunNewParamsAdditionalMessagesAttachmentsToolsType] `json:"type,required"`
}
func (r BetaThreadRunNewParamsAdditionalMessagesAttachmentsTool) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
func (r BetaThreadRunNewParamsAdditionalMessagesAttachmentsTool) implementsBetaThreadRunNewParamsAdditionalMessagesAttachmentsToolUnion() {
}
// Satisfied by [CodeInterpreterToolParam],
// [BetaThreadRunNewParamsAdditionalMessagesAttachmentsToolsFileSearch],
// [BetaThreadRunNewParamsAdditionalMessagesAttachmentsTool].
type BetaThreadRunNewParamsAdditionalMessagesAttachmentsToolUnion interface {
implementsBetaThreadRunNewParamsAdditionalMessagesAttachmentsToolUnion()
}
type BetaThreadRunNewParamsAdditionalMessagesAttachmentsToolsFileSearch struct {
// The type of tool being defined: `file_search`
Type param.Field[BetaThreadRunNewParamsAdditionalMessagesAttachmentsToolsFileSearchType] `json:"type,required"`
}
func (r BetaThreadRunNewParamsAdditionalMessagesAttachmentsToolsFileSearch) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
func (r BetaThreadRunNewParamsAdditionalMessagesAttachmentsToolsFileSearch) implementsBetaThreadRunNewParamsAdditionalMessagesAttachmentsToolUnion() {
}
// The type of tool being defined: `file_search`
type BetaThreadRunNewParamsAdditionalMessagesAttachmentsToolsFileSearchType string
const (
BetaThreadRunNewParamsAdditionalMessagesAttachmentsToolsFileSearchTypeFileSearch BetaThreadRunNewParamsAdditionalMessagesAttachmentsToolsFileSearchType = "file_search"
)
func (r BetaThreadRunNewParamsAdditionalMessagesAttachmentsToolsFileSearchType) IsKnown() bool {
switch r {
case BetaThreadRunNewParamsAdditionalMessagesAttachmentsToolsFileSearchTypeFileSearch:
return true
}
return false
}
// The type of tool being defined: `code_interpreter`
type BetaThreadRunNewParamsAdditionalMessagesAttachmentsToolsType string
const (
BetaThreadRunNewParamsAdditionalMessagesAttachmentsToolsTypeCodeInterpreter BetaThreadRunNewParamsAdditionalMessagesAttachmentsToolsType = "code_interpreter"
BetaThreadRunNewParamsAdditionalMessagesAttachmentsToolsTypeFileSearch BetaThreadRunNewParamsAdditionalMessagesAttachmentsToolsType = "file_search"
)
func (r BetaThreadRunNewParamsAdditionalMessagesAttachmentsToolsType) IsKnown() bool {
switch r {
case BetaThreadRunNewParamsAdditionalMessagesAttachmentsToolsTypeCodeInterpreter, BetaThreadRunNewParamsAdditionalMessagesAttachmentsToolsTypeFileSearch:
return true
}
return false
}
// Controls for how a thread will be truncated prior to the run. Use this to
// control the intial context window of the run.
type BetaThreadRunNewParamsTruncationStrategy struct {
// The truncation strategy to use for the thread. The default is `auto`. If set to
// `last_messages`, the thread will be truncated to the n most recent messages in
// the thread. When set to `auto`, messages in the middle of the thread will be
// dropped to fit the context length of the model, `max_prompt_tokens`.
Type param.Field[BetaThreadRunNewParamsTruncationStrategyType] `json:"type,required"`
// The number of most recent messages from the thread when constructing the context
// for the run.
LastMessages param.Field[int64] `json:"last_messages"`
}
func (r BetaThreadRunNewParamsTruncationStrategy) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
// The truncation strategy to use for the thread. The default is `auto`. If set to
// `last_messages`, the thread will be truncated to the n most recent messages in
// the thread. When set to `auto`, messages in the middle of the thread will be
// dropped to fit the context length of the model, `max_prompt_tokens`.
type BetaThreadRunNewParamsTruncationStrategyType string
const (
BetaThreadRunNewParamsTruncationStrategyTypeAuto BetaThreadRunNewParamsTruncationStrategyType = "auto"
BetaThreadRunNewParamsTruncationStrategyTypeLastMessages BetaThreadRunNewParamsTruncationStrategyType = "last_messages"
)
func (r BetaThreadRunNewParamsTruncationStrategyType) IsKnown() bool {
switch r {
case BetaThreadRunNewParamsTruncationStrategyTypeAuto, BetaThreadRunNewParamsTruncationStrategyTypeLastMessages:
return true
}
return false
}
type BetaThreadRunUpdateParams struct {
// Set of 16 key-value pairs that can be attached to an object. This can be useful
// for storing additional information about the object in a structured format. Keys
// can be a maximum of 64 characters long and values can be a maximum of 512
// characters long.
Metadata param.Field[interface{}] `json:"metadata"`
}
func (r BetaThreadRunUpdateParams) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
type BetaThreadRunListParams struct {
// A cursor for use in pagination. `after` is an object ID that defines your place
// in the list. For instance, if you make a list request and receive 100 objects,
// ending with obj_foo, your subsequent call can include after=obj_foo in order to
// fetch the next page of the list.
After param.Field[string] `query:"after"`
// A cursor for use in pagination. `before` is an object ID that defines your place
// in the list. For instance, if you make a list request and receive 100 objects,
// starting with obj_foo, your subsequent call can include before=obj_foo in order
// to fetch the previous page of the list.
Before param.Field[string] `query:"before"`
// A limit on the number of objects to be returned. Limit can range between 1 and
// 100, and the default is 20.
Limit param.Field[int64] `query:"limit"`
// Sort order by the `created_at` timestamp of the objects. `asc` for ascending
// order and `desc` for descending order.
Order param.Field[BetaThreadRunListParamsOrder] `query:"order"`
}
// URLQuery serializes [BetaThreadRunListParams]'s query parameters as
// `url.Values`.
func (r BetaThreadRunListParams) URLQuery() (v url.Values) {
return apiquery.MarshalWithSettings(r, apiquery.QuerySettings{
ArrayFormat: apiquery.ArrayQueryFormatBrackets,
NestedFormat: apiquery.NestedQueryFormatBrackets,
})
}
// Sort order by the `created_at` timestamp of the objects. `asc` for ascending
// order and `desc` for descending order.
type BetaThreadRunListParamsOrder string
const (
BetaThreadRunListParamsOrderAsc BetaThreadRunListParamsOrder = "asc"
BetaThreadRunListParamsOrderDesc BetaThreadRunListParamsOrder = "desc"
)
func (r BetaThreadRunListParamsOrder) IsKnown() bool {
switch r {
case BetaThreadRunListParamsOrderAsc, BetaThreadRunListParamsOrderDesc:
return true
}
return false
}
type BetaThreadRunSubmitToolOutputsParams struct {
// A list of tools for which the outputs are being submitted.
ToolOutputs param.Field[[]BetaThreadRunSubmitToolOutputsParamsToolOutput] `json:"tool_outputs,required"`
}
func (r BetaThreadRunSubmitToolOutputsParams) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
type BetaThreadRunSubmitToolOutputsParamsToolOutput struct {
// The output of the tool call to be submitted to continue the run.
Output param.Field[string] `json:"output"`
// The ID of the tool call in the `required_action` object within the run object
// the output is being submitted for.
ToolCallID param.Field[string] `json:"tool_call_id"`
}