Skip to content

Commit 5bb67fb

Browse files
authored
Python & .NET: Amazon Nova Text & Image Generation (#7250)
1 parent c32ed3c commit 5bb67fb

File tree

45 files changed

+615
-87
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+615
-87
lines changed

.doc_gen/metadata/bedrock-runtime_metadata.yaml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,22 @@ bedrock-runtime_Converse_AmazonNovaText:
100100
- description: Send a text message to Amazon Nova, using Bedrock's Converse API.
101101
snippet_tags:
102102
- bedrock-runtime.java2.Converse_AmazonNovaText
103+
.NET:
104+
versions:
105+
- sdk_version: 3
106+
github: dotnetv3/Bedrock-runtime
107+
excerpts:
108+
- description: Send a text message to Amazon Nova, using Bedrock's Converse API.
109+
snippet_tags:
110+
- BedrockRuntime.dotnetv3.Converse_AmazonNovaText
111+
Python:
112+
versions:
113+
- sdk_version: 3
114+
github: python/example_code/bedrock-runtime
115+
excerpts:
116+
- description: Send a text message to Amazon Nova, using Bedrock's Converse API.
117+
snippet_tags:
118+
- python.example_code.bedrock-runtime.Converse_AmazonNovaText
103119
services:
104120
bedrock-runtime: {Converse}
105121

@@ -349,6 +365,22 @@ bedrock-runtime_ConverseStream_AmazonNovaText:
349365
- description: Send a text message to Amazon Nova using Bedrock's Converse API and process the response stream in real-time.
350366
snippet_tags:
351367
- bedrock-runtime.java2.ConverseStream_AmazonNovaText
368+
.NET:
369+
versions:
370+
- sdk_version: 3
371+
github: dotnetv3/Bedrock-runtime
372+
excerpts:
373+
- description: Send a text message to Amazon Nova, using Bedrock's Converse API and process the response stream in real-time.
374+
snippet_tags:
375+
- BedrockRuntime.dotnetv3.ConverseStream_AmazonNovaText
376+
Python:
377+
versions:
378+
- sdk_version: 3
379+
github: python/example_code/bedrock-runtime
380+
excerpts:
381+
- description: Send a text message to Amazon Nova, using Bedrock's Converse API and process the response stream in real-time.
382+
snippet_tags:
383+
- python.example_code.bedrock-runtime.ConverseStream_AmazonNovaText
352384
services:
353385
bedrock-runtime: {ConverseStream}
354386

@@ -1137,6 +1169,22 @@ bedrock-runtime_InvokeModel_AmazonNovaImageGeneration:
11371169
- description: Create an image with Amazon Nova Canvas.
11381170
snippet_tags:
11391171
- bedrock-runtime.java2.InvokeModel_AmazonNovaImageGeneration
1172+
.NET:
1173+
versions:
1174+
- sdk_version: 3
1175+
github: dotnetv3/Bedrock-runtime
1176+
excerpts:
1177+
- description: Create an image with Amazon Nova Canvas.
1178+
snippet_tags:
1179+
- BedrockRuntime.dotnetv3.InvokeModel_AmazonNovaImageGeneration
1180+
Python:
1181+
versions:
1182+
- sdk_version: 3
1183+
github: python/example_code/bedrock-runtime
1184+
excerpts:
1185+
- description: Create an image with the Amazon Nova Canvas.
1186+
snippet_tags:
1187+
- python.example_code.bedrock-runtime.InvokeModel_AmazonNovaImageGeneration
11401188
services:
11411189
bedrock-runtime: {InvokeModel}
11421190

dotnetv3/Bedrock-runtime/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/.vs/
22
/Tools/
3+
**/generated-images/

dotnetv3/Bedrock-runtime/BedrockRuntimeExamples.sln

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,18 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "InvokeModelWithResponseStre
9494
EndProject
9595
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "InvokeModelWithResponseStream", "Models\AnthropicClaude\InvokeModelWithResponseStream\InvokeModelWithResponseStream.csproj", "{C75F2BBE-7C84-4B01-9836-7279DAE41499}"
9696
EndProject
97+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "AmazonNovaText", "AmazonNovaText", "{02EA681E-C7D8-13C7-8484-4AC65E1B71E8}"
98+
EndProject
99+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "AmazonNova", "AmazonNova", "{3AF63EC9-2EB0-4A0B-8C3B-0CA3595080F6}"
100+
EndProject
101+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Converse", "Models\AmazonNova\AmazonNovaText\Converse\Converse.csproj", "{2E4C9BFE-C49C-0567-D73C-F2915AB62CA6}"
102+
EndProject
103+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConverseStream", "Models\AmazonNova\AmazonNovaText\ConverseStream\ConverseStream.csproj", "{E144492A-337A-0755-EAB4-DA083C3A2DDB}"
104+
EndProject
105+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "AmazonNovaCanvas", "AmazonNovaCanvas", "{4D3E429C-CCAE-42DE-A062-4717E71D8403}"
106+
EndProject
107+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "InvokeModel", "Models\AmazonNova\AmazonNovaCanvas\InvokeModel\InvokeModel.csproj", "{2B39D4E2-C6B6-4340-A9AD-5F5C25CA8C1D}"
108+
EndProject
97109
Global
98110
GlobalSection(SolutionConfigurationPlatforms) = preSolution
99111
Debug|Any CPU = Debug|Any CPU
@@ -200,6 +212,18 @@ Global
200212
{C75F2BBE-7C84-4B01-9836-7279DAE41499}.Debug|Any CPU.Build.0 = Debug|Any CPU
201213
{C75F2BBE-7C84-4B01-9836-7279DAE41499}.Release|Any CPU.ActiveCfg = Release|Any CPU
202214
{C75F2BBE-7C84-4B01-9836-7279DAE41499}.Release|Any CPU.Build.0 = Release|Any CPU
215+
{2E4C9BFE-C49C-0567-D73C-F2915AB62CA6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
216+
{2E4C9BFE-C49C-0567-D73C-F2915AB62CA6}.Debug|Any CPU.Build.0 = Debug|Any CPU
217+
{2E4C9BFE-C49C-0567-D73C-F2915AB62CA6}.Release|Any CPU.ActiveCfg = Release|Any CPU
218+
{2E4C9BFE-C49C-0567-D73C-F2915AB62CA6}.Release|Any CPU.Build.0 = Release|Any CPU
219+
{E144492A-337A-0755-EAB4-DA083C3A2DDB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
220+
{E144492A-337A-0755-EAB4-DA083C3A2DDB}.Debug|Any CPU.Build.0 = Debug|Any CPU
221+
{E144492A-337A-0755-EAB4-DA083C3A2DDB}.Release|Any CPU.ActiveCfg = Release|Any CPU
222+
{E144492A-337A-0755-EAB4-DA083C3A2DDB}.Release|Any CPU.Build.0 = Release|Any CPU
223+
{2B39D4E2-C6B6-4340-A9AD-5F5C25CA8C1D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
224+
{2B39D4E2-C6B6-4340-A9AD-5F5C25CA8C1D}.Debug|Any CPU.Build.0 = Debug|Any CPU
225+
{2B39D4E2-C6B6-4340-A9AD-5F5C25CA8C1D}.Release|Any CPU.ActiveCfg = Release|Any CPU
226+
{2B39D4E2-C6B6-4340-A9AD-5F5C25CA8C1D}.Release|Any CPU.Build.0 = Release|Any CPU
203227
EndGlobalSection
204228
GlobalSection(SolutionProperties) = preSolution
205229
HideSolutionNode = FALSE
@@ -227,6 +251,7 @@ Global
227251
{3D6441FC-0FE8-4D0C-910D-3D9310599C71} = {3F96ECB4-1644-43E8-8643-2CDCF9E679F1}
228252
{D1B0719F-4F84-4DBC-BCAD-E856FB3193D7} = {8BAC2322-AD3C-484A-B51D-8263BC4E6646}
229253
{1E62D4FB-CC59-4F1E-BB22-574CEC08C94B} = {BBB79D3E-5DF2-4FF6-B467-52D0EEB91C4B}
254+
{B753CEB9-EA53-4AE1-997E-B7D54A299D58} = {65504C76-7E32-4A12-A42E-BCDA4FE79BC1}
230255
{2A6989CB-B273-4841-BD3E-7B1BBA4DD25F} = {EF45C0B9-ED76-4B7A-A0A7-F102E979B71C}
231256
{BCC66C37-4980-484F-819D-066D2FF2669C} = {EF45C0B9-ED76-4B7A-A0A7-F102E979B71C}
232257
{52CDA3F4-F090-4224-978A-5F42388DCF92} = {3F96ECB4-1644-43E8-8643-2CDCF9E679F1}
@@ -235,6 +260,12 @@ Global
235260
{4B5A00D6-B9F1-449F-A9D2-80E860D6BD75} = {65504C76-7E32-4A12-A42E-BCDA4FE79BC1}
236261
{EFC7D088-EF45-464B-97CD-0BBA486B224A} = {BBB79D3E-5DF2-4FF6-B467-52D0EEB91C4B}
237262
{C75F2BBE-7C84-4B01-9836-7279DAE41499} = {8BAC2322-AD3C-484A-B51D-8263BC4E6646}
263+
{02EA681E-C7D8-13C7-8484-4AC65E1B71E8} = {3AF63EC9-2EB0-4A0B-8C3B-0CA3595080F6}
264+
{3AF63EC9-2EB0-4A0B-8C3B-0CA3595080F6} = {41B69207-8F29-41BC-9114-78EE740485C8}
265+
{2E4C9BFE-C49C-0567-D73C-F2915AB62CA6} = {02EA681E-C7D8-13C7-8484-4AC65E1B71E8}
266+
{E144492A-337A-0755-EAB4-DA083C3A2DDB} = {02EA681E-C7D8-13C7-8484-4AC65E1B71E8}
267+
{4D3E429C-CCAE-42DE-A062-4717E71D8403} = {3AF63EC9-2EB0-4A0B-8C3B-0CA3595080F6}
268+
{2B39D4E2-C6B6-4340-A9AD-5F5C25CA8C1D} = {4D3E429C-CCAE-42DE-A062-4717E71D8403}
238269
EndGlobalSection
239270
GlobalSection(ExtensibilityGlobals) = postSolution
240271
SolutionGuid = {E48A5088-1BBB-4A8B-9AB2-CC5CE0482466}

dotnetv3/Bedrock-runtime/Models/Ai21LabsJurassic2/Converse/Converse.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
</PropertyGroup>
77

88
<ItemGroup>
9-
<PackageReference Include="AWSSDK.BedrockRuntime" Version="3.7.403.14" />
10-
<PackageReference Include="AWSSDK.Core" Version="3.7.400.26" />
9+
<PackageReference Include="AWSSDK.BedrockRuntime" Version="3.7.412.14" />
10+
<PackageReference Include="AWSSDK.Core" Version="3.7.402.2" />
1111
</ItemGroup>
1212
</Project>

dotnetv3/Bedrock-runtime/Models/Ai21LabsJurassic2/InvokeModel/InvokeModel.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
</PropertyGroup>
77

88
<ItemGroup>
9-
<PackageReference Include="AWSSDK.BedrockRuntime" Version="3.7.403.14" />
10-
<PackageReference Include="AWSSDK.Core" Version="3.7.400.26" />
9+
<PackageReference Include="AWSSDK.BedrockRuntime" Version="3.7.412.14" />
10+
<PackageReference Include="AWSSDK.Core" Version="3.7.402.2" />
1111
</ItemGroup>
1212
</Project>
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
// snippet-start:[BedrockRuntime.dotnetv3.InvokeModel_AmazonNovaImageGeneration]
5+
// Use the native inference API to create an image with Amazon Nova Canvas.
6+
7+
using System;
8+
using System.IO;
9+
using System.Text.Json;
10+
using System.Text.Json.Nodes;
11+
using Amazon;
12+
using Amazon.BedrockRuntime;
13+
using Amazon.BedrockRuntime.Model;
14+
15+
// Create a Bedrock Runtime client in the AWS Region you want to use.
16+
var client = new AmazonBedrockRuntimeClient(RegionEndpoint.USEast1);
17+
18+
// Set the model ID.
19+
var modelId = "amazon.nova-canvas-v1:0";
20+
21+
// Define the image generation prompt for the model.
22+
var prompt = "A stylized picture of a cute old steampunk robot.";
23+
24+
// Create a random seed between 0 and 858,993,459
25+
int seed = new Random().Next(0, 858993460);
26+
27+
//Format the request payload using the model's native structure.
28+
var nativeRequest = JsonSerializer.Serialize(new
29+
{
30+
taskType = "TEXT_IMAGE",
31+
textToImageParams = new
32+
{
33+
text = prompt
34+
},
35+
imageGenerationConfig = new
36+
{
37+
seed,
38+
quality = "standard",
39+
width = 512,
40+
height = 512,
41+
numberOfImages = 1
42+
}
43+
});
44+
45+
// Create a request with the model ID and the model's native request payload.
46+
var request = new InvokeModelRequest()
47+
{
48+
ModelId = modelId,
49+
Body = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(nativeRequest)),
50+
ContentType = "application/json"
51+
};
52+
53+
try
54+
{
55+
// Send the request to the Bedrock Runtime and wait for the response.
56+
var response = await client.InvokeModelAsync(request);
57+
58+
// Decode the response body.
59+
var modelResponse = await JsonNode.ParseAsync(response.Body);
60+
61+
// Extract the image data.
62+
var base64Image = modelResponse["images"]?[0].ToString() ?? "";
63+
64+
// Save the image in a local folder
65+
string savedPath = AmazonNovaCanvas.InvokeModel.SaveBase64Image(base64Image);
66+
Console.WriteLine($"Image saved to: {savedPath}");
67+
}
68+
catch (AmazonBedrockRuntimeException e)
69+
{
70+
Console.WriteLine($"ERROR: Can't invoke '{modelId}'. Reason: {e.Message}");
71+
throw;
72+
}
73+
74+
// snippet-end:[BedrockRuntime.dotnetv3.InvokeModel_AmazonNovaImageGeneration]
75+
76+
// Create a partial class to make the top-level script testable.
77+
namespace AmazonNovaCanvas
78+
{
79+
public partial class InvokeModel
80+
{
81+
public static string SaveBase64Image(string base64String, string outputFolderName = "generated-images")
82+
{
83+
// Get the directory where the script is located
84+
string scriptDirectory = AppDomain.CurrentDomain.BaseDirectory;
85+
86+
// Navigate to the script's folder
87+
if (scriptDirectory.Contains("bin"))
88+
{
89+
scriptDirectory = Directory.GetParent(scriptDirectory)?.Parent?.Parent?.Parent?.FullName
90+
?? throw new DirectoryNotFoundException("Could not find script directory");
91+
}
92+
93+
// Combine script directory with output folder
94+
string outputPath = Path.Combine(scriptDirectory, outputFolderName);
95+
96+
// Create directory if it doesn't exist
97+
if (!Directory.Exists(outputPath))
98+
{
99+
Directory.CreateDirectory(outputPath);
100+
}
101+
102+
// Remove base64 header if present (e.g., "data:image/jpeg;base64,")
103+
string base64Data = base64String;
104+
if (base64String.Contains(","))
105+
{
106+
base64Data = base64String.Split(',')[1];
107+
}
108+
109+
// Convert base64 to bytes
110+
byte[] imageBytes = Convert.FromBase64String(base64Data);
111+
112+
// Find the next available number
113+
int fileNumber = 1;
114+
string filePath;
115+
do
116+
{
117+
string paddedNumber = fileNumber.ToString("D2"); // Pads with leading zero
118+
filePath = Path.Combine(outputPath, $"image_{paddedNumber}.jpg");
119+
fileNumber++;
120+
} while (File.Exists(filePath));
121+
122+
// Save the image
123+
File.WriteAllBytes(filePath, imageBytes);
124+
125+
return filePath;
126+
}
127+
}
128+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<AssemblyName>AmazonNovaCanvas.$(MSBuildProjectName)</AssemblyName>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="AWSSDK.BedrockRuntime" Version="3.7.412.14" />
10+
<PackageReference Include="AWSSDK.Core" Version="3.7.402.2" />
11+
</ItemGroup>
12+
</Project>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
// snippet-start:[BedrockRuntime.dotnetv3.Converse_AmazonNovaText]
5+
// Use the Converse API to send a text message to Amazon Nova.
6+
7+
using System;
8+
using System.Collections.Generic;
9+
using Amazon;
10+
using Amazon.BedrockRuntime;
11+
using Amazon.BedrockRuntime.Model;
12+
13+
// Create a Bedrock Runtime client in the AWS Region you want to use.
14+
var client = new AmazonBedrockRuntimeClient(RegionEndpoint.USEast1);
15+
16+
// Set the model ID, e.g., Amazon Nova Lite.
17+
var modelId = "amazon.nova-lite-v1:0";
18+
19+
// Define the user message.
20+
var userMessage = "Describe the purpose of a 'hello world' program in one line.";
21+
22+
// Create a request with the model ID, the user message, and an inference configuration.
23+
var request = new ConverseRequest
24+
{
25+
ModelId = modelId,
26+
Messages = new List<Message>
27+
{
28+
new Message
29+
{
30+
Role = ConversationRole.User,
31+
Content = new List<ContentBlock> { new ContentBlock { Text = userMessage } }
32+
}
33+
},
34+
InferenceConfig = new InferenceConfiguration()
35+
{
36+
MaxTokens = 512,
37+
Temperature = 0.5F,
38+
TopP = 0.9F
39+
}
40+
};
41+
42+
try
43+
{
44+
// Send the request to the Bedrock Runtime and wait for the result.
45+
var response = await client.ConverseAsync(request);
46+
47+
// Extract and print the response text.
48+
string responseText = response?.Output?.Message?.Content?[0]?.Text ?? "";
49+
Console.WriteLine(responseText);
50+
}
51+
catch (AmazonBedrockRuntimeException e)
52+
{
53+
Console.WriteLine($"ERROR: Can't invoke '{modelId}'. Reason: {e.Message}");
54+
throw;
55+
}
56+
57+
// snippet-end:[BedrockRuntime.dotnetv3.Converse_AmazonNovaText]
58+
59+
// Create a partial class to make the top-level script testable.
60+
namespace AmazonNovaText { public partial class Converse { } }
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<AssemblyName>AmazonNovaText.$(MSBuildProjectName)</AssemblyName>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="AWSSDK.BedrockRuntime" Version="3.7.412.14" />
10+
<PackageReference Include="AWSSDK.Core" Version="3.7.402.2" />
11+
</ItemGroup>
12+
</Project>

0 commit comments

Comments
 (0)