-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathv_2.go
5110 lines (4559 loc) · 138 KB
/
v_2.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
// This file was auto-generated by Fern from our API Definition.
package api
import (
json "encoding/json"
fmt "fmt"
internal "github.com/cohere-ai/cohere-go/v2/internal"
)
type V2ChatRequest struct {
// Defaults to `false`.
//
// When `true`, the response will be a SSE stream of events. The final event will contain the complete response, and will have an `event_type` of `"stream-end"`.
//
// Streaming is beneficial for user interfaces that render the contents of the response piece by piece, as it gets generated.
//
// Compatible Deployments: Cohere Platform, Azure, AWS Sagemaker/Bedrock, Private Deployments
//
// The name of a compatible [Cohere model](https://docs.cohere.com/v2/docs/models) (such as command-r or command-r-plus) or the ID of a [fine-tuned](https://docs.cohere.com/v2/docs/chat-fine-tuning) model.
Model string `json:"model" url:"-"`
Messages ChatMessages `json:"messages,omitempty" url:"-"`
// A list of available tools (functions) that the model may suggest invoking before producing a text response.
//
// When `tools` is passed (without `tool_results`), the `text` content in the response will be empty and the `tool_calls` field in the response will be populated with a list of tool calls that need to be made. If no calls need to be made, the `tool_calls` array will be empty.
Tools []*ToolV2 `json:"tools,omitempty" url:"-"`
// When set to `true`, tool calls in the Assistant message will be forced to follow the tool definition strictly. Learn more in the [Structured Outputs (Tools) guide](https://docs.cohere.com/docs/structured-outputs-json#structured-outputs-tools).
//
// **Note**: The first few requests with a new set of tools will take longer to process.
StrictTools *bool `json:"strict_tools,omitempty" url:"-"`
// A list of relevant documents that the model can cite to generate a more accurate reply. Each document is either a string or document object with content and metadata.
Documents []*V2ChatRequestDocumentsItem `json:"documents,omitempty" url:"-"`
CitationOptions *CitationOptions `json:"citation_options,omitempty" url:"-"`
ResponseFormat *ResponseFormatV2 `json:"response_format,omitempty" url:"-"`
// Used to select the [safety instruction](https://docs.cohere.com/v2/docs/safety-modes) inserted into the prompt. Defaults to `CONTEXTUAL`.
// When `OFF` is specified, the safety instruction will be omitted.
//
// Safety modes are not yet configurable in combination with `tools`, `tool_results` and `documents` parameters.
//
// **Note**: This parameter is only compatible with models [Command R 08-2024](https://docs.cohere.com/v2/docs/command-r#august-2024-release), [Command R+ 08-2024](https://docs.cohere.com/v2/docs/command-r-plus#august-2024-release) and newer.
//
// **Note**: `command-r7b-12-2024` only supports `"CONTEXTUAL"` and `"STRICT"` modes.
SafetyMode *V2ChatRequestSafetyMode `json:"safety_mode,omitempty" url:"-"`
// The maximum number of tokens the model will generate as part of the response.
//
// **Note**: Setting a low value may result in incomplete generations.
MaxTokens *int `json:"max_tokens,omitempty" url:"-"`
// A list of up to 5 strings that the model will use to stop generation. If the model generates a string that matches any of the strings in the list, it will stop generating tokens and return the generated text up to that point not including the stop sequence.
StopSequences []string `json:"stop_sequences,omitempty" url:"-"`
// Defaults to `0.3`.
//
// A non-negative float that tunes the degree of randomness in generation. Lower temperatures mean less random generations, and higher temperatures mean more random generations.
//
// Randomness can be further maximized by increasing the value of the `p` parameter.
Temperature *float64 `json:"temperature,omitempty" url:"-"`
// If specified, the backend will make a best effort to sample tokens
// deterministically, such that repeated requests with the same
// seed and parameters should return the same result. However,
// determinism cannot be totally guaranteed.
Seed *int `json:"seed,omitempty" url:"-"`
// Defaults to `0.0`, min value of `0.0`, max value of `1.0`.
// Used to reduce repetitiveness of generated tokens. The higher the value, the stronger a penalty is applied to previously present tokens, proportional to how many times they have already appeared in the prompt or prior generation.
FrequencyPenalty *float64 `json:"frequency_penalty,omitempty" url:"-"`
// Defaults to `0.0`, min value of `0.0`, max value of `1.0`.
// Used to reduce repetitiveness of generated tokens. Similar to `frequency_penalty`, except that this penalty is applied equally to all tokens that have already appeared, regardless of their exact frequencies.
PresencePenalty *float64 `json:"presence_penalty,omitempty" url:"-"`
// Ensures that only the top `k` most likely tokens are considered for generation at each step. When `k` is set to `0`, k-sampling is disabled.
// Defaults to `0`, min value of `0`, max value of `500`.
K *float64 `json:"k,omitempty" url:"-"`
// Ensures that only the most likely tokens, with total probability mass of `p`, are considered for generation at each step. If both `k` and `p` are enabled, `p` acts after `k`.
// Defaults to `0.75`. min value of `0.01`, max value of `0.99`.
P *float64 `json:"p,omitempty" url:"-"`
// Whether to return the prompt in the response.
ReturnPrompt *bool `json:"return_prompt,omitempty" url:"-"`
// Defaults to `false`. When set to `true`, the log probabilities of the generated tokens will be included in the response.
Logprobs *bool `json:"logprobs,omitempty" url:"-"`
stream bool
}
func (v *V2ChatRequest) Stream() bool {
return v.stream
}
func (v *V2ChatRequest) UnmarshalJSON(data []byte) error {
type unmarshaler V2ChatRequest
var body unmarshaler
if err := json.Unmarshal(data, &body); err != nil {
return err
}
*v = V2ChatRequest(body)
v.stream = false
return nil
}
func (v *V2ChatRequest) MarshalJSON() ([]byte, error) {
type embed V2ChatRequest
var marshaler = struct {
embed
Stream bool `json:"stream"`
}{
embed: embed(*v),
Stream: false,
}
return json.Marshal(marshaler)
}
type V2ChatStreamRequest struct {
// Defaults to `false`.
//
// When `true`, the response will be a SSE stream of events. The final event will contain the complete response, and will have an `event_type` of `"stream-end"`.
//
// Streaming is beneficial for user interfaces that render the contents of the response piece by piece, as it gets generated.
//
// Compatible Deployments: Cohere Platform, Azure, AWS Sagemaker/Bedrock, Private Deployments
//
// The name of a compatible [Cohere model](https://docs.cohere.com/v2/docs/models) (such as command-r or command-r-plus) or the ID of a [fine-tuned](https://docs.cohere.com/v2/docs/chat-fine-tuning) model.
Model string `json:"model" url:"-"`
Messages ChatMessages `json:"messages,omitempty" url:"-"`
// A list of available tools (functions) that the model may suggest invoking before producing a text response.
//
// When `tools` is passed (without `tool_results`), the `text` content in the response will be empty and the `tool_calls` field in the response will be populated with a list of tool calls that need to be made. If no calls need to be made, the `tool_calls` array will be empty.
Tools []*ToolV2 `json:"tools,omitempty" url:"-"`
// When set to `true`, tool calls in the Assistant message will be forced to follow the tool definition strictly. Learn more in the [Structured Outputs (Tools) guide](https://docs.cohere.com/docs/structured-outputs-json#structured-outputs-tools).
//
// **Note**: The first few requests with a new set of tools will take longer to process.
StrictTools *bool `json:"strict_tools,omitempty" url:"-"`
// A list of relevant documents that the model can cite to generate a more accurate reply. Each document is either a string or document object with content and metadata.
Documents []*V2ChatStreamRequestDocumentsItem `json:"documents,omitempty" url:"-"`
CitationOptions *CitationOptions `json:"citation_options,omitempty" url:"-"`
ResponseFormat *ResponseFormatV2 `json:"response_format,omitempty" url:"-"`
// Used to select the [safety instruction](https://docs.cohere.com/v2/docs/safety-modes) inserted into the prompt. Defaults to `CONTEXTUAL`.
// When `OFF` is specified, the safety instruction will be omitted.
//
// Safety modes are not yet configurable in combination with `tools`, `tool_results` and `documents` parameters.
//
// **Note**: This parameter is only compatible with models [Command R 08-2024](https://docs.cohere.com/v2/docs/command-r#august-2024-release), [Command R+ 08-2024](https://docs.cohere.com/v2/docs/command-r-plus#august-2024-release) and newer.
//
// **Note**: `command-r7b-12-2024` only supports `"CONTEXTUAL"` and `"STRICT"` modes.
SafetyMode *V2ChatStreamRequestSafetyMode `json:"safety_mode,omitempty" url:"-"`
// The maximum number of tokens the model will generate as part of the response.
//
// **Note**: Setting a low value may result in incomplete generations.
MaxTokens *int `json:"max_tokens,omitempty" url:"-"`
// A list of up to 5 strings that the model will use to stop generation. If the model generates a string that matches any of the strings in the list, it will stop generating tokens and return the generated text up to that point not including the stop sequence.
StopSequences []string `json:"stop_sequences,omitempty" url:"-"`
// Defaults to `0.3`.
//
// A non-negative float that tunes the degree of randomness in generation. Lower temperatures mean less random generations, and higher temperatures mean more random generations.
//
// Randomness can be further maximized by increasing the value of the `p` parameter.
Temperature *float64 `json:"temperature,omitempty" url:"-"`
// If specified, the backend will make a best effort to sample tokens
// deterministically, such that repeated requests with the same
// seed and parameters should return the same result. However,
// determinism cannot be totally guaranteed.
Seed *int `json:"seed,omitempty" url:"-"`
// Defaults to `0.0`, min value of `0.0`, max value of `1.0`.
// Used to reduce repetitiveness of generated tokens. The higher the value, the stronger a penalty is applied to previously present tokens, proportional to how many times they have already appeared in the prompt or prior generation.
FrequencyPenalty *float64 `json:"frequency_penalty,omitempty" url:"-"`
// Defaults to `0.0`, min value of `0.0`, max value of `1.0`.
// Used to reduce repetitiveness of generated tokens. Similar to `frequency_penalty`, except that this penalty is applied equally to all tokens that have already appeared, regardless of their exact frequencies.
PresencePenalty *float64 `json:"presence_penalty,omitempty" url:"-"`
// Ensures that only the top `k` most likely tokens are considered for generation at each step. When `k` is set to `0`, k-sampling is disabled.
// Defaults to `0`, min value of `0`, max value of `500`.
K *float64 `json:"k,omitempty" url:"-"`
// Ensures that only the most likely tokens, with total probability mass of `p`, are considered for generation at each step. If both `k` and `p` are enabled, `p` acts after `k`.
// Defaults to `0.75`. min value of `0.01`, max value of `0.99`.
P *float64 `json:"p,omitempty" url:"-"`
// Whether to return the prompt in the response.
ReturnPrompt *bool `json:"return_prompt,omitempty" url:"-"`
// Defaults to `false`. When set to `true`, the log probabilities of the generated tokens will be included in the response.
Logprobs *bool `json:"logprobs,omitempty" url:"-"`
stream bool
}
func (v *V2ChatStreamRequest) Stream() bool {
return v.stream
}
func (v *V2ChatStreamRequest) UnmarshalJSON(data []byte) error {
type unmarshaler V2ChatStreamRequest
var body unmarshaler
if err := json.Unmarshal(data, &body); err != nil {
return err
}
*v = V2ChatStreamRequest(body)
v.stream = true
return nil
}
func (v *V2ChatStreamRequest) MarshalJSON() ([]byte, error) {
type embed V2ChatStreamRequest
var marshaler = struct {
embed
Stream bool `json:"stream"`
}{
embed: embed(*v),
Stream: true,
}
return json.Marshal(marshaler)
}
type V2EmbedRequest struct {
// An array of strings for the model to embed. Maximum number of texts per call is `96`. We recommend reducing the length of each text to be under `512` tokens for optimal quality.
Texts []string `json:"texts,omitempty" url:"-"`
// An array of image data URIs for the model to embed. Maximum number of images per call is `1`.
//
// The image must be a valid [data URI](https://developer.mozilla.org/en-US/docs/Web/URI/Schemes/data). The image must be in either `image/jpeg` or `image/png` format and has a maximum size of 5MB.
Images []string `json:"images,omitempty" url:"-"`
// Defaults to embed-english-v2.0
//
// The identifier of the model. Smaller "light" models are faster, while larger models will perform better. [Custom models](https://docs.cohere.com/docs/training-custom-models) can also be supplied with their full ID.
//
// Available models and corresponding embedding dimensions:
//
// * `embed-english-v3.0` 1024
// * `embed-multilingual-v3.0` 1024
// * `embed-english-light-v3.0` 384
// * `embed-multilingual-light-v3.0` 384
//
// * `embed-english-v2.0` 4096
// * `embed-english-light-v2.0` 1024
// * `embed-multilingual-v2.0` 768
Model string `json:"model" url:"-"`
InputType EmbedInputType `json:"input_type" url:"-"`
// Specifies the types of embeddings you want to get back. Can be one or more of the following types.
//
// * `"float"`: Use this when you want to get back the default float embeddings. Valid for all models.
// * `"int8"`: Use this when you want to get back signed int8 embeddings. Valid for only v3 models.
// * `"uint8"`: Use this when you want to get back unsigned int8 embeddings. Valid for only v3 models.
// * `"binary"`: Use this when you want to get back signed binary embeddings. Valid for only v3 models.
// * `"ubinary"`: Use this when you want to get back unsigned binary embeddings. Valid for only v3 models.
EmbeddingTypes []EmbeddingType `json:"embedding_types,omitempty" url:"-"`
// One of `NONE|START|END` to specify how the API will handle inputs longer than the maximum token length.
//
// Passing `START` will discard the start of the input. `END` will discard the end of the input. In both cases, input is discarded until the remaining input is exactly the maximum input token length for the model.
//
// If `NONE` is selected, when the input exceeds the maximum input token length an error will be returned.
Truncate *V2EmbedRequestTruncate `json:"truncate,omitempty" url:"-"`
}
type V2RerankRequest struct {
// The identifier of the model to use, eg `rerank-v3.5`.
Model string `json:"model" url:"-"`
// The search query
Query string `json:"query" url:"-"`
// A list of texts that will be compared to the `query`.
// For optimal performance we recommend against sending more than 1,000 documents in a single request.
//
// **Note**: long documents will automatically be truncated to the value of `max_tokens_per_doc`.
//
// **Note**: structured data should be formatted as YAML strings for best performance.
Documents []string `json:"documents,omitempty" url:"-"`
// Limits the number of returned rerank results to the specified value. If not passed, all the rerank results will be returned.
TopN *int `json:"top_n,omitempty" url:"-"`
// - If false, returns results without the doc text - the api will return a list of {index, relevance score} where index is inferred from the list passed into the request.
// - If true, returns results with the doc text passed in - the api will return an ordered list of {index, text, relevance score} where index + text refers to the list passed into the request.
ReturnDocuments *bool `json:"return_documents,omitempty" url:"-"`
// Defaults to `4096`. Long documents will be automatically truncated to the specified number of tokens.
MaxTokensPerDoc *int `json:"max_tokens_per_doc,omitempty" url:"-"`
}
// A message from the assistant role can contain text and tool call information.
type AssistantMessage struct {
ToolCalls []*ToolCallV2 `json:"tool_calls,omitempty" url:"tool_calls,omitempty"`
// A chain-of-thought style reflection and plan that the model generates when working with Tools.
ToolPlan *string `json:"tool_plan,omitempty" url:"tool_plan,omitempty"`
Content *AssistantMessageContent `json:"content,omitempty" url:"content,omitempty"`
Citations []*Citation `json:"citations,omitempty" url:"citations,omitempty"`
extraProperties map[string]interface{}
rawJSON json.RawMessage
}
func (a *AssistantMessage) GetToolCalls() []*ToolCallV2 {
if a == nil {
return nil
}
return a.ToolCalls
}
func (a *AssistantMessage) GetToolPlan() *string {
if a == nil {
return nil
}
return a.ToolPlan
}
func (a *AssistantMessage) GetContent() *AssistantMessageContent {
if a == nil {
return nil
}
return a.Content
}
func (a *AssistantMessage) GetCitations() []*Citation {
if a == nil {
return nil
}
return a.Citations
}
func (a *AssistantMessage) GetExtraProperties() map[string]interface{} {
return a.extraProperties
}
func (a *AssistantMessage) UnmarshalJSON(data []byte) error {
type unmarshaler AssistantMessage
var value unmarshaler
if err := json.Unmarshal(data, &value); err != nil {
return err
}
*a = AssistantMessage(value)
extraProperties, err := internal.ExtractExtraProperties(data, *a)
if err != nil {
return err
}
a.extraProperties = extraProperties
a.rawJSON = json.RawMessage(data)
return nil
}
func (a *AssistantMessage) String() string {
if len(a.rawJSON) > 0 {
if value, err := internal.StringifyJSON(a.rawJSON); err == nil {
return value
}
}
if value, err := internal.StringifyJSON(a); err == nil {
return value
}
return fmt.Sprintf("%#v", a)
}
type AssistantMessageContent struct {
String string
AssistantMessageContentItemList []*AssistantMessageContentItem
typ string
}
func (a *AssistantMessageContent) GetString() string {
if a == nil {
return ""
}
return a.String
}
func (a *AssistantMessageContent) GetAssistantMessageContentItemList() []*AssistantMessageContentItem {
if a == nil {
return nil
}
return a.AssistantMessageContentItemList
}
func (a *AssistantMessageContent) UnmarshalJSON(data []byte) error {
var valueString string
if err := json.Unmarshal(data, &valueString); err == nil {
a.typ = "String"
a.String = valueString
return nil
}
var valueAssistantMessageContentItemList []*AssistantMessageContentItem
if err := json.Unmarshal(data, &valueAssistantMessageContentItemList); err == nil {
a.typ = "AssistantMessageContentItemList"
a.AssistantMessageContentItemList = valueAssistantMessageContentItemList
return nil
}
return fmt.Errorf("%s cannot be deserialized as a %T", data, a)
}
func (a AssistantMessageContent) MarshalJSON() ([]byte, error) {
if a.typ == "String" || a.String != "" {
return json.Marshal(a.String)
}
if a.typ == "AssistantMessageContentItemList" || a.AssistantMessageContentItemList != nil {
return json.Marshal(a.AssistantMessageContentItemList)
}
return nil, fmt.Errorf("type %T does not include a non-empty union type", a)
}
type AssistantMessageContentVisitor interface {
VisitString(string) error
VisitAssistantMessageContentItemList([]*AssistantMessageContentItem) error
}
func (a *AssistantMessageContent) Accept(visitor AssistantMessageContentVisitor) error {
if a.typ == "String" || a.String != "" {
return visitor.VisitString(a.String)
}
if a.typ == "AssistantMessageContentItemList" || a.AssistantMessageContentItemList != nil {
return visitor.VisitAssistantMessageContentItemList(a.AssistantMessageContentItemList)
}
return fmt.Errorf("type %T does not include a non-empty union type", a)
}
type AssistantMessageContentItem struct {
Type string
Text *TextContent
}
func (a *AssistantMessageContentItem) GetType() string {
if a == nil {
return ""
}
return a.Type
}
func (a *AssistantMessageContentItem) GetText() *TextContent {
if a == nil {
return nil
}
return a.Text
}
func (a *AssistantMessageContentItem) UnmarshalJSON(data []byte) error {
var unmarshaler struct {
Type string `json:"type"`
}
if err := json.Unmarshal(data, &unmarshaler); err != nil {
return err
}
a.Type = unmarshaler.Type
if unmarshaler.Type == "" {
return fmt.Errorf("%T did not include discriminant type", a)
}
switch unmarshaler.Type {
case "text":
value := new(TextContent)
if err := json.Unmarshal(data, &value); err != nil {
return err
}
a.Text = value
}
return nil
}
func (a AssistantMessageContentItem) MarshalJSON() ([]byte, error) {
if err := a.validate(); err != nil {
return nil, err
}
if a.Text != nil {
return internal.MarshalJSONWithExtraProperty(a.Text, "type", "text")
}
return nil, fmt.Errorf("type %T does not define a non-empty union type", a)
}
type AssistantMessageContentItemVisitor interface {
VisitText(*TextContent) error
}
func (a *AssistantMessageContentItem) Accept(visitor AssistantMessageContentItemVisitor) error {
if a.Text != nil {
return visitor.VisitText(a.Text)
}
return fmt.Errorf("type %T does not define a non-empty union type", a)
}
func (a *AssistantMessageContentItem) validate() error {
if a == nil {
return fmt.Errorf("type %T is nil", a)
}
var fields []string
if a.Text != nil {
fields = append(fields, "text")
}
if len(fields) == 0 {
if a.Type != "" {
return fmt.Errorf("type %T defines a discriminant set to %q but the field is not set", a, a.Type)
}
return fmt.Errorf("type %T is empty", a)
}
if len(fields) > 1 {
return fmt.Errorf("type %T defines values for %s, but only one value is allowed", a, fields)
}
if a.Type != "" {
field := fields[0]
if a.Type != field {
return fmt.Errorf(
"type %T defines a discriminant set to %q, but it does not match the %T field; either remove or update the discriminant to match",
a,
a.Type,
a,
)
}
}
return nil
}
// A message from the assistant role can contain text and tool call information.
type AssistantMessageResponse struct {
ToolCalls []*ToolCallV2 `json:"tool_calls,omitempty" url:"tool_calls,omitempty"`
// A chain-of-thought style reflection and plan that the model generates when working with Tools.
ToolPlan *string `json:"tool_plan,omitempty" url:"tool_plan,omitempty"`
Content []*AssistantMessageResponseContentItem `json:"content,omitempty" url:"content,omitempty"`
Citations []*Citation `json:"citations,omitempty" url:"citations,omitempty"`
role string
extraProperties map[string]interface{}
rawJSON json.RawMessage
}
func (a *AssistantMessageResponse) GetToolCalls() []*ToolCallV2 {
if a == nil {
return nil
}
return a.ToolCalls
}
func (a *AssistantMessageResponse) GetToolPlan() *string {
if a == nil {
return nil
}
return a.ToolPlan
}
func (a *AssistantMessageResponse) GetContent() []*AssistantMessageResponseContentItem {
if a == nil {
return nil
}
return a.Content
}
func (a *AssistantMessageResponse) GetCitations() []*Citation {
if a == nil {
return nil
}
return a.Citations
}
func (a *AssistantMessageResponse) Role() string {
return a.role
}
func (a *AssistantMessageResponse) GetExtraProperties() map[string]interface{} {
return a.extraProperties
}
func (a *AssistantMessageResponse) UnmarshalJSON(data []byte) error {
type embed AssistantMessageResponse
var unmarshaler = struct {
embed
Role string `json:"role"`
}{
embed: embed(*a),
}
if err := json.Unmarshal(data, &unmarshaler); err != nil {
return err
}
*a = AssistantMessageResponse(unmarshaler.embed)
if unmarshaler.Role != "assistant" {
return fmt.Errorf("unexpected value for literal on type %T; expected %v got %v", a, "assistant", unmarshaler.Role)
}
a.role = unmarshaler.Role
extraProperties, err := internal.ExtractExtraProperties(data, *a, "role")
if err != nil {
return err
}
a.extraProperties = extraProperties
a.rawJSON = json.RawMessage(data)
return nil
}
func (a *AssistantMessageResponse) MarshalJSON() ([]byte, error) {
type embed AssistantMessageResponse
var marshaler = struct {
embed
Role string `json:"role"`
}{
embed: embed(*a),
Role: "assistant",
}
return json.Marshal(marshaler)
}
func (a *AssistantMessageResponse) String() string {
if len(a.rawJSON) > 0 {
if value, err := internal.StringifyJSON(a.rawJSON); err == nil {
return value
}
}
if value, err := internal.StringifyJSON(a); err == nil {
return value
}
return fmt.Sprintf("%#v", a)
}
type AssistantMessageResponseContentItem struct {
Type string
Text *TextContent
}
func (a *AssistantMessageResponseContentItem) GetType() string {
if a == nil {
return ""
}
return a.Type
}
func (a *AssistantMessageResponseContentItem) GetText() *TextContent {
if a == nil {
return nil
}
return a.Text
}
func (a *AssistantMessageResponseContentItem) UnmarshalJSON(data []byte) error {
var unmarshaler struct {
Type string `json:"type"`
}
if err := json.Unmarshal(data, &unmarshaler); err != nil {
return err
}
a.Type = unmarshaler.Type
if unmarshaler.Type == "" {
return fmt.Errorf("%T did not include discriminant type", a)
}
switch unmarshaler.Type {
case "text":
value := new(TextContent)
if err := json.Unmarshal(data, &value); err != nil {
return err
}
a.Text = value
}
return nil
}
func (a AssistantMessageResponseContentItem) MarshalJSON() ([]byte, error) {
if err := a.validate(); err != nil {
return nil, err
}
if a.Text != nil {
return internal.MarshalJSONWithExtraProperty(a.Text, "type", "text")
}
return nil, fmt.Errorf("type %T does not define a non-empty union type", a)
}
type AssistantMessageResponseContentItemVisitor interface {
VisitText(*TextContent) error
}
func (a *AssistantMessageResponseContentItem) Accept(visitor AssistantMessageResponseContentItemVisitor) error {
if a.Text != nil {
return visitor.VisitText(a.Text)
}
return fmt.Errorf("type %T does not define a non-empty union type", a)
}
func (a *AssistantMessageResponseContentItem) validate() error {
if a == nil {
return fmt.Errorf("type %T is nil", a)
}
var fields []string
if a.Text != nil {
fields = append(fields, "text")
}
if len(fields) == 0 {
if a.Type != "" {
return fmt.Errorf("type %T defines a discriminant set to %q but the field is not set", a, a.Type)
}
return fmt.Errorf("type %T is empty", a)
}
if len(fields) > 1 {
return fmt.Errorf("type %T defines values for %s, but only one value is allowed", a, fields)
}
if a.Type != "" {
field := fields[0]
if a.Type != field {
return fmt.Errorf(
"type %T defines a discriminant set to %q, but it does not match the %T field; either remove or update the discriminant to match",
a,
a.Type,
a,
)
}
}
return nil
}
// A streamed delta event which contains a delta of chat text content.
type ChatContentDeltaEvent struct {
Index *int `json:"index,omitempty" url:"index,omitempty"`
Delta *ChatContentDeltaEventDelta `json:"delta,omitempty" url:"delta,omitempty"`
Logprobs *LogprobItem `json:"logprobs,omitempty" url:"logprobs,omitempty"`
extraProperties map[string]interface{}
rawJSON json.RawMessage
}
func (c *ChatContentDeltaEvent) GetIndex() *int {
if c == nil {
return nil
}
return c.Index
}
func (c *ChatContentDeltaEvent) GetDelta() *ChatContentDeltaEventDelta {
if c == nil {
return nil
}
return c.Delta
}
func (c *ChatContentDeltaEvent) GetLogprobs() *LogprobItem {
if c == nil {
return nil
}
return c.Logprobs
}
func (c *ChatContentDeltaEvent) GetExtraProperties() map[string]interface{} {
return c.extraProperties
}
func (c *ChatContentDeltaEvent) UnmarshalJSON(data []byte) error {
type unmarshaler ChatContentDeltaEvent
var value unmarshaler
if err := json.Unmarshal(data, &value); err != nil {
return err
}
*c = ChatContentDeltaEvent(value)
extraProperties, err := internal.ExtractExtraProperties(data, *c)
if err != nil {
return err
}
c.extraProperties = extraProperties
c.rawJSON = json.RawMessage(data)
return nil
}
func (c *ChatContentDeltaEvent) String() string {
if len(c.rawJSON) > 0 {
if value, err := internal.StringifyJSON(c.rawJSON); err == nil {
return value
}
}
if value, err := internal.StringifyJSON(c); err == nil {
return value
}
return fmt.Sprintf("%#v", c)
}
type ChatContentDeltaEventDelta struct {
Message *ChatContentDeltaEventDeltaMessage `json:"message,omitempty" url:"message,omitempty"`
extraProperties map[string]interface{}
rawJSON json.RawMessage
}
func (c *ChatContentDeltaEventDelta) GetMessage() *ChatContentDeltaEventDeltaMessage {
if c == nil {
return nil
}
return c.Message
}
func (c *ChatContentDeltaEventDelta) GetExtraProperties() map[string]interface{} {
return c.extraProperties
}
func (c *ChatContentDeltaEventDelta) UnmarshalJSON(data []byte) error {
type unmarshaler ChatContentDeltaEventDelta
var value unmarshaler
if err := json.Unmarshal(data, &value); err != nil {
return err
}
*c = ChatContentDeltaEventDelta(value)
extraProperties, err := internal.ExtractExtraProperties(data, *c)
if err != nil {
return err
}
c.extraProperties = extraProperties
c.rawJSON = json.RawMessage(data)
return nil
}
func (c *ChatContentDeltaEventDelta) String() string {
if len(c.rawJSON) > 0 {
if value, err := internal.StringifyJSON(c.rawJSON); err == nil {
return value
}
}
if value, err := internal.StringifyJSON(c); err == nil {
return value
}
return fmt.Sprintf("%#v", c)
}
type ChatContentDeltaEventDeltaMessage struct {
Content *ChatContentDeltaEventDeltaMessageContent `json:"content,omitempty" url:"content,omitempty"`
extraProperties map[string]interface{}
rawJSON json.RawMessage
}
func (c *ChatContentDeltaEventDeltaMessage) GetContent() *ChatContentDeltaEventDeltaMessageContent {
if c == nil {
return nil
}
return c.Content
}
func (c *ChatContentDeltaEventDeltaMessage) GetExtraProperties() map[string]interface{} {
return c.extraProperties
}
func (c *ChatContentDeltaEventDeltaMessage) UnmarshalJSON(data []byte) error {
type unmarshaler ChatContentDeltaEventDeltaMessage
var value unmarshaler
if err := json.Unmarshal(data, &value); err != nil {
return err
}
*c = ChatContentDeltaEventDeltaMessage(value)
extraProperties, err := internal.ExtractExtraProperties(data, *c)
if err != nil {
return err
}
c.extraProperties = extraProperties
c.rawJSON = json.RawMessage(data)
return nil
}
func (c *ChatContentDeltaEventDeltaMessage) String() string {
if len(c.rawJSON) > 0 {
if value, err := internal.StringifyJSON(c.rawJSON); err == nil {
return value
}
}
if value, err := internal.StringifyJSON(c); err == nil {
return value
}
return fmt.Sprintf("%#v", c)
}
type ChatContentDeltaEventDeltaMessageContent struct {
Text *string `json:"text,omitempty" url:"text,omitempty"`
extraProperties map[string]interface{}
rawJSON json.RawMessage
}
func (c *ChatContentDeltaEventDeltaMessageContent) GetText() *string {
if c == nil {
return nil
}
return c.Text
}
func (c *ChatContentDeltaEventDeltaMessageContent) GetExtraProperties() map[string]interface{} {
return c.extraProperties
}
func (c *ChatContentDeltaEventDeltaMessageContent) UnmarshalJSON(data []byte) error {
type unmarshaler ChatContentDeltaEventDeltaMessageContent
var value unmarshaler
if err := json.Unmarshal(data, &value); err != nil {
return err
}
*c = ChatContentDeltaEventDeltaMessageContent(value)
extraProperties, err := internal.ExtractExtraProperties(data, *c)
if err != nil {
return err
}
c.extraProperties = extraProperties
c.rawJSON = json.RawMessage(data)
return nil
}
func (c *ChatContentDeltaEventDeltaMessageContent) String() string {
if len(c.rawJSON) > 0 {
if value, err := internal.StringifyJSON(c.rawJSON); err == nil {
return value
}
}
if value, err := internal.StringifyJSON(c); err == nil {
return value
}
return fmt.Sprintf("%#v", c)
}
// A streamed delta event which signifies that the content block has ended.
type ChatContentEndEvent struct {
Index *int `json:"index,omitempty" url:"index,omitempty"`
extraProperties map[string]interface{}
rawJSON json.RawMessage
}
func (c *ChatContentEndEvent) GetIndex() *int {
if c == nil {
return nil
}
return c.Index
}
func (c *ChatContentEndEvent) GetExtraProperties() map[string]interface{} {
return c.extraProperties
}
func (c *ChatContentEndEvent) UnmarshalJSON(data []byte) error {
type unmarshaler ChatContentEndEvent
var value unmarshaler
if err := json.Unmarshal(data, &value); err != nil {
return err
}
*c = ChatContentEndEvent(value)
extraProperties, err := internal.ExtractExtraProperties(data, *c)
if err != nil {
return err
}
c.extraProperties = extraProperties
c.rawJSON = json.RawMessage(data)
return nil
}
func (c *ChatContentEndEvent) String() string {
if len(c.rawJSON) > 0 {
if value, err := internal.StringifyJSON(c.rawJSON); err == nil {
return value
}
}
if value, err := internal.StringifyJSON(c); err == nil {
return value
}
return fmt.Sprintf("%#v", c)
}
// A streamed delta event which signifies that a new content block has started.
type ChatContentStartEvent struct {
Index *int `json:"index,omitempty" url:"index,omitempty"`
Delta *ChatContentStartEventDelta `json:"delta,omitempty" url:"delta,omitempty"`
extraProperties map[string]interface{}
rawJSON json.RawMessage
}
func (c *ChatContentStartEvent) GetIndex() *int {
if c == nil {
return nil
}
return c.Index
}
func (c *ChatContentStartEvent) GetDelta() *ChatContentStartEventDelta {
if c == nil {
return nil
}
return c.Delta
}
func (c *ChatContentStartEvent) GetExtraProperties() map[string]interface{} {
return c.extraProperties
}
func (c *ChatContentStartEvent) UnmarshalJSON(data []byte) error {
type unmarshaler ChatContentStartEvent
var value unmarshaler
if err := json.Unmarshal(data, &value); err != nil {
return err
}
*c = ChatContentStartEvent(value)
extraProperties, err := internal.ExtractExtraProperties(data, *c)
if err != nil {
return err
}
c.extraProperties = extraProperties
c.rawJSON = json.RawMessage(data)
return nil
}
func (c *ChatContentStartEvent) String() string {
if len(c.rawJSON) > 0 {
if value, err := internal.StringifyJSON(c.rawJSON); err == nil {
return value
}
}
if value, err := internal.StringifyJSON(c); err == nil {
return value
}
return fmt.Sprintf("%#v", c)
}
type ChatContentStartEventDelta struct {
Message *ChatContentStartEventDeltaMessage `json:"message,omitempty" url:"message,omitempty"`
extraProperties map[string]interface{}
rawJSON json.RawMessage
}
func (c *ChatContentStartEventDelta) GetMessage() *ChatContentStartEventDeltaMessage {
if c == nil {
return nil
}
return c.Message
}
func (c *ChatContentStartEventDelta) GetExtraProperties() map[string]interface{} {
return c.extraProperties
}