AWS code-library documentation change
Summary
Added new code example for Anthropic Claude integration using Bedrock's Converse API
Security assessment
Addition of standard API usage example without security-specific content or vulnerability mitigation
Diff
diff --git a/code-library/latest/ug/go_2_bedrock-runtime_code_examples.md b/code-library/latest/ug/go_2_bedrock-runtime_code_examples.md index 2e75cb920..af7d2db2c 100644 --- a//code-library/latest/ug/go_2_bedrock-runtime_code_examples.md +++ b//code-library/latest/ug/go_2_bedrock-runtime_code_examples.md @@ -614,0 +615,60 @@ Use the Invoke Model API to send a text message. +The following code example shows how to send a text message to Anthropic Claude, using Bedrock's Converse API. + +**SDK for Go V2** + + +###### Note + +There's more on GitHub. Find the complete example and learn how to set up and run in the [AWS Code Examples Repository](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/gov2/bedrock-runtime#code-examples). + +Send a text message to Anthropic Claude, using Bedrock's Converse API. + + + 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 + } + + + + 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 + + } + + + + + * For API details, see [Converse](https://pkg.go.dev/github.com/aws/aws-sdk-go-v2/service/bedrockruntime#Client.Converse) in _AWS SDK for Go API Reference_. + + + +