Skip to content

Commit ee9be1b

Browse files
authored
Go: Add Bedrock Runtime Converse example for Amazon Nova (#7877)
1 parent 722e3a1 commit ee9be1b

8 files changed

Lines changed: 68 additions & 8 deletions

File tree

.doc_gen/metadata/bedrock-runtime_metadata.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,14 @@ bedrock-runtime_Converse_AmazonNovaText:
9191
- description: Send a text message to Amazon Nova, using Bedrock's Converse API.
9292
snippet_tags:
9393
- python.example_code.bedrock-runtime.Converse_AmazonNovaText
94+
Go:
95+
versions:
96+
- sdk_version: 2
97+
github: gov2/bedrock-runtime
98+
excerpts:
99+
- description: Send a text message to Amazon Nova, using Bedrock's Converse API.
100+
snippet_tags:
101+
- gov2.bedrock-runtime.ConverseNova
94102
Swift:
95103
versions:
96104
- sdk_version: 1

.github/workflows/lint-golang.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ jobs:
2121
uses: tj-actions/changed-files@e9772d140489982e0e3704fea5ee93d536f1e275
2222
with:
2323
files: "gov2/**/*.go"
24-
- uses: actions/setup-go@v3
24+
- uses: actions/setup-go@v5
2525
if: steps.changed-files.outputs.any_changed == 'true'
2626
with:
27-
go-version: 1.21
27+
go-version: 1.24
2828
- name: install golangci-lint
2929
if: steps.changed-files.outputs.any_changed == 'true'
30-
run: curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.61.0
30+
run: curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.64.8
3131
- name: run golangci-lint
3232
if: steps.changed-files.outputs.any_changed == 'true'
3333
run: | # If you get a linting error of "File is not `goimports`-ed", the solution is to run "gofmt -w ./<folder>".

.tools/readmes/requirements_freeze.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
aws_doc_sdk_examples_tools @ git+https://github.com/awsdocs/aws-doc-sdk-examples-tools@2025.41.0
2-
black==26.3.1
2+
black==25.11.0
33
certifi==2025.1.31
44
charset-normalizer==3.4.1
55
click==8.1.8
@@ -20,9 +20,9 @@ pluggy==1.5.0
2020
pycodestyle==2.11.1
2121
pyflakes==3.1.0
2222
Pygments==2.19.1
23-
pytest==9.0.3
23+
pytest==8.4.2
2424
PyYAML==6.0.1
25-
requests==2.33.0
25+
requests==2.32.5
2626
rich==13.9.4
2727
shellingham==1.5.4
2828
typer==0.15.1

gov2/bedrock-runtime/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ functions within the same service.
4040

4141
- [Invoke multiple foundation models on Amazon Bedrock](scenarios/scenario_invoke_models.go)
4242

43+
### Amazon Nova
44+
45+
- [Converse](actions/converse.go#L53)
46+
4347
### Amazon Titan Image Generator
4448

4549
- [InvokeModel](actions/invoke_model.go#L7)

gov2/bedrock-runtime/actions/converse.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,32 @@ func (wrapper ConverseWrapper) ConverseClaude(ctx context.Context, prompt string
4949
}
5050

5151
// snippet-end:[gov2.bedrock-runtime.ConverseClaude]
52+
53+
// snippet-start:[gov2.bedrock-runtime.ConverseNova]
54+
55+
func (wrapper ConverseWrapper) ConverseNova(ctx context.Context, prompt string) (string, error) {
56+
var content = types.ContentBlockMemberText{
57+
Value: prompt,
58+
}
59+
var message = types.Message{
60+
Content: []types.ContentBlock{&content},
61+
Role: "user",
62+
}
63+
modelId := "amazon.nova-lite-v1:0"
64+
var converseInput = bedrockruntime.ConverseInput{
65+
ModelId: aws.String(modelId),
66+
Messages: []types.Message{message},
67+
}
68+
response, err := wrapper.BedrockRuntimeClient.Converse(ctx, &converseInput)
69+
if err != nil {
70+
ProcessError(err, modelId)
71+
}
72+
73+
responseText, _ := response.Output.(*types.ConverseOutputMemberMessage)
74+
responseContentBlock := responseText.Value.Content[0]
75+
text, _ := responseContentBlock.(*types.ContentBlockMemberText)
76+
return text.Value, nil
77+
}
78+
79+
// snippet-end:[gov2.bedrock-runtime.ConverseNova]
5280
// snippet-end:[gov2.bedrock-runtime.Converse.complete]

gov2/bedrock-runtime/actions/converse_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
)
1818

1919
const CONVERSE_CLAUDE_MODEL_ID = "anthropic.claude-3-haiku-20240307-v1:0"
20+
const CONVERSE_NOVA_MODEL_ID = "amazon.nova-lite-v1:0"
2021
const CONVERSE_PROMPT = "Converse test prompt"
2122

2223
func CallConverseActions(sdkConfig aws.Config) {
@@ -36,6 +37,12 @@ func CallConverseActions(sdkConfig aws.Config) {
3637
}
3738
log.Println(claudeCompletion)
3839

40+
novaCompletion, err := wrapper.ConverseNova(ctx, CONVERSE_PROMPT)
41+
if err != nil {
42+
panic(err)
43+
}
44+
log.Println(novaCompletion)
45+
3946
log.Printf("Thanks for watching!")
4047
}
4148

@@ -71,6 +78,7 @@ func stubConverse(modelId string) testtools.Stub {
7178
func (scenTest *ConverseActionsTest) SetupDataAndStubs() []testtools.Stub {
7279
var stubList []testtools.Stub
7380
stubList = append(stubList, stubConverse(CONVERSE_CLAUDE_MODEL_ID))
81+
stubList = append(stubList, stubConverse(CONVERSE_NOVA_MODEL_ID))
7482

7583
return stubList
7684
}

gov2/bedrock-runtime/scenarios/scenario_converse.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
)
1818

1919
// ConverseScenario demonstrates how to use the Amazon Bedrock Runtime client
20-
// to Converse with Anthropic Claude
20+
// to Converse with Anthropic Claude, Amazon Nova
2121
type ConverseScenario struct {
2222
sdkConfig aws.Config
2323
questioner demotools.IQuestioner
@@ -56,6 +56,10 @@ func (scenario ConverseScenario) Run(ctx context.Context) {
5656
log.Printf("Invoking Claude with prompt: %v\n", text2textPrompt)
5757
scenario.ConverseClaude(ctx, text2textPrompt)
5858

59+
log.Println(strings.Repeat("-", 77))
60+
log.Printf("Invoking Amazon Nova with prompt: %v\n", text2textPrompt)
61+
scenario.ConverseNova(ctx, text2textPrompt)
62+
5963
log.Println(strings.Repeat("=", 77))
6064
log.Println("Thanks for watching!")
6165
log.Println(strings.Repeat("=", 77))
@@ -69,4 +73,12 @@ func (scenario ConverseScenario) ConverseClaude(ctx context.Context, prompt stri
6973
log.Printf("\nClaude : %v\n", strings.TrimSpace(completion))
7074
}
7175

76+
func (scenario ConverseScenario) ConverseNova(ctx context.Context, prompt string) {
77+
completion, err := scenario.converseWrapper.ConverseNova(ctx, prompt)
78+
if err != nil {
79+
panic(err)
80+
}
81+
log.Printf("\nAmazon Nova: %v\n", strings.TrimSpace(completion))
82+
}
83+
7284
// snippet-end:[gov2.bedrock-runtime.Scenario_Converse]

gov2/s3/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,4 +188,4 @@ in the `gov2` folder.
188188

189189
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
190190

191-
SPDX-License-Identifier: Apache-2.0
191+
SPDX-License-Identifier: Apache-2.0

0 commit comments

Comments
 (0)