diff --git a/.doc_gen/metadata/bedrock-runtime_metadata.yaml b/.doc_gen/metadata/bedrock-runtime_metadata.yaml index a0c867e1753..616f019c9ef 100644 --- a/.doc_gen/metadata/bedrock-runtime_metadata.yaml +++ b/.doc_gen/metadata/bedrock-runtime_metadata.yaml @@ -262,6 +262,15 @@ bedrock-runtime_Converse_AnthropicClaude: - description: Send a text message to Anthropic Claude, using Bedrock's Converse API. snippet_tags: - BedrockRuntime.dotnetv3.Converse_AnthropicClaude + Go: + versions: + - sdk_version: 2 + github: gov2/bedrock-runtime + excerpts: + - description: Send a text message to Anthropic Claude, using Bedrock's Converse API. + snippet_tags: + - gov2.bedrock-runtime.Converse.struct + - gov2.bedrock-runtime.ConverseClaude Python: versions: - sdk_version: 3 diff --git a/gov2/bedrock-runtime/README.md b/gov2/bedrock-runtime/README.md index 249b452f44f..6c2047a3b52 100644 --- a/gov2/bedrock-runtime/README.md +++ b/gov2/bedrock-runtime/README.md @@ -54,6 +54,7 @@ functions within the same service. ### Anthropic Claude +- [Converse](actions/converse.go#L7) - [InvokeModel](actions/invoke_model.go#L7) - [InvokeModelWithResponseStream](actions/invoke_model_with_response_stream.go#L30) diff --git a/gov2/bedrock-runtime/actions/converse.go b/gov2/bedrock-runtime/actions/converse.go new file mode 100644 index 00000000000..8cacc849af0 --- /dev/null +++ b/gov2/bedrock-runtime/actions/converse.go @@ -0,0 +1,52 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +package actions + +// snippet-start:[gov2.bedrock-runtime.Converse.complete] +// snippet-start:[gov2.bedrock-runtime.Converse.struct] + +import ( + "context" + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/service/bedrockruntime" + "github.com/aws/aws-sdk-go-v2/service/bedrockruntime/types" +) + +// ConverseWrapper encapsulates Amazon Bedrock actions used in the examples. +// It contains a Bedrock Runtime client that is used to invoke Bedrock. +type ConverseWrapper struct { + BedrockRuntimeClient *bedrockruntime.Client +} + +// snippet-end:[gov2.bedrock-runtime.Converse.struct] + +// snippet-start:[gov2.bedrock-runtime.ConverseClaude] + +func (wrapper ConverseWrapper) ConverseClaude(ctx context.Context, prompt string) (string, error) { + var content = types.ContentBlockMemberText{ + Value: prompt, + } + var message = types.Message{ + Content: []types.ContentBlock{&content}, + Role: "user", + } + modelId := "anthropic.claude-3-haiku-20240307-v1:0" + var converseInput = bedrockruntime.ConverseInput{ + ModelId: aws.String(modelId), + Messages: []types.Message{message}, + } + response, err := wrapper.BedrockRuntimeClient.Converse(ctx, &converseInput) + if err != nil { + ProcessError(err, modelId) + } + + responseText, _ := response.Output.(*types.ConverseOutputMemberMessage) + responseContentBlock := responseText.Value.Content[0] + text, _ := responseContentBlock.(*types.ContentBlockMemberText) + return text.Value, nil + +} + +// snippet-end:[gov2.bedrock-runtime.ConverseClaude] +// snippet-end:[gov2.bedrock-runtime.Converse.complete] diff --git a/gov2/bedrock-runtime/actions/converse_test.go b/gov2/bedrock-runtime/actions/converse_test.go new file mode 100644 index 00000000000..09e34eb0db9 --- /dev/null +++ b/gov2/bedrock-runtime/actions/converse_test.go @@ -0,0 +1,82 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +// Unit tests for the bedrock runtime actions. + +package actions + +import ( + "context" + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/service/bedrockruntime" + "github.com/aws/aws-sdk-go-v2/service/bedrockruntime/types" + "github.com/awsdocs/aws-doc-sdk-examples/gov2/bedrock-runtime/stubs" + "github.com/awsdocs/aws-doc-sdk-examples/gov2/testtools" + "log" + "testing" +) + +const CONVERSE_CLAUDE_MODEL_ID = "anthropic.claude-3-haiku-20240307-v1:0" +const CONVERSE_PROMPT = "Converse test prompt" + +func CallConverseActions(sdkConfig aws.Config) { + defer func() { + if r := recover(); r != nil { + log.Println(r) + } + }() + + client := bedrockruntime.NewFromConfig(sdkConfig) + wrapper := ConverseWrapper{client} + ctx := context.Background() + + claudeCompletion, err := wrapper.ConverseClaude(ctx, CONVERSE_PROMPT) + if err != nil { + panic(err) + } + log.Println(claudeCompletion) + + log.Printf("Thanks for watching!") +} + +func TestConverse(t *testing.T) { + scenTest := ConverseActionsTest{} + testtools.RunScenarioTests(&scenTest, t) +} + +type ConverseActionsTest struct{} + +func stubConverse(modelId string) testtools.Stub { + var content = types.ContentBlockMemberText{ + Value: CONVERSE_PROMPT, + } + var message = types.Message{ + Content: []types.ContentBlock{&content}, + Role: "user", + } + var resultContent = types.ContentBlockMemberText{ + Value: "A proper result", + } + var result = types.Message{ + Content: []types.ContentBlock{&resultContent}, + Role: "user", + } + return stubs.StubConverse(stubs.StubConverseParams{ + ModelId: modelId, + Messages: []types.Message{message}, + Result: result, + RaiseErr: nil, + }) +} +func (scenTest *ConverseActionsTest) SetupDataAndStubs() []testtools.Stub { + var stubList []testtools.Stub + stubList = append(stubList, stubConverse(CONVERSE_CLAUDE_MODEL_ID)) + + return stubList +} + +func (scenTest *ConverseActionsTest) RunSubTest(stubber *testtools.AwsmStubber) { + CallConverseActions(*stubber.SdkConfig) +} + +func (scenTest *ConverseActionsTest) Cleanup() {} diff --git a/gov2/bedrock-runtime/cmd/main.go b/gov2/bedrock-runtime/cmd/main.go index 8703c768ce9..ae01f6a6666 100644 --- a/gov2/bedrock-runtime/cmd/main.go +++ b/gov2/bedrock-runtime/cmd/main.go @@ -20,12 +20,14 @@ import ( // // `-scenario` can be one of the following: // +// - `converse` - Runs a scenario that shows how to use the converse command // - `invokemodels` - Runs a scenario that shows how to invoke various image and text // generation models on Amazon Bedrock. func main() { scenarioMap := map[string]func(ctx context.Context, sdkConfig aws.Config){ "invokemodels": runInvokeModelsScenario, + "converse": runConverseScenario, } choices := make([]string, len(scenarioMap)) choiceIndex := 0 @@ -61,3 +63,8 @@ func runInvokeModelsScenario(ctx context.Context, sdkConfig aws.Config) { scenario := scenarios.NewInvokeModelsScenario(sdkConfig, demotools.NewQuestioner()) scenario.Run(ctx) } + +func runConverseScenario(ctx context.Context, sdkConfig aws.Config) { + scenario := scenarios.NewConverseScenario(sdkConfig, demotools.NewQuestioner()) + scenario.Run(ctx) +} diff --git a/gov2/bedrock-runtime/go.mod b/gov2/bedrock-runtime/go.mod index ee78b8893d8..92c5d7d2f04 100644 --- a/gov2/bedrock-runtime/go.mod +++ b/gov2/bedrock-runtime/go.mod @@ -1,28 +1,30 @@ module github.com/awsdocs/aws-doc-sdk-examples/gov2/bedrock-runtime -go 1.21 +go 1.22 + +toolchain go1.22.2 require ( - github.com/aws/aws-sdk-go-v2 v1.30.5 + github.com/aws/aws-sdk-go-v2 v1.36.3 github.com/aws/aws-sdk-go-v2/config v1.27.33 - github.com/aws/aws-sdk-go-v2/service/bedrockruntime v1.16.2 + github.com/aws/aws-sdk-go-v2/service/bedrockruntime v1.26.1 github.com/awsdocs/aws-doc-sdk-examples/gov2/demotools v0.0.0-20240907001412-a9375541143b - github.com/awsdocs/aws-doc-sdk-examples/gov2/testtools v0.0.0-20240907001412-a9375541143b + github.com/awsdocs/aws-doc-sdk-examples/gov2/testtools v0.0.0-20250401185104-e9ddacfbb4a0 ) require ( - github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.4 // indirect + github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.10 // indirect github.com/aws/aws-sdk-go-v2/credentials v1.17.32 // indirect github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.13 // indirect - github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.17 // indirect - github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.17 // indirect + github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.34 // indirect + github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.34 // indirect github.com/aws/aws-sdk-go-v2/internal/ini v1.8.1 // indirect github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.11.4 // indirect github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.11.19 // indirect github.com/aws/aws-sdk-go-v2/service/sso v1.22.7 // indirect github.com/aws/aws-sdk-go-v2/service/ssooidc v1.26.7 // indirect github.com/aws/aws-sdk-go-v2/service/sts v1.30.7 // indirect - github.com/aws/smithy-go v1.20.4 // indirect + github.com/aws/smithy-go v1.22.3 // indirect golang.org/x/sys v0.25.0 // indirect golang.org/x/term v0.24.0 // indirect ) diff --git a/gov2/bedrock-runtime/go.sum b/gov2/bedrock-runtime/go.sum index d09e50b0daa..e744098e97b 100644 --- a/gov2/bedrock-runtime/go.sum +++ b/gov2/bedrock-runtime/go.sum @@ -1,7 +1,11 @@ github.com/aws/aws-sdk-go-v2 v1.30.5 h1:mWSRTwQAb0aLE17dSzztCVJWI9+cRMgqebndjwDyK0g= github.com/aws/aws-sdk-go-v2 v1.30.5/go.mod h1:CT+ZPWXbYrci8chcARI3OmI/qgd+f6WtuLOoaIA8PR0= +github.com/aws/aws-sdk-go-v2 v1.36.3 h1:mJoei2CxPutQVxaATCzDUjcZEjVRdpsiiXi2o38yqWM= +github.com/aws/aws-sdk-go-v2 v1.36.3/go.mod h1:LLXuLpgzEbD766Z5ECcRmi8AzSwfZItDtmABVkRLGzg= github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.4 h1:70PVAiL15/aBMh5LThwgXdSQorVr91L127ttckI9QQU= github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.4/go.mod h1:/MQxMqci8tlqDH+pjmoLu1i0tbWCUP1hhyMRuFxpQCw= +github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.10 h1:zAybnyUQXIZ5mok5Jqwlf58/TFE7uvd3IAsa1aF9cXs= +github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.10/go.mod h1:qqvMj6gHLR/EXWZw4ZbqlPbQUyenf4h82UQUlKc+l14= github.com/aws/aws-sdk-go-v2/config v1.27.33 h1:Nof9o/MsmH4oa0s2q9a0k7tMz5x/Yj5k06lDODWz3BU= github.com/aws/aws-sdk-go-v2/config v1.27.33/go.mod h1:kEqdYzRb8dd8Sy2pOdEbExTTF5v7ozEXX0McgPE7xks= github.com/aws/aws-sdk-go-v2/credentials v1.17.32 h1:7Cxhp/BnT2RcGy4VisJ9miUPecY+lyE9I8JvcZofn9I= @@ -10,12 +14,18 @@ github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.13 h1:pfQ2sqNpMVK6xz2RbqLEL0 github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.13/go.mod h1:NG7RXPUlqfsCLLFfi0+IpKN4sCB9D9fw/qTaSB+xRoU= github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.17 h1:pI7Bzt0BJtYA0N/JEC6B8fJ4RBrEMi1LBrkMdFYNSnQ= github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.17/go.mod h1:Dh5zzJYMtxfIjYW+/evjQ8uj2OyR/ve2KROHGHlSFqE= +github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.34 h1:ZK5jHhnrioRkUNOc+hOgQKlUL5JeC3S6JgLxtQ+Rm0Q= +github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.34/go.mod h1:p4VfIceZokChbA9FzMbRGz5OV+lekcVtHlPKEO0gSZY= github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.17 h1:Mqr/V5gvrhA2gvgnF42Zh5iMiQNcOYthFYwCyrnuWlc= github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.17/go.mod h1:aLJpZlCmjE+V+KtN1q1uyZkfnUWpQGpbsn89XPKyzfU= +github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.34 h1:SZwFm17ZUNNg5Np0ioo/gq8Mn6u9w19Mri8DnJ15Jf0= +github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.34/go.mod h1:dFZsC0BLo346mvKQLWmoJxT+Sjp+qcVR1tRVHQGOH9Q= github.com/aws/aws-sdk-go-v2/internal/ini v1.8.1 h1:VaRN3TlFdd6KxX1x3ILT5ynH6HvKgqdiXoTxAF4HQcQ= github.com/aws/aws-sdk-go-v2/internal/ini v1.8.1/go.mod h1:FbtygfRFze9usAadmnGJNc8KsP346kEe+y2/oyhGAGc= github.com/aws/aws-sdk-go-v2/service/bedrockruntime v1.16.2 h1:hmzsX43PIJ8x+dwJwruqMjE2F8tZuCQMxVz9Vn0EZkc= github.com/aws/aws-sdk-go-v2/service/bedrockruntime v1.16.2/go.mod h1:emMKL0OTFG+l9pW11RMgfvJRxZ5e093OS1o102YEGoA= +github.com/aws/aws-sdk-go-v2/service/bedrockruntime v1.26.1 h1:nTOWCzqT20Muat5amktS5NwATkp6AWBTMYweQMtXvBk= +github.com/aws/aws-sdk-go-v2/service/bedrockruntime v1.26.1/go.mod h1:0b5Rq7rUvSQFYHI1UO0zFTV/S6j6DUyuykXA80C+YOI= github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.11.4 h1:KypMCbLPPHEmf9DgMGw51jMj77VfGPAN2Kv4cfhlfgI= github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.11.4/go.mod h1:Vz1JQXliGcQktFTN/LN6uGppAIRoLBR2bMvIMP0gOjc= github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.11.19 h1:rfprUlsdzgl7ZL2KlXiUAoJnI/VxfHCvDFr2QDFj6u4= @@ -28,10 +38,14 @@ github.com/aws/aws-sdk-go-v2/service/sts v1.30.7 h1:NKTa1eqZYw8tiHSRGpP0VtTdub/8 github.com/aws/aws-sdk-go-v2/service/sts v1.30.7/go.mod h1:NXi1dIAGteSaRLqYgarlhP/Ij0cFT+qmCwiJqWh/U5o= github.com/aws/smithy-go v1.20.4 h1:2HK1zBdPgRbjFOHlfeQZfpC4r72MOb9bZkiFwggKO+4= github.com/aws/smithy-go v1.20.4/go.mod h1:irrKGvNn1InZwb2d7fkIRNucdfwR8R+Ts3wxYa/cJHg= +github.com/aws/smithy-go v1.22.3 h1:Z//5NuZCSW6R4PhQ93hShNbyBbn8BWCmCVCt+Q8Io5k= +github.com/aws/smithy-go v1.22.3/go.mod h1:t1ufH5HMublsJYulve2RKmHDC15xu1f26kHCp/HgceI= github.com/awsdocs/aws-doc-sdk-examples/gov2/demotools v0.0.0-20240907001412-a9375541143b h1:hht3uw0tZ+rQrgtecFzVsuPmssyIBlqhWWpnui9wuQA= github.com/awsdocs/aws-doc-sdk-examples/gov2/demotools v0.0.0-20240907001412-a9375541143b/go.mod h1:iBzksyiv5HVU+cymGDQbbvcecca+rsARJlDFL8np8oE= github.com/awsdocs/aws-doc-sdk-examples/gov2/testtools v0.0.0-20240907001412-a9375541143b h1:UmPy4pArM7SIhTX2Xn5bhOkgI9onSUQ1Y9fxgDJ3pHU= github.com/awsdocs/aws-doc-sdk-examples/gov2/testtools v0.0.0-20240907001412-a9375541143b/go.mod h1:9Oj/8PZn3D5Ftp/Z1QWrIEFE0daERMqfJawL9duHRfc= +github.com/awsdocs/aws-doc-sdk-examples/gov2/testtools v0.0.0-20250401185104-e9ddacfbb4a0 h1:8wIrkz0/gDLjtTmDbRDMvxkM9Uk5ZMVa1kLKY+j5Plg= +github.com/awsdocs/aws-doc-sdk-examples/gov2/testtools v0.0.0-20250401185104-e9ddacfbb4a0/go.mod h1:9Oj/8PZn3D5Ftp/Z1QWrIEFE0daERMqfJawL9duHRfc= golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34= golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.24.0 h1:Mh5cbb+Zk2hqqXNO7S1iTjEphVL+jb8ZWaqh/g+JWkM= diff --git a/gov2/bedrock-runtime/scenarios/scenario_converse.go b/gov2/bedrock-runtime/scenarios/scenario_converse.go new file mode 100644 index 00000000000..6f807a6a3fa --- /dev/null +++ b/gov2/bedrock-runtime/scenarios/scenario_converse.go @@ -0,0 +1,72 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +package scenarios + +// snippet-start:[gov2.bedrock-runtime.Scenario_Converse] + +import ( + "context" + "log" + "strings" + + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/service/bedrockruntime" + "github.com/awsdocs/aws-doc-sdk-examples/gov2/bedrock-runtime/actions" + "github.com/awsdocs/aws-doc-sdk-examples/gov2/demotools" +) + +// ConverseScenario demonstrates how to use the Amazon Bedrock Runtime client +// to Converse with Anthropic Claude +type ConverseScenario struct { + sdkConfig aws.Config + questioner demotools.IQuestioner + converseWrapper actions.ConverseWrapper +} + +// NewConverseScenario constructs a ConverseScenario instance from a configuration. +// It uses the specified config to get a Bedrock Runtime client and create wrappers for the +// actions used in the scenario. +func NewConverseScenario(sdkConfig aws.Config, questioner demotools.IQuestioner) ConverseScenario { + client := bedrockruntime.NewFromConfig(sdkConfig) + return ConverseScenario{ + sdkConfig: sdkConfig, + questioner: questioner, + converseWrapper: actions.ConverseWrapper{BedrockRuntimeClient: client}, + } +} + +// Run runs the interactive scenario. +func (scenario ConverseScenario) Run(ctx context.Context) { + defer func() { + if r := recover(); r != nil { + log.Printf("Something went wrong with the demo: %v\n", r) + } + }() + + log.Println(strings.Repeat("=", 77)) + log.Println("Welcome to the Amazon Bedrock Runtime model invocation demo.") + log.Println(strings.Repeat("=", 77)) + + log.Printf("First, let's invoke a few large-language models using the synchronous client:\n\n") + + text2textPrompt := "In one paragraph, who are you?" + + log.Println(strings.Repeat("-", 77)) + log.Printf("Invoking Claude with prompt: %v\n", text2textPrompt) + scenario.ConverseClaude(ctx, text2textPrompt) + + log.Println(strings.Repeat("=", 77)) + log.Println("Thanks for watching!") + log.Println(strings.Repeat("=", 77)) +} + +func (scenario ConverseScenario) ConverseClaude(ctx context.Context, prompt string) { + completion, err := scenario.converseWrapper.ConverseClaude(ctx, prompt) + if err != nil { + panic(err) + } + log.Printf("\nClaude : %v\n", strings.TrimSpace(completion)) +} + +// snippet-end:[gov2.bedrock-runtime.Scenario_Converse] diff --git a/gov2/bedrock-runtime/stubs/converse_stubs.go b/gov2/bedrock-runtime/stubs/converse_stubs.go new file mode 100644 index 00000000000..6a7c7c9136b --- /dev/null +++ b/gov2/bedrock-runtime/stubs/converse_stubs.go @@ -0,0 +1,34 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +// Defines stubs used for unit testing the Bedrock Runtime actions. + +package stubs + +import ( + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/service/bedrockruntime" + "github.com/aws/aws-sdk-go-v2/service/bedrockruntime/types" + "github.com/awsdocs/aws-doc-sdk-examples/gov2/testtools" +) + +type StubConverseParams struct { + ModelId string + Messages []types.Message + Result types.Message + RaiseErr *testtools.StubError +} + +func StubConverse(params StubConverseParams) testtools.Stub { + return testtools.Stub{ + OperationName: "Converse", + Input: &bedrockruntime.ConverseInput{ + ModelId: aws.String(params.ModelId), + Messages: params.Messages, + }, + Output: &bedrockruntime.ConverseOutput{ + Output: &types.ConverseOutputMemberMessage{Value: params.Result}, + }, + Error: params.RaiseErr, + } +}