-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Python, C#, JavaScript: Examples for the new Amazon Bedrock Converse …
…API (#6499)
- Loading branch information
1 parent
92274d3
commit a89efb2
Showing
133 changed files
with
4,698 additions
and
584 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/.vs/ | ||
/Tools/ |
Large diffs are not rendered by default.
Oops, something went wrong.
15 changes: 15 additions & 0 deletions
15
dotnetv3/Bedrock-runtime/Models/Ai21LabsJurassic2/Converse/Converse.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net8.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<Compile Include="Converse.csx" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="AWSSDK.BedrockRuntime" Version="3.7.303.1" /> | ||
<PackageReference Include="AWSSDK.Core" Version="3.7.304.10" /> | ||
</ItemGroup> | ||
</Project> |
57 changes: 57 additions & 0 deletions
57
dotnetv3/Bedrock-runtime/Models/Ai21LabsJurassic2/Converse/Converse.csx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
// snippet-start:[BedrockRuntime.dotnetv3.Converse_Ai21LabsJurassic2] | ||
// Use the Converse API to send a text message to AI21 Labs Jurassic-2. | ||
|
||
using Amazon; | ||
using Amazon.BedrockRuntime; | ||
using Amazon.BedrockRuntime.Model; | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
// Create a Bedrock Runtime client in the AWS Region you want to use. | ||
var client = new AmazonBedrockRuntimeClient(RegionEndpoint.USEast1); | ||
|
||
// Set the model ID, e.g., Jurassic-2 Mid. | ||
var modelId = "ai21.j2-mid-v1"; | ||
|
||
// Define the user message. | ||
var userMessage = "Describe the purpose of a 'hello world' program in one line."; | ||
|
||
// Create a request with the model ID, the user message, and an inference configuration. | ||
var request = new ConverseRequest | ||
{ | ||
ModelId = modelId, | ||
Messages = new List<Message> | ||
{ | ||
new Message | ||
{ | ||
Role = ConversationRole.User, | ||
Content = new List<ContentBlock> { new ContentBlock { Text = userMessage } } | ||
} | ||
}, | ||
InferenceConfig = new InferenceConfiguration() | ||
{ | ||
MaxTokens = 512, | ||
Temperature = 0.5F, | ||
TopP = 0.9F | ||
} | ||
}; | ||
|
||
try | ||
{ | ||
// Send the request to the Bedrock Runtime and wait for the result. | ||
var response = await client.ConverseAsync(request); | ||
|
||
// Extract and print the response text. | ||
string responseText = response?.Output?.Message?.Content?[0]?.Text ?? ""; | ||
Console.WriteLine(responseText); | ||
} | ||
catch (AmazonBedrockRuntimeException e) | ||
{ | ||
Console.WriteLine($"ERROR: Can't invoke '{modelId}'. Reason: {e.Message}"); | ||
throw; | ||
} | ||
|
||
// snippet-end:[BedrockRuntime.dotnetv3.Converse_Ai21LabsJurassic2] |
15 changes: 15 additions & 0 deletions
15
dotnetv3/Bedrock-runtime/Models/Ai21LabsJurassic2/InvokeModel/InvokeModel.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net8.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<Compile Include="InvokeModel.csx" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="AWSSDK.BedrockRuntime" Version="3.7.303.1" /> | ||
<PackageReference Include="AWSSDK.Core" Version="3.7.304.10" /> | ||
</ItemGroup> | ||
</Project> |
58 changes: 58 additions & 0 deletions
58
dotnetv3/Bedrock-runtime/Models/Ai21LabsJurassic2/InvokeModel/InvokeModel.csx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
// snippet-start:[BedrockRuntime.dotnetv3.InvokeModel_Ai21LabsJurassic2] | ||
// Use the native inference API to send a text message to AI21 Labs Jurassic-2. | ||
|
||
using Amazon; | ||
using Amazon.BedrockRuntime; | ||
using Amazon.BedrockRuntime.Model; | ||
using System; | ||
using System.IO; | ||
using System.Text.Json; | ||
using System.Text.Json.Nodes; | ||
|
||
// Create a Bedrock Runtime client in the AWS Region you want to use. | ||
var client = new AmazonBedrockRuntimeClient(RegionEndpoint.USEast1); | ||
|
||
// Set the model ID, e.g., Jurassic-2 Mid. | ||
var modelId = "ai21.j2-mid-v1"; | ||
|
||
// Define the user message. | ||
var userMessage = "Describe the purpose of a 'hello world' program in one line."; | ||
|
||
//Format the request payload using the model's native structure. | ||
var nativeRequest = JsonSerializer.Serialize(new | ||
{ | ||
prompt = userMessage, | ||
maxTokens = 512, | ||
temperature = 0.5 | ||
}); | ||
|
||
// Create a request with the model ID and the model's native request payload. | ||
var request = new InvokeModelRequest() | ||
{ | ||
ModelId = modelId, | ||
Body = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(nativeRequest)), | ||
ContentType = "application/json" | ||
}; | ||
|
||
try | ||
{ | ||
// Send the request to the Bedrock Runtime and wait for the response. | ||
var response = await client.InvokeModelAsync(request); | ||
|
||
// Decode the response body. | ||
var modelResponse = await JsonNode.ParseAsync(response.Body); | ||
|
||
// Extract and print the response text. | ||
var responseText = modelResponse["completions"]?[0]?["data"]?["text"] ?? ""; | ||
Console.WriteLine(responseText); | ||
} | ||
catch (AmazonBedrockRuntimeException e) | ||
{ | ||
Console.WriteLine($"ERROR: Can't invoke '{modelId}'. Reason: {e.Message}"); | ||
throw; | ||
} | ||
|
||
// snippet-end:[BedrockRuntime.dotnetv3.InvokeModel_Ai21LabsJurassic2] |
15 changes: 15 additions & 0 deletions
15
dotnetv3/Bedrock-runtime/Models/AmazonTitanText/Converse/Converse.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net8.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<Compile Include="Converse.csx" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="AWSSDK.BedrockRuntime" Version="3.7.303.1" /> | ||
<PackageReference Include="AWSSDK.Core" Version="3.7.304.10" /> | ||
</ItemGroup> | ||
</Project> |
57 changes: 57 additions & 0 deletions
57
dotnetv3/Bedrock-runtime/Models/AmazonTitanText/Converse/Converse.csx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
// snippet-start:[BedrockRuntime.dotnetv3.Converse_AmazonTitanText] | ||
// Use the Converse API to send a text message to Amazon Titan Text. | ||
|
||
using Amazon; | ||
using Amazon.BedrockRuntime; | ||
using Amazon.BedrockRuntime.Model; | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
// Create a Bedrock Runtime client in the AWS Region you want to use. | ||
var client = new AmazonBedrockRuntimeClient(RegionEndpoint.USEast1); | ||
|
||
// Set the model ID, e.g., Titan Text Premier. | ||
var modelId = "amazon.titan-text-premier-v1:0"; | ||
|
||
// Define the user message. | ||
var userMessage = "Describe the purpose of a 'hello world' program in one line."; | ||
|
||
// Create a request with the model ID, the user message, and an inference configuration. | ||
var request = new ConverseRequest | ||
{ | ||
ModelId = modelId, | ||
Messages = new List<Message> | ||
{ | ||
new Message | ||
{ | ||
Role = ConversationRole.User, | ||
Content = new List<ContentBlock> { new ContentBlock { Text = userMessage } } | ||
} | ||
}, | ||
InferenceConfig = new InferenceConfiguration() | ||
{ | ||
MaxTokens = 512, | ||
Temperature = 0.5F, | ||
TopP = 0.9F | ||
} | ||
}; | ||
|
||
try | ||
{ | ||
// Send the request to the Bedrock Runtime and wait for the result. | ||
var response = await client.ConverseAsync(request); | ||
|
||
// Extract and print the response text. | ||
string responseText = response?.Output?.Message?.Content?[0]?.Text ?? ""; | ||
Console.WriteLine(responseText); | ||
} | ||
catch (AmazonBedrockRuntimeException e) | ||
{ | ||
Console.WriteLine($"ERROR: Can't invoke '{modelId}'. Reason: {e.Message}"); | ||
throw; | ||
} | ||
|
||
// snippet-end:[BedrockRuntime.dotnetv3.Converse_AmazonTitanText] |
15 changes: 15 additions & 0 deletions
15
dotnetv3/Bedrock-runtime/Models/AmazonTitanText/ConverseStream/ConverseStream.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net8.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<Compile Include="ConverseStream.csx" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="AWSSDK.BedrockRuntime" Version="3.7.303.1" /> | ||
<PackageReference Include="AWSSDK.Core" Version="3.7.304.10" /> | ||
</ItemGroup> | ||
</Project> |
64 changes: 64 additions & 0 deletions
64
dotnetv3/Bedrock-runtime/Models/AmazonTitanText/ConverseStream/ConverseStream.csx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
// snippet-start:[BedrockRuntime.dotnetv3.ConverseStream_AmazonTitanText] | ||
// Use the Converse API to send a text message to Amazon Titan Text | ||
// and print the response stream. | ||
|
||
using Amazon; | ||
using Amazon.BedrockRuntime; | ||
using Amazon.BedrockRuntime.Model; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
// Create a Bedrock Runtime client in the AWS Region you want to use. | ||
var client = new AmazonBedrockRuntimeClient(RegionEndpoint.USEast1); | ||
|
||
// Set the model ID, e.g., Titan Text Premier. | ||
var modelId = "amazon.titan-text-premier-v1:0"; | ||
|
||
// Define the user message. | ||
var userMessage = "Describe the purpose of a 'hello world' program in one line."; | ||
|
||
// Create a request with the model ID, the user message, and an inference configuration. | ||
var request = new ConverseStreamRequest | ||
{ | ||
ModelId = modelId, | ||
Messages = new List<Message> | ||
{ | ||
new Message | ||
{ | ||
Role = ConversationRole.User, | ||
Content = new List<ContentBlock> { new ContentBlock { Text = userMessage } } | ||
} | ||
}, | ||
InferenceConfig = new InferenceConfiguration() | ||
{ | ||
MaxTokens = 512, | ||
Temperature = 0.5F, | ||
TopP = 0.9F | ||
} | ||
}; | ||
|
||
try | ||
{ | ||
// Send the request to the Bedrock Runtime and wait for the result. | ||
var response = await client.ConverseStreamAsync(request); | ||
|
||
// Extract and print the streamed response text in real-time. | ||
foreach (var chunk in response.Stream.AsEnumerable()) | ||
{ | ||
if (chunk is ContentBlockDeltaEvent) | ||
{ | ||
Console.Write((chunk as ContentBlockDeltaEvent).Delta.Text); | ||
} | ||
} | ||
} | ||
catch (AmazonBedrockRuntimeException e) | ||
{ | ||
Console.WriteLine($"ERROR: Can't invoke '{modelId}'. Reason: {e.Message}"); | ||
throw; | ||
} | ||
|
||
// snippet-end:[BedrockRuntime.dotnetv3.ConverseStream_AmazonTitanText] |
15 changes: 15 additions & 0 deletions
15
dotnetv3/Bedrock-runtime/Models/AmazonTitanText/InvokeModel/InvokeModel.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net8.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<Compile Include="InvokeModel.csx" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="AWSSDK.BedrockRuntime" Version="3.7.303.1" /> | ||
<PackageReference Include="AWSSDK.Core" Version="3.7.304.10" /> | ||
</ItemGroup> | ||
</Project> |
61 changes: 61 additions & 0 deletions
61
dotnetv3/Bedrock-runtime/Models/AmazonTitanText/InvokeModel/InvokeModel.csx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
// snippet-start:[BedrockRuntime.dotnetv3.InvokeModel_AmazonTitanText] | ||
// Use the native inference API to send a text message to Amazon Titan Text. | ||
|
||
using Amazon; | ||
using Amazon.BedrockRuntime; | ||
using Amazon.BedrockRuntime.Model; | ||
using System; | ||
using System.IO; | ||
using System.Text.Json; | ||
using System.Text.Json.Nodes; | ||
|
||
// Create a Bedrock Runtime client in the AWS Region you want to use. | ||
var client = new AmazonBedrockRuntimeClient(RegionEndpoint.USEast1); | ||
|
||
// Set the model ID, e.g., Titan Text Premier. | ||
var modelId = "amazon.titan-text-premier-v1:0"; | ||
|
||
// Define the user message. | ||
var userMessage = "Describe the purpose of a 'hello world' program in one line."; | ||
|
||
//Format the request payload using the model's native structure. | ||
var nativeRequest = JsonSerializer.Serialize(new | ||
{ | ||
inputText = userMessage, | ||
textGenerationConfig = new | ||
{ | ||
maxTokenCount = 512, | ||
temperature = 0.5 | ||
} | ||
}); | ||
|
||
// Create a request with the model ID and the model's native request payload. | ||
var request = new InvokeModelRequest() | ||
{ | ||
ModelId = modelId, | ||
Body = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(nativeRequest)), | ||
ContentType = "application/json" | ||
}; | ||
|
||
try | ||
{ | ||
// Send the request to the Bedrock Runtime and wait for the response. | ||
var response = await client.InvokeModelAsync(request); | ||
|
||
// Decode the response body. | ||
var modelResponse = await JsonNode.ParseAsync(response.Body); | ||
|
||
// Extract and print the response text. | ||
var responseText = modelResponse["results"]?[0]?["outputText"] ?? ""; | ||
Console.WriteLine(responseText); | ||
} | ||
catch (AmazonBedrockRuntimeException e) | ||
{ | ||
Console.WriteLine($"ERROR: Can't invoke '{modelId}'. Reason: {e.Message}"); | ||
throw; | ||
} | ||
|
||
// snippet-end:[BedrockRuntime.dotnetv3.InvokeModel_AmazonTitanText] |
Oops, something went wrong.