-
Notifications
You must be signed in to change notification settings - Fork 12
/
betamessage.go
2400 lines (2076 loc) · 75.8 KB
/
betamessage.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 anthropic
import (
"context"
"net/http"
"reflect"
"github.com/anthropics/anthropic-sdk-go/internal/apijson"
"github.com/anthropics/anthropic-sdk-go/internal/param"
"github.com/anthropics/anthropic-sdk-go/internal/requestconfig"
"github.com/anthropics/anthropic-sdk-go/option"
"github.com/anthropics/anthropic-sdk-go/packages/ssestream"
"github.com/tidwall/gjson"
)
// BetaMessageService contains methods and other services that help with
// interacting with the anthropic 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 [NewBetaMessageService] method instead.
type BetaMessageService struct {
Options []option.RequestOption
Batches *BetaMessageBatchService
}
// NewBetaMessageService 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 NewBetaMessageService(opts ...option.RequestOption) (r *BetaMessageService) {
r = &BetaMessageService{}
r.Options = opts
r.Batches = NewBetaMessageBatchService(opts...)
return
}
// Send a structured list of input messages with text and/or image content, and the
// model will generate the next message in the conversation.
//
// The Messages API can be used for either single queries or stateless multi-turn
// conversations.
//
// Note: If you choose to set a timeout for this request, we recommend 10 minutes.
func (r *BetaMessageService) New(ctx context.Context, params BetaMessageNewParams, opts ...option.RequestOption) (res *BetaMessage, err error) {
opts = append(r.Options[:], opts...)
path := "v1/messages?beta=true"
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, params, &res, opts...)
return
}
// Send a structured list of input messages with text and/or image content, and the
// model will generate the next message in the conversation.
//
// The Messages API can be used for either single queries or stateless multi-turn
// conversations.
//
// Note: If you choose to set a timeout for this request, we recommend 10 minutes.
func (r *BetaMessageService) NewStreaming(ctx context.Context, params BetaMessageNewParams, opts ...option.RequestOption) (stream *ssestream.Stream[BetaRawMessageStreamEvent]) {
var (
raw *http.Response
err error
)
opts = append(r.Options[:], opts...)
opts = append([]option.RequestOption{option.WithJSONSet("stream", true)}, opts...)
path := "v1/messages?beta=true"
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, params, &raw, opts...)
return ssestream.NewStream[BetaRawMessageStreamEvent](ssestream.NewDecoder(raw), err)
}
// Count the number of tokens in a Message.
//
// The Token Count API can be used to count the number of tokens in a Message,
// including tools, images, and documents, without creating it.
func (r *BetaMessageService) CountTokens(ctx context.Context, params BetaMessageCountTokensParams, opts ...option.RequestOption) (res *BetaMessageTokensCount, err error) {
opts = append(r.Options[:], opts...)
path := "v1/messages/count_tokens?beta=true"
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, params, &res, opts...)
return
}
type BetaBase64PDFBlockParam struct {
Source param.Field[BetaBase64PDFSourceParam] `json:"source,required"`
Type param.Field[BetaBase64PDFBlockType] `json:"type,required"`
CacheControl param.Field[BetaCacheControlEphemeralParam] `json:"cache_control"`
}
func (r BetaBase64PDFBlockParam) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
func (r BetaBase64PDFBlockParam) implementsBetaContentBlockParamUnion() {}
type BetaBase64PDFBlockType string
const (
BetaBase64PDFBlockTypeDocument BetaBase64PDFBlockType = "document"
)
func (r BetaBase64PDFBlockType) IsKnown() bool {
switch r {
case BetaBase64PDFBlockTypeDocument:
return true
}
return false
}
type BetaBase64PDFSourceParam struct {
Data param.Field[string] `json:"data,required" format:"byte"`
MediaType param.Field[BetaBase64PDFSourceMediaType] `json:"media_type,required"`
Type param.Field[BetaBase64PDFSourceType] `json:"type,required"`
}
func (r BetaBase64PDFSourceParam) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
type BetaBase64PDFSourceMediaType string
const (
BetaBase64PDFSourceMediaTypeApplicationPDF BetaBase64PDFSourceMediaType = "application/pdf"
)
func (r BetaBase64PDFSourceMediaType) IsKnown() bool {
switch r {
case BetaBase64PDFSourceMediaTypeApplicationPDF:
return true
}
return false
}
type BetaBase64PDFSourceType string
const (
BetaBase64PDFSourceTypeBase64 BetaBase64PDFSourceType = "base64"
)
func (r BetaBase64PDFSourceType) IsKnown() bool {
switch r {
case BetaBase64PDFSourceTypeBase64:
return true
}
return false
}
type BetaCacheControlEphemeralParam struct {
Type param.Field[BetaCacheControlEphemeralType] `json:"type,required"`
}
func (r BetaCacheControlEphemeralParam) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
type BetaCacheControlEphemeralType string
const (
BetaCacheControlEphemeralTypeEphemeral BetaCacheControlEphemeralType = "ephemeral"
)
func (r BetaCacheControlEphemeralType) IsKnown() bool {
switch r {
case BetaCacheControlEphemeralTypeEphemeral:
return true
}
return false
}
type BetaContentBlock struct {
Type BetaContentBlockType `json:"type,required"`
ID string `json:"id"`
// This field can have the runtime type of [interface{}].
Input interface{} `json:"input"`
Name string `json:"name"`
Text string `json:"text"`
JSON betaContentBlockJSON `json:"-"`
union BetaContentBlockUnion
}
// betaContentBlockJSON contains the JSON metadata for the struct
// [BetaContentBlock]
type betaContentBlockJSON struct {
Type apijson.Field
ID apijson.Field
Input apijson.Field
Name apijson.Field
Text apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r betaContentBlockJSON) RawJSON() string {
return r.raw
}
func (r *BetaContentBlock) UnmarshalJSON(data []byte) (err error) {
*r = BetaContentBlock{}
err = apijson.UnmarshalRoot(data, &r.union)
if err != nil {
return err
}
return apijson.Port(r.union, &r)
}
// AsUnion returns a [BetaContentBlockUnion] interface which you can cast to the
// specific types for more type safety.
//
// Possible runtime types of the union are [BetaTextBlock], [BetaToolUseBlock].
func (r BetaContentBlock) AsUnion() BetaContentBlockUnion {
return r.union
}
// Union satisfied by [BetaTextBlock] or [BetaToolUseBlock].
type BetaContentBlockUnion interface {
implementsBetaContentBlock()
}
func init() {
apijson.RegisterUnion(
reflect.TypeOf((*BetaContentBlockUnion)(nil)).Elem(),
"type",
apijson.UnionVariant{
TypeFilter: gjson.JSON,
Type: reflect.TypeOf(BetaTextBlock{}),
DiscriminatorValue: "text",
},
apijson.UnionVariant{
TypeFilter: gjson.JSON,
Type: reflect.TypeOf(BetaToolUseBlock{}),
DiscriminatorValue: "tool_use",
},
)
}
type BetaContentBlockType string
const (
BetaContentBlockTypeText BetaContentBlockType = "text"
BetaContentBlockTypeToolUse BetaContentBlockType = "tool_use"
)
func (r BetaContentBlockType) IsKnown() bool {
switch r {
case BetaContentBlockTypeText, BetaContentBlockTypeToolUse:
return true
}
return false
}
type BetaContentBlockParam struct {
Type param.Field[BetaContentBlockParamType] `json:"type,required"`
ID param.Field[string] `json:"id"`
CacheControl param.Field[BetaCacheControlEphemeralParam] `json:"cache_control"`
Content param.Field[interface{}] `json:"content"`
Input param.Field[interface{}] `json:"input"`
IsError param.Field[bool] `json:"is_error"`
Name param.Field[string] `json:"name"`
Source param.Field[interface{}] `json:"source"`
Text param.Field[string] `json:"text"`
ToolUseID param.Field[string] `json:"tool_use_id"`
}
func (r BetaContentBlockParam) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
func (r BetaContentBlockParam) implementsBetaContentBlockParamUnion() {}
// Satisfied by [BetaTextBlockParam], [BetaImageBlockParam],
// [BetaToolUseBlockParam], [BetaToolResultBlockParam], [BetaBase64PDFBlockParam],
// [BetaContentBlockParam].
type BetaContentBlockParamUnion interface {
implementsBetaContentBlockParamUnion()
}
type BetaContentBlockParamType string
const (
BetaContentBlockParamTypeText BetaContentBlockParamType = "text"
BetaContentBlockParamTypeImage BetaContentBlockParamType = "image"
BetaContentBlockParamTypeToolUse BetaContentBlockParamType = "tool_use"
BetaContentBlockParamTypeToolResult BetaContentBlockParamType = "tool_result"
BetaContentBlockParamTypeDocument BetaContentBlockParamType = "document"
)
func (r BetaContentBlockParamType) IsKnown() bool {
switch r {
case BetaContentBlockParamTypeText, BetaContentBlockParamTypeImage, BetaContentBlockParamTypeToolUse, BetaContentBlockParamTypeToolResult, BetaContentBlockParamTypeDocument:
return true
}
return false
}
type BetaImageBlockParam struct {
Source param.Field[BetaImageBlockParamSource] `json:"source,required"`
Type param.Field[BetaImageBlockParamType] `json:"type,required"`
CacheControl param.Field[BetaCacheControlEphemeralParam] `json:"cache_control"`
}
func (r BetaImageBlockParam) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
func (r BetaImageBlockParam) implementsBetaContentBlockParamUnion() {}
func (r BetaImageBlockParam) implementsBetaToolResultBlockParamContentUnion() {}
type BetaImageBlockParamSource struct {
Data param.Field[string] `json:"data,required" format:"byte"`
MediaType param.Field[BetaImageBlockParamSourceMediaType] `json:"media_type,required"`
Type param.Field[BetaImageBlockParamSourceType] `json:"type,required"`
}
func (r BetaImageBlockParamSource) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
type BetaImageBlockParamSourceMediaType string
const (
BetaImageBlockParamSourceMediaTypeImageJPEG BetaImageBlockParamSourceMediaType = "image/jpeg"
BetaImageBlockParamSourceMediaTypeImagePNG BetaImageBlockParamSourceMediaType = "image/png"
BetaImageBlockParamSourceMediaTypeImageGIF BetaImageBlockParamSourceMediaType = "image/gif"
BetaImageBlockParamSourceMediaTypeImageWebP BetaImageBlockParamSourceMediaType = "image/webp"
)
func (r BetaImageBlockParamSourceMediaType) IsKnown() bool {
switch r {
case BetaImageBlockParamSourceMediaTypeImageJPEG, BetaImageBlockParamSourceMediaTypeImagePNG, BetaImageBlockParamSourceMediaTypeImageGIF, BetaImageBlockParamSourceMediaTypeImageWebP:
return true
}
return false
}
type BetaImageBlockParamSourceType string
const (
BetaImageBlockParamSourceTypeBase64 BetaImageBlockParamSourceType = "base64"
)
func (r BetaImageBlockParamSourceType) IsKnown() bool {
switch r {
case BetaImageBlockParamSourceTypeBase64:
return true
}
return false
}
type BetaImageBlockParamType string
const (
BetaImageBlockParamTypeImage BetaImageBlockParamType = "image"
)
func (r BetaImageBlockParamType) IsKnown() bool {
switch r {
case BetaImageBlockParamTypeImage:
return true
}
return false
}
type BetaInputJSONDelta struct {
PartialJSON string `json:"partial_json,required"`
Type BetaInputJSONDeltaType `json:"type,required"`
JSON betaInputJSONDeltaJSON `json:"-"`
}
// betaInputJSONDeltaJSON contains the JSON metadata for the struct
// [BetaInputJSONDelta]
type betaInputJSONDeltaJSON struct {
PartialJSON apijson.Field
Type apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *BetaInputJSONDelta) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r betaInputJSONDeltaJSON) RawJSON() string {
return r.raw
}
func (r BetaInputJSONDelta) implementsBetaRawContentBlockDeltaEventDelta() {}
type BetaInputJSONDeltaType string
const (
BetaInputJSONDeltaTypeInputJSONDelta BetaInputJSONDeltaType = "input_json_delta"
)
func (r BetaInputJSONDeltaType) IsKnown() bool {
switch r {
case BetaInputJSONDeltaTypeInputJSONDelta:
return true
}
return false
}
type BetaMessage struct {
// Unique object identifier.
//
// The format and length of IDs may change over time.
ID string `json:"id,required"`
// Content generated by the model.
//
// This is an array of content blocks, each of which has a `type` that determines
// its shape.
//
// Example:
//
// ```json
// [{ "type": "text", "text": "Hi, I'm Claude." }]
// ```
//
// If the request input `messages` ended with an `assistant` turn, then the
// response `content` will continue directly from that last turn. You can use this
// to constrain the model's output.
//
// For example, if the input `messages` were:
//
// ```json
// [
//
// {
// "role": "user",
// "content": "What's the Greek name for Sun? (A) Sol (B) Helios (C) Sun"
// },
// { "role": "assistant", "content": "The best answer is (" }
//
// ]
// ```
//
// Then the response `content` might be:
//
// ```json
// [{ "type": "text", "text": "B)" }]
// ```
Content []BetaContentBlock `json:"content,required"`
// The model that will complete your prompt.\n\nSee
// [models](https://docs.anthropic.com/en/docs/models-overview) for additional
// details and options.
Model Model `json:"model,required"`
// Conversational role of the generated message.
//
// This will always be `"assistant"`.
Role BetaMessageRole `json:"role,required"`
// The reason that we stopped.
//
// This may be one the following values:
//
// - `"end_turn"`: the model reached a natural stopping point
// - `"max_tokens"`: we exceeded the requested `max_tokens` or the model's maximum
// - `"stop_sequence"`: one of your provided custom `stop_sequences` was generated
// - `"tool_use"`: the model invoked one or more tools
//
// In non-streaming mode this value is always non-null. In streaming mode, it is
// null in the `message_start` event and non-null otherwise.
StopReason BetaMessageStopReason `json:"stop_reason,required,nullable"`
// Which custom stop sequence was generated, if any.
//
// This value will be a non-null string if one of your custom stop sequences was
// generated.
StopSequence string `json:"stop_sequence,required,nullable"`
// Object type.
//
// For Messages, this is always `"message"`.
Type BetaMessageType `json:"type,required"`
// Billing and rate-limit usage.
//
// Anthropic's API bills and rate-limits by token counts, as tokens represent the
// underlying cost to our systems.
//
// Under the hood, the API transforms requests into a format suitable for the
// model. The model's output then goes through a parsing stage before becoming an
// API response. As a result, the token counts in `usage` will not match one-to-one
// with the exact visible content of an API request or response.
//
// For example, `output_tokens` will be non-zero, even for an empty string response
// from Claude.
Usage BetaUsage `json:"usage,required"`
JSON betaMessageJSON `json:"-"`
}
// betaMessageJSON contains the JSON metadata for the struct [BetaMessage]
type betaMessageJSON struct {
ID apijson.Field
Content apijson.Field
Model apijson.Field
Role apijson.Field
StopReason apijson.Field
StopSequence apijson.Field
Type apijson.Field
Usage apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *BetaMessage) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r betaMessageJSON) RawJSON() string {
return r.raw
}
// Conversational role of the generated message.
//
// This will always be `"assistant"`.
type BetaMessageRole string
const (
BetaMessageRoleAssistant BetaMessageRole = "assistant"
)
func (r BetaMessageRole) IsKnown() bool {
switch r {
case BetaMessageRoleAssistant:
return true
}
return false
}
// The reason that we stopped.
//
// This may be one the following values:
//
// - `"end_turn"`: the model reached a natural stopping point
// - `"max_tokens"`: we exceeded the requested `max_tokens` or the model's maximum
// - `"stop_sequence"`: one of your provided custom `stop_sequences` was generated
// - `"tool_use"`: the model invoked one or more tools
//
// In non-streaming mode this value is always non-null. In streaming mode, it is
// null in the `message_start` event and non-null otherwise.
type BetaMessageStopReason string
const (
BetaMessageStopReasonEndTurn BetaMessageStopReason = "end_turn"
BetaMessageStopReasonMaxTokens BetaMessageStopReason = "max_tokens"
BetaMessageStopReasonStopSequence BetaMessageStopReason = "stop_sequence"
BetaMessageStopReasonToolUse BetaMessageStopReason = "tool_use"
)
func (r BetaMessageStopReason) IsKnown() bool {
switch r {
case BetaMessageStopReasonEndTurn, BetaMessageStopReasonMaxTokens, BetaMessageStopReasonStopSequence, BetaMessageStopReasonToolUse:
return true
}
return false
}
// Object type.
//
// For Messages, this is always `"message"`.
type BetaMessageType string
const (
BetaMessageTypeMessage BetaMessageType = "message"
)
func (r BetaMessageType) IsKnown() bool {
switch r {
case BetaMessageTypeMessage:
return true
}
return false
}
type BetaMessageDeltaUsage struct {
// The cumulative number of output tokens which were used.
OutputTokens int64 `json:"output_tokens,required"`
JSON betaMessageDeltaUsageJSON `json:"-"`
}
// betaMessageDeltaUsageJSON contains the JSON metadata for the struct
// [BetaMessageDeltaUsage]
type betaMessageDeltaUsageJSON struct {
OutputTokens apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *BetaMessageDeltaUsage) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r betaMessageDeltaUsageJSON) RawJSON() string {
return r.raw
}
type BetaMessageParam struct {
Content param.Field[[]BetaContentBlockParamUnion] `json:"content,required"`
Role param.Field[BetaMessageParamRole] `json:"role,required"`
}
func (r BetaMessageParam) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
type BetaMessageParamRole string
const (
BetaMessageParamRoleUser BetaMessageParamRole = "user"
BetaMessageParamRoleAssistant BetaMessageParamRole = "assistant"
)
func (r BetaMessageParamRole) IsKnown() bool {
switch r {
case BetaMessageParamRoleUser, BetaMessageParamRoleAssistant:
return true
}
return false
}
type BetaMessageTokensCount struct {
// The total number of tokens across the provided list of messages, system prompt,
// and tools.
InputTokens int64 `json:"input_tokens,required"`
JSON betaMessageTokensCountJSON `json:"-"`
}
// betaMessageTokensCountJSON contains the JSON metadata for the struct
// [BetaMessageTokensCount]
type betaMessageTokensCountJSON struct {
InputTokens apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *BetaMessageTokensCount) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r betaMessageTokensCountJSON) RawJSON() string {
return r.raw
}
type BetaMetadataParam struct {
// An external identifier for the user who is associated with the request.
//
// This should be a uuid, hash value, or other opaque identifier. Anthropic may use
// this id to help detect abuse. Do not include any identifying information such as
// name, email address, or phone number.
UserID param.Field[string] `json:"user_id"`
}
func (r BetaMetadataParam) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
type BetaRawContentBlockDeltaEvent struct {
Delta BetaRawContentBlockDeltaEventDelta `json:"delta,required"`
Index int64 `json:"index,required"`
Type BetaRawContentBlockDeltaEventType `json:"type,required"`
JSON betaRawContentBlockDeltaEventJSON `json:"-"`
}
// betaRawContentBlockDeltaEventJSON contains the JSON metadata for the struct
// [BetaRawContentBlockDeltaEvent]
type betaRawContentBlockDeltaEventJSON struct {
Delta apijson.Field
Index apijson.Field
Type apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *BetaRawContentBlockDeltaEvent) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r betaRawContentBlockDeltaEventJSON) RawJSON() string {
return r.raw
}
func (r BetaRawContentBlockDeltaEvent) implementsBetaRawMessageStreamEvent() {}
type BetaRawContentBlockDeltaEventDelta struct {
Type BetaRawContentBlockDeltaEventDeltaType `json:"type,required"`
PartialJSON string `json:"partial_json"`
Text string `json:"text"`
JSON betaRawContentBlockDeltaEventDeltaJSON `json:"-"`
union BetaRawContentBlockDeltaEventDeltaUnion
}
// betaRawContentBlockDeltaEventDeltaJSON contains the JSON metadata for the struct
// [BetaRawContentBlockDeltaEventDelta]
type betaRawContentBlockDeltaEventDeltaJSON struct {
Type apijson.Field
PartialJSON apijson.Field
Text apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r betaRawContentBlockDeltaEventDeltaJSON) RawJSON() string {
return r.raw
}
func (r *BetaRawContentBlockDeltaEventDelta) UnmarshalJSON(data []byte) (err error) {
*r = BetaRawContentBlockDeltaEventDelta{}
err = apijson.UnmarshalRoot(data, &r.union)
if err != nil {
return err
}
return apijson.Port(r.union, &r)
}
// AsUnion returns a [BetaRawContentBlockDeltaEventDeltaUnion] interface which you
// can cast to the specific types for more type safety.
//
// Possible runtime types of the union are [BetaTextDelta], [BetaInputJSONDelta].
func (r BetaRawContentBlockDeltaEventDelta) AsUnion() BetaRawContentBlockDeltaEventDeltaUnion {
return r.union
}
// Union satisfied by [BetaTextDelta] or [BetaInputJSONDelta].
type BetaRawContentBlockDeltaEventDeltaUnion interface {
implementsBetaRawContentBlockDeltaEventDelta()
}
func init() {
apijson.RegisterUnion(
reflect.TypeOf((*BetaRawContentBlockDeltaEventDeltaUnion)(nil)).Elem(),
"",
apijson.UnionVariant{
TypeFilter: gjson.JSON,
Type: reflect.TypeOf(BetaTextDelta{}),
},
apijson.UnionVariant{
TypeFilter: gjson.JSON,
Type: reflect.TypeOf(BetaInputJSONDelta{}),
},
)
}
type BetaRawContentBlockDeltaEventDeltaType string
const (
BetaRawContentBlockDeltaEventDeltaTypeTextDelta BetaRawContentBlockDeltaEventDeltaType = "text_delta"
BetaRawContentBlockDeltaEventDeltaTypeInputJSONDelta BetaRawContentBlockDeltaEventDeltaType = "input_json_delta"
)
func (r BetaRawContentBlockDeltaEventDeltaType) IsKnown() bool {
switch r {
case BetaRawContentBlockDeltaEventDeltaTypeTextDelta, BetaRawContentBlockDeltaEventDeltaTypeInputJSONDelta:
return true
}
return false
}
type BetaRawContentBlockDeltaEventType string
const (
BetaRawContentBlockDeltaEventTypeContentBlockDelta BetaRawContentBlockDeltaEventType = "content_block_delta"
)
func (r BetaRawContentBlockDeltaEventType) IsKnown() bool {
switch r {
case BetaRawContentBlockDeltaEventTypeContentBlockDelta:
return true
}
return false
}
type BetaRawContentBlockStartEvent struct {
ContentBlock BetaRawContentBlockStartEventContentBlock `json:"content_block,required"`
Index int64 `json:"index,required"`
Type BetaRawContentBlockStartEventType `json:"type,required"`
JSON betaRawContentBlockStartEventJSON `json:"-"`
}
// betaRawContentBlockStartEventJSON contains the JSON metadata for the struct
// [BetaRawContentBlockStartEvent]
type betaRawContentBlockStartEventJSON struct {
ContentBlock apijson.Field
Index apijson.Field
Type apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *BetaRawContentBlockStartEvent) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r betaRawContentBlockStartEventJSON) RawJSON() string {
return r.raw
}
func (r BetaRawContentBlockStartEvent) implementsBetaRawMessageStreamEvent() {}
type BetaRawContentBlockStartEventContentBlock struct {
Type BetaRawContentBlockStartEventContentBlockType `json:"type,required"`
ID string `json:"id"`
// This field can have the runtime type of [interface{}].
Input interface{} `json:"input"`
Name string `json:"name"`
Text string `json:"text"`
JSON betaRawContentBlockStartEventContentBlockJSON `json:"-"`
union BetaRawContentBlockStartEventContentBlockUnion
}
// betaRawContentBlockStartEventContentBlockJSON contains the JSON metadata for the
// struct [BetaRawContentBlockStartEventContentBlock]
type betaRawContentBlockStartEventContentBlockJSON struct {
Type apijson.Field
ID apijson.Field
Input apijson.Field
Name apijson.Field
Text apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r betaRawContentBlockStartEventContentBlockJSON) RawJSON() string {
return r.raw
}
func (r *BetaRawContentBlockStartEventContentBlock) UnmarshalJSON(data []byte) (err error) {
*r = BetaRawContentBlockStartEventContentBlock{}
err = apijson.UnmarshalRoot(data, &r.union)
if err != nil {
return err
}
return apijson.Port(r.union, &r)
}
// AsUnion returns a [BetaRawContentBlockStartEventContentBlockUnion] interface
// which you can cast to the specific types for more type safety.
//
// Possible runtime types of the union are [BetaTextBlock], [BetaToolUseBlock].
func (r BetaRawContentBlockStartEventContentBlock) AsUnion() BetaRawContentBlockStartEventContentBlockUnion {
return r.union
}
// Union satisfied by [BetaTextBlock] or [BetaToolUseBlock].
type BetaRawContentBlockStartEventContentBlockUnion interface {
implementsBetaRawContentBlockStartEventContentBlock()
}
func init() {
apijson.RegisterUnion(
reflect.TypeOf((*BetaRawContentBlockStartEventContentBlockUnion)(nil)).Elem(),
"type",
apijson.UnionVariant{
TypeFilter: gjson.JSON,
Type: reflect.TypeOf(BetaTextBlock{}),
DiscriminatorValue: "text",
},
apijson.UnionVariant{
TypeFilter: gjson.JSON,
Type: reflect.TypeOf(BetaToolUseBlock{}),
DiscriminatorValue: "tool_use",
},
)
}
type BetaRawContentBlockStartEventContentBlockType string
const (
BetaRawContentBlockStartEventContentBlockTypeText BetaRawContentBlockStartEventContentBlockType = "text"
BetaRawContentBlockStartEventContentBlockTypeToolUse BetaRawContentBlockStartEventContentBlockType = "tool_use"
)
func (r BetaRawContentBlockStartEventContentBlockType) IsKnown() bool {
switch r {
case BetaRawContentBlockStartEventContentBlockTypeText, BetaRawContentBlockStartEventContentBlockTypeToolUse:
return true
}
return false
}
type BetaRawContentBlockStartEventType string
const (
BetaRawContentBlockStartEventTypeContentBlockStart BetaRawContentBlockStartEventType = "content_block_start"
)
func (r BetaRawContentBlockStartEventType) IsKnown() bool {
switch r {
case BetaRawContentBlockStartEventTypeContentBlockStart:
return true
}
return false
}
type BetaRawContentBlockStopEvent struct {
Index int64 `json:"index,required"`
Type BetaRawContentBlockStopEventType `json:"type,required"`
JSON betaRawContentBlockStopEventJSON `json:"-"`
}
// betaRawContentBlockStopEventJSON contains the JSON metadata for the struct
// [BetaRawContentBlockStopEvent]
type betaRawContentBlockStopEventJSON struct {
Index apijson.Field
Type apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *BetaRawContentBlockStopEvent) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r betaRawContentBlockStopEventJSON) RawJSON() string {
return r.raw
}
func (r BetaRawContentBlockStopEvent) implementsBetaRawMessageStreamEvent() {}
type BetaRawContentBlockStopEventType string
const (
BetaRawContentBlockStopEventTypeContentBlockStop BetaRawContentBlockStopEventType = "content_block_stop"
)
func (r BetaRawContentBlockStopEventType) IsKnown() bool {
switch r {
case BetaRawContentBlockStopEventTypeContentBlockStop:
return true
}
return false
}
type BetaRawMessageDeltaEvent struct {
Delta BetaRawMessageDeltaEventDelta `json:"delta,required"`
Type BetaRawMessageDeltaEventType `json:"type,required"`
// Billing and rate-limit usage.
//
// Anthropic's API bills and rate-limits by token counts, as tokens represent the
// underlying cost to our systems.
//
// Under the hood, the API transforms requests into a format suitable for the
// model. The model's output then goes through a parsing stage before becoming an
// API response. As a result, the token counts in `usage` will not match one-to-one
// with the exact visible content of an API request or response.
//
// For example, `output_tokens` will be non-zero, even for an empty string response
// from Claude.
Usage BetaMessageDeltaUsage `json:"usage,required"`
JSON betaRawMessageDeltaEventJSON `json:"-"`
}
// betaRawMessageDeltaEventJSON contains the JSON metadata for the struct
// [BetaRawMessageDeltaEvent]
type betaRawMessageDeltaEventJSON struct {
Delta apijson.Field
Type apijson.Field
Usage apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *BetaRawMessageDeltaEvent) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r betaRawMessageDeltaEventJSON) RawJSON() string {
return r.raw
}
func (r BetaRawMessageDeltaEvent) implementsBetaRawMessageStreamEvent() {}
type BetaRawMessageDeltaEventDelta struct {
StopReason BetaRawMessageDeltaEventDeltaStopReason `json:"stop_reason,required,nullable"`
StopSequence string `json:"stop_sequence,required,nullable"`
JSON betaRawMessageDeltaEventDeltaJSON `json:"-"`
}
// betaRawMessageDeltaEventDeltaJSON contains the JSON metadata for the struct
// [BetaRawMessageDeltaEventDelta]
type betaRawMessageDeltaEventDeltaJSON struct {
StopReason apijson.Field
StopSequence apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *BetaRawMessageDeltaEventDelta) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r betaRawMessageDeltaEventDeltaJSON) RawJSON() string {
return r.raw
}
type BetaRawMessageDeltaEventDeltaStopReason string
const (
BetaRawMessageDeltaEventDeltaStopReasonEndTurn BetaRawMessageDeltaEventDeltaStopReason = "end_turn"
BetaRawMessageDeltaEventDeltaStopReasonMaxTokens BetaRawMessageDeltaEventDeltaStopReason = "max_tokens"
BetaRawMessageDeltaEventDeltaStopReasonStopSequence BetaRawMessageDeltaEventDeltaStopReason = "stop_sequence"
BetaRawMessageDeltaEventDeltaStopReasonToolUse BetaRawMessageDeltaEventDeltaStopReason = "tool_use"
)
func (r BetaRawMessageDeltaEventDeltaStopReason) IsKnown() bool {