From d187f9b4ac9a9242de101b41847257d06db2e9a4 Mon Sep 17 00:00:00 2001 From: Young-Jin Park Date: Tue, 30 Jul 2024 23:54:04 -0400 Subject: [PATCH 01/13] Initial commit From 2bb232557a9f75d58b7e7145c69771c927574dd3 Mon Sep 17 00:00:00 2001 From: "stainless-app[bot]" <142633134+stainless-app[bot]@users.noreply.github.com> Date: Wed, 31 Jul 2024 20:21:46 +0000 Subject: [PATCH 02/13] fix: handle nil pagination responses when HTTP status is 200 (#2) --- README.md | 84 ++------ bedrock/bedrock.go | 210 -------------------- examples/tools-streaming-jsonschema/main.go | 2 +- go.mod | 43 +--- go.sum | 178 ----------------- internal/requestconfig/requestconfig.go | 8 +- internal/version.go | 2 +- message.go | 200 ++++--------------- message_test.go | 40 +++- vertex/vertex.go | 99 --------- 10 files changed, 101 insertions(+), 765 deletions(-) delete mode 100644 bedrock/bedrock.go delete mode 100644 vertex/vertex.go diff --git a/README.md b/README.md index 883643a..6bef330 100644 --- a/README.md +++ b/README.md @@ -52,9 +52,10 @@ func main() { ) message, err := client.Messages.New(context.TODO(), anthropic.MessageNewParams{ MaxTokens: anthropic.F(int64(1024)), - Messages: anthropic.F([]anthropic.MessageParam{ - anthropic.NewUserMessage(anthropic.NewTextBlock("What is the weather in SF?")), - }), + Messages: anthropic.F([]anthropic.MessageParam{{ + Role: anthropic.F(anthropic.MessageParamRoleUser), + Content: anthropic.F([]anthropic.MessageParamContentUnion{anthropic.TextBlockParam{Type: anthropic.F(anthropic.TextBlockParamTypeText), Text: anthropic.F("What is a quaternion?")}}), + }}), Model: anthropic.F(anthropic.ModelClaude_3_5_Sonnet_20240620), }) if err != nil { @@ -180,9 +181,10 @@ To handle errors, we recommend that you use the `errors.As` pattern: ```go _, err := client.Messages.New(context.TODO(), anthropic.MessageNewParams{ MaxTokens: anthropic.F(int64(1024)), - Messages: anthropic.F([]anthropic.MessageParam{ - anthropic.NewUserMessage(anthropic.NewTextBlock("What is the weather in SF?")), - }), + Messages: anthropic.F([]anthropic.MessageParam{{ + Role: anthropic.F(anthropic.MessageParamRoleUser), + Content: anthropic.F([]anthropic.MessageParamContentUnion{anthropic.TextBlockParam{Type: anthropic.F(anthropic.TextBlockParamTypeText), Text: anthropic.F("What is a quaternion?")}}), + }}), Model: anthropic.F(anthropic.ModelClaude_3_5_Sonnet_20240620), }) if err != nil { @@ -213,9 +215,10 @@ client.Messages.New( ctx, anthropic.MessageNewParams{ MaxTokens: anthropic.F(int64(1024)), - Messages: anthropic.F([]anthropic.MessageParam{ - anthropic.NewUserMessage(anthropic.NewTextBlock("What is the weather in SF?")), - }), + Messages: anthropic.F([]anthropic.MessageParam{{ + Role: anthropic.F(anthropic.MessageParamRoleUser), + Content: anthropic.F([]anthropic.MessageParamContentUnion{anthropic.TextBlockParam{Type: anthropic.F(anthropic.TextBlockParamTypeText), Text: anthropic.F("What is a quaternion?")}}), + }}), Model: anthropic.F(anthropic.ModelClaude_3_5_Sonnet_20240620), }, // This sets the per-retry timeout @@ -255,9 +258,10 @@ client.Messages.New( context.TODO(), anthropic.MessageNewParams{ MaxTokens: anthropic.F(int64(1024)), - Messages: anthropic.F([]anthropic.MessageParam{ - anthropic.NewUserMessage(anthropic.NewTextBlock("What is the weather in SF?")), - }), + Messages: anthropic.F([]anthropic.MessageParam{{ + Role: anthropic.F(anthropic.MessageParamRoleUser), + Content: anthropic.F([]anthropic.MessageParamContentUnion{anthropic.TextBlockParam{Type: anthropic.F(anthropic.TextBlockParamTypeText), Text: anthropic.F("What is a quaternion?")}}), + }}), Model: anthropic.F(anthropic.ModelClaude_3_5_Sonnet_20240620), }, option.WithMaxRetries(5), @@ -350,62 +354,6 @@ You may also replace the default `http.Client` with accepted (this overwrites any previous client) and receives requests after any middleware has been applied. -## Amazon Bedrock - -To use this library with [Amazon Bedrock](https://aws.amazon.com/bedrock/claude/), -use the bedrock request option `bedrock.WithLoadDefaultConfig(…)` which reads the -[default config](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html). - -Importing the `bedrock` library also globally registers a decoder for `application/vnd.amazon.eventstream` for -streaming. - -```go -package main - -import ( - "github.com/anthropics/anthropic-sdk-go" - "github.com/anthropics/anthropic-sdk-go/bedrock" -) - -func main() { - client := anthropic.NewClient( - bedrock.WithLoadDefaultConfig(context.Background()), - ) -} -``` - -If you already have an `aws.Config`, you can also use it directly with `bedrock.WithConfig(cfg)`. - -Read more about Anthropic and Amazon Bedrock [here](https://docs.anthropic.com/en/api/claude-on-amazon-bedrock). - -## Google Vertex AI - -To use this library with [Google Vertex AI](https://cloud.google.com/vertex-ai/generative-ai/docs/partner-models/use-claude), -use the request option `vertex.WithGoogleAuth(…)` which reads the -[Application Default Credentials](https://cloud.google.com/docs/authentication/application-default-credentials). - -```go -package main - -import ( - "context" - - "github.com/anthropics/anthropic-sdk-go" - "github.com/anthropics/anthropic-sdk-go/vertex" -) - -func main() { - client := anthropic.NewClient( - vertex.WithGoogleAuth(context.Background(), "us-central1", "stainless-399616"), - ) -} -``` - -If you already have `*google.Credentials`, you can also use it directly with -`vertex.WithCredentials(ctx, region, projectId, creds)`. - -Read more about Anthropic and Google Vertex [here](https://docs.anthropic.com/en/api/claude-on-vertex-ai). - ## Semantic versioning This package generally follows [SemVer](https://semver.org/spec/v2.0.0.html) conventions, though certain backwards-incompatible changes may be released as minor versions: diff --git a/bedrock/bedrock.go b/bedrock/bedrock.go deleted file mode 100644 index a4729e5..0000000 --- a/bedrock/bedrock.go +++ /dev/null @@ -1,210 +0,0 @@ -package bedrock - -import ( - "bytes" - "context" - "crypto/sha256" - "encoding/base64" - "encoding/hex" - "encoding/json" - "fmt" - "io" - "net/http" - "time" - - "github.com/aws/aws-sdk-go-v2/aws" - "github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream" - "github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream/eventstreamapi" - v4 "github.com/aws/aws-sdk-go-v2/aws/signer/v4" - "github.com/aws/aws-sdk-go-v2/config" - "github.com/tidwall/gjson" - "github.com/tidwall/sjson" - - "github.com/anthropics/anthropic-sdk-go/internal/requestconfig" - "github.com/anthropics/anthropic-sdk-go/option" - "github.com/anthropics/anthropic-sdk-go/packages/ssestream" -) - -const DefaultVersion = "bedrock-2023-05-31" - -var DefaultEndpoints = map[string]bool{ - "/v1/complete": true, - "/v1/messages": true, -} - -type eventstreamChunk struct { - Bytes string `json:"bytes"` - P string `json:"p"` -} - -type eventstreamDecoder struct { - eventstream.Decoder - - rc io.ReadCloser - evt ssestream.Event - err error -} - -func (e *eventstreamDecoder) Close() error { - return nil -} - -func (e *eventstreamDecoder) Err() error { - return nil -} - -func (e *eventstreamDecoder) Next() bool { - if e.err != nil { - return false - } - - msg, err := e.Decoder.Decode(e.rc, nil) - if err != nil { - e.err = err - return false - } - - messageType := msg.Headers.Get(eventstreamapi.MessageTypeHeader) - if messageType == nil { - e.err = fmt.Errorf("%s event header not present", eventstreamapi.MessageTypeHeader) - return false - } - - switch messageType.String() { - case eventstreamapi.EventMessageType: - eventType := msg.Headers.Get(eventstreamapi.EventTypeHeader) - if eventType == nil { - e.err = fmt.Errorf("%s event header not present", eventstreamapi.EventTypeHeader) - return false - } - - if eventType.String() == "chunk" { - chunk := eventstreamChunk{} - err = json.Unmarshal(msg.Payload, &chunk) - if err != nil { - e.err = err - return false - } - decoded, err := base64.StdEncoding.DecodeString(chunk.Bytes) - if err != nil { - e.err = err - return false - } - e.evt = ssestream.Event{ - Type: gjson.GetBytes(decoded, "type").String(), - Data: decoded, - } - } - - case eventstreamapi.ExceptionMessageType: - case eventstreamapi.ErrorMessageType: - errorCode := "UnknownError" - errorMessage := errorCode - if header := msg.Headers.Get(eventstreamapi.ErrorCodeHeader); header != nil { - errorCode = header.String() - } - if header := msg.Headers.Get(eventstreamapi.ErrorMessageHeader); header != nil { - errorMessage = header.String() - } - e.err = fmt.Errorf("received error or exception %s: %s", errorCode, errorMessage) - return false - } - - return true -} - -func (e *eventstreamDecoder) Event() ssestream.Event { - return e.evt -} - -var ( - _ ssestream.Decoder = &eventstreamDecoder{} -) - -func init() { - ssestream.RegisterDecoder("application/vnd.amazon.eventstream", func(rc io.ReadCloser) ssestream.Decoder { - return &eventstreamDecoder{rc: rc} - }) -} - -// WithLoadDefaultConfig returns a request option which loads the default config for Amazon and registers -// middleware that intercepts request to the Messages API so that this SDK can be used with Amazon Bedrock. -// -// If you already have an [aws.Config], it is recommended that you instead call [WithConfig] directly. -func WithLoadDefaultConfig(ctx context.Context, optFns ...func(*config.LoadOptions) error) option.RequestOption { - cfg, err := config.LoadDefaultConfig(ctx, optFns...) - if err != nil { - panic(err) - } - return WithConfig(cfg) -} - -// WithConfig returns a request option which uses the provided config and registers middleware that -// intercepts request to the Messages API so that this SDK can be used with Amazon Bedrock. -func WithConfig(cfg aws.Config) option.RequestOption { - signer := v4.NewSigner() - middleware := bedrockMiddleware(signer, cfg) - - return func(rc *requestconfig.RequestConfig) error { - return rc.Apply( - option.WithBaseURL(fmt.Sprintf("https://bedrock-runtime.%s.amazonaws.com", cfg.Region)), - option.WithMiddleware(middleware), - ) - } -} - -func bedrockMiddleware(signer *v4.Signer, cfg aws.Config) option.Middleware { - return func(r *http.Request, next option.MiddlewareNext) (res *http.Response, err error) { - var body []byte - if r.Body != nil { - body, err = io.ReadAll(r.Body) - if err != nil { - return nil, err - } - r.Body.Close() - - if !gjson.GetBytes(body, "anthropic_version").Exists() { - body, _ = sjson.SetBytes(body, "anthropic_version", DefaultVersion) - } - - if r.Method == http.MethodPost && DefaultEndpoints[r.URL.Path] { - model := gjson.GetBytes(body, "model").String() - stream := gjson.GetBytes(body, "stream").Bool() - - body, _ = sjson.DeleteBytes(body, "model") - body, _ = sjson.DeleteBytes(body, "stream") - - var path string - if stream { - path = fmt.Sprintf("/model/%s/invoke-with-response-stream", model) - } else { - path = fmt.Sprintf("/model/%s/invoke", model) - } - - r.URL.Path = path - } - - reader := bytes.NewReader(body) - r.Body = io.NopCloser(reader) - r.GetBody = func() (io.ReadCloser, error) { - _, err := reader.Seek(0, 0) - return io.NopCloser(reader), err - } - r.ContentLength = int64(len(body)) - } - - ctx := r.Context() - credentials, err := cfg.Credentials.Retrieve(ctx) - if err != nil { - return nil, err - } - - hash := sha256.Sum256(body) - err = signer.SignHTTP(ctx, credentials, r, hex.EncodeToString(hash[:]), "bedrock", cfg.Region, time.Now()) - if err != nil { - return nil, err - } - - return next(r) - } -} diff --git a/examples/tools-streaming-jsonschema/main.go b/examples/tools-streaming-jsonschema/main.go index ba745d6..7569e8c 100644 --- a/examples/tools-streaming-jsonschema/main.go +++ b/examples/tools-streaming-jsonschema/main.go @@ -5,8 +5,8 @@ import ( "encoding/json" "fmt" - "github.com/invopop/jsonschema" "github.com/anthropics/anthropic-sdk-go" + "github.com/invopop/jsonschema" ) func main() { diff --git a/go.mod b/go.mod index c3f3578..5b49773 100644 --- a/go.mod +++ b/go.mod @@ -3,50 +3,9 @@ module github.com/anthropics/anthropic-sdk-go go 1.19 require ( - cloud.google.com/go/auth v0.7.2 // indirect - cloud.google.com/go/auth/oauth2adapt v0.2.3 // indirect - cloud.google.com/go/compute/metadata v0.5.0 // indirect - github.com/aws/aws-sdk-go-v2 v1.30.3 // indirect - github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.3 // indirect - github.com/aws/aws-sdk-go-v2/config v1.27.27 // indirect - github.com/aws/aws-sdk-go-v2/credentials v1.17.27 // indirect - github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.11 // indirect - github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.15 // indirect - github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.15 // indirect - github.com/aws/aws-sdk-go-v2/internal/ini v1.8.0 // indirect - github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.11.3 // indirect - github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.11.17 // indirect - github.com/aws/aws-sdk-go-v2/service/sso v1.22.4 // indirect - github.com/aws/aws-sdk-go-v2/service/ssooidc v1.26.4 // indirect - github.com/aws/aws-sdk-go-v2/service/sts v1.30.3 // indirect - github.com/aws/smithy-go v1.20.3 // indirect - github.com/felixge/httpsnoop v1.0.4 // indirect - github.com/go-logr/logr v1.4.2 // indirect - github.com/go-logr/stdr v1.2.2 // indirect - github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect - github.com/golang/protobuf v1.5.4 // indirect - github.com/google/s2a-go v0.1.7 // indirect - github.com/google/uuid v1.6.0 // indirect - github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect + github.com/google/uuid v1.3.0 // indirect github.com/tidwall/gjson v1.14.4 // indirect github.com/tidwall/match v1.1.1 // indirect github.com/tidwall/pretty v1.2.1 // indirect github.com/tidwall/sjson v1.2.5 // indirect - go.opencensus.io v0.24.0 // indirect - go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.49.0 // indirect - go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0 // indirect - go.opentelemetry.io/otel v1.24.0 // indirect - go.opentelemetry.io/otel/metric v1.24.0 // indirect - go.opentelemetry.io/otel/trace v1.24.0 // indirect - golang.org/x/crypto v0.25.0 // indirect - golang.org/x/net v0.27.0 // indirect - golang.org/x/oauth2 v0.21.0 // indirect - golang.org/x/sync v0.7.0 // indirect - golang.org/x/sys v0.22.0 // indirect - golang.org/x/text v0.16.0 // indirect - golang.org/x/time v0.5.0 // indirect - google.golang.org/api v0.189.0 // indirect - google.golang.org/genproto/googleapis/rpc v0.0.0-20240722135656-d784300faade // indirect - google.golang.org/grpc v1.64.1 // indirect - google.golang.org/protobuf v1.34.2 // indirect ) diff --git a/go.sum b/go.sum index 5d70b97..569e555 100644 --- a/go.sum +++ b/go.sum @@ -1,96 +1,5 @@ -cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= -cloud.google.com/go v0.115.0 h1:CnFSK6Xo3lDYRoBKEcAtia6VSC837/ZkJuRduSFnr14= -cloud.google.com/go/auth v0.7.2 h1:uiha352VrCDMXg+yoBtaD0tUF4Kv9vrtrWPYXwutnDE= -cloud.google.com/go/auth v0.7.2/go.mod h1:VEc4p5NNxycWQTMQEDQF0bd6aTMb6VgYDXEwiJJQAbs= -cloud.google.com/go/auth/oauth2adapt v0.2.3 h1:MlxF+Pd3OmSudg/b1yZ5lJwoXCEaeedAguodky1PcKI= -cloud.google.com/go/auth/oauth2adapt v0.2.3/go.mod h1:tMQXOfZzFuNuUxOypHlQEXgdfX5cuhwU+ffUuXRJE8I= -cloud.google.com/go/compute/metadata v0.3.0 h1:Tz+eQXMEqDIKRsmY3cHTL6FVaynIjX2QxYC4trgAKZc= -cloud.google.com/go/compute/metadata v0.3.0/go.mod h1:zFmK7XCadkQkj6TtorcaGlCW1hT1fIilQDwofLpJ20k= -cloud.google.com/go/compute/metadata v0.5.0 h1:Zr0eK8JbFv6+Wi4ilXAR8FJ3wyNdpxHKJNPos6LTZOY= -cloud.google.com/go/compute/metadata v0.5.0/go.mod h1:aHnloV2TPI38yx4s9+wAZhHykWvVCfu7hQbF+9CWoiY= -github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= -github.com/aws/aws-sdk-go-v2 v1.30.3 h1:jUeBtG0Ih+ZIFH0F4UkmL9w3cSpaMv9tYYDbzILP8dY= -github.com/aws/aws-sdk-go-v2 v1.30.3/go.mod h1:nIQjQVp5sfpQcTc9mPSr1B0PaWK5ByX9MOoDadSN4lc= -github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.3 h1:tW1/Rkad38LA15X4UQtjXZXNKsCgkshC3EbmcUmghTg= -github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.3/go.mod h1:UbnqO+zjqk3uIt9yCACHJ9IVNhyhOCnYk8yA19SAWrM= -github.com/aws/aws-sdk-go-v2/config v1.27.27 h1:HdqgGt1OAP0HkEDDShEl0oSYa9ZZBSOmKpdpsDMdO90= -github.com/aws/aws-sdk-go-v2/config v1.27.27/go.mod h1:MVYamCg76dFNINkZFu4n4RjDixhVr51HLj4ErWzrVwg= -github.com/aws/aws-sdk-go-v2/credentials v1.17.27 h1:2raNba6gr2IfA0eqqiP2XiQ0UVOpGPgDSi0I9iAP+UI= -github.com/aws/aws-sdk-go-v2/credentials v1.17.27/go.mod h1:gniiwbGahQByxan6YjQUMcW4Aov6bLC3m+evgcoN4r4= -github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.11 h1:KreluoV8FZDEtI6Co2xuNk/UqI9iwMrOx/87PBNIKqw= -github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.11/go.mod h1:SeSUYBLsMYFoRvHE0Tjvn7kbxaUhl75CJi1sbfhMxkU= -github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.15 h1:SoNJ4RlFEQEbtDcCEt+QG56MY4fm4W8rYirAmq+/DdU= -github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.15/go.mod h1:U9ke74k1n2bf+RIgoX1SXFed1HLs51OgUSs+Ph0KJP8= -github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.15 h1:C6WHdGnTDIYETAm5iErQUiVNsclNx9qbJVPIt03B6bI= -github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.15/go.mod h1:ZQLZqhcu+JhSrA9/NXRm8SkDvsycE+JkV3WGY41e+IM= -github.com/aws/aws-sdk-go-v2/internal/ini v1.8.0 h1:hT8rVHwugYE2lEfdFE0QWVo81lF7jMrYJVDWI+f+VxU= -github.com/aws/aws-sdk-go-v2/internal/ini v1.8.0/go.mod h1:8tu/lYfQfFe6IGnaOdrpVgEL2IrrDOf6/m9RQum4NkY= -github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.11.3 h1:dT3MqvGhSoaIhRseqw2I0yH81l7wiR2vjs57O51EAm8= -github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.11.3/go.mod h1:GlAeCkHwugxdHaueRr4nhPuY+WW+gR8UjlcqzPr1SPI= -github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.11.17 h1:HGErhhrxZlQ044RiM+WdoZxp0p+EGM62y3L6pwA4olE= -github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.11.17/go.mod h1:RkZEx4l0EHYDJpWppMJ3nD9wZJAa8/0lq9aVC+r2UII= -github.com/aws/aws-sdk-go-v2/service/sso v1.22.4 h1:BXx0ZIxvrJdSgSvKTZ+yRBeSqqgPM89VPlulEcl37tM= -github.com/aws/aws-sdk-go-v2/service/sso v1.22.4/go.mod h1:ooyCOXjvJEsUw7x+ZDHeISPMhtwI3ZCB7ggFMcFfWLU= -github.com/aws/aws-sdk-go-v2/service/ssooidc v1.26.4 h1:yiwVzJW2ZxZTurVbYWA7QOrAaCYQR72t0wrSBfoesUE= -github.com/aws/aws-sdk-go-v2/service/ssooidc v1.26.4/go.mod h1:0oxfLkpz3rQ/CHlx5hB7H69YUpFiI1tql6Q6Ne+1bCw= -github.com/aws/aws-sdk-go-v2/service/sts v1.30.3 h1:ZsDKRLXGWHk8WdtyYMoGNO7bTudrvuKpDKgMVRlepGE= -github.com/aws/aws-sdk-go-v2/service/sts v1.30.3/go.mod h1:zwySh8fpFyXp9yOr/KVzxOl8SRqgf/IDw5aUt9UKFcQ= -github.com/aws/smithy-go v1.20.3 h1:ryHwveWzPV5BIof6fyDvor6V3iUL7nTfiTKXHiW05nE= -github.com/aws/smithy-go v1.20.3/go.mod h1:krry+ya/rV9RDcV/Q16kpu6ypI4K2czasz0NC3qS14E= -github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= -github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= -github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc= -github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= -github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= -github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98= -github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= -github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg= -github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U= -github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= -github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY= -github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= -github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag= -github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE= -github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= -github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= -github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da h1:oI5xCqsCo564l8iNU+DwB5epxmsaqB+rhGL0m5jtYqE= -github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= -github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= -github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= -github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= -github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8= -github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA= -github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs= -github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w= -github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0= -github.com/golang/protobuf v1.4.1/go.mod h1:U8fpvMrcmy5pZrNK1lt4xCsGvpyWQ/VVv6QDs8UjoX8= -github.com/golang/protobuf v1.4.3/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= -github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek= -github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps= -github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= -github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= -github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= -github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/google/go-cmp v0.5.3/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/google/s2a-go v0.1.7 h1:60BLSyTrOV4/haCDW4zb1guZItoSq8foHCXrAnjBo/o= -github.com/google/s2a-go v0.1.7/go.mod h1:50CgR4k1jNlWBu4UfS4AcfhVe1r6pdZPygJ3R8F0Qdw= -github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= -github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= -github.com/googleapis/enterprise-certificate-proxy v0.3.2 h1:Vie5ybvEvT75RniqhfFxPRy3Bf7vr3h0cechB90XaQs= -github.com/googleapis/enterprise-certificate-proxy v0.3.2/go.mod h1:VLSiSSBs/ksPL8kq3OBOQ6WRI2QnaFynd1DCjZ62+V0= -github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= -github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= -github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= -github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= -github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/tidwall/gjson v1.14.2/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= github.com/tidwall/gjson v1.14.4 h1:uo0p8EbA09J7RQaflQ1aBRffTR7xedD2bcIVSYxLnkM= github.com/tidwall/gjson v1.14.4/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= @@ -101,90 +10,3 @@ github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4= github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= github.com/tidwall/sjson v1.2.5 h1:kLy8mja+1c9jlljvWTlSazM7cKDRfJuR/bOJhcY5NcY= github.com/tidwall/sjson v1.2.5/go.mod h1:Fvgq9kS/6ociJEDnK0Fk1cpYF4FIW6ZF7LAe+6jwd28= -go.opencensus.io v0.24.0 h1:y73uSU6J157QMP2kn2r30vwW1A2W2WFwSCGnAVxeaD0= -go.opencensus.io v0.24.0/go.mod h1:vNK8G9p7aAivkbmorf4v+7Hgx+Zs0yY+0fOtgBfjQKo= -go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.49.0 h1:4Pp6oUg3+e/6M4C0A/3kJ2VYa++dsWVTtGgLVj5xtHg= -go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.49.0/go.mod h1:Mjt1i1INqiaoZOMGR1RIUJN+i3ChKoFRqzrRQhlkbs0= -go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0 h1:jq9TW8u3so/bN+JPT166wjOI6/vQPF6Xe7nMNIltagk= -go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0/go.mod h1:p8pYQP+m5XfbZm9fxtSKAbM6oIllS7s2AfxrChvc7iw= -go.opentelemetry.io/otel v1.24.0 h1:0LAOdjNmQeSTzGBzduGe/rU4tZhMwL5rWgtp9Ku5Jfo= -go.opentelemetry.io/otel v1.24.0/go.mod h1:W7b9Ozg4nkF5tWI5zsXkaKKDjdVjpD4oAt9Qi/MArHo= -go.opentelemetry.io/otel/metric v1.24.0 h1:6EhoGWWK28x1fbpA4tYTOWBkPefTDQnb8WSGXlc88kI= -go.opentelemetry.io/otel/metric v1.24.0/go.mod h1:VYhLe1rFfxuTXLgj4CBiyz+9WYBA8pNGJgDcSFRKBco= -go.opentelemetry.io/otel/trace v1.24.0 h1:CsKnnL4dUAr/0llH9FKuc698G04IrpWV0MQA/Y1YELI= -go.opentelemetry.io/otel/trace v1.24.0/go.mod h1:HPc3Xr/cOApsBI154IU0OI0HJexz+aw5uPdbs3UCjNU= -golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= -golang.org/x/crypto v0.25.0 h1:ypSNr+bnYL2YhwoMt2zPxHFmbAN1KZs/njMG3hxUp30= -golang.org/x/crypto v0.25.0/go.mod h1:T+wALwcMOSE0kXgUAnPAHqTLW+XHgcELELW8VaDgm/M= -golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= -golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= -golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= -golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= -golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= -golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= -golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= -golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= -golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= -golang.org/x/net v0.0.0-20201110031124-69a78807bb2b/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= -golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys= -golang.org/x/net v0.27.0/go.mod h1:dDi0PyhWNoiUOrAS8uXv/vnScO4wnHQO4mj9fn/RytE= -golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= -golang.org/x/oauth2 v0.21.0 h1:tsimM75w1tF/uws5rbeHzIWxEqElMehnc+iW793zsZs= -golang.org/x/oauth2 v0.21.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI= -golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M= -golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= -golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI= -golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= -golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4= -golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI= -golang.org/x/time v0.5.0 h1:o7cqy6amK/52YcAKIPlM3a+Fpj35zvRj2TP+e1xFSfk= -golang.org/x/time v0.5.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM= -golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= -golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= -golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= -golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= -golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= -golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -google.golang.org/api v0.189.0 h1:equMo30LypAkdkLMBqfeIqtyAnlyig1JSZArl4XPwdI= -google.golang.org/api v0.189.0/go.mod h1:FLWGJKb0hb+pU2j+rJqwbnsF+ym+fQs73rbJ+KAUgy8= -google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= -google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= -google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= -google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc= -google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo= -google.golang.org/genproto v0.0.0-20240722135656-d784300faade h1:lKFsS7wpngDgSCeFn7MoLy+wBDQZ1UQIJD4UNM1Qvkg= -google.golang.org/genproto/googleapis/rpc v0.0.0-20240722135656-d784300faade h1:oCRSWfwGXQsqlVdErcyTt4A93Y8fo0/9D4b1gnI++qo= -google.golang.org/genproto/googleapis/rpc v0.0.0-20240722135656-d784300faade/go.mod h1:Ue6ibwXGpU+dqIcODieyLOcgj7z8+IcskoNIgZxtrFY= -google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= -google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg= -google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQciAY= -google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= -google.golang.org/grpc v1.33.2/go.mod h1:JMHMWHQWaTccqQQlmk3MJZS+GWXOdAesneDmEnv2fbc= -google.golang.org/grpc v1.64.1 h1:LKtvyfbX3UGVPFcGqJ9ItpVWW6oN/2XqTxfAnwRRXiA= -google.golang.org/grpc v1.64.1/go.mod h1:hiQF4LFZelK2WKaP6W0L92zGHtiQdZxk8CrSdvyjeP0= -google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= -google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= -google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= -google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE= -google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo= -google.golang.org/protobuf v1.22.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= -google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= -google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= -google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c= -google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg= -google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= -honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= diff --git a/internal/requestconfig/requestconfig.go b/internal/requestconfig/requestconfig.go index bd78b51..b48ff1e 100644 --- a/internal/requestconfig/requestconfig.go +++ b/internal/requestconfig/requestconfig.go @@ -332,7 +332,6 @@ func (cfg *RequestConfig) Execute() (err error) { handler = applyMiddleware(cfg.Middlewares[i], handler) } - var req *http.Request var res *http.Response for retryCount := 0; retryCount <= cfg.MaxRetries; retryCount += 1 { ctx := cfg.Request.Context() @@ -342,8 +341,7 @@ func (cfg *RequestConfig) Execute() (err error) { defer cancel() } - req = cfg.Request.Clone(ctx) - res, err = handler(req) + res, err = handler(cfg.Request.Clone(ctx)) if ctx != nil && ctx.Err() != nil { return ctx.Err() } @@ -395,7 +393,7 @@ func (cfg *RequestConfig) Execute() (err error) { res.Body = io.NopCloser(bytes.NewBuffer(contents)) // Load the contents into the error format if it is provided. - aerr := apierror.Error{Request: req, Response: res, StatusCode: res.StatusCode} + aerr := apierror.Error{Request: cfg.Request, Response: res, StatusCode: res.StatusCode} err = aerr.UnmarshalJSON(contents) if err != nil { return err @@ -441,7 +439,7 @@ func (cfg *RequestConfig) Execute() (err error) { err = json.NewDecoder(bytes.NewReader(contents)).Decode(cfg.ResponseBodyInto) if err != nil { - err = fmt.Errorf("error parsing response json: %w", err) + return fmt.Errorf("error parsing response json: %w", err) } return nil diff --git a/internal/version.go b/internal/version.go index 1e49ee4..4ff68e4 100644 --- a/internal/version.go +++ b/internal/version.go @@ -2,4 +2,4 @@ package internal -const PackageVersion = "0.0.1-alpha.0" +const PackageVersion = "0.0.1-alpha.0" // x-release-please-version diff --git a/message.go b/message.go index 4863f83..c0be88e 100644 --- a/message.go +++ b/message.go @@ -4,8 +4,6 @@ package anthropic import ( "context" - "encoding/json" - "fmt" "net/http" "reflect" @@ -74,11 +72,12 @@ func (r *MessageService) NewStreaming(ctx context.Context, body MessageNewParams } type ContentBlock struct { - Type ContentBlockType `json:"type,required"` - Text string `json:"text"` - ID string `json:"id"` - Name string `json:"name"` - Input json.RawMessage `json:"input,required"` + Type ContentBlockType `json:"type,required"` + Text string `json:"text"` + ID string `json:"id"` + Name string `json:"name"` + // This field can have the runtime type of [interface{}]. + Input interface{} `json:"input,required"` JSON contentBlockJSON `json:"-"` union ContentBlockUnion } @@ -157,17 +156,6 @@ type ImageBlockParam struct { Type param.Field[ImageBlockParamType] `json:"type,required"` } -func NewImageBlockBase64(mediaType string, encodedData string) ImageBlockParam { - return ImageBlockParam{ - Type: F(ImageBlockParamTypeImage), - Source: F(ImageBlockParamSource{ - Type: F(ImageBlockParamSourceTypeBase64), - Data: F(encodedData), - MediaType: F(ImageBlockParamSourceMediaType(mediaType)), - }), - } -} - func (r ImageBlockParam) MarshalJSON() (data []byte, err error) { return apijson.MarshalRoot(r) } @@ -269,65 +257,6 @@ func (r InputJSONDeltaType) IsKnown() bool { return false } -// Accumulate builds up the Message incrementally from a MessageStreamEvent. The Message then can be used as -// any other Message, except with the caveat that the Message.JSON field which normally can be used to inspect -// the JSON sent over the network may not be populated fully. -// -// message := anthropic.Message{} -// for stream.Next() { -// event := stream.Current() -// message.Accumulate(event) -// } -func (a *Message) Accumulate(event MessageStreamEvent) error { - if a == nil { - *a = Message{} - } - - switch event := event.AsUnion().(type) { - case MessageStartEvent: - *a = event.Message - - case MessageDeltaEvent: - a.StopReason = MessageStopReason(event.Delta.StopReason) - a.JSON.StopReason = event.Delta.JSON.StopReason - a.StopSequence = event.Delta.StopSequence - a.JSON.StopSequence = event.Delta.JSON.StopSequence - a.Usage.OutputTokens = event.Usage.OutputTokens - a.Usage.JSON.OutputTokens = event.Usage.JSON.OutputTokens - - case MessageStopEvent: - - case ContentBlockStartEvent: - a.Content = append(a.Content, ContentBlock{}) - err := a.Content[len(a.Content)-1].UnmarshalJSON([]byte(event.ContentBlock.JSON.RawJSON())) - if err != nil { - return err - } - - case ContentBlockDeltaEvent: - if len(a.Content) == 0 { - return fmt.Errorf("received event of type %s but there was no content block", event.Type) - } - switch delta := event.Delta.AsUnion().(type) { - case TextDelta: - a.Content[len(a.Content)-1].Text += delta.Text - case InputJSONDelta: - cb := &a.Content[len(a.Content)-1] - if string(cb.Input) == "{}" { - cb.Input = json.RawMessage{} - } - cb.Input = append(cb.Input, []byte(delta.PartialJSON)...) - } - - case ContentBlockStopEvent: - if len(a.Content) == 0 { - return fmt.Errorf("received event of type %s but there was no content block", event.Type) - } - } - - return nil -} - type Message struct { // Unique object identifier. // @@ -427,39 +356,6 @@ type messageJSON struct { ExtraFields map[string]apijson.Field } -func (r *Message) ToParam() MessageParam { - content := []MessageParamContentUnion{} - - for _, block := range r.Content { - content = append(content, MessageParamContent{ - Type: F(MessageParamContentType(block.Type)), - ID: param.Field[string]{ - Value: block.ID, - Present: !block.JSON.ID.IsNull(), - }, - Text: param.Field[string]{ - Value: block.Text, - Present: !block.JSON.Text.IsNull(), - }, - Name: param.Field[string]{ - Value: block.Name, - Present: !block.JSON.Name.IsNull(), - }, - Input: param.Field[interface{}]{ - Value: block.Input, - Present: len(block.Input) > 0 && !block.JSON.Input.IsNull(), - }, - }) - } - - message := MessageParam{ - Role: F(MessageParamRole(r.Role)), - Content: F(content), - } - - return message -} - func (r *Message) UnmarshalJSON(data []byte) (err error) { return apijson.UnmarshalRoot(data, r) } @@ -557,20 +453,6 @@ type MessageParam struct { Role param.Field[MessageParamRole] `json:"role,required"` } -func NewUserMessage(blocks ...MessageParamContentUnion) MessageParam { - return MessageParam{ - Role: F(MessageParamRoleUser), - Content: F(blocks), - } -} - -func NewAssistantMessage(blocks ...MessageParamContentUnion) MessageParam { - return MessageParam{ - Role: F(MessageParamRoleAssistant), - Content: F(blocks), - } -} - func (r MessageParam) MarshalJSON() (data []byte, err error) { return apijson.MarshalRoot(r) } @@ -792,11 +674,12 @@ func (r contentBlockStartEventJSON) RawJSON() string { func (r ContentBlockStartEvent) implementsMessageStreamEvent() {} type ContentBlockStartEventContentBlock struct { - Type ContentBlockStartEventContentBlockType `json:"type,required"` - Text string `json:"text"` - ID string `json:"id"` - Name string `json:"name"` - Input json.RawMessage `json:"input,required"` + Type ContentBlockStartEventContentBlockType `json:"type,required"` + Text string `json:"text"` + ID string `json:"id"` + Name string `json:"name"` + // This field can have the runtime type of [interface{}]. + Input interface{} `json:"input,required"` JSON contentBlockStartEventContentBlockJSON `json:"-"` union ContentBlockStartEventContentBlockUnion } @@ -1262,13 +1145,6 @@ type TextBlockParam struct { Type param.Field[TextBlockParamType] `json:"type,required"` } -func NewTextBlock(text string) TextBlockParam { - return TextBlockParam{ - Text: F(text), - Type: F(TextBlockParamTypeText), - } -} - func (r TextBlockParam) MarshalJSON() (data []byte, err error) { return apijson.MarshalRoot(r) } @@ -1334,8 +1210,8 @@ type ToolParam struct { // // This defines the shape of the `input` that your tool accepts and that the model // will produce. - InputSchema param.Field[interface{}] `json:"input_schema,required"` - Name param.Field[string] `json:"name,required"` + InputSchema param.Field[ToolInputSchemaParam] `json:"input_schema,required"` + Name param.Field[string] `json:"name,required"` // Description of what this tool does. // // Tool descriptions should be as detailed as possible. The more information that @@ -1349,6 +1225,34 @@ func (r ToolParam) MarshalJSON() (data []byte, err error) { return apijson.MarshalRoot(r) } +// [JSON schema](https://json-schema.org/) for this tool's input. +// +// This defines the shape of the `input` that your tool accepts and that the model +// will produce. +type ToolInputSchemaParam struct { + Type param.Field[ToolInputSchemaType] `json:"type,required"` + Properties param.Field[interface{}] `json:"properties"` + ExtraFields map[string]interface{} `json:"-,extras"` +} + +func (r ToolInputSchemaParam) MarshalJSON() (data []byte, err error) { + return apijson.MarshalRoot(r) +} + +type ToolInputSchemaType string + +const ( + ToolInputSchemaTypeObject ToolInputSchemaType = "object" +) + +func (r ToolInputSchemaType) IsKnown() bool { + switch r { + case ToolInputSchemaTypeObject: + return true + } + return false +} + type ToolResultBlockParam struct { ToolUseID param.Field[string] `json:"tool_use_id,required"` Type param.Field[ToolResultBlockParamType] `json:"type,required"` @@ -1356,15 +1260,6 @@ type ToolResultBlockParam struct { IsError param.Field[bool] `json:"is_error"` } -func NewToolResultBlock(toolUseID string, content string, isError bool) ToolResultBlockParam { - return ToolResultBlockParam{ - Type: F(ToolResultBlockParamTypeToolResult), - ToolUseID: F(toolUseID), - Content: F([]ToolResultBlockParamContentUnion{NewTextBlock(content)}), - IsError: F(isError), - } -} - func (r ToolResultBlockParam) MarshalJSON() (data []byte, err error) { return apijson.MarshalRoot(r) } @@ -1419,7 +1314,7 @@ func (r ToolResultBlockParamContentType) IsKnown() bool { type ToolUseBlock struct { ID string `json:"id,required"` - Input json.RawMessage `json:"input,required"` + Input interface{} `json:"input,required"` Name string `json:"name,required"` Type ToolUseBlockType `json:"type,required"` JSON toolUseBlockJSON `json:"-"` @@ -1468,15 +1363,6 @@ type ToolUseBlockParam struct { Type param.Field[ToolUseBlockParamType] `json:"type,required"` } -func NewToolUseBlockParam(id string, name string, input interface{}) ToolUseBlockParam { - return ToolUseBlockParam{ - ID: F(id), - Input: F(input), - Name: F(name), - Type: F(ToolUseBlockParamTypeToolUse), - } -} - func (r ToolUseBlockParam) MarshalJSON() (data []byte, err error) { return apijson.MarshalRoot(r) } diff --git a/message_test.go b/message_test.go index 72b2d63..0cb7067 100644 --- a/message_test.go +++ b/message_test.go @@ -47,9 +47,9 @@ func TestMessageNewWithOptionalParams(t *testing.T) { Tools: anthropic.F([]anthropic.ToolParam{{ Description: anthropic.F("Get the current weather in a given location"), Name: anthropic.F("x"), - InputSchema: anthropic.F[interface{}](map[string]interface{}{ - "type": "object", - "properties": map[string]interface{}{ + InputSchema: anthropic.F(anthropic.ToolInputSchemaParam{ + Type: anthropic.F(anthropic.ToolInputSchemaTypeObject), + Properties: anthropic.F[any](map[string]interface{}{ "location": map[string]interface{}{ "description": "The city and state, e.g. San Francisco, CA", "type": "string", @@ -58,7 +58,39 @@ func TestMessageNewWithOptionalParams(t *testing.T) { "description": "Unit for the output - one of (celsius, fahrenheit)", "type": "string", }, - }, + }), + }), + }, { + Description: anthropic.F("Get the current weather in a given location"), + Name: anthropic.F("x"), + InputSchema: anthropic.F(anthropic.ToolInputSchemaParam{ + Type: anthropic.F(anthropic.ToolInputSchemaTypeObject), + Properties: anthropic.F[any](map[string]interface{}{ + "location": map[string]interface{}{ + "description": "The city and state, e.g. San Francisco, CA", + "type": "string", + }, + "unit": map[string]interface{}{ + "description": "Unit for the output - one of (celsius, fahrenheit)", + "type": "string", + }, + }), + }), + }, { + Description: anthropic.F("Get the current weather in a given location"), + Name: anthropic.F("x"), + InputSchema: anthropic.F(anthropic.ToolInputSchemaParam{ + Type: anthropic.F(anthropic.ToolInputSchemaTypeObject), + Properties: anthropic.F[any](map[string]interface{}{ + "location": map[string]interface{}{ + "description": "The city and state, e.g. San Francisco, CA", + "type": "string", + }, + "unit": map[string]interface{}{ + "description": "Unit for the output - one of (celsius, fahrenheit)", + "type": "string", + }, + }), }), }}), TopK: anthropic.F(int64(5)), diff --git a/vertex/vertex.go b/vertex/vertex.go deleted file mode 100644 index baeb7c3..0000000 --- a/vertex/vertex.go +++ /dev/null @@ -1,99 +0,0 @@ -package vertex - -import ( - "bytes" - "context" - "fmt" - "io" - "net/http" - - "golang.org/x/oauth2/google" - "google.golang.org/api/option" - "google.golang.org/api/transport" - - "github.com/anthropics/anthropic-sdk-go/internal/requestconfig" - sdkoption "github.com/anthropics/anthropic-sdk-go/option" - "github.com/tidwall/gjson" - "github.com/tidwall/sjson" -) - -const DefaultVersion = "vertex-2023-10-16" - -// WithGoogleAuth returns a request option which loads the [Application Default Credentials] for Google Vertex AI and registers -// middleware that intercepts requests to the Messages API. -// -// If you already have a [*google.Credentials], it is recommended that you instead call [WithCredentials] directly. -// -// [Application Default Credentials]: https://cloud.google.com/docs/authentication/application-default-credentials -func WithGoogleAuth(ctx context.Context, region string, projectID string, scopes ...string) sdkoption.RequestOption { - if region == "" { - panic("region must be provided") - } - creds, err := google.FindDefaultCredentials(ctx, scopes...) - if err != nil { - panic(fmt.Errorf("failed to find default credentials: %v", err)) - } - return WithCredentials(ctx, region, projectID, creds) -} - -// WithCredentials returns a request option which uses the provided credentials for Google Vertex AI and registers middleware that -// intercepts request to the Messages API. -func WithCredentials(ctx context.Context, region string, projectID string, creds *google.Credentials) sdkoption.RequestOption { - client, _, err := transport.NewHTTPClient(ctx, option.WithTokenSource(creds.TokenSource)) - if err != nil { - panic(fmt.Errorf("failed to create HTTP client: %v", err)) - } - middleware := vertexMiddleware(region, projectID) - - return func(rc *requestconfig.RequestConfig) error { - return rc.Apply( - sdkoption.WithBaseURL(fmt.Sprintf("https://%s-aiplatform.googleapis.com/v1", region)), - sdkoption.WithMiddleware(middleware), - sdkoption.WithHTTPClient(client), - ) - } -} - -func vertexMiddleware(region, projectID string) sdkoption.Middleware { - return func(r *http.Request, next sdkoption.MiddlewareNext) (*http.Response, error) { - if r.Body != nil { - body, err := io.ReadAll(r.Body) - if err != nil { - return nil, err - } - r.Body.Close() - - if !gjson.GetBytes(body, "anthropic_version").Exists() { - body, _ = sjson.SetBytes(body, "anthropic_version", DefaultVersion) - } - - if r.URL.Path == "/v1/messages" && r.Method == http.MethodPost { - if projectID == "" { - return nil, fmt.Errorf("no projectId was given and it could not be resolved from credentials") - } - - model := gjson.GetBytes(body, "model").String() - stream := gjson.GetBytes(body, "stream").Bool() - - body, _ = sjson.DeleteBytes(body, "model") - - specifier := "rawPredict" - if stream { - specifier = "streamRawPredict" - } - - r.URL.Path = fmt.Sprintf("/v1/projects/%s/locations/%s/publishers/anthropic/models/%s:%s", projectID, region, model, specifier) - } - - reader := bytes.NewReader(body) - r.Body = io.NopCloser(reader) - r.GetBody = func() (io.ReadCloser, error) { - _, err := reader.Seek(0, 0) - return io.NopCloser(reader), err - } - r.ContentLength = int64(len(body)) - } - - return next(r) - } -} From 106c40466382daaa403e7f472647248e14d939d7 Mon Sep 17 00:00:00 2001 From: Young-Jin Park Date: Sun, 4 Aug 2024 23:11:43 -0400 Subject: [PATCH 03/13] chore: add back custom code This reverts commit ad38619dc8095e4022a958f74c7f3285d5d199e1. --- README.md | 84 ++++++-- bedrock/bedrock.go | 210 ++++++++++++++++++++ examples/tools-streaming-jsonschema/main.go | 2 +- go.mod | 43 +++- go.sum | 178 +++++++++++++++++ internal/requestconfig/requestconfig.go | 8 +- internal/version.go | 2 +- message.go | 200 +++++++++++++++---- message_test.go | 40 +--- vertex/vertex.go | 99 +++++++++ 10 files changed, 765 insertions(+), 101 deletions(-) create mode 100644 bedrock/bedrock.go create mode 100644 vertex/vertex.go diff --git a/README.md b/README.md index 6bef330..883643a 100644 --- a/README.md +++ b/README.md @@ -52,10 +52,9 @@ func main() { ) message, err := client.Messages.New(context.TODO(), anthropic.MessageNewParams{ MaxTokens: anthropic.F(int64(1024)), - Messages: anthropic.F([]anthropic.MessageParam{{ - Role: anthropic.F(anthropic.MessageParamRoleUser), - Content: anthropic.F([]anthropic.MessageParamContentUnion{anthropic.TextBlockParam{Type: anthropic.F(anthropic.TextBlockParamTypeText), Text: anthropic.F("What is a quaternion?")}}), - }}), + Messages: anthropic.F([]anthropic.MessageParam{ + anthropic.NewUserMessage(anthropic.NewTextBlock("What is the weather in SF?")), + }), Model: anthropic.F(anthropic.ModelClaude_3_5_Sonnet_20240620), }) if err != nil { @@ -181,10 +180,9 @@ To handle errors, we recommend that you use the `errors.As` pattern: ```go _, err := client.Messages.New(context.TODO(), anthropic.MessageNewParams{ MaxTokens: anthropic.F(int64(1024)), - Messages: anthropic.F([]anthropic.MessageParam{{ - Role: anthropic.F(anthropic.MessageParamRoleUser), - Content: anthropic.F([]anthropic.MessageParamContentUnion{anthropic.TextBlockParam{Type: anthropic.F(anthropic.TextBlockParamTypeText), Text: anthropic.F("What is a quaternion?")}}), - }}), + Messages: anthropic.F([]anthropic.MessageParam{ + anthropic.NewUserMessage(anthropic.NewTextBlock("What is the weather in SF?")), + }), Model: anthropic.F(anthropic.ModelClaude_3_5_Sonnet_20240620), }) if err != nil { @@ -215,10 +213,9 @@ client.Messages.New( ctx, anthropic.MessageNewParams{ MaxTokens: anthropic.F(int64(1024)), - Messages: anthropic.F([]anthropic.MessageParam{{ - Role: anthropic.F(anthropic.MessageParamRoleUser), - Content: anthropic.F([]anthropic.MessageParamContentUnion{anthropic.TextBlockParam{Type: anthropic.F(anthropic.TextBlockParamTypeText), Text: anthropic.F("What is a quaternion?")}}), - }}), + Messages: anthropic.F([]anthropic.MessageParam{ + anthropic.NewUserMessage(anthropic.NewTextBlock("What is the weather in SF?")), + }), Model: anthropic.F(anthropic.ModelClaude_3_5_Sonnet_20240620), }, // This sets the per-retry timeout @@ -258,10 +255,9 @@ client.Messages.New( context.TODO(), anthropic.MessageNewParams{ MaxTokens: anthropic.F(int64(1024)), - Messages: anthropic.F([]anthropic.MessageParam{{ - Role: anthropic.F(anthropic.MessageParamRoleUser), - Content: anthropic.F([]anthropic.MessageParamContentUnion{anthropic.TextBlockParam{Type: anthropic.F(anthropic.TextBlockParamTypeText), Text: anthropic.F("What is a quaternion?")}}), - }}), + Messages: anthropic.F([]anthropic.MessageParam{ + anthropic.NewUserMessage(anthropic.NewTextBlock("What is the weather in SF?")), + }), Model: anthropic.F(anthropic.ModelClaude_3_5_Sonnet_20240620), }, option.WithMaxRetries(5), @@ -354,6 +350,62 @@ You may also replace the default `http.Client` with accepted (this overwrites any previous client) and receives requests after any middleware has been applied. +## Amazon Bedrock + +To use this library with [Amazon Bedrock](https://aws.amazon.com/bedrock/claude/), +use the bedrock request option `bedrock.WithLoadDefaultConfig(…)` which reads the +[default config](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html). + +Importing the `bedrock` library also globally registers a decoder for `application/vnd.amazon.eventstream` for +streaming. + +```go +package main + +import ( + "github.com/anthropics/anthropic-sdk-go" + "github.com/anthropics/anthropic-sdk-go/bedrock" +) + +func main() { + client := anthropic.NewClient( + bedrock.WithLoadDefaultConfig(context.Background()), + ) +} +``` + +If you already have an `aws.Config`, you can also use it directly with `bedrock.WithConfig(cfg)`. + +Read more about Anthropic and Amazon Bedrock [here](https://docs.anthropic.com/en/api/claude-on-amazon-bedrock). + +## Google Vertex AI + +To use this library with [Google Vertex AI](https://cloud.google.com/vertex-ai/generative-ai/docs/partner-models/use-claude), +use the request option `vertex.WithGoogleAuth(…)` which reads the +[Application Default Credentials](https://cloud.google.com/docs/authentication/application-default-credentials). + +```go +package main + +import ( + "context" + + "github.com/anthropics/anthropic-sdk-go" + "github.com/anthropics/anthropic-sdk-go/vertex" +) + +func main() { + client := anthropic.NewClient( + vertex.WithGoogleAuth(context.Background(), "us-central1", "stainless-399616"), + ) +} +``` + +If you already have `*google.Credentials`, you can also use it directly with +`vertex.WithCredentials(ctx, region, projectId, creds)`. + +Read more about Anthropic and Google Vertex [here](https://docs.anthropic.com/en/api/claude-on-vertex-ai). + ## Semantic versioning This package generally follows [SemVer](https://semver.org/spec/v2.0.0.html) conventions, though certain backwards-incompatible changes may be released as minor versions: diff --git a/bedrock/bedrock.go b/bedrock/bedrock.go new file mode 100644 index 0000000..a4729e5 --- /dev/null +++ b/bedrock/bedrock.go @@ -0,0 +1,210 @@ +package bedrock + +import ( + "bytes" + "context" + "crypto/sha256" + "encoding/base64" + "encoding/hex" + "encoding/json" + "fmt" + "io" + "net/http" + "time" + + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream" + "github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream/eventstreamapi" + v4 "github.com/aws/aws-sdk-go-v2/aws/signer/v4" + "github.com/aws/aws-sdk-go-v2/config" + "github.com/tidwall/gjson" + "github.com/tidwall/sjson" + + "github.com/anthropics/anthropic-sdk-go/internal/requestconfig" + "github.com/anthropics/anthropic-sdk-go/option" + "github.com/anthropics/anthropic-sdk-go/packages/ssestream" +) + +const DefaultVersion = "bedrock-2023-05-31" + +var DefaultEndpoints = map[string]bool{ + "/v1/complete": true, + "/v1/messages": true, +} + +type eventstreamChunk struct { + Bytes string `json:"bytes"` + P string `json:"p"` +} + +type eventstreamDecoder struct { + eventstream.Decoder + + rc io.ReadCloser + evt ssestream.Event + err error +} + +func (e *eventstreamDecoder) Close() error { + return nil +} + +func (e *eventstreamDecoder) Err() error { + return nil +} + +func (e *eventstreamDecoder) Next() bool { + if e.err != nil { + return false + } + + msg, err := e.Decoder.Decode(e.rc, nil) + if err != nil { + e.err = err + return false + } + + messageType := msg.Headers.Get(eventstreamapi.MessageTypeHeader) + if messageType == nil { + e.err = fmt.Errorf("%s event header not present", eventstreamapi.MessageTypeHeader) + return false + } + + switch messageType.String() { + case eventstreamapi.EventMessageType: + eventType := msg.Headers.Get(eventstreamapi.EventTypeHeader) + if eventType == nil { + e.err = fmt.Errorf("%s event header not present", eventstreamapi.EventTypeHeader) + return false + } + + if eventType.String() == "chunk" { + chunk := eventstreamChunk{} + err = json.Unmarshal(msg.Payload, &chunk) + if err != nil { + e.err = err + return false + } + decoded, err := base64.StdEncoding.DecodeString(chunk.Bytes) + if err != nil { + e.err = err + return false + } + e.evt = ssestream.Event{ + Type: gjson.GetBytes(decoded, "type").String(), + Data: decoded, + } + } + + case eventstreamapi.ExceptionMessageType: + case eventstreamapi.ErrorMessageType: + errorCode := "UnknownError" + errorMessage := errorCode + if header := msg.Headers.Get(eventstreamapi.ErrorCodeHeader); header != nil { + errorCode = header.String() + } + if header := msg.Headers.Get(eventstreamapi.ErrorMessageHeader); header != nil { + errorMessage = header.String() + } + e.err = fmt.Errorf("received error or exception %s: %s", errorCode, errorMessage) + return false + } + + return true +} + +func (e *eventstreamDecoder) Event() ssestream.Event { + return e.evt +} + +var ( + _ ssestream.Decoder = &eventstreamDecoder{} +) + +func init() { + ssestream.RegisterDecoder("application/vnd.amazon.eventstream", func(rc io.ReadCloser) ssestream.Decoder { + return &eventstreamDecoder{rc: rc} + }) +} + +// WithLoadDefaultConfig returns a request option which loads the default config for Amazon and registers +// middleware that intercepts request to the Messages API so that this SDK can be used with Amazon Bedrock. +// +// If you already have an [aws.Config], it is recommended that you instead call [WithConfig] directly. +func WithLoadDefaultConfig(ctx context.Context, optFns ...func(*config.LoadOptions) error) option.RequestOption { + cfg, err := config.LoadDefaultConfig(ctx, optFns...) + if err != nil { + panic(err) + } + return WithConfig(cfg) +} + +// WithConfig returns a request option which uses the provided config and registers middleware that +// intercepts request to the Messages API so that this SDK can be used with Amazon Bedrock. +func WithConfig(cfg aws.Config) option.RequestOption { + signer := v4.NewSigner() + middleware := bedrockMiddleware(signer, cfg) + + return func(rc *requestconfig.RequestConfig) error { + return rc.Apply( + option.WithBaseURL(fmt.Sprintf("https://bedrock-runtime.%s.amazonaws.com", cfg.Region)), + option.WithMiddleware(middleware), + ) + } +} + +func bedrockMiddleware(signer *v4.Signer, cfg aws.Config) option.Middleware { + return func(r *http.Request, next option.MiddlewareNext) (res *http.Response, err error) { + var body []byte + if r.Body != nil { + body, err = io.ReadAll(r.Body) + if err != nil { + return nil, err + } + r.Body.Close() + + if !gjson.GetBytes(body, "anthropic_version").Exists() { + body, _ = sjson.SetBytes(body, "anthropic_version", DefaultVersion) + } + + if r.Method == http.MethodPost && DefaultEndpoints[r.URL.Path] { + model := gjson.GetBytes(body, "model").String() + stream := gjson.GetBytes(body, "stream").Bool() + + body, _ = sjson.DeleteBytes(body, "model") + body, _ = sjson.DeleteBytes(body, "stream") + + var path string + if stream { + path = fmt.Sprintf("/model/%s/invoke-with-response-stream", model) + } else { + path = fmt.Sprintf("/model/%s/invoke", model) + } + + r.URL.Path = path + } + + reader := bytes.NewReader(body) + r.Body = io.NopCloser(reader) + r.GetBody = func() (io.ReadCloser, error) { + _, err := reader.Seek(0, 0) + return io.NopCloser(reader), err + } + r.ContentLength = int64(len(body)) + } + + ctx := r.Context() + credentials, err := cfg.Credentials.Retrieve(ctx) + if err != nil { + return nil, err + } + + hash := sha256.Sum256(body) + err = signer.SignHTTP(ctx, credentials, r, hex.EncodeToString(hash[:]), "bedrock", cfg.Region, time.Now()) + if err != nil { + return nil, err + } + + return next(r) + } +} diff --git a/examples/tools-streaming-jsonschema/main.go b/examples/tools-streaming-jsonschema/main.go index 7569e8c..ba745d6 100644 --- a/examples/tools-streaming-jsonschema/main.go +++ b/examples/tools-streaming-jsonschema/main.go @@ -5,8 +5,8 @@ import ( "encoding/json" "fmt" - "github.com/anthropics/anthropic-sdk-go" "github.com/invopop/jsonschema" + "github.com/anthropics/anthropic-sdk-go" ) func main() { diff --git a/go.mod b/go.mod index 5b49773..c3f3578 100644 --- a/go.mod +++ b/go.mod @@ -3,9 +3,50 @@ module github.com/anthropics/anthropic-sdk-go go 1.19 require ( - github.com/google/uuid v1.3.0 // indirect + cloud.google.com/go/auth v0.7.2 // indirect + cloud.google.com/go/auth/oauth2adapt v0.2.3 // indirect + cloud.google.com/go/compute/metadata v0.5.0 // indirect + github.com/aws/aws-sdk-go-v2 v1.30.3 // indirect + github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.3 // indirect + github.com/aws/aws-sdk-go-v2/config v1.27.27 // indirect + github.com/aws/aws-sdk-go-v2/credentials v1.17.27 // indirect + github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.11 // indirect + github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.15 // indirect + github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.15 // indirect + github.com/aws/aws-sdk-go-v2/internal/ini v1.8.0 // indirect + github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.11.3 // indirect + github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.11.17 // indirect + github.com/aws/aws-sdk-go-v2/service/sso v1.22.4 // indirect + github.com/aws/aws-sdk-go-v2/service/ssooidc v1.26.4 // indirect + github.com/aws/aws-sdk-go-v2/service/sts v1.30.3 // indirect + github.com/aws/smithy-go v1.20.3 // indirect + github.com/felixge/httpsnoop v1.0.4 // indirect + github.com/go-logr/logr v1.4.2 // indirect + github.com/go-logr/stdr v1.2.2 // indirect + github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect + github.com/golang/protobuf v1.5.4 // indirect + github.com/google/s2a-go v0.1.7 // indirect + github.com/google/uuid v1.6.0 // indirect + github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect github.com/tidwall/gjson v1.14.4 // indirect github.com/tidwall/match v1.1.1 // indirect github.com/tidwall/pretty v1.2.1 // indirect github.com/tidwall/sjson v1.2.5 // indirect + go.opencensus.io v0.24.0 // indirect + go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.49.0 // indirect + go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0 // indirect + go.opentelemetry.io/otel v1.24.0 // indirect + go.opentelemetry.io/otel/metric v1.24.0 // indirect + go.opentelemetry.io/otel/trace v1.24.0 // indirect + golang.org/x/crypto v0.25.0 // indirect + golang.org/x/net v0.27.0 // indirect + golang.org/x/oauth2 v0.21.0 // indirect + golang.org/x/sync v0.7.0 // indirect + golang.org/x/sys v0.22.0 // indirect + golang.org/x/text v0.16.0 // indirect + golang.org/x/time v0.5.0 // indirect + google.golang.org/api v0.189.0 // indirect + google.golang.org/genproto/googleapis/rpc v0.0.0-20240722135656-d784300faade // indirect + google.golang.org/grpc v1.64.1 // indirect + google.golang.org/protobuf v1.34.2 // indirect ) diff --git a/go.sum b/go.sum index 569e555..5d70b97 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,96 @@ +cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= +cloud.google.com/go v0.115.0 h1:CnFSK6Xo3lDYRoBKEcAtia6VSC837/ZkJuRduSFnr14= +cloud.google.com/go/auth v0.7.2 h1:uiha352VrCDMXg+yoBtaD0tUF4Kv9vrtrWPYXwutnDE= +cloud.google.com/go/auth v0.7.2/go.mod h1:VEc4p5NNxycWQTMQEDQF0bd6aTMb6VgYDXEwiJJQAbs= +cloud.google.com/go/auth/oauth2adapt v0.2.3 h1:MlxF+Pd3OmSudg/b1yZ5lJwoXCEaeedAguodky1PcKI= +cloud.google.com/go/auth/oauth2adapt v0.2.3/go.mod h1:tMQXOfZzFuNuUxOypHlQEXgdfX5cuhwU+ffUuXRJE8I= +cloud.google.com/go/compute/metadata v0.3.0 h1:Tz+eQXMEqDIKRsmY3cHTL6FVaynIjX2QxYC4trgAKZc= +cloud.google.com/go/compute/metadata v0.3.0/go.mod h1:zFmK7XCadkQkj6TtorcaGlCW1hT1fIilQDwofLpJ20k= +cloud.google.com/go/compute/metadata v0.5.0 h1:Zr0eK8JbFv6+Wi4ilXAR8FJ3wyNdpxHKJNPos6LTZOY= +cloud.google.com/go/compute/metadata v0.5.0/go.mod h1:aHnloV2TPI38yx4s9+wAZhHykWvVCfu7hQbF+9CWoiY= +github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/aws/aws-sdk-go-v2 v1.30.3 h1:jUeBtG0Ih+ZIFH0F4UkmL9w3cSpaMv9tYYDbzILP8dY= +github.com/aws/aws-sdk-go-v2 v1.30.3/go.mod h1:nIQjQVp5sfpQcTc9mPSr1B0PaWK5ByX9MOoDadSN4lc= +github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.3 h1:tW1/Rkad38LA15X4UQtjXZXNKsCgkshC3EbmcUmghTg= +github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.3/go.mod h1:UbnqO+zjqk3uIt9yCACHJ9IVNhyhOCnYk8yA19SAWrM= +github.com/aws/aws-sdk-go-v2/config v1.27.27 h1:HdqgGt1OAP0HkEDDShEl0oSYa9ZZBSOmKpdpsDMdO90= +github.com/aws/aws-sdk-go-v2/config v1.27.27/go.mod h1:MVYamCg76dFNINkZFu4n4RjDixhVr51HLj4ErWzrVwg= +github.com/aws/aws-sdk-go-v2/credentials v1.17.27 h1:2raNba6gr2IfA0eqqiP2XiQ0UVOpGPgDSi0I9iAP+UI= +github.com/aws/aws-sdk-go-v2/credentials v1.17.27/go.mod h1:gniiwbGahQByxan6YjQUMcW4Aov6bLC3m+evgcoN4r4= +github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.11 h1:KreluoV8FZDEtI6Co2xuNk/UqI9iwMrOx/87PBNIKqw= +github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.11/go.mod h1:SeSUYBLsMYFoRvHE0Tjvn7kbxaUhl75CJi1sbfhMxkU= +github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.15 h1:SoNJ4RlFEQEbtDcCEt+QG56MY4fm4W8rYirAmq+/DdU= +github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.15/go.mod h1:U9ke74k1n2bf+RIgoX1SXFed1HLs51OgUSs+Ph0KJP8= +github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.15 h1:C6WHdGnTDIYETAm5iErQUiVNsclNx9qbJVPIt03B6bI= +github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.15/go.mod h1:ZQLZqhcu+JhSrA9/NXRm8SkDvsycE+JkV3WGY41e+IM= +github.com/aws/aws-sdk-go-v2/internal/ini v1.8.0 h1:hT8rVHwugYE2lEfdFE0QWVo81lF7jMrYJVDWI+f+VxU= +github.com/aws/aws-sdk-go-v2/internal/ini v1.8.0/go.mod h1:8tu/lYfQfFe6IGnaOdrpVgEL2IrrDOf6/m9RQum4NkY= +github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.11.3 h1:dT3MqvGhSoaIhRseqw2I0yH81l7wiR2vjs57O51EAm8= +github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.11.3/go.mod h1:GlAeCkHwugxdHaueRr4nhPuY+WW+gR8UjlcqzPr1SPI= +github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.11.17 h1:HGErhhrxZlQ044RiM+WdoZxp0p+EGM62y3L6pwA4olE= +github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.11.17/go.mod h1:RkZEx4l0EHYDJpWppMJ3nD9wZJAa8/0lq9aVC+r2UII= +github.com/aws/aws-sdk-go-v2/service/sso v1.22.4 h1:BXx0ZIxvrJdSgSvKTZ+yRBeSqqgPM89VPlulEcl37tM= +github.com/aws/aws-sdk-go-v2/service/sso v1.22.4/go.mod h1:ooyCOXjvJEsUw7x+ZDHeISPMhtwI3ZCB7ggFMcFfWLU= +github.com/aws/aws-sdk-go-v2/service/ssooidc v1.26.4 h1:yiwVzJW2ZxZTurVbYWA7QOrAaCYQR72t0wrSBfoesUE= +github.com/aws/aws-sdk-go-v2/service/ssooidc v1.26.4/go.mod h1:0oxfLkpz3rQ/CHlx5hB7H69YUpFiI1tql6Q6Ne+1bCw= +github.com/aws/aws-sdk-go-v2/service/sts v1.30.3 h1:ZsDKRLXGWHk8WdtyYMoGNO7bTudrvuKpDKgMVRlepGE= +github.com/aws/aws-sdk-go-v2/service/sts v1.30.3/go.mod h1:zwySh8fpFyXp9yOr/KVzxOl8SRqgf/IDw5aUt9UKFcQ= +github.com/aws/smithy-go v1.20.3 h1:ryHwveWzPV5BIof6fyDvor6V3iUL7nTfiTKXHiW05nE= +github.com/aws/smithy-go v1.20.3/go.mod h1:krry+ya/rV9RDcV/Q16kpu6ypI4K2czasz0NC3qS14E= +github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= +github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= +github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= +github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= +github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98= +github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= +github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg= +github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U= +github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= +github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY= +github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= +github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag= +github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE= +github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= +github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= +github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da h1:oI5xCqsCo564l8iNU+DwB5epxmsaqB+rhGL0m5jtYqE= +github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= +github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= +github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8= +github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA= +github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs= +github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w= +github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0= +github.com/golang/protobuf v1.4.1/go.mod h1:U8fpvMrcmy5pZrNK1lt4xCsGvpyWQ/VVv6QDs8UjoX8= +github.com/golang/protobuf v1.4.3/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= +github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek= +github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps= +github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= +github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.3/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/s2a-go v0.1.7 h1:60BLSyTrOV4/haCDW4zb1guZItoSq8foHCXrAnjBo/o= +github.com/google/s2a-go v0.1.7/go.mod h1:50CgR4k1jNlWBu4UfS4AcfhVe1r6pdZPygJ3R8F0Qdw= +github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/googleapis/enterprise-certificate-proxy v0.3.2 h1:Vie5ybvEvT75RniqhfFxPRy3Bf7vr3h0cechB90XaQs= +github.com/googleapis/enterprise-certificate-proxy v0.3.2/go.mod h1:VLSiSSBs/ksPL8kq3OBOQ6WRI2QnaFynd1DCjZ62+V0= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/tidwall/gjson v1.14.2/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= github.com/tidwall/gjson v1.14.4 h1:uo0p8EbA09J7RQaflQ1aBRffTR7xedD2bcIVSYxLnkM= github.com/tidwall/gjson v1.14.4/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= @@ -10,3 +101,90 @@ github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4= github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= github.com/tidwall/sjson v1.2.5 h1:kLy8mja+1c9jlljvWTlSazM7cKDRfJuR/bOJhcY5NcY= github.com/tidwall/sjson v1.2.5/go.mod h1:Fvgq9kS/6ociJEDnK0Fk1cpYF4FIW6ZF7LAe+6jwd28= +go.opencensus.io v0.24.0 h1:y73uSU6J157QMP2kn2r30vwW1A2W2WFwSCGnAVxeaD0= +go.opencensus.io v0.24.0/go.mod h1:vNK8G9p7aAivkbmorf4v+7Hgx+Zs0yY+0fOtgBfjQKo= +go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.49.0 h1:4Pp6oUg3+e/6M4C0A/3kJ2VYa++dsWVTtGgLVj5xtHg= +go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.49.0/go.mod h1:Mjt1i1INqiaoZOMGR1RIUJN+i3ChKoFRqzrRQhlkbs0= +go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0 h1:jq9TW8u3so/bN+JPT166wjOI6/vQPF6Xe7nMNIltagk= +go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0/go.mod h1:p8pYQP+m5XfbZm9fxtSKAbM6oIllS7s2AfxrChvc7iw= +go.opentelemetry.io/otel v1.24.0 h1:0LAOdjNmQeSTzGBzduGe/rU4tZhMwL5rWgtp9Ku5Jfo= +go.opentelemetry.io/otel v1.24.0/go.mod h1:W7b9Ozg4nkF5tWI5zsXkaKKDjdVjpD4oAt9Qi/MArHo= +go.opentelemetry.io/otel/metric v1.24.0 h1:6EhoGWWK28x1fbpA4tYTOWBkPefTDQnb8WSGXlc88kI= +go.opentelemetry.io/otel/metric v1.24.0/go.mod h1:VYhLe1rFfxuTXLgj4CBiyz+9WYBA8pNGJgDcSFRKBco= +go.opentelemetry.io/otel/trace v1.24.0 h1:CsKnnL4dUAr/0llH9FKuc698G04IrpWV0MQA/Y1YELI= +go.opentelemetry.io/otel/trace v1.24.0/go.mod h1:HPc3Xr/cOApsBI154IU0OI0HJexz+aw5uPdbs3UCjNU= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/crypto v0.25.0 h1:ypSNr+bnYL2YhwoMt2zPxHFmbAN1KZs/njMG3hxUp30= +golang.org/x/crypto v0.25.0/go.mod h1:T+wALwcMOSE0kXgUAnPAHqTLW+XHgcELELW8VaDgm/M= +golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= +golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= +golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= +golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= +golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20201110031124-69a78807bb2b/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= +golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys= +golang.org/x/net v0.27.0/go.mod h1:dDi0PyhWNoiUOrAS8uXv/vnScO4wnHQO4mj9fn/RytE= +golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= +golang.org/x/oauth2 v0.21.0 h1:tsimM75w1tF/uws5rbeHzIWxEqElMehnc+iW793zsZs= +golang.org/x/oauth2 v0.21.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI= +golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M= +golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= +golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI= +golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4= +golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI= +golang.org/x/time v0.5.0 h1:o7cqy6amK/52YcAKIPlM3a+Fpj35zvRj2TP+e1xFSfk= +golang.org/x/time v0.5.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= +golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/api v0.189.0 h1:equMo30LypAkdkLMBqfeIqtyAnlyig1JSZArl4XPwdI= +google.golang.org/api v0.189.0/go.mod h1:FLWGJKb0hb+pU2j+rJqwbnsF+ym+fQs73rbJ+KAUgy8= +google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= +google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= +google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= +google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc= +google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo= +google.golang.org/genproto v0.0.0-20240722135656-d784300faade h1:lKFsS7wpngDgSCeFn7MoLy+wBDQZ1UQIJD4UNM1Qvkg= +google.golang.org/genproto/googleapis/rpc v0.0.0-20240722135656-d784300faade h1:oCRSWfwGXQsqlVdErcyTt4A93Y8fo0/9D4b1gnI++qo= +google.golang.org/genproto/googleapis/rpc v0.0.0-20240722135656-d784300faade/go.mod h1:Ue6ibwXGpU+dqIcODieyLOcgj7z8+IcskoNIgZxtrFY= +google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= +google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg= +google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQciAY= +google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= +google.golang.org/grpc v1.33.2/go.mod h1:JMHMWHQWaTccqQQlmk3MJZS+GWXOdAesneDmEnv2fbc= +google.golang.org/grpc v1.64.1 h1:LKtvyfbX3UGVPFcGqJ9ItpVWW6oN/2XqTxfAnwRRXiA= +google.golang.org/grpc v1.64.1/go.mod h1:hiQF4LFZelK2WKaP6W0L92zGHtiQdZxk8CrSdvyjeP0= +google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= +google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= +google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= +google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE= +google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo= +google.golang.org/protobuf v1.22.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= +google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= +google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= +google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c= +google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg= +google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= +honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= diff --git a/internal/requestconfig/requestconfig.go b/internal/requestconfig/requestconfig.go index b48ff1e..bd78b51 100644 --- a/internal/requestconfig/requestconfig.go +++ b/internal/requestconfig/requestconfig.go @@ -332,6 +332,7 @@ func (cfg *RequestConfig) Execute() (err error) { handler = applyMiddleware(cfg.Middlewares[i], handler) } + var req *http.Request var res *http.Response for retryCount := 0; retryCount <= cfg.MaxRetries; retryCount += 1 { ctx := cfg.Request.Context() @@ -341,7 +342,8 @@ func (cfg *RequestConfig) Execute() (err error) { defer cancel() } - res, err = handler(cfg.Request.Clone(ctx)) + req = cfg.Request.Clone(ctx) + res, err = handler(req) if ctx != nil && ctx.Err() != nil { return ctx.Err() } @@ -393,7 +395,7 @@ func (cfg *RequestConfig) Execute() (err error) { res.Body = io.NopCloser(bytes.NewBuffer(contents)) // Load the contents into the error format if it is provided. - aerr := apierror.Error{Request: cfg.Request, Response: res, StatusCode: res.StatusCode} + aerr := apierror.Error{Request: req, Response: res, StatusCode: res.StatusCode} err = aerr.UnmarshalJSON(contents) if err != nil { return err @@ -439,7 +441,7 @@ func (cfg *RequestConfig) Execute() (err error) { err = json.NewDecoder(bytes.NewReader(contents)).Decode(cfg.ResponseBodyInto) if err != nil { - return fmt.Errorf("error parsing response json: %w", err) + err = fmt.Errorf("error parsing response json: %w", err) } return nil diff --git a/internal/version.go b/internal/version.go index 4ff68e4..1e49ee4 100644 --- a/internal/version.go +++ b/internal/version.go @@ -2,4 +2,4 @@ package internal -const PackageVersion = "0.0.1-alpha.0" // x-release-please-version +const PackageVersion = "0.0.1-alpha.0" diff --git a/message.go b/message.go index c0be88e..4863f83 100644 --- a/message.go +++ b/message.go @@ -4,6 +4,8 @@ package anthropic import ( "context" + "encoding/json" + "fmt" "net/http" "reflect" @@ -72,12 +74,11 @@ func (r *MessageService) NewStreaming(ctx context.Context, body MessageNewParams } type ContentBlock struct { - Type ContentBlockType `json:"type,required"` - Text string `json:"text"` - ID string `json:"id"` - Name string `json:"name"` - // This field can have the runtime type of [interface{}]. - Input interface{} `json:"input,required"` + Type ContentBlockType `json:"type,required"` + Text string `json:"text"` + ID string `json:"id"` + Name string `json:"name"` + Input json.RawMessage `json:"input,required"` JSON contentBlockJSON `json:"-"` union ContentBlockUnion } @@ -156,6 +157,17 @@ type ImageBlockParam struct { Type param.Field[ImageBlockParamType] `json:"type,required"` } +func NewImageBlockBase64(mediaType string, encodedData string) ImageBlockParam { + return ImageBlockParam{ + Type: F(ImageBlockParamTypeImage), + Source: F(ImageBlockParamSource{ + Type: F(ImageBlockParamSourceTypeBase64), + Data: F(encodedData), + MediaType: F(ImageBlockParamSourceMediaType(mediaType)), + }), + } +} + func (r ImageBlockParam) MarshalJSON() (data []byte, err error) { return apijson.MarshalRoot(r) } @@ -257,6 +269,65 @@ func (r InputJSONDeltaType) IsKnown() bool { return false } +// Accumulate builds up the Message incrementally from a MessageStreamEvent. The Message then can be used as +// any other Message, except with the caveat that the Message.JSON field which normally can be used to inspect +// the JSON sent over the network may not be populated fully. +// +// message := anthropic.Message{} +// for stream.Next() { +// event := stream.Current() +// message.Accumulate(event) +// } +func (a *Message) Accumulate(event MessageStreamEvent) error { + if a == nil { + *a = Message{} + } + + switch event := event.AsUnion().(type) { + case MessageStartEvent: + *a = event.Message + + case MessageDeltaEvent: + a.StopReason = MessageStopReason(event.Delta.StopReason) + a.JSON.StopReason = event.Delta.JSON.StopReason + a.StopSequence = event.Delta.StopSequence + a.JSON.StopSequence = event.Delta.JSON.StopSequence + a.Usage.OutputTokens = event.Usage.OutputTokens + a.Usage.JSON.OutputTokens = event.Usage.JSON.OutputTokens + + case MessageStopEvent: + + case ContentBlockStartEvent: + a.Content = append(a.Content, ContentBlock{}) + err := a.Content[len(a.Content)-1].UnmarshalJSON([]byte(event.ContentBlock.JSON.RawJSON())) + if err != nil { + return err + } + + case ContentBlockDeltaEvent: + if len(a.Content) == 0 { + return fmt.Errorf("received event of type %s but there was no content block", event.Type) + } + switch delta := event.Delta.AsUnion().(type) { + case TextDelta: + a.Content[len(a.Content)-1].Text += delta.Text + case InputJSONDelta: + cb := &a.Content[len(a.Content)-1] + if string(cb.Input) == "{}" { + cb.Input = json.RawMessage{} + } + cb.Input = append(cb.Input, []byte(delta.PartialJSON)...) + } + + case ContentBlockStopEvent: + if len(a.Content) == 0 { + return fmt.Errorf("received event of type %s but there was no content block", event.Type) + } + } + + return nil +} + type Message struct { // Unique object identifier. // @@ -356,6 +427,39 @@ type messageJSON struct { ExtraFields map[string]apijson.Field } +func (r *Message) ToParam() MessageParam { + content := []MessageParamContentUnion{} + + for _, block := range r.Content { + content = append(content, MessageParamContent{ + Type: F(MessageParamContentType(block.Type)), + ID: param.Field[string]{ + Value: block.ID, + Present: !block.JSON.ID.IsNull(), + }, + Text: param.Field[string]{ + Value: block.Text, + Present: !block.JSON.Text.IsNull(), + }, + Name: param.Field[string]{ + Value: block.Name, + Present: !block.JSON.Name.IsNull(), + }, + Input: param.Field[interface{}]{ + Value: block.Input, + Present: len(block.Input) > 0 && !block.JSON.Input.IsNull(), + }, + }) + } + + message := MessageParam{ + Role: F(MessageParamRole(r.Role)), + Content: F(content), + } + + return message +} + func (r *Message) UnmarshalJSON(data []byte) (err error) { return apijson.UnmarshalRoot(data, r) } @@ -453,6 +557,20 @@ type MessageParam struct { Role param.Field[MessageParamRole] `json:"role,required"` } +func NewUserMessage(blocks ...MessageParamContentUnion) MessageParam { + return MessageParam{ + Role: F(MessageParamRoleUser), + Content: F(blocks), + } +} + +func NewAssistantMessage(blocks ...MessageParamContentUnion) MessageParam { + return MessageParam{ + Role: F(MessageParamRoleAssistant), + Content: F(blocks), + } +} + func (r MessageParam) MarshalJSON() (data []byte, err error) { return apijson.MarshalRoot(r) } @@ -674,12 +792,11 @@ func (r contentBlockStartEventJSON) RawJSON() string { func (r ContentBlockStartEvent) implementsMessageStreamEvent() {} type ContentBlockStartEventContentBlock struct { - Type ContentBlockStartEventContentBlockType `json:"type,required"` - Text string `json:"text"` - ID string `json:"id"` - Name string `json:"name"` - // This field can have the runtime type of [interface{}]. - Input interface{} `json:"input,required"` + Type ContentBlockStartEventContentBlockType `json:"type,required"` + Text string `json:"text"` + ID string `json:"id"` + Name string `json:"name"` + Input json.RawMessage `json:"input,required"` JSON contentBlockStartEventContentBlockJSON `json:"-"` union ContentBlockStartEventContentBlockUnion } @@ -1145,6 +1262,13 @@ type TextBlockParam struct { Type param.Field[TextBlockParamType] `json:"type,required"` } +func NewTextBlock(text string) TextBlockParam { + return TextBlockParam{ + Text: F(text), + Type: F(TextBlockParamTypeText), + } +} + func (r TextBlockParam) MarshalJSON() (data []byte, err error) { return apijson.MarshalRoot(r) } @@ -1210,8 +1334,8 @@ type ToolParam struct { // // This defines the shape of the `input` that your tool accepts and that the model // will produce. - InputSchema param.Field[ToolInputSchemaParam] `json:"input_schema,required"` - Name param.Field[string] `json:"name,required"` + InputSchema param.Field[interface{}] `json:"input_schema,required"` + Name param.Field[string] `json:"name,required"` // Description of what this tool does. // // Tool descriptions should be as detailed as possible. The more information that @@ -1225,34 +1349,6 @@ func (r ToolParam) MarshalJSON() (data []byte, err error) { return apijson.MarshalRoot(r) } -// [JSON schema](https://json-schema.org/) for this tool's input. -// -// This defines the shape of the `input` that your tool accepts and that the model -// will produce. -type ToolInputSchemaParam struct { - Type param.Field[ToolInputSchemaType] `json:"type,required"` - Properties param.Field[interface{}] `json:"properties"` - ExtraFields map[string]interface{} `json:"-,extras"` -} - -func (r ToolInputSchemaParam) MarshalJSON() (data []byte, err error) { - return apijson.MarshalRoot(r) -} - -type ToolInputSchemaType string - -const ( - ToolInputSchemaTypeObject ToolInputSchemaType = "object" -) - -func (r ToolInputSchemaType) IsKnown() bool { - switch r { - case ToolInputSchemaTypeObject: - return true - } - return false -} - type ToolResultBlockParam struct { ToolUseID param.Field[string] `json:"tool_use_id,required"` Type param.Field[ToolResultBlockParamType] `json:"type,required"` @@ -1260,6 +1356,15 @@ type ToolResultBlockParam struct { IsError param.Field[bool] `json:"is_error"` } +func NewToolResultBlock(toolUseID string, content string, isError bool) ToolResultBlockParam { + return ToolResultBlockParam{ + Type: F(ToolResultBlockParamTypeToolResult), + ToolUseID: F(toolUseID), + Content: F([]ToolResultBlockParamContentUnion{NewTextBlock(content)}), + IsError: F(isError), + } +} + func (r ToolResultBlockParam) MarshalJSON() (data []byte, err error) { return apijson.MarshalRoot(r) } @@ -1314,7 +1419,7 @@ func (r ToolResultBlockParamContentType) IsKnown() bool { type ToolUseBlock struct { ID string `json:"id,required"` - Input interface{} `json:"input,required"` + Input json.RawMessage `json:"input,required"` Name string `json:"name,required"` Type ToolUseBlockType `json:"type,required"` JSON toolUseBlockJSON `json:"-"` @@ -1363,6 +1468,15 @@ type ToolUseBlockParam struct { Type param.Field[ToolUseBlockParamType] `json:"type,required"` } +func NewToolUseBlockParam(id string, name string, input interface{}) ToolUseBlockParam { + return ToolUseBlockParam{ + ID: F(id), + Input: F(input), + Name: F(name), + Type: F(ToolUseBlockParamTypeToolUse), + } +} + func (r ToolUseBlockParam) MarshalJSON() (data []byte, err error) { return apijson.MarshalRoot(r) } diff --git a/message_test.go b/message_test.go index 0cb7067..72b2d63 100644 --- a/message_test.go +++ b/message_test.go @@ -47,9 +47,9 @@ func TestMessageNewWithOptionalParams(t *testing.T) { Tools: anthropic.F([]anthropic.ToolParam{{ Description: anthropic.F("Get the current weather in a given location"), Name: anthropic.F("x"), - InputSchema: anthropic.F(anthropic.ToolInputSchemaParam{ - Type: anthropic.F(anthropic.ToolInputSchemaTypeObject), - Properties: anthropic.F[any](map[string]interface{}{ + InputSchema: anthropic.F[interface{}](map[string]interface{}{ + "type": "object", + "properties": map[string]interface{}{ "location": map[string]interface{}{ "description": "The city and state, e.g. San Francisco, CA", "type": "string", @@ -58,39 +58,7 @@ func TestMessageNewWithOptionalParams(t *testing.T) { "description": "Unit for the output - one of (celsius, fahrenheit)", "type": "string", }, - }), - }), - }, { - Description: anthropic.F("Get the current weather in a given location"), - Name: anthropic.F("x"), - InputSchema: anthropic.F(anthropic.ToolInputSchemaParam{ - Type: anthropic.F(anthropic.ToolInputSchemaTypeObject), - Properties: anthropic.F[any](map[string]interface{}{ - "location": map[string]interface{}{ - "description": "The city and state, e.g. San Francisco, CA", - "type": "string", - }, - "unit": map[string]interface{}{ - "description": "Unit for the output - one of (celsius, fahrenheit)", - "type": "string", - }, - }), - }), - }, { - Description: anthropic.F("Get the current weather in a given location"), - Name: anthropic.F("x"), - InputSchema: anthropic.F(anthropic.ToolInputSchemaParam{ - Type: anthropic.F(anthropic.ToolInputSchemaTypeObject), - Properties: anthropic.F[any](map[string]interface{}{ - "location": map[string]interface{}{ - "description": "The city and state, e.g. San Francisco, CA", - "type": "string", - }, - "unit": map[string]interface{}{ - "description": "Unit for the output - one of (celsius, fahrenheit)", - "type": "string", - }, - }), + }, }), }}), TopK: anthropic.F(int64(5)), diff --git a/vertex/vertex.go b/vertex/vertex.go new file mode 100644 index 0000000..baeb7c3 --- /dev/null +++ b/vertex/vertex.go @@ -0,0 +1,99 @@ +package vertex + +import ( + "bytes" + "context" + "fmt" + "io" + "net/http" + + "golang.org/x/oauth2/google" + "google.golang.org/api/option" + "google.golang.org/api/transport" + + "github.com/anthropics/anthropic-sdk-go/internal/requestconfig" + sdkoption "github.com/anthropics/anthropic-sdk-go/option" + "github.com/tidwall/gjson" + "github.com/tidwall/sjson" +) + +const DefaultVersion = "vertex-2023-10-16" + +// WithGoogleAuth returns a request option which loads the [Application Default Credentials] for Google Vertex AI and registers +// middleware that intercepts requests to the Messages API. +// +// If you already have a [*google.Credentials], it is recommended that you instead call [WithCredentials] directly. +// +// [Application Default Credentials]: https://cloud.google.com/docs/authentication/application-default-credentials +func WithGoogleAuth(ctx context.Context, region string, projectID string, scopes ...string) sdkoption.RequestOption { + if region == "" { + panic("region must be provided") + } + creds, err := google.FindDefaultCredentials(ctx, scopes...) + if err != nil { + panic(fmt.Errorf("failed to find default credentials: %v", err)) + } + return WithCredentials(ctx, region, projectID, creds) +} + +// WithCredentials returns a request option which uses the provided credentials for Google Vertex AI and registers middleware that +// intercepts request to the Messages API. +func WithCredentials(ctx context.Context, region string, projectID string, creds *google.Credentials) sdkoption.RequestOption { + client, _, err := transport.NewHTTPClient(ctx, option.WithTokenSource(creds.TokenSource)) + if err != nil { + panic(fmt.Errorf("failed to create HTTP client: %v", err)) + } + middleware := vertexMiddleware(region, projectID) + + return func(rc *requestconfig.RequestConfig) error { + return rc.Apply( + sdkoption.WithBaseURL(fmt.Sprintf("https://%s-aiplatform.googleapis.com/v1", region)), + sdkoption.WithMiddleware(middleware), + sdkoption.WithHTTPClient(client), + ) + } +} + +func vertexMiddleware(region, projectID string) sdkoption.Middleware { + return func(r *http.Request, next sdkoption.MiddlewareNext) (*http.Response, error) { + if r.Body != nil { + body, err := io.ReadAll(r.Body) + if err != nil { + return nil, err + } + r.Body.Close() + + if !gjson.GetBytes(body, "anthropic_version").Exists() { + body, _ = sjson.SetBytes(body, "anthropic_version", DefaultVersion) + } + + if r.URL.Path == "/v1/messages" && r.Method == http.MethodPost { + if projectID == "" { + return nil, fmt.Errorf("no projectId was given and it could not be resolved from credentials") + } + + model := gjson.GetBytes(body, "model").String() + stream := gjson.GetBytes(body, "stream").Bool() + + body, _ = sjson.DeleteBytes(body, "model") + + specifier := "rawPredict" + if stream { + specifier = "streamRawPredict" + } + + r.URL.Path = fmt.Sprintf("/v1/projects/%s/locations/%s/publishers/anthropic/models/%s:%s", projectID, region, model, specifier) + } + + reader := bytes.NewReader(body) + r.Body = io.NopCloser(reader) + r.GetBody = func() (io.ReadCloser, error) { + _, err := reader.Seek(0, 0) + return io.NopCloser(reader), err + } + r.ContentLength = int64(len(body)) + } + + return next(r) + } +} From 09457cb2ef8019cc23bcdefa0d3102e642d64b3d Mon Sep 17 00:00:00 2001 From: Young-Jin Park Date: Sun, 4 Aug 2024 23:12:15 -0400 Subject: [PATCH 04/13] fix: message accumulation with union content block --- message.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/message.go b/message.go index 4863f83..7e08af8 100644 --- a/message.go +++ b/message.go @@ -310,13 +310,23 @@ func (a *Message) Accumulate(event MessageStreamEvent) error { } switch delta := event.Delta.AsUnion().(type) { case TextDelta: - a.Content[len(a.Content)-1].Text += delta.Text + cb := &a.Content[len(a.Content)-1] + cb.Text += delta.Text + if tb, ok := cb.union.(TextBlock); ok { + tb.Text = cb.Text + cb.union = tb + } + case InputJSONDelta: cb := &a.Content[len(a.Content)-1] if string(cb.Input) == "{}" { cb.Input = json.RawMessage{} } cb.Input = append(cb.Input, []byte(delta.PartialJSON)...) + if tb, ok := cb.union.(ToolUseBlock); ok { + tb.Input = cb.Input + cb.union = tb + } } case ContentBlockStopEvent: From cd3fcefad20baef3c28375adf16ab266f97e7d94 Mon Sep 17 00:00:00 2001 From: "stainless-app[bot]" <142633134+stainless-app[bot]@users.noreply.github.com> Date: Mon, 5 Aug 2024 03:51:27 +0000 Subject: [PATCH 05/13] feat: simplify system prompt (#3) --- .stats.yml | 2 +- message.go | 17 +---------------- message_test.go | 7 ++----- shared/union.go | 3 +-- 4 files changed, 5 insertions(+), 24 deletions(-) diff --git a/.stats.yml b/.stats.yml index 42a8d19..a5c66d7 100644 --- a/.stats.yml +++ b/.stats.yml @@ -1,2 +1,2 @@ configured_endpoints: 2 -openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/anthropic-15089862b682046b13deff5bf5f09d786d9ec4aecc40b9f7ef40b84ef17d3348.yml +openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/anthropic-170d46f46ce0b3a48a0d361b7d6c0fb5b2a1f5567c83f157abff62c10b095834.yml diff --git a/message.go b/message.go index 7e08af8..9f05d55 100644 --- a/message.go +++ b/message.go @@ -1653,7 +1653,7 @@ type MessageNewParams struct { // A system prompt is a way of providing context and instructions to Claude, such // as specifying a particular goal or role. See our // [guide to system prompts](https://docs.anthropic.com/en/docs/system-prompts). - System param.Field[MessageNewParamsSystemUnion] `json:"system"` + System param.Field[string] `json:"system"` // Amount of randomness injected into the response. // // Defaults to `1.0`. Ranges from `0.0` to `1.0`. Use `temperature` closer to `0.0` @@ -1779,21 +1779,6 @@ func (r MessageNewParamsMetadata) MarshalJSON() (data []byte, err error) { return apijson.MarshalRoot(r) } -// System prompt. -// -// A system prompt is a way of providing context and instructions to Claude, such -// as specifying a particular goal or role. See our -// [guide to system prompts](https://docs.anthropic.com/en/docs/system-prompts). -// -// Satisfied by [shared.UnionString], [MessageNewParamsSystemArray]. -type MessageNewParamsSystemUnion interface { - ImplementsMessageNewParamsSystemUnion() -} - -type MessageNewParamsSystemArray []TextBlockParam - -func (r MessageNewParamsSystemArray) ImplementsMessageNewParamsSystemUnion() {} - // How the model should use the provided tools. The model can use a specific tool, // any available tool, or decide by itself. type MessageNewParamsToolChoice struct { diff --git a/message_test.go b/message_test.go index 72b2d63..54ef1f8 100644 --- a/message_test.go +++ b/message_test.go @@ -36,11 +36,8 @@ func TestMessageNewWithOptionalParams(t *testing.T) { UserID: anthropic.F("13803d75-b4b5-4c3e-b2a2-6f21399b021b"), }), StopSequences: anthropic.F([]string{"string", "string", "string"}), - System: anthropic.F[anthropic.MessageNewParamsSystemUnion](anthropic.MessageNewParamsSystemArray([]anthropic.TextBlockParam{{ - Type: anthropic.F(anthropic.TextBlockParamTypeText), - Text: anthropic.F("Today's date is 2024-06-01."), - }})), - Temperature: anthropic.F(1.000000), + System: anthropic.F("string"), + Temperature: anthropic.F(1.000000), ToolChoice: anthropic.F[anthropic.MessageNewParamsToolChoiceUnion](anthropic.MessageNewParamsToolChoiceToolChoiceAuto{ Type: anthropic.F(anthropic.MessageNewParamsToolChoiceToolChoiceAutoTypeAuto), }), diff --git a/shared/union.go b/shared/union.go index f5c7461..3b11c21 100644 --- a/shared/union.go +++ b/shared/union.go @@ -4,5 +4,4 @@ package shared type UnionString string -func (UnionString) ImplementsModel() {} -func (UnionString) ImplementsMessageNewParamsSystemUnion() {} +func (UnionString) ImplementsModel() {} From 85e1b349619e7dd26c06ed0d9f566ddbbe80db2a Mon Sep 17 00:00:00 2001 From: "stainless-app[bot]" <142633134+stainless-app[bot]@users.noreply.github.com> Date: Tue, 6 Aug 2024 20:41:58 +0000 Subject: [PATCH 06/13] feat: simplify system prompt (#4) --- .stats.yml | 2 +- message.go | 2 +- message_test.go | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.stats.yml b/.stats.yml index a5c66d7..4d2e194 100644 --- a/.stats.yml +++ b/.stats.yml @@ -1,2 +1,2 @@ configured_endpoints: 2 -openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/anthropic-170d46f46ce0b3a48a0d361b7d6c0fb5b2a1f5567c83f157abff62c10b095834.yml +openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/anthropic-4769b27b6e13acc458cc71fbadd8676ea8074d76f91e37b96eaa97464c4e97af.yml diff --git a/message.go b/message.go index 9f05d55..32fcd64 100644 --- a/message.go +++ b/message.go @@ -1653,7 +1653,7 @@ type MessageNewParams struct { // A system prompt is a way of providing context and instructions to Claude, such // as specifying a particular goal or role. See our // [guide to system prompts](https://docs.anthropic.com/en/docs/system-prompts). - System param.Field[string] `json:"system"` + System param.Field[[]TextBlockParam] `json:"system"` // Amount of randomness injected into the response. // // Defaults to `1.0`. Ranges from `0.0` to `1.0`. Use `temperature` closer to `0.0` diff --git a/message_test.go b/message_test.go index 54ef1f8..293c8ad 100644 --- a/message_test.go +++ b/message_test.go @@ -36,7 +36,7 @@ func TestMessageNewWithOptionalParams(t *testing.T) { UserID: anthropic.F("13803d75-b4b5-4c3e-b2a2-6f21399b021b"), }), StopSequences: anthropic.F([]string{"string", "string", "string"}), - System: anthropic.F("string"), + System: anthropic.F([]anthropic.TextBlockParam{{Type: anthropic.F(anthropic.TextBlockParamTypeText), Text: anthropic.F("x")}, {Type: anthropic.F(anthropic.TextBlockParamTypeText), Text: anthropic.F("x")}, {Type: anthropic.F(anthropic.TextBlockParamTypeText), Text: anthropic.F("x")}}), Temperature: anthropic.F(1.000000), ToolChoice: anthropic.F[anthropic.MessageNewParamsToolChoiceUnion](anthropic.MessageNewParamsToolChoiceToolChoiceAuto{ Type: anthropic.F(anthropic.MessageNewParamsToolChoiceToolChoiceAutoTypeAuto), From df4729897b782faeaa6a0795359ecf20b4a833ca Mon Sep 17 00:00:00 2001 From: Young-Jin Park Date: Tue, 6 Aug 2024 21:49:08 -0400 Subject: [PATCH 07/13] docs: add examples to README --- README.md | 225 ++++++++++++++++++++++++++++++++++++--- examples/message/main.go | 3 + examples/tools/main.go | 5 +- message.go | 25 +++++ 4 files changed, 241 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 883643a..5636feb 100644 --- a/README.md +++ b/README.md @@ -51,11 +51,11 @@ func main() { option.WithAPIKey("my-anthropic-api-key"), // defaults to os.LookupEnv("ANTHROPIC_API_KEY") ) message, err := client.Messages.New(context.TODO(), anthropic.MessageNewParams{ + Model: anthropic.F(anthropic.ModelClaude_3_5_Sonnet_20240620), MaxTokens: anthropic.F(int64(1024)), Messages: anthropic.F([]anthropic.MessageParam{ anthropic.NewUserMessage(anthropic.NewTextBlock("What is the weather in SF?")), }), - Model: anthropic.F(anthropic.ModelClaude_3_5_Sonnet_20240620), }) if err != nil { panic(err.Error()) @@ -65,6 +65,205 @@ func main() { ``` +
+Conversations + +```go +messages := []anthropic.MessageParam{ + anthropic.NewUserMessage(anthropic.NewTextBlock("What is my first name?")), +} + +message, err := client.Messages.New(context.TODO(), anthropic.MessageNewParams{ + Model: anthropic.F(anthropic.ModelClaude_3_5_Sonnet_20240620), + Messages: anthropic.F(messages), + MaxTokens: anthropic.F(int64(1024)), +}) + +messages = append(messages, message.ToParam()) +messages = append(messages, anthropic.NewUserMessage( + anthropic.NewTextBlock("My full name is John Doe"), +)) + +message, err = client.Messages.New(context.TODO(), anthropic.MessageNewParams{ + Model: anthropic.F(anthropic.ModelClaude_3_5_Sonnet_20240620), + Messages: anthropic.F(messages), + MaxTokens: anthropic.F(int64(1024)), +}) +``` + +
+ +
+System prompts + +```go +messages := []anthropic.MessageParam{ + anthropic.NewUserMessage(anthropic.NewTextBlock("What is my first name?")), +} + +message, err := client.Messages.New(context.TODO(), anthropic.MessageNewParams{ + Model: anthropic.F(anthropic.ModelClaude_3_5_Sonnet_20240620), + MaxTokens: anthropic.Int(1024), + System: anthropic.F([]anthropic.TextBlockParam{ + anthropic.NewTextBlock("Be very serious at all times."), + }), + Messages: anthropic.F(messages), +}) +``` + +
+ +
+Streaming + +```go +stream := client.Messages.NewStreaming(context.TODO(), anthropic.MessageNewParams{ + Model: anthropic.F(anthropic.ModelClaude_3_5_Sonnet_20240620), + MaxTokens: anthropic.Int(1024), + Messages: anthropic.F([]anthropic.MessageParam{ + anthropic.NewUserMessage(anthropic.NewTextBlock(content)), + }), +}) + +message := anthropic.Message{} +for stream.Next() { + event := stream.Current() + message.Accumulate(event) + + switch delta := event.Delta.(type) { + case anthropic.ContentBlockDeltaEventDelta: + if delta.Text != "" { + print(delta.Text) + } + } +} + +if stream.Err() != nil { + panic(stream.Err()) +} +``` + +
+ +
+Tool calling + +```go +package main + +import ( + "context" + "encoding/json" + "fmt" + + "github.com/invopop/jsonschema" + "github.com/anthropics/anthropic-sdk-go" +) + +func main() { + client := anthropic.NewClient() + + content := "Where is San Francisco?" + + println("[user]: " + content) + + messages := []anthropic.MessageParam{ + anthropic.NewUserMessage(anthropic.NewTextBlock(content)), + } + + tools := []anthropic.ToolParam{ + { + Name: anthropic.F("get_coordinates"), + Description: anthropic.F("Accepts a place as an address, then returns the latitude and longitude coordinates."), + InputSchema: anthropic.F(GetCoordinatesInputSchema), + }, + } + + for { + message, err := client.Messages.New(context.TODO(), anthropic.MessageNewParams{ + Model: anthropic.F(anthropic.ModelClaude_3_5_Sonnet_20240620), + MaxTokens: anthropic.Int(1024), + Messages: anthropic.F(messages), + Tools: anthropic.F(tools), + }) + + if err != nil { + panic(err) + } + + print(color("[assistant]: ")) + for _, block := range message.Content { + switch block := block.AsUnion().(type) { + case anthropic.TextBlock: + println(block.Text) + case anthropic.ToolUseBlock: + println(block.Name + ": " + string(block.Input)) + } + } + + messages = append(messages, message.ToParam()) + toolResults := []anthropic.MessageParamContentUnion{} + + for _, block := range message.Content { + if block.Type == anthropic.ContentBlockTypeToolUse { + print("[user (" + block.Name + ")]: ") + + var response interface{} + switch block.Name { + case "get_coordinates": + input := GetCoordinatesInput{} + err := json.Unmarshal(block.Input, &input) + if err != nil { + panic(err) + } + response = GetCoordinates(input.Location) + } + + b, err := json.Marshal(response) + if err != nil { + panic(err) + } + + toolResults = append(toolResults, anthropic.NewToolResultBlock(block.ID, string(b), false)) + } + } + if len(toolResults) == 0 { + break + } + messages = append(messages, anthropic.NewUserMessage(toolResults...)) + } +} + +type GetCoordinatesInput struct { + Location string `json:"location" jsonschema_description:"The location to look up."` +} + +var GetCoordinatesInputSchema = GenerateSchema[GetCoordinatesInput]() + +type GetCoordinateResponse struct { + Long float64 `json:"long"` + Lat float64 `json:"lat"` +} + +func GetCoordinates(location string) GetCoordinateResponse { + return GetCoordinateResponse{ + Long: -122.4194, + Lat: 37.7749, + } +} + +func GenerateSchema[T any]() interface{} { + reflector := jsonschema.Reflector{ + AllowAdditionalProperties: false, + DoNotReference: true, + } + var v T + return reflector.Reflect(v) +} +``` + +
+ ### Request fields All request parameters are wrapped in a generic `Field` type, @@ -276,17 +475,17 @@ To make requests to undocumented endpoints, you can use `client.Get`, `client.Po ```go var ( - // params can be an io.Reader, a []byte, an encoding/json serializable object, - // or a "…Params" struct defined in this library. - params map[string]interface{} + // params can be an io.Reader, a []byte, an encoding/json serializable object, + // or a "…Params" struct defined in this library. + params map[string]interface{} - // result can be an []byte, *http.Response, a encoding/json deserializable object, - // or a model defined in this library. - result *http.Response + // result can be an []byte, *http.Response, a encoding/json deserializable object, + // or a model defined in this library. + result *http.Response ) err := client.Post(context.Background(), "/unspecified", params, &result) if err != nil { - … + … } ``` @@ -297,10 +496,10 @@ or the `option.WithJSONSet()` methods. ```go params := FooNewParams{ - ID: anthropic.F("id_xxxx"), - Data: anthropic.F(FooNewParamsData{ - FirstName: anthropic.F("John"), - }), + ID: anthropic.F("id_xxxx"), + Data: anthropic.F(FooNewParamsData{ + FirstName: anthropic.F("John"), + }), } client.Foo.New(context.Background(), params, option.WithJSONSet("data.last_name", "Doe")) ``` @@ -331,7 +530,7 @@ func Logger(req *http.Request, next option.MiddlewareNext) (res *http.Response, end := time.Now() LogRes(res, err, start - end) - return res, err + return res, err } client := anthropic.NewClient( diff --git a/examples/message/main.go b/examples/message/main.go index 00806f0..5ccf36c 100644 --- a/examples/message/main.go +++ b/examples/message/main.go @@ -15,6 +15,9 @@ func main() { message, err := client.Messages.New(context.TODO(), anthropic.MessageNewParams{ MaxTokens: anthropic.Int(1024), + System: anthropic.F([]anthropic.TextBlockParam{ + anthropic.NewTextBlock("Be very serious"), + }), Messages: anthropic.F([]anthropic.MessageParam{ anthropic.NewUserMessage(anthropic.NewTextBlock(content)), }), diff --git a/examples/tools/main.go b/examples/tools/main.go index b426396..1b6494d 100644 --- a/examples/tools/main.go +++ b/examples/tools/main.go @@ -150,10 +150,7 @@ func main() { break } - messages = append(messages, anthropic.MessageParam{ - Role: anthropic.F(anthropic.MessageParamRoleUser), - Content: anthropic.F(toolResults), - }) + messages = append(messages, anthropic.NewUserMessage(toolResults...)) } } diff --git a/message.go b/message.go index 32fcd64..32d644e 100644 --- a/message.go +++ b/message.go @@ -338,6 +338,8 @@ func (a *Message) Accumulate(event MessageStreamEvent) error { return nil } +// ToParam converts a Message to a MessageParam, which can be used when constructing a new +// Create type Message struct { // Unique object identifier. // @@ -437,6 +439,29 @@ type messageJSON struct { ExtraFields map[string]apijson.Field } +// ToParam converts a Message to a MessageParam which can be used when making another network +// request. This is useful when interacting with Claude conversationally or when tool calling. +// +// messages := []anthropic.MessageParam{ +// anthropic.NewUserMessage(anthropic.NewTextBlock("What is my first name?")), +// } +// +// message, err := client.Messages.New(context.TODO(), anthropic.MessageNewParams{ +// MaxTokens: anthropic.F(int64(1024)), +// Messages: anthropic.F(messages), +// Model: anthropic.F(anthropic.ModelClaude_3_5_Sonnet_20240620), +// }) +// +// messages = append(messages, message.ToParam()) +// messages = append(messages, anthropic.NewUserMessage( +// anthropic.NewTextBlock("My full name is John Doe"), +// )) +// +// message, err = client.Messages.New(context.TODO(), anthropic.MessageNewParams{ +// MaxTokens: anthropic.F(int64(1024)), +// Messages: anthropic.F(messages), +// Model: anthropic.F(anthropic.ModelClaude_3_5_Sonnet_20240620), +// }) func (r *Message) ToParam() MessageParam { content := []MessageParamContentUnion{} From 0b326c6b18effa222b8b03a17c1e562d0aedce1d Mon Sep 17 00:00:00 2001 From: "stainless-app[bot]" <142633134+stainless-app[bot]@users.noreply.github.com> Date: Fri, 9 Aug 2024 17:42:23 +0000 Subject: [PATCH 08/13] chore(ci): bump prism mock server version (#5) --- scripts/mock | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/mock b/scripts/mock index f586157..d2814ae 100755 --- a/scripts/mock +++ b/scripts/mock @@ -21,7 +21,7 @@ echo "==> Starting mock server with URL ${URL}" # Run prism mock on the given spec if [ "$1" == "--daemon" ]; then - npm exec --package=@stainless-api/prism-cli@5.8.4 -- prism mock "$URL" &> .prism.log & + npm exec --package=@stainless-api/prism-cli@5.8.5 -- prism mock "$URL" &> .prism.log & # Wait for server to come online echo -n "Waiting for server" @@ -37,5 +37,5 @@ if [ "$1" == "--daemon" ]; then echo else - npm exec --package=@stainless-api/prism-cli@5.8.4 -- prism mock "$URL" + npm exec --package=@stainless-api/prism-cli@5.8.5 -- prism mock "$URL" fi From a883a3a8232dfca1ce8a139047a0356a3fd6015f Mon Sep 17 00:00:00 2001 From: "stainless-app[bot]" <142633134+stainless-app[bot]@users.noreply.github.com> Date: Fri, 9 Aug 2024 19:58:19 +0000 Subject: [PATCH 09/13] fix: deserialization of struct unions that implement json.Unmarshaler (#6) --- internal/apijson/decoder.go | 4 ++- internal/apijson/json_test.go | 62 +++++++++++++++++++++++++++++++++++ internal/apijson/registry.go | 4 +++ 3 files changed, 69 insertions(+), 1 deletion(-) diff --git a/internal/apijson/decoder.go b/internal/apijson/decoder.go index e1b21b7..68b7ed6 100644 --- a/internal/apijson/decoder.go +++ b/internal/apijson/decoder.go @@ -143,7 +143,9 @@ func (d *decoderBuilder) newTypeDecoder(t reflect.Type) decoderFunc { return unmarshalerDecoder } if !d.root && reflect.PointerTo(t).Implements(reflect.TypeOf((*json.Unmarshaler)(nil)).Elem()) { - return indirectUnmarshalerDecoder + if _, ok := unionVariants[t]; !ok { + return indirectUnmarshalerDecoder + } } d.root = false diff --git a/internal/apijson/json_test.go b/internal/apijson/json_test.go index 72bc4c2..85cd2b5 100644 --- a/internal/apijson/json_test.go +++ b/internal/apijson/json_test.go @@ -261,6 +261,59 @@ func init() { ) } +type MarshallingUnionStruct struct { + Union MarshallingUnion +} + +func (r *MarshallingUnionStruct) UnmarshalJSON(data []byte) (err error) { + *r = MarshallingUnionStruct{} + err = UnmarshalRoot(data, &r.Union) + return +} + +func (r MarshallingUnionStruct) MarshalJSON() (data []byte, err error) { + return MarshalRoot(r.Union) +} + +type MarshallingUnion interface { + marshallingUnion() +} + +type MarshallingUnionA struct { + Boo string `json:"boo"` +} + +func (MarshallingUnionA) marshallingUnion() {} + +func (r *MarshallingUnionA) UnmarshalJSON(data []byte) (err error) { + return UnmarshalRoot(data, r) +} + +type MarshallingUnionB struct { + Foo string `json:"foo"` +} + +func (MarshallingUnionB) marshallingUnion() {} + +func (r *MarshallingUnionB) UnmarshalJSON(data []byte) (err error) { + return UnmarshalRoot(data, r) +} + +func init() { + RegisterUnion( + reflect.TypeOf((*MarshallingUnion)(nil)).Elem(), + "", + UnionVariant{ + TypeFilter: gjson.JSON, + Type: reflect.TypeOf(MarshallingUnionA{}), + }, + UnionVariant{ + TypeFilter: gjson.JSON, + Type: reflect.TypeOf(MarshallingUnionB{}), + }, + ) +} + var tests = map[string]struct { buf string val interface{} @@ -489,6 +542,15 @@ var tests = map[string]struct { ComplexUnionStruct{Union: ComplexUnionTypeB{Baz: 12, Type: TypeB("b")}}, }, + "marshalling_union_a": { + `{"boo":"hello"}`, + MarshallingUnionStruct{Union: MarshallingUnionA{Boo: "hello"}}, + }, + "marshalling_union_b": { + `{"foo":"hi"}`, + MarshallingUnionStruct{Union: MarshallingUnionB{Foo: "hi"}}, + }, + "unmarshal": { `{"foo":"hello"}`, &UnmarshalStruct{Foo: "hello", prop: true}, diff --git a/internal/apijson/registry.go b/internal/apijson/registry.go index fcc518b..2ea00ae 100644 --- a/internal/apijson/registry.go +++ b/internal/apijson/registry.go @@ -13,6 +13,7 @@ type UnionVariant struct { } var unionRegistry = map[reflect.Type]unionEntry{} +var unionVariants = map[reflect.Type]interface{}{} type unionEntry struct { discriminatorKey string @@ -24,4 +25,7 @@ func RegisterUnion(typ reflect.Type, discriminator string, variants ...UnionVari discriminatorKey: discriminator, variants: variants, } + for _, variant := range variants { + unionVariants[variant.Type] = typ + } } From 928ed50c83154eb4f56575cf9f405a132000888e Mon Sep 17 00:00:00 2001 From: "stainless-app[bot]" <142633134+stainless-app[bot]@users.noreply.github.com> Date: Mon, 12 Aug 2024 15:47:04 +0000 Subject: [PATCH 10/13] chore: bump Go to v1.21 (#7) --- .github/workflows/ci.yml | 4 ++++ go.mod | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5725bbd..9ff53ed 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -19,6 +19,8 @@ jobs: - name: Setup go uses: actions/setup-go@v5 + with: + go-version-file: ./go.mod - name: Run lints run: ./scripts/lint @@ -32,6 +34,8 @@ jobs: - name: Setup go uses: actions/setup-go@v5 + with: + go-version-file: ./go.mod - name: Bootstrap run: ./scripts/bootstrap diff --git a/go.mod b/go.mod index c3f3578..59d9048 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/anthropics/anthropic-sdk-go -go 1.19 +go 1.21 require ( cloud.google.com/go/auth v0.7.2 // indirect From 4195c5541a1a517a3890bfe43eb84e3ddc496bfe Mon Sep 17 00:00:00 2001 From: Stainless Bot Date: Mon, 12 Aug 2024 20:26:57 +0000 Subject: [PATCH 11/13] chore(examples): minor formatting changes (#8) --- README.md | 10 ++++------ client_test.go | 12 ++++++------ message_test.go | 4 ++-- usage_test.go | 2 +- 4 files changed, 13 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 5636feb..cc8def5 100644 --- a/README.md +++ b/README.md @@ -54,7 +54,7 @@ func main() { Model: anthropic.F(anthropic.ModelClaude_3_5_Sonnet_20240620), MaxTokens: anthropic.F(int64(1024)), Messages: anthropic.F([]anthropic.MessageParam{ - anthropic.NewUserMessage(anthropic.NewTextBlock("What is the weather in SF?")), + anthropic.NewUserMessage(anthropic.NewTextBlock("What is a quaternion?")), }), }) if err != nil { @@ -379,9 +379,7 @@ To handle errors, we recommend that you use the `errors.As` pattern: ```go _, err := client.Messages.New(context.TODO(), anthropic.MessageNewParams{ MaxTokens: anthropic.F(int64(1024)), - Messages: anthropic.F([]anthropic.MessageParam{ - anthropic.NewUserMessage(anthropic.NewTextBlock("What is the weather in SF?")), - }), + Model: anthropic.F(anthropic.ModelClaude_3_5_Sonnet_20240620), }) if err != nil { @@ -413,7 +411,7 @@ client.Messages.New( anthropic.MessageNewParams{ MaxTokens: anthropic.F(int64(1024)), Messages: anthropic.F([]anthropic.MessageParam{ - anthropic.NewUserMessage(anthropic.NewTextBlock("What is the weather in SF?")), + anthropic.NewUserMessage(anthropic.NewTextBlock("What is a quaternion?")), }), Model: anthropic.F(anthropic.ModelClaude_3_5_Sonnet_20240620), }, @@ -455,7 +453,7 @@ client.Messages.New( anthropic.MessageNewParams{ MaxTokens: anthropic.F(int64(1024)), Messages: anthropic.F([]anthropic.MessageParam{ - anthropic.NewUserMessage(anthropic.NewTextBlock("What is the weather in SF?")), + anthropic.NewUserMessage(anthropic.NewTextBlock("What is a quaternion?")), }), Model: anthropic.F(anthropic.ModelClaude_3_5_Sonnet_20240620), }, diff --git a/client_test.go b/client_test.go index c4c85e0..71dff57 100644 --- a/client_test.go +++ b/client_test.go @@ -40,7 +40,7 @@ func TestUserAgentHeader(t *testing.T) { MaxTokens: anthropic.F(int64(1024)), Messages: anthropic.F([]anthropic.MessageParam{{ Role: anthropic.F(anthropic.MessageParamRoleUser), - Content: anthropic.F([]anthropic.MessageParamContentUnion{anthropic.TextBlockParam{Type: anthropic.F(anthropic.TextBlockParamTypeText), Text: anthropic.F("What is a quaternion?")}}), + Content: anthropic.F([]anthropic.MessageParamContentUnion{anthropic.TextBlockParam{Text: anthropic.F("What is a quaternion?"), Type: anthropic.F(anthropic.TextBlockParamTypeText)}}), }}), Model: anthropic.F(anthropic.ModelClaude_3_5_Sonnet_20240620), }) @@ -70,7 +70,7 @@ func TestRetryAfter(t *testing.T) { MaxTokens: anthropic.F(int64(1024)), Messages: anthropic.F([]anthropic.MessageParam{{ Role: anthropic.F(anthropic.MessageParamRoleUser), - Content: anthropic.F([]anthropic.MessageParamContentUnion{anthropic.TextBlockParam{Type: anthropic.F(anthropic.TextBlockParamTypeText), Text: anthropic.F("What is a quaternion?")}}), + Content: anthropic.F([]anthropic.MessageParamContentUnion{anthropic.TextBlockParam{Text: anthropic.F("What is a quaternion?"), Type: anthropic.F(anthropic.TextBlockParamTypeText)}}), }}), Model: anthropic.F(anthropic.ModelClaude_3_5_Sonnet_20240620), }) @@ -103,7 +103,7 @@ func TestRetryAfterMs(t *testing.T) { MaxTokens: anthropic.F(int64(1024)), Messages: anthropic.F([]anthropic.MessageParam{{ Role: anthropic.F(anthropic.MessageParamRoleUser), - Content: anthropic.F([]anthropic.MessageParamContentUnion{anthropic.TextBlockParam{Type: anthropic.F(anthropic.TextBlockParamTypeText), Text: anthropic.F("What is a quaternion?")}}), + Content: anthropic.F([]anthropic.MessageParamContentUnion{anthropic.TextBlockParam{Text: anthropic.F("What is a quaternion?"), Type: anthropic.F(anthropic.TextBlockParamTypeText)}}), }}), Model: anthropic.F(anthropic.ModelClaude_3_5_Sonnet_20240620), }) @@ -132,7 +132,7 @@ func TestContextCancel(t *testing.T) { MaxTokens: anthropic.F(int64(1024)), Messages: anthropic.F([]anthropic.MessageParam{{ Role: anthropic.F(anthropic.MessageParamRoleUser), - Content: anthropic.F([]anthropic.MessageParamContentUnion{anthropic.TextBlockParam{Type: anthropic.F(anthropic.TextBlockParamTypeText), Text: anthropic.F("What is a quaternion?")}}), + Content: anthropic.F([]anthropic.MessageParamContentUnion{anthropic.TextBlockParam{Text: anthropic.F("What is a quaternion?"), Type: anthropic.F(anthropic.TextBlockParamTypeText)}}), }}), Model: anthropic.F(anthropic.ModelClaude_3_5_Sonnet_20240620), }) @@ -158,7 +158,7 @@ func TestContextCancelDelay(t *testing.T) { MaxTokens: anthropic.F(int64(1024)), Messages: anthropic.F([]anthropic.MessageParam{{ Role: anthropic.F(anthropic.MessageParamRoleUser), - Content: anthropic.F([]anthropic.MessageParamContentUnion{anthropic.TextBlockParam{Type: anthropic.F(anthropic.TextBlockParamTypeText), Text: anthropic.F("What is a quaternion?")}}), + Content: anthropic.F([]anthropic.MessageParamContentUnion{anthropic.TextBlockParam{Text: anthropic.F("What is a quaternion?"), Type: anthropic.F(anthropic.TextBlockParamTypeText)}}), }}), Model: anthropic.F(anthropic.ModelClaude_3_5_Sonnet_20240620), }) @@ -190,7 +190,7 @@ func TestContextDeadline(t *testing.T) { MaxTokens: anthropic.F(int64(1024)), Messages: anthropic.F([]anthropic.MessageParam{{ Role: anthropic.F(anthropic.MessageParamRoleUser), - Content: anthropic.F([]anthropic.MessageParamContentUnion{anthropic.TextBlockParam{Type: anthropic.F(anthropic.TextBlockParamTypeText), Text: anthropic.F("What is a quaternion?")}}), + Content: anthropic.F([]anthropic.MessageParamContentUnion{anthropic.TextBlockParam{Text: anthropic.F("What is a quaternion?"), Type: anthropic.F(anthropic.TextBlockParamTypeText)}}), }}), Model: anthropic.F(anthropic.ModelClaude_3_5_Sonnet_20240620), }) diff --git a/message_test.go b/message_test.go index 293c8ad..199c71c 100644 --- a/message_test.go +++ b/message_test.go @@ -28,15 +28,15 @@ func TestMessageNewWithOptionalParams(t *testing.T) { _, err := client.Messages.New(context.TODO(), anthropic.MessageNewParams{ MaxTokens: anthropic.F(int64(1024)), Messages: anthropic.F([]anthropic.MessageParam{{ + Content: anthropic.F([]anthropic.MessageParamContentUnion{anthropic.TextBlockParam{Text: anthropic.F("What is a quaternion?"), Type: anthropic.F(anthropic.TextBlockParamTypeText)}}), Role: anthropic.F(anthropic.MessageParamRoleUser), - Content: anthropic.F([]anthropic.MessageParamContentUnion{anthropic.TextBlockParam{Type: anthropic.F(anthropic.TextBlockParamTypeText), Text: anthropic.F("What is a quaternion?")}}), }}), Model: anthropic.F(anthropic.ModelClaude_3_5_Sonnet_20240620), Metadata: anthropic.F(anthropic.MessageNewParamsMetadata{ UserID: anthropic.F("13803d75-b4b5-4c3e-b2a2-6f21399b021b"), }), StopSequences: anthropic.F([]string{"string", "string", "string"}), - System: anthropic.F([]anthropic.TextBlockParam{{Type: anthropic.F(anthropic.TextBlockParamTypeText), Text: anthropic.F("x")}, {Type: anthropic.F(anthropic.TextBlockParamTypeText), Text: anthropic.F("x")}, {Type: anthropic.F(anthropic.TextBlockParamTypeText), Text: anthropic.F("x")}}), + System: anthropic.F([]anthropic.TextBlockParam{{Text: anthropic.F("x"), Type: anthropic.F(anthropic.TextBlockParamTypeText)}, {Text: anthropic.F("x"), Type: anthropic.F(anthropic.TextBlockParamTypeText)}, {Text: anthropic.F("x"), Type: anthropic.F(anthropic.TextBlockParamTypeText)}}), Temperature: anthropic.F(1.000000), ToolChoice: anthropic.F[anthropic.MessageNewParamsToolChoiceUnion](anthropic.MessageNewParamsToolChoiceToolChoiceAuto{ Type: anthropic.F(anthropic.MessageNewParamsToolChoiceToolChoiceAutoTypeAuto), diff --git a/usage_test.go b/usage_test.go index c2d2be5..d9aaf8c 100644 --- a/usage_test.go +++ b/usage_test.go @@ -28,7 +28,7 @@ func TestUsage(t *testing.T) { MaxTokens: anthropic.F(int64(1024)), Messages: anthropic.F([]anthropic.MessageParam{{ Role: anthropic.F(anthropic.MessageParamRoleUser), - Content: anthropic.F([]anthropic.MessageParamContentUnion{anthropic.TextBlockParam{Type: anthropic.F(anthropic.TextBlockParamTypeText), Text: anthropic.F("What is a quaternion?")}}), + Content: anthropic.F([]anthropic.MessageParamContentUnion{anthropic.TextBlockParam{Text: anthropic.F("What is a quaternion?"), Type: anthropic.F(anthropic.TextBlockParamTypeText)}}), }}), Model: anthropic.F(anthropic.ModelClaude_3_5_Sonnet_20240620), }) From 78f8c7266dd98ef5f76d258f485ee284b7a0e590 Mon Sep 17 00:00:00 2001 From: "stainless-app[bot]" <142633134+stainless-app[bot]@users.noreply.github.com> Date: Wed, 14 Aug 2024 16:03:23 +0000 Subject: [PATCH 12/13] feat(api): add prompt caching beta (#11) --- .stats.yml | 4 +- api.md | 27 + beta.go | 28 + betapromptcaching.go | 28 + betapromptcachingmessage.go | 1165 ++++++++++++++++++++++++++++++ betapromptcachingmessage_test.go | 118 +++ client.go | 2 + message.go | 14 +- shared/union.go | 4 +- 9 files changed, 1385 insertions(+), 5 deletions(-) create mode 100644 beta.go create mode 100644 betapromptcaching.go create mode 100644 betapromptcachingmessage.go create mode 100644 betapromptcachingmessage_test.go diff --git a/.stats.yml b/.stats.yml index 4d2e194..49961a7 100644 --- a/.stats.yml +++ b/.stats.yml @@ -1,2 +1,2 @@ -configured_endpoints: 2 -openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/anthropic-4769b27b6e13acc458cc71fbadd8676ea8074d76f91e37b96eaa97464c4e97af.yml +configured_endpoints: 3 +openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/anthropic-fb94a03f85580f7eacef034518becfb463502e6d74b0f7932f6153239de23a5b.yml diff --git a/api.md b/api.md index 7a3238a..90e5103 100644 --- a/api.md +++ b/api.md @@ -32,3 +32,30 @@ Response Types: Methods: - client.Messages.New(ctx context.Context, body anthropic.MessageNewParams) (anthropic.Message, error) + +# Beta + +## PromptCaching + +### Messages + +Params Types: + +- anthropic.PromptCachingBetaCacheControlEphemeralParam +- anthropic.PromptCachingBetaImageBlockParam +- anthropic.PromptCachingBetaMessageParam +- anthropic.PromptCachingBetaTextBlockParam +- anthropic.PromptCachingBetaToolParam +- anthropic.PromptCachingBetaToolResultBlockParam +- anthropic.PromptCachingBetaToolUseBlockParam + +Response Types: + +- anthropic.PromptCachingBetaMessage +- anthropic.PromptCachingBetaUsage +- anthropic.RawPromptCachingBetaMessageStartEvent +- anthropic.RawPromptCachingBetaMessageStreamEvent + +Methods: + +- client.Beta.PromptCaching.Messages.New(ctx context.Context, body anthropic.BetaPromptCachingMessageNewParams) (anthropic.PromptCachingBetaMessage, error) diff --git a/beta.go b/beta.go new file mode 100644 index 0000000..12fbd88 --- /dev/null +++ b/beta.go @@ -0,0 +1,28 @@ +// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. + +package anthropic + +import ( + "github.com/anthropics/anthropic-sdk-go/option" +) + +// BetaService 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 [NewBetaService] method instead. +type BetaService struct { + Options []option.RequestOption + PromptCaching *BetaPromptCachingService +} + +// NewBetaService 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 NewBetaService(opts ...option.RequestOption) (r *BetaService) { + r = &BetaService{} + r.Options = opts + r.PromptCaching = NewBetaPromptCachingService(opts...) + return +} diff --git a/betapromptcaching.go b/betapromptcaching.go new file mode 100644 index 0000000..496012c --- /dev/null +++ b/betapromptcaching.go @@ -0,0 +1,28 @@ +// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. + +package anthropic + +import ( + "github.com/anthropics/anthropic-sdk-go/option" +) + +// BetaPromptCachingService 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 [NewBetaPromptCachingService] method instead. +type BetaPromptCachingService struct { + Options []option.RequestOption + Messages *BetaPromptCachingMessageService +} + +// NewBetaPromptCachingService 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 NewBetaPromptCachingService(opts ...option.RequestOption) (r *BetaPromptCachingService) { + r = &BetaPromptCachingService{} + r.Options = opts + r.Messages = NewBetaPromptCachingMessageService(opts...) + return +} diff --git a/betapromptcachingmessage.go b/betapromptcachingmessage.go new file mode 100644 index 0000000..3487eea --- /dev/null +++ b/betapromptcachingmessage.go @@ -0,0 +1,1165 @@ +// 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" +) + +// BetaPromptCachingMessageService 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 [NewBetaPromptCachingMessageService] method instead. +type BetaPromptCachingMessageService struct { + Options []option.RequestOption +} + +// NewBetaPromptCachingMessageService 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 NewBetaPromptCachingMessageService(opts ...option.RequestOption) (r *BetaPromptCachingMessageService) { + r = &BetaPromptCachingMessageService{} + r.Options = opts + return +} + +// Create a Message. +// +// 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 *BetaPromptCachingMessageService) New(ctx context.Context, body BetaPromptCachingMessageNewParams, opts ...option.RequestOption) (res *PromptCachingBetaMessage, err error) { + opts = append(r.Options[:], opts...) + path := "v1/messages?beta=prompt_caching" + err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, body, &res, opts...) + return +} + +// Create a Message. +// +// 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 *BetaPromptCachingMessageService) NewStreaming(ctx context.Context, body BetaPromptCachingMessageNewParams, opts ...option.RequestOption) (stream *ssestream.Stream[RawPromptCachingBetaMessageStreamEvent]) { + var ( + raw *http.Response + err error + ) + opts = append(r.Options[:], opts...) + opts = append([]option.RequestOption{option.WithJSONSet("stream", true)}, opts...) + path := "v1/messages?beta=prompt_caching" + err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, body, &raw, opts...) + return ssestream.NewStream[RawPromptCachingBetaMessageStreamEvent](ssestream.NewDecoder(raw), err) +} + +type PromptCachingBetaCacheControlEphemeralParam struct { + Type param.Field[PromptCachingBetaCacheControlEphemeralType] `json:"type,required"` +} + +func (r PromptCachingBetaCacheControlEphemeralParam) MarshalJSON() (data []byte, err error) { + return apijson.MarshalRoot(r) +} + +type PromptCachingBetaCacheControlEphemeralType string + +const ( + PromptCachingBetaCacheControlEphemeralTypeEphemeral PromptCachingBetaCacheControlEphemeralType = "ephemeral" +) + +func (r PromptCachingBetaCacheControlEphemeralType) IsKnown() bool { + switch r { + case PromptCachingBetaCacheControlEphemeralTypeEphemeral: + return true + } + return false +} + +type PromptCachingBetaImageBlockParam struct { + Source param.Field[PromptCachingBetaImageBlockParamSource] `json:"source,required"` + Type param.Field[PromptCachingBetaImageBlockParamType] `json:"type,required"` + CacheControl param.Field[PromptCachingBetaCacheControlEphemeralParam] `json:"cache_control"` +} + +func (r PromptCachingBetaImageBlockParam) MarshalJSON() (data []byte, err error) { + return apijson.MarshalRoot(r) +} + +func (r PromptCachingBetaImageBlockParam) implementsPromptCachingBetaMessageParamContentUnion() {} + +func (r PromptCachingBetaImageBlockParam) implementsPromptCachingBetaToolResultBlockParamContentArrayUnionItem() { +} + +type PromptCachingBetaImageBlockParamSource struct { + Data param.Field[string] `json:"data,required" format:"byte"` + MediaType param.Field[PromptCachingBetaImageBlockParamSourceMediaType] `json:"media_type,required"` + Type param.Field[PromptCachingBetaImageBlockParamSourceType] `json:"type,required"` +} + +func (r PromptCachingBetaImageBlockParamSource) MarshalJSON() (data []byte, err error) { + return apijson.MarshalRoot(r) +} + +type PromptCachingBetaImageBlockParamSourceMediaType string + +const ( + PromptCachingBetaImageBlockParamSourceMediaTypeImageJPEG PromptCachingBetaImageBlockParamSourceMediaType = "image/jpeg" + PromptCachingBetaImageBlockParamSourceMediaTypeImagePNG PromptCachingBetaImageBlockParamSourceMediaType = "image/png" + PromptCachingBetaImageBlockParamSourceMediaTypeImageGIF PromptCachingBetaImageBlockParamSourceMediaType = "image/gif" + PromptCachingBetaImageBlockParamSourceMediaTypeImageWebP PromptCachingBetaImageBlockParamSourceMediaType = "image/webp" +) + +func (r PromptCachingBetaImageBlockParamSourceMediaType) IsKnown() bool { + switch r { + case PromptCachingBetaImageBlockParamSourceMediaTypeImageJPEG, PromptCachingBetaImageBlockParamSourceMediaTypeImagePNG, PromptCachingBetaImageBlockParamSourceMediaTypeImageGIF, PromptCachingBetaImageBlockParamSourceMediaTypeImageWebP: + return true + } + return false +} + +type PromptCachingBetaImageBlockParamSourceType string + +const ( + PromptCachingBetaImageBlockParamSourceTypeBase64 PromptCachingBetaImageBlockParamSourceType = "base64" +) + +func (r PromptCachingBetaImageBlockParamSourceType) IsKnown() bool { + switch r { + case PromptCachingBetaImageBlockParamSourceTypeBase64: + return true + } + return false +} + +type PromptCachingBetaImageBlockParamType string + +const ( + PromptCachingBetaImageBlockParamTypeImage PromptCachingBetaImageBlockParamType = "image" +) + +func (r PromptCachingBetaImageBlockParamType) IsKnown() bool { + switch r { + case PromptCachingBetaImageBlockParamTypeImage: + return true + } + return false +} + +type PromptCachingBetaMessage 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 []ContentBlock `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 PromptCachingBetaMessageRole `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 PromptCachingBetaMessageStopReason `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 PromptCachingBetaMessageType `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 PromptCachingBetaUsage `json:"usage,required"` + JSON promptCachingBetaMessageJSON `json:"-"` +} + +// promptCachingBetaMessageJSON contains the JSON metadata for the struct +// [PromptCachingBetaMessage] +type promptCachingBetaMessageJSON 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 *PromptCachingBetaMessage) UnmarshalJSON(data []byte) (err error) { + return apijson.UnmarshalRoot(data, r) +} + +func (r promptCachingBetaMessageJSON) RawJSON() string { + return r.raw +} + +// Conversational role of the generated message. +// +// This will always be `"assistant"`. +type PromptCachingBetaMessageRole string + +const ( + PromptCachingBetaMessageRoleAssistant PromptCachingBetaMessageRole = "assistant" +) + +func (r PromptCachingBetaMessageRole) IsKnown() bool { + switch r { + case PromptCachingBetaMessageRoleAssistant: + 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 PromptCachingBetaMessageStopReason string + +const ( + PromptCachingBetaMessageStopReasonEndTurn PromptCachingBetaMessageStopReason = "end_turn" + PromptCachingBetaMessageStopReasonMaxTokens PromptCachingBetaMessageStopReason = "max_tokens" + PromptCachingBetaMessageStopReasonStopSequence PromptCachingBetaMessageStopReason = "stop_sequence" + PromptCachingBetaMessageStopReasonToolUse PromptCachingBetaMessageStopReason = "tool_use" +) + +func (r PromptCachingBetaMessageStopReason) IsKnown() bool { + switch r { + case PromptCachingBetaMessageStopReasonEndTurn, PromptCachingBetaMessageStopReasonMaxTokens, PromptCachingBetaMessageStopReasonStopSequence, PromptCachingBetaMessageStopReasonToolUse: + return true + } + return false +} + +// Object type. +// +// For Messages, this is always `"message"`. +type PromptCachingBetaMessageType string + +const ( + PromptCachingBetaMessageTypeMessage PromptCachingBetaMessageType = "message" +) + +func (r PromptCachingBetaMessageType) IsKnown() bool { + switch r { + case PromptCachingBetaMessageTypeMessage: + return true + } + return false +} + +type PromptCachingBetaMessageParam struct { + Content param.Field[[]PromptCachingBetaMessageParamContentUnion] `json:"content,required"` + Role param.Field[PromptCachingBetaMessageParamRole] `json:"role,required"` +} + +func (r PromptCachingBetaMessageParam) MarshalJSON() (data []byte, err error) { + return apijson.MarshalRoot(r) +} + +type PromptCachingBetaMessageParamContent struct { + CacheControl param.Field[PromptCachingBetaCacheControlEphemeralParam] `json:"cache_control"` + Type param.Field[PromptCachingBetaMessageParamContentType] `json:"type,required"` + Text param.Field[string] `json:"text"` + Source param.Field[interface{}] `json:"source,required"` + ID param.Field[string] `json:"id"` + Name param.Field[string] `json:"name"` + Input param.Field[interface{}] `json:"input,required"` + ToolUseID param.Field[string] `json:"tool_use_id"` + IsError param.Field[bool] `json:"is_error"` + Content param.Field[interface{}] `json:"content,required"` +} + +func (r PromptCachingBetaMessageParamContent) MarshalJSON() (data []byte, err error) { + return apijson.MarshalRoot(r) +} + +func (r PromptCachingBetaMessageParamContent) implementsPromptCachingBetaMessageParamContentUnion() {} + +// Satisfied by [PromptCachingBetaTextBlockParam], +// [PromptCachingBetaImageBlockParam], [PromptCachingBetaToolUseBlockParam], +// [PromptCachingBetaToolResultBlockParam], [PromptCachingBetaMessageParamContent]. +type PromptCachingBetaMessageParamContentUnion interface { + implementsPromptCachingBetaMessageParamContentUnion() +} + +type PromptCachingBetaMessageParamContentType string + +const ( + PromptCachingBetaMessageParamContentTypeText PromptCachingBetaMessageParamContentType = "text" + PromptCachingBetaMessageParamContentTypeImage PromptCachingBetaMessageParamContentType = "image" + PromptCachingBetaMessageParamContentTypeToolUse PromptCachingBetaMessageParamContentType = "tool_use" + PromptCachingBetaMessageParamContentTypeToolResult PromptCachingBetaMessageParamContentType = "tool_result" +) + +func (r PromptCachingBetaMessageParamContentType) IsKnown() bool { + switch r { + case PromptCachingBetaMessageParamContentTypeText, PromptCachingBetaMessageParamContentTypeImage, PromptCachingBetaMessageParamContentTypeToolUse, PromptCachingBetaMessageParamContentTypeToolResult: + return true + } + return false +} + +type PromptCachingBetaMessageParamRole string + +const ( + PromptCachingBetaMessageParamRoleUser PromptCachingBetaMessageParamRole = "user" + PromptCachingBetaMessageParamRoleAssistant PromptCachingBetaMessageParamRole = "assistant" +) + +func (r PromptCachingBetaMessageParamRole) IsKnown() bool { + switch r { + case PromptCachingBetaMessageParamRoleUser, PromptCachingBetaMessageParamRoleAssistant: + return true + } + return false +} + +type PromptCachingBetaTextBlockParam struct { + Text param.Field[string] `json:"text,required"` + Type param.Field[PromptCachingBetaTextBlockParamType] `json:"type,required"` + CacheControl param.Field[PromptCachingBetaCacheControlEphemeralParam] `json:"cache_control"` +} + +func (r PromptCachingBetaTextBlockParam) MarshalJSON() (data []byte, err error) { + return apijson.MarshalRoot(r) +} + +func (r PromptCachingBetaTextBlockParam) implementsPromptCachingBetaMessageParamContentUnion() {} + +func (r PromptCachingBetaTextBlockParam) implementsPromptCachingBetaToolResultBlockParamContentArrayUnionItem() { +} + +type PromptCachingBetaTextBlockParamType string + +const ( + PromptCachingBetaTextBlockParamTypeText PromptCachingBetaTextBlockParamType = "text" +) + +func (r PromptCachingBetaTextBlockParamType) IsKnown() bool { + switch r { + case PromptCachingBetaTextBlockParamTypeText: + return true + } + return false +} + +type PromptCachingBetaToolParam struct { + // [JSON schema](https://json-schema.org/) for this tool's input. + // + // This defines the shape of the `input` that your tool accepts and that the model + // will produce. + InputSchema param.Field[PromptCachingBetaToolInputSchemaParam] `json:"input_schema,required"` + Name param.Field[string] `json:"name,required"` + CacheControl param.Field[PromptCachingBetaCacheControlEphemeralParam] `json:"cache_control"` + // Description of what this tool does. + // + // Tool descriptions should be as detailed as possible. The more information that + // the model has about what the tool is and how to use it, the better it will + // perform. You can use natural language descriptions to reinforce important + // aspects of the tool input JSON schema. + Description param.Field[string] `json:"description"` +} + +func (r PromptCachingBetaToolParam) MarshalJSON() (data []byte, err error) { + return apijson.MarshalRoot(r) +} + +// [JSON schema](https://json-schema.org/) for this tool's input. +// +// This defines the shape of the `input` that your tool accepts and that the model +// will produce. +type PromptCachingBetaToolInputSchemaParam struct { + Type param.Field[PromptCachingBetaToolInputSchemaType] `json:"type,required"` + Properties param.Field[interface{}] `json:"properties"` + ExtraFields map[string]interface{} `json:"-,extras"` +} + +func (r PromptCachingBetaToolInputSchemaParam) MarshalJSON() (data []byte, err error) { + return apijson.MarshalRoot(r) +} + +type PromptCachingBetaToolInputSchemaType string + +const ( + PromptCachingBetaToolInputSchemaTypeObject PromptCachingBetaToolInputSchemaType = "object" +) + +func (r PromptCachingBetaToolInputSchemaType) IsKnown() bool { + switch r { + case PromptCachingBetaToolInputSchemaTypeObject: + return true + } + return false +} + +type PromptCachingBetaToolResultBlockParam struct { + ToolUseID param.Field[string] `json:"tool_use_id,required"` + Type param.Field[PromptCachingBetaToolResultBlockParamType] `json:"type,required"` + CacheControl param.Field[PromptCachingBetaCacheControlEphemeralParam] `json:"cache_control"` + Content param.Field[PromptCachingBetaToolResultBlockParamContentUnion] `json:"content"` + IsError param.Field[bool] `json:"is_error"` +} + +func (r PromptCachingBetaToolResultBlockParam) MarshalJSON() (data []byte, err error) { + return apijson.MarshalRoot(r) +} + +func (r PromptCachingBetaToolResultBlockParam) implementsPromptCachingBetaMessageParamContentUnion() { +} + +type PromptCachingBetaToolResultBlockParamType string + +const ( + PromptCachingBetaToolResultBlockParamTypeToolResult PromptCachingBetaToolResultBlockParamType = "tool_result" +) + +func (r PromptCachingBetaToolResultBlockParamType) IsKnown() bool { + switch r { + case PromptCachingBetaToolResultBlockParamTypeToolResult: + return true + } + return false +} + +// Satisfied by [shared.UnionString], +// [PromptCachingBetaToolResultBlockParamContentArray]. +type PromptCachingBetaToolResultBlockParamContentUnion interface { + ImplementsPromptCachingBetaToolResultBlockParamContentUnion() +} + +type PromptCachingBetaToolResultBlockParamContentArray []PromptCachingBetaToolResultBlockParamContentArrayUnionItem + +func (r PromptCachingBetaToolResultBlockParamContentArray) ImplementsPromptCachingBetaToolResultBlockParamContentUnion() { +} + +type PromptCachingBetaToolResultBlockParamContentArrayItem struct { + CacheControl param.Field[PromptCachingBetaCacheControlEphemeralParam] `json:"cache_control"` + Type param.Field[PromptCachingBetaToolResultBlockParamContentArrayType] `json:"type,required"` + Text param.Field[string] `json:"text"` + Source param.Field[interface{}] `json:"source,required"` +} + +func (r PromptCachingBetaToolResultBlockParamContentArrayItem) MarshalJSON() (data []byte, err error) { + return apijson.MarshalRoot(r) +} + +func (r PromptCachingBetaToolResultBlockParamContentArrayItem) implementsPromptCachingBetaToolResultBlockParamContentArrayUnionItem() { +} + +// Satisfied by [PromptCachingBetaTextBlockParam], +// [PromptCachingBetaImageBlockParam], +// [PromptCachingBetaToolResultBlockParamContentArrayItem]. +type PromptCachingBetaToolResultBlockParamContentArrayUnionItem interface { + implementsPromptCachingBetaToolResultBlockParamContentArrayUnionItem() +} + +type PromptCachingBetaToolResultBlockParamContentArrayType string + +const ( + PromptCachingBetaToolResultBlockParamContentArrayTypeText PromptCachingBetaToolResultBlockParamContentArrayType = "text" + PromptCachingBetaToolResultBlockParamContentArrayTypeImage PromptCachingBetaToolResultBlockParamContentArrayType = "image" +) + +func (r PromptCachingBetaToolResultBlockParamContentArrayType) IsKnown() bool { + switch r { + case PromptCachingBetaToolResultBlockParamContentArrayTypeText, PromptCachingBetaToolResultBlockParamContentArrayTypeImage: + return true + } + return false +} + +type PromptCachingBetaToolUseBlockParam struct { + ID param.Field[string] `json:"id,required"` + Input param.Field[interface{}] `json:"input,required"` + Name param.Field[string] `json:"name,required"` + Type param.Field[PromptCachingBetaToolUseBlockParamType] `json:"type,required"` + CacheControl param.Field[PromptCachingBetaCacheControlEphemeralParam] `json:"cache_control"` +} + +func (r PromptCachingBetaToolUseBlockParam) MarshalJSON() (data []byte, err error) { + return apijson.MarshalRoot(r) +} + +func (r PromptCachingBetaToolUseBlockParam) implementsPromptCachingBetaMessageParamContentUnion() {} + +type PromptCachingBetaToolUseBlockParamType string + +const ( + PromptCachingBetaToolUseBlockParamTypeToolUse PromptCachingBetaToolUseBlockParamType = "tool_use" +) + +func (r PromptCachingBetaToolUseBlockParamType) IsKnown() bool { + switch r { + case PromptCachingBetaToolUseBlockParamTypeToolUse: + return true + } + return false +} + +type PromptCachingBetaUsage struct { + // The number of input tokens used to create the cache entry. + CacheCreationInputTokens int64 `json:"cache_creation_input_tokens,required,nullable"` + // The number of input tokens read from the cache. + CacheReadInputTokens int64 `json:"cache_read_input_tokens,required,nullable"` + // The number of input tokens which were used. + InputTokens int64 `json:"input_tokens,required"` + // The number of output tokens which were used. + OutputTokens int64 `json:"output_tokens,required"` + JSON promptCachingBetaUsageJSON `json:"-"` +} + +// promptCachingBetaUsageJSON contains the JSON metadata for the struct +// [PromptCachingBetaUsage] +type promptCachingBetaUsageJSON struct { + CacheCreationInputTokens apijson.Field + CacheReadInputTokens apijson.Field + InputTokens apijson.Field + OutputTokens apijson.Field + raw string + ExtraFields map[string]apijson.Field +} + +func (r *PromptCachingBetaUsage) UnmarshalJSON(data []byte) (err error) { + return apijson.UnmarshalRoot(data, r) +} + +func (r promptCachingBetaUsageJSON) RawJSON() string { + return r.raw +} + +type RawPromptCachingBetaMessageStartEvent struct { + Message PromptCachingBetaMessage `json:"message,required"` + Type RawPromptCachingBetaMessageStartEventType `json:"type,required"` + JSON rawPromptCachingBetaMessageStartEventJSON `json:"-"` +} + +// rawPromptCachingBetaMessageStartEventJSON contains the JSON metadata for the +// struct [RawPromptCachingBetaMessageStartEvent] +type rawPromptCachingBetaMessageStartEventJSON struct { + Message apijson.Field + Type apijson.Field + raw string + ExtraFields map[string]apijson.Field +} + +func (r *RawPromptCachingBetaMessageStartEvent) UnmarshalJSON(data []byte) (err error) { + return apijson.UnmarshalRoot(data, r) +} + +func (r rawPromptCachingBetaMessageStartEventJSON) RawJSON() string { + return r.raw +} + +func (r RawPromptCachingBetaMessageStartEvent) implementsRawPromptCachingBetaMessageStreamEvent() {} + +type RawPromptCachingBetaMessageStartEventType string + +const ( + RawPromptCachingBetaMessageStartEventTypeMessageStart RawPromptCachingBetaMessageStartEventType = "message_start" +) + +func (r RawPromptCachingBetaMessageStartEventType) IsKnown() bool { + switch r { + case RawPromptCachingBetaMessageStartEventTypeMessageStart: + return true + } + return false +} + +type RawPromptCachingBetaMessageStreamEvent struct { + Type RawPromptCachingBetaMessageStreamEventType `json:"type,required"` + Message PromptCachingBetaMessage `json:"message"` + // This field can have the runtime type of [MessageDeltaEventDelta], + // [ContentBlockDeltaEventDelta]. + Delta interface{} `json:"delta,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 MessageDeltaUsage `json:"usage"` + Index int64 `json:"index"` + // This field can have the runtime type of [ContentBlockStartEventContentBlock]. + ContentBlock interface{} `json:"content_block,required"` + JSON rawPromptCachingBetaMessageStreamEventJSON `json:"-"` + union RawPromptCachingBetaMessageStreamEventUnion +} + +// rawPromptCachingBetaMessageStreamEventJSON contains the JSON metadata for the +// struct [RawPromptCachingBetaMessageStreamEvent] +type rawPromptCachingBetaMessageStreamEventJSON struct { + Type apijson.Field + Message apijson.Field + Delta apijson.Field + Usage apijson.Field + Index apijson.Field + ContentBlock apijson.Field + raw string + ExtraFields map[string]apijson.Field +} + +func (r rawPromptCachingBetaMessageStreamEventJSON) RawJSON() string { + return r.raw +} + +func (r *RawPromptCachingBetaMessageStreamEvent) UnmarshalJSON(data []byte) (err error) { + *r = RawPromptCachingBetaMessageStreamEvent{} + err = apijson.UnmarshalRoot(data, &r.union) + if err != nil { + return err + } + return apijson.Port(r.union, &r) +} + +// AsUnion returns a [RawPromptCachingBetaMessageStreamEventUnion] interface which +// you can cast to the specific types for more type safety. +// +// Possible runtime types of the union are [RawPromptCachingBetaMessageStartEvent], +// [MessageDeltaEvent], [MessageStopEvent], [ContentBlockStartEvent], +// [ContentBlockDeltaEvent], [ContentBlockStopEvent]. +func (r RawPromptCachingBetaMessageStreamEvent) AsUnion() RawPromptCachingBetaMessageStreamEventUnion { + return r.union +} + +// Union satisfied by [RawPromptCachingBetaMessageStartEvent], [MessageDeltaEvent], +// [MessageStopEvent], [ContentBlockStartEvent], [ContentBlockDeltaEvent] or +// [ContentBlockStopEvent]. +type RawPromptCachingBetaMessageStreamEventUnion interface { + implementsRawPromptCachingBetaMessageStreamEvent() +} + +func init() { + apijson.RegisterUnion( + reflect.TypeOf((*RawPromptCachingBetaMessageStreamEventUnion)(nil)).Elem(), + "type", + apijson.UnionVariant{ + TypeFilter: gjson.JSON, + Type: reflect.TypeOf(RawPromptCachingBetaMessageStartEvent{}), + DiscriminatorValue: "message_start", + }, + apijson.UnionVariant{ + TypeFilter: gjson.JSON, + Type: reflect.TypeOf(MessageDeltaEvent{}), + DiscriminatorValue: "message_delta", + }, + apijson.UnionVariant{ + TypeFilter: gjson.JSON, + Type: reflect.TypeOf(MessageStopEvent{}), + DiscriminatorValue: "message_stop", + }, + apijson.UnionVariant{ + TypeFilter: gjson.JSON, + Type: reflect.TypeOf(ContentBlockStartEvent{}), + DiscriminatorValue: "content_block_start", + }, + apijson.UnionVariant{ + TypeFilter: gjson.JSON, + Type: reflect.TypeOf(ContentBlockDeltaEvent{}), + DiscriminatorValue: "content_block_delta", + }, + apijson.UnionVariant{ + TypeFilter: gjson.JSON, + Type: reflect.TypeOf(ContentBlockStopEvent{}), + DiscriminatorValue: "content_block_stop", + }, + ) +} + +type RawPromptCachingBetaMessageStreamEventType string + +const ( + RawPromptCachingBetaMessageStreamEventTypeMessageStart RawPromptCachingBetaMessageStreamEventType = "message_start" + RawPromptCachingBetaMessageStreamEventTypeMessageDelta RawPromptCachingBetaMessageStreamEventType = "message_delta" + RawPromptCachingBetaMessageStreamEventTypeMessageStop RawPromptCachingBetaMessageStreamEventType = "message_stop" + RawPromptCachingBetaMessageStreamEventTypeContentBlockStart RawPromptCachingBetaMessageStreamEventType = "content_block_start" + RawPromptCachingBetaMessageStreamEventTypeContentBlockDelta RawPromptCachingBetaMessageStreamEventType = "content_block_delta" + RawPromptCachingBetaMessageStreamEventTypeContentBlockStop RawPromptCachingBetaMessageStreamEventType = "content_block_stop" +) + +func (r RawPromptCachingBetaMessageStreamEventType) IsKnown() bool { + switch r { + case RawPromptCachingBetaMessageStreamEventTypeMessageStart, RawPromptCachingBetaMessageStreamEventTypeMessageDelta, RawPromptCachingBetaMessageStreamEventTypeMessageStop, RawPromptCachingBetaMessageStreamEventTypeContentBlockStart, RawPromptCachingBetaMessageStreamEventTypeContentBlockDelta, RawPromptCachingBetaMessageStreamEventTypeContentBlockStop: + return true + } + return false +} + +type BetaPromptCachingMessageNewParams struct { + // The maximum number of tokens to generate before stopping. + // + // Note that our models may stop _before_ reaching this maximum. This parameter + // only specifies the absolute maximum number of tokens to generate. + // + // Different models have different maximum values for this parameter. See + // [models](https://docs.anthropic.com/en/docs/models-overview) for details. + MaxTokens param.Field[int64] `json:"max_tokens,required"` + // Input messages. + // + // Our models are trained to operate on alternating `user` and `assistant` + // conversational turns. When creating a new `Message`, you specify the prior + // conversational turns with the `messages` parameter, and the model then generates + // the next `Message` in the conversation. + // + // Each input message must be an object with a `role` and `content`. You can + // specify a single `user`-role message, or you can include multiple `user` and + // `assistant` messages. The first message must always use the `user` role. + // + // If the final message uses the `assistant` role, the response content will + // continue immediately from the content in that message. This can be used to + // constrain part of the model's response. + // + // Example with a single `user` message: + // + // ```json + // [{ "role": "user", "content": "Hello, Claude" }] + // ``` + // + // Example with multiple conversational turns: + // + // ```json + // [ + // + // { "role": "user", "content": "Hello there." }, + // { "role": "assistant", "content": "Hi, I'm Claude. How can I help you?" }, + // { "role": "user", "content": "Can you explain LLMs in plain English?" } + // + // ] + // ``` + // + // Example with a partially-filled response from Claude: + // + // ```json + // [ + // + // { + // "role": "user", + // "content": "What's the Greek name for Sun? (A) Sol (B) Helios (C) Sun" + // }, + // { "role": "assistant", "content": "The best answer is (" } + // + // ] + // ``` + // + // Each input message `content` may be either a single `string` or an array of + // content blocks, where each block has a specific `type`. Using a `string` for + // `content` is shorthand for an array of one content block of type `"text"`. The + // following input messages are equivalent: + // + // ```json + // { "role": "user", "content": "Hello, Claude" } + // ``` + // + // ```json + // { "role": "user", "content": [{ "type": "text", "text": "Hello, Claude" }] } + // ``` + // + // Starting with Claude 3 models, you can also send image content blocks: + // + // ```json + // + // { + // "role": "user", + // "content": [ + // { + // "type": "image", + // "source": { + // "type": "base64", + // "media_type": "image/jpeg", + // "data": "/9j/4AAQSkZJRg..." + // } + // }, + // { "type": "text", "text": "What is in this image?" } + // ] + // } + // + // ``` + // + // We currently support the `base64` source type for images, and the `image/jpeg`, + // `image/png`, `image/gif`, and `image/webp` media types. + // + // See [examples](https://docs.anthropic.com/en/api/messages-examples#vision) for + // more input examples. + // + // Note that if you want to include a + // [system prompt](https://docs.anthropic.com/en/docs/system-prompts), you can use + // the top-level `system` parameter — there is no `"system"` role for input + // messages in the Messages API. + Messages param.Field[[]PromptCachingBetaMessageParam] `json:"messages,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 param.Field[Model] `json:"model,required"` + // An object describing metadata about the request. + Metadata param.Field[BetaPromptCachingMessageNewParamsMetadata] `json:"metadata"` + // Custom text sequences that will cause the model to stop generating. + // + // Our models will normally stop when they have naturally completed their turn, + // which will result in a response `stop_reason` of `"end_turn"`. + // + // If you want the model to stop generating when it encounters custom strings of + // text, you can use the `stop_sequences` parameter. If the model encounters one of + // the custom sequences, the response `stop_reason` value will be `"stop_sequence"` + // and the response `stop_sequence` value will contain the matched stop sequence. + StopSequences param.Field[[]string] `json:"stop_sequences"` + // System prompt. + // + // A system prompt is a way of providing context and instructions to Claude, such + // as specifying a particular goal or role. See our + // [guide to system prompts](https://docs.anthropic.com/en/docs/system-prompts). + System param.Field[BetaPromptCachingMessageNewParamsSystemUnion] `json:"system"` + // Amount of randomness injected into the response. + // + // Defaults to `1.0`. Ranges from `0.0` to `1.0`. Use `temperature` closer to `0.0` + // for analytical / multiple choice, and closer to `1.0` for creative and + // generative tasks. + // + // Note that even with `temperature` of `0.0`, the results will not be fully + // deterministic. + Temperature param.Field[float64] `json:"temperature"` + // How the model should use the provided tools. The model can use a specific tool, + // any available tool, or decide by itself. + ToolChoice param.Field[BetaPromptCachingMessageNewParamsToolChoiceUnion] `json:"tool_choice"` + // Definitions of tools that the model may use. + // + // If you include `tools` in your API request, the model may return `tool_use` + // content blocks that represent the model's use of those tools. You can then run + // those tools using the tool input generated by the model and then optionally + // return results back to the model using `tool_result` content blocks. + // + // Each tool definition includes: + // + // - `name`: Name of the tool. + // - `description`: Optional, but strongly-recommended description of the tool. + // - `input_schema`: [JSON schema](https://json-schema.org/) for the tool `input` + // shape that the model will produce in `tool_use` output content blocks. + // + // For example, if you defined `tools` as: + // + // ```json + // [ + // + // { + // "name": "get_stock_price", + // "description": "Get the current stock price for a given ticker symbol.", + // "input_schema": { + // "type": "object", + // "properties": { + // "ticker": { + // "type": "string", + // "description": "The stock ticker symbol, e.g. AAPL for Apple Inc." + // } + // }, + // "required": ["ticker"] + // } + // } + // + // ] + // ``` + // + // And then asked the model "What's the S&P 500 at today?", the model might produce + // `tool_use` content blocks in the response like this: + // + // ```json + // [ + // + // { + // "type": "tool_use", + // "id": "toolu_01D7FLrfh4GYq7yT1ULFeyMV", + // "name": "get_stock_price", + // "input": { "ticker": "^GSPC" } + // } + // + // ] + // ``` + // + // You might then run your `get_stock_price` tool with `{"ticker": "^GSPC"}` as an + // input, and return the following back to the model in a subsequent `user` + // message: + // + // ```json + // [ + // + // { + // "type": "tool_result", + // "tool_use_id": "toolu_01D7FLrfh4GYq7yT1ULFeyMV", + // "content": "259.75 USD" + // } + // + // ] + // ``` + // + // Tools can be used for workflows that include running client-side tools and + // functions, or more generally whenever you want the model to produce a particular + // JSON structure of output. + // + // See our [guide](https://docs.anthropic.com/en/docs/tool-use) for more details. + Tools param.Field[[]PromptCachingBetaToolParam] `json:"tools"` + // Only sample from the top K options for each subsequent token. + // + // Used to remove "long tail" low probability responses. + // [Learn more technical details here](https://towardsdatascience.com/how-to-sample-from-language-models-682bceb97277). + // + // Recommended for advanced use cases only. You usually only need to use + // `temperature`. + TopK param.Field[int64] `json:"top_k"` + // Use nucleus sampling. + // + // In nucleus sampling, we compute the cumulative distribution over all the options + // for each subsequent token in decreasing probability order and cut it off once it + // reaches a particular probability specified by `top_p`. You should either alter + // `temperature` or `top_p`, but not both. + // + // Recommended for advanced use cases only. You usually only need to use + // `temperature`. + TopP param.Field[float64] `json:"top_p"` +} + +func (r BetaPromptCachingMessageNewParams) MarshalJSON() (data []byte, err error) { + return apijson.MarshalRoot(r) +} + +// An object describing metadata about the request. +type BetaPromptCachingMessageNewParamsMetadata 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 BetaPromptCachingMessageNewParamsMetadata) MarshalJSON() (data []byte, err error) { + return apijson.MarshalRoot(r) +} + +// System prompt. +// +// A system prompt is a way of providing context and instructions to Claude, such +// as specifying a particular goal or role. See our +// [guide to system prompts](https://docs.anthropic.com/en/docs/system-prompts). +// +// Satisfied by [shared.UnionString], +// [BetaPromptCachingMessageNewParamsSystemArray]. +type BetaPromptCachingMessageNewParamsSystemUnion interface { + ImplementsBetaPromptCachingMessageNewParamsSystemUnion() +} + +type BetaPromptCachingMessageNewParamsSystemArray []PromptCachingBetaTextBlockParam + +func (r BetaPromptCachingMessageNewParamsSystemArray) ImplementsBetaPromptCachingMessageNewParamsSystemUnion() { +} + +// How the model should use the provided tools. The model can use a specific tool, +// any available tool, or decide by itself. +type BetaPromptCachingMessageNewParamsToolChoice struct { + Type param.Field[BetaPromptCachingMessageNewParamsToolChoiceType] `json:"type,required"` + // The name of the tool to use. + Name param.Field[string] `json:"name"` +} + +func (r BetaPromptCachingMessageNewParamsToolChoice) MarshalJSON() (data []byte, err error) { + return apijson.MarshalRoot(r) +} + +func (r BetaPromptCachingMessageNewParamsToolChoice) implementsBetaPromptCachingMessageNewParamsToolChoiceUnion() { +} + +// How the model should use the provided tools. The model can use a specific tool, +// any available tool, or decide by itself. +// +// Satisfied by [BetaPromptCachingMessageNewParamsToolChoiceToolChoiceAuto], +// [BetaPromptCachingMessageNewParamsToolChoiceToolChoiceAny], +// [BetaPromptCachingMessageNewParamsToolChoiceToolChoiceTool], +// [BetaPromptCachingMessageNewParamsToolChoice]. +type BetaPromptCachingMessageNewParamsToolChoiceUnion interface { + implementsBetaPromptCachingMessageNewParamsToolChoiceUnion() +} + +// The model will automatically decide whether to use tools. +type BetaPromptCachingMessageNewParamsToolChoiceToolChoiceAuto struct { + Type param.Field[BetaPromptCachingMessageNewParamsToolChoiceToolChoiceAutoType] `json:"type,required"` +} + +func (r BetaPromptCachingMessageNewParamsToolChoiceToolChoiceAuto) MarshalJSON() (data []byte, err error) { + return apijson.MarshalRoot(r) +} + +func (r BetaPromptCachingMessageNewParamsToolChoiceToolChoiceAuto) implementsBetaPromptCachingMessageNewParamsToolChoiceUnion() { +} + +type BetaPromptCachingMessageNewParamsToolChoiceToolChoiceAutoType string + +const ( + BetaPromptCachingMessageNewParamsToolChoiceToolChoiceAutoTypeAuto BetaPromptCachingMessageNewParamsToolChoiceToolChoiceAutoType = "auto" +) + +func (r BetaPromptCachingMessageNewParamsToolChoiceToolChoiceAutoType) IsKnown() bool { + switch r { + case BetaPromptCachingMessageNewParamsToolChoiceToolChoiceAutoTypeAuto: + return true + } + return false +} + +// The model will use any available tools. +type BetaPromptCachingMessageNewParamsToolChoiceToolChoiceAny struct { + Type param.Field[BetaPromptCachingMessageNewParamsToolChoiceToolChoiceAnyType] `json:"type,required"` +} + +func (r BetaPromptCachingMessageNewParamsToolChoiceToolChoiceAny) MarshalJSON() (data []byte, err error) { + return apijson.MarshalRoot(r) +} + +func (r BetaPromptCachingMessageNewParamsToolChoiceToolChoiceAny) implementsBetaPromptCachingMessageNewParamsToolChoiceUnion() { +} + +type BetaPromptCachingMessageNewParamsToolChoiceToolChoiceAnyType string + +const ( + BetaPromptCachingMessageNewParamsToolChoiceToolChoiceAnyTypeAny BetaPromptCachingMessageNewParamsToolChoiceToolChoiceAnyType = "any" +) + +func (r BetaPromptCachingMessageNewParamsToolChoiceToolChoiceAnyType) IsKnown() bool { + switch r { + case BetaPromptCachingMessageNewParamsToolChoiceToolChoiceAnyTypeAny: + return true + } + return false +} + +// The model will use the specified tool with `tool_choice.name`. +type BetaPromptCachingMessageNewParamsToolChoiceToolChoiceTool struct { + // The name of the tool to use. + Name param.Field[string] `json:"name,required"` + Type param.Field[BetaPromptCachingMessageNewParamsToolChoiceToolChoiceToolType] `json:"type,required"` +} + +func (r BetaPromptCachingMessageNewParamsToolChoiceToolChoiceTool) MarshalJSON() (data []byte, err error) { + return apijson.MarshalRoot(r) +} + +func (r BetaPromptCachingMessageNewParamsToolChoiceToolChoiceTool) implementsBetaPromptCachingMessageNewParamsToolChoiceUnion() { +} + +type BetaPromptCachingMessageNewParamsToolChoiceToolChoiceToolType string + +const ( + BetaPromptCachingMessageNewParamsToolChoiceToolChoiceToolTypeTool BetaPromptCachingMessageNewParamsToolChoiceToolChoiceToolType = "tool" +) + +func (r BetaPromptCachingMessageNewParamsToolChoiceToolChoiceToolType) IsKnown() bool { + switch r { + case BetaPromptCachingMessageNewParamsToolChoiceToolChoiceToolTypeTool: + return true + } + return false +} + +type BetaPromptCachingMessageNewParamsToolChoiceType string + +const ( + BetaPromptCachingMessageNewParamsToolChoiceTypeAuto BetaPromptCachingMessageNewParamsToolChoiceType = "auto" + BetaPromptCachingMessageNewParamsToolChoiceTypeAny BetaPromptCachingMessageNewParamsToolChoiceType = "any" + BetaPromptCachingMessageNewParamsToolChoiceTypeTool BetaPromptCachingMessageNewParamsToolChoiceType = "tool" +) + +func (r BetaPromptCachingMessageNewParamsToolChoiceType) IsKnown() bool { + switch r { + case BetaPromptCachingMessageNewParamsToolChoiceTypeAuto, BetaPromptCachingMessageNewParamsToolChoiceTypeAny, BetaPromptCachingMessageNewParamsToolChoiceTypeTool: + return true + } + return false +} diff --git a/betapromptcachingmessage_test.go b/betapromptcachingmessage_test.go new file mode 100644 index 0000000..24e623b --- /dev/null +++ b/betapromptcachingmessage_test.go @@ -0,0 +1,118 @@ +// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. + +package anthropic_test + +import ( + "context" + "errors" + "os" + "testing" + + "github.com/anthropics/anthropic-sdk-go" + "github.com/anthropics/anthropic-sdk-go/internal/testutil" + "github.com/anthropics/anthropic-sdk-go/option" +) + +func TestBetaPromptCachingMessageNewWithOptionalParams(t *testing.T) { + baseURL := "http://localhost:4010" + if envURL, ok := os.LookupEnv("TEST_API_BASE_URL"); ok { + baseURL = envURL + } + if !testutil.CheckTestServer(t, baseURL) { + return + } + client := anthropic.NewClient( + option.WithBaseURL(baseURL), + option.WithAPIKey("my-anthropic-api-key"), + ) + _, err := client.Beta.PromptCaching.Messages.New(context.TODO(), anthropic.BetaPromptCachingMessageNewParams{ + MaxTokens: anthropic.F(int64(1024)), + Messages: anthropic.F([]anthropic.PromptCachingBetaMessageParam{{ + Content: anthropic.F([]anthropic.PromptCachingBetaMessageParamContentUnion{anthropic.PromptCachingBetaTextBlockParam{Text: anthropic.F("What is a quaternion?"), Type: anthropic.F(anthropic.PromptCachingBetaTextBlockParamTypeText), CacheControl: anthropic.F(anthropic.PromptCachingBetaCacheControlEphemeralParam{Type: anthropic.F(anthropic.PromptCachingBetaCacheControlEphemeralTypeEphemeral)})}}), + Role: anthropic.F(anthropic.PromptCachingBetaMessageParamRoleUser), + }}), + Model: anthropic.F(anthropic.ModelClaude_3_5_Sonnet_20240620), + Metadata: anthropic.F(anthropic.BetaPromptCachingMessageNewParamsMetadata{ + UserID: anthropic.F("13803d75-b4b5-4c3e-b2a2-6f21399b021b"), + }), + StopSequences: anthropic.F([]string{"string", "string", "string"}), + System: anthropic.F[anthropic.BetaPromptCachingMessageNewParamsSystemUnion](anthropic.BetaPromptCachingMessageNewParamsSystemArray([]anthropic.PromptCachingBetaTextBlockParam{{ + Text: anthropic.F("Today's date is 2024-06-01."), + Type: anthropic.F(anthropic.PromptCachingBetaTextBlockParamTypeText), + CacheControl: anthropic.F(anthropic.PromptCachingBetaCacheControlEphemeralParam{ + Type: anthropic.F(anthropic.PromptCachingBetaCacheControlEphemeralTypeEphemeral), + }), + }})), + Temperature: anthropic.F(1.000000), + ToolChoice: anthropic.F[anthropic.BetaPromptCachingMessageNewParamsToolChoiceUnion](anthropic.BetaPromptCachingMessageNewParamsToolChoiceToolChoiceAuto{ + Type: anthropic.F(anthropic.BetaPromptCachingMessageNewParamsToolChoiceToolChoiceAutoTypeAuto), + }), + Tools: anthropic.F([]anthropic.PromptCachingBetaToolParam{{ + InputSchema: anthropic.F(anthropic.PromptCachingBetaToolInputSchemaParam{ + Type: anthropic.F(anthropic.PromptCachingBetaToolInputSchemaTypeObject), + Properties: anthropic.F[any](map[string]interface{}{ + "location": map[string]interface{}{ + "description": "The city and state, e.g. San Francisco, CA", + "type": "string", + }, + "unit": map[string]interface{}{ + "description": "Unit for the output - one of (celsius, fahrenheit)", + "type": "string", + }, + }), + }), + Name: anthropic.F("x"), + CacheControl: anthropic.F(anthropic.PromptCachingBetaCacheControlEphemeralParam{ + Type: anthropic.F(anthropic.PromptCachingBetaCacheControlEphemeralTypeEphemeral), + }), + Description: anthropic.F("Get the current weather in a given location"), + }, { + InputSchema: anthropic.F(anthropic.PromptCachingBetaToolInputSchemaParam{ + Type: anthropic.F(anthropic.PromptCachingBetaToolInputSchemaTypeObject), + Properties: anthropic.F[any](map[string]interface{}{ + "location": map[string]interface{}{ + "description": "The city and state, e.g. San Francisco, CA", + "type": "string", + }, + "unit": map[string]interface{}{ + "description": "Unit for the output - one of (celsius, fahrenheit)", + "type": "string", + }, + }), + }), + Name: anthropic.F("x"), + CacheControl: anthropic.F(anthropic.PromptCachingBetaCacheControlEphemeralParam{ + Type: anthropic.F(anthropic.PromptCachingBetaCacheControlEphemeralTypeEphemeral), + }), + Description: anthropic.F("Get the current weather in a given location"), + }, { + InputSchema: anthropic.F(anthropic.PromptCachingBetaToolInputSchemaParam{ + Type: anthropic.F(anthropic.PromptCachingBetaToolInputSchemaTypeObject), + Properties: anthropic.F[any](map[string]interface{}{ + "location": map[string]interface{}{ + "description": "The city and state, e.g. San Francisco, CA", + "type": "string", + }, + "unit": map[string]interface{}{ + "description": "Unit for the output - one of (celsius, fahrenheit)", + "type": "string", + }, + }), + }), + Name: anthropic.F("x"), + CacheControl: anthropic.F(anthropic.PromptCachingBetaCacheControlEphemeralParam{ + Type: anthropic.F(anthropic.PromptCachingBetaCacheControlEphemeralTypeEphemeral), + }), + Description: anthropic.F("Get the current weather in a given location"), + }}), + TopK: anthropic.F(int64(5)), + TopP: anthropic.F(0.700000), + }) + if err != nil { + var apierr *anthropic.Error + if errors.As(err, &apierr) { + t.Log(string(apierr.DumpRequest(true))) + } + t.Fatalf("err should be nil: %s", err.Error()) + } +} diff --git a/client.go b/client.go index 0cc61ed..42a86f2 100644 --- a/client.go +++ b/client.go @@ -18,6 +18,7 @@ type Client struct { Options []option.RequestOption Completions *CompletionService Messages *MessageService + Beta *BetaService } // NewClient generates a new client with the default option read from the @@ -38,6 +39,7 @@ func NewClient(opts ...option.RequestOption) (r *Client) { r.Completions = NewCompletionService(opts...) r.Messages = NewMessageService(opts...) + r.Beta = NewBetaService(opts...) return } diff --git a/message.go b/message.go index 32d644e..33111b1 100644 --- a/message.go +++ b/message.go @@ -709,6 +709,8 @@ func (r contentBlockDeltaEventJSON) RawJSON() string { func (r ContentBlockDeltaEvent) implementsMessageStreamEvent() {} +func (r ContentBlockDeltaEvent) implementsRawPromptCachingBetaMessageStreamEvent() {} + type ContentBlockDeltaEventDelta struct { Type ContentBlockDeltaEventDeltaType `json:"type,required"` Text string `json:"text"` @@ -826,6 +828,8 @@ func (r contentBlockStartEventJSON) RawJSON() string { func (r ContentBlockStartEvent) implementsMessageStreamEvent() {} +func (r ContentBlockStartEvent) implementsRawPromptCachingBetaMessageStreamEvent() {} + type ContentBlockStartEventContentBlock struct { Type ContentBlockStartEventContentBlockType `json:"type,required"` Text string `json:"text"` @@ -945,6 +949,8 @@ func (r contentBlockStopEventJSON) RawJSON() string { func (r ContentBlockStopEvent) implementsMessageStreamEvent() {} +func (r ContentBlockStopEvent) implementsRawPromptCachingBetaMessageStreamEvent() {} + type ContentBlockStopEventType string const ( @@ -998,6 +1004,8 @@ func (r messageDeltaEventJSON) RawJSON() string { func (r MessageDeltaEvent) implementsMessageStreamEvent() {} +func (r MessageDeltaEvent) implementsRawPromptCachingBetaMessageStreamEvent() {} + type MessageDeltaEventDelta struct { StopReason MessageDeltaEventDeltaStopReason `json:"stop_reason,required,nullable"` StopSequence string `json:"stop_sequence,required,nullable"` @@ -1114,6 +1122,8 @@ func (r messageStopEventJSON) RawJSON() string { func (r MessageStopEvent) implementsMessageStreamEvent() {} +func (r MessageStopEvent) implementsRawPromptCachingBetaMessageStreamEvent() {} + type MessageStopEventType string const ( @@ -1649,8 +1659,8 @@ type MessageNewParams struct { // We currently support the `base64` source type for images, and the `image/jpeg`, // `image/png`, `image/gif`, and `image/webp` media types. // - // See [examples](https://docs.anthropic.com/en/api/messages-examples) for more - // input examples. + // See [examples](https://docs.anthropic.com/en/api/messages-examples#vision) for + // more input examples. // // Note that if you want to include a // [system prompt](https://docs.anthropic.com/en/docs/system-prompts), you can use diff --git a/shared/union.go b/shared/union.go index 3b11c21..21952f1 100644 --- a/shared/union.go +++ b/shared/union.go @@ -4,4 +4,6 @@ package shared type UnionString string -func (UnionString) ImplementsModel() {} +func (UnionString) ImplementsModel() {} +func (UnionString) ImplementsPromptCachingBetaToolResultBlockParamContentUnion() {} +func (UnionString) ImplementsBetaPromptCachingMessageNewParamsSystemUnion() {} From 559aeb1ff9cbbbdef3974ff9f208d7de08c70582 Mon Sep 17 00:00:00 2001 From: "stainless-app[bot]" <142633134+stainless-app[bot]@users.noreply.github.com> Date: Wed, 14 Aug 2024 16:03:39 +0000 Subject: [PATCH 13/13] release: 0.1.0-alpha.1 --- .release-please-manifest.json | 2 +- CHANGELOG.md | 32 ++++++++++++++++++++++++++++++++ README.md | 2 +- 3 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 CHANGELOG.md diff --git a/.release-please-manifest.json b/.release-please-manifest.json index c476280..ba6c348 100644 --- a/.release-please-manifest.json +++ b/.release-please-manifest.json @@ -1,3 +1,3 @@ { - ".": "0.0.1-alpha.0" + ".": "0.1.0-alpha.1" } \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..cfc2506 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,32 @@ +# Changelog + +## 0.1.0-alpha.1 (2024-08-14) + +Full Changelog: [v0.0.1-alpha.0...v0.1.0-alpha.1](https://github.com/anthropics/anthropic-sdk-go/compare/v0.0.1-alpha.0...v0.1.0-alpha.1) + +### Features + +* **api:** add prompt caching beta ([#11](https://github.com/anthropics/anthropic-sdk-go/issues/11)) ([78f8c72](https://github.com/anthropics/anthropic-sdk-go/commit/78f8c7266dd98ef5f76d258f485ee284b7a0e590)) +* publish ([5ff0ff8](https://github.com/anthropics/anthropic-sdk-go/commit/5ff0ff8cc5706c39a6dde75ae69d11c892ef8bb3)) +* simplify system prompt ([#3](https://github.com/anthropics/anthropic-sdk-go/issues/3)) ([cd3fcef](https://github.com/anthropics/anthropic-sdk-go/commit/cd3fcefad20baef3c28375adf16ab266f97e7d94)) +* simplify system prompt ([#4](https://github.com/anthropics/anthropic-sdk-go/issues/4)) ([85e1b34](https://github.com/anthropics/anthropic-sdk-go/commit/85e1b349619e7dd26c06ed0d9f566ddbbe80db2a)) + + +### Bug Fixes + +* deserialization of struct unions that implement json.Unmarshaler ([#6](https://github.com/anthropics/anthropic-sdk-go/issues/6)) ([a883a3a](https://github.com/anthropics/anthropic-sdk-go/commit/a883a3a8232dfca1ce8a139047a0356a3fd6015f)) +* handle nil pagination responses when HTTP status is 200 ([#2](https://github.com/anthropics/anthropic-sdk-go/issues/2)) ([2bb2325](https://github.com/anthropics/anthropic-sdk-go/commit/2bb232557a9f75d58b7e7145c69771c927574dd3)) +* message accumulation with union content block ([09457cb](https://github.com/anthropics/anthropic-sdk-go/commit/09457cb2ef8019cc23bcdefa0d3102e642d64b3d)) + + +### Chores + +* add back custom code ([106c404](https://github.com/anthropics/anthropic-sdk-go/commit/106c40466382daaa403e7f472647248e14d939d7)) +* bump Go to v1.21 ([#7](https://github.com/anthropics/anthropic-sdk-go/issues/7)) ([928ed50](https://github.com/anthropics/anthropic-sdk-go/commit/928ed50c83154eb4f56575cf9f405a132000888e)) +* **ci:** bump prism mock server version ([#5](https://github.com/anthropics/anthropic-sdk-go/issues/5)) ([0b326c6](https://github.com/anthropics/anthropic-sdk-go/commit/0b326c6b18effa222b8b03a17c1e562d0aedce1d)) +* **examples:** minor formatting changes ([#8](https://github.com/anthropics/anthropic-sdk-go/issues/8)) ([4195c55](https://github.com/anthropics/anthropic-sdk-go/commit/4195c5541a1a517a3890bfe43eb84e3ddc496bfe)) + + +### Documentation + +* add examples to README ([df47298](https://github.com/anthropics/anthropic-sdk-go/commit/df4729897b782faeaa6a0795359ecf20b4a833ca)) diff --git a/README.md b/README.md index cc8def5..91c61f8 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ Or to pin the version: ```sh -go get -u 'github.com/anthropics/anthropic-sdk-go@v0.0.1-alpha.0' +go get -u 'github.com/anthropics/anthropic-sdk-go@v0.1.0-alpha.1' ```