Skip to content

Commit 103927a

Browse files
authored
.NET v3: Bedrock Converse API with Tool Feature Scenario Scout (#7229)
1 parent ae2207e commit 103927a

File tree

19 files changed

+1166
-9
lines changed

19 files changed

+1166
-9
lines changed

.doc_gen/metadata/bedrock-runtime_metadata.yaml

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ bedrock-runtime_Converse_AmazonNovaText:
108108
- description: Send a text message to Amazon Nova, using Bedrock's Converse API.
109109
snippet_tags:
110110
- BedrockRuntime.dotnetv3.Converse_AmazonNovaText
111+
- description: Send a conversation of messages to Amazon Nova using Bedrock's Converse API with a tool configuration.
112+
genai: some
113+
snippet_tags:
114+
- Bedrock.ConverseTool.dotnetv3.SendConverseRequest
111115
Python:
112116
versions:
113117
- sdk_version: 3
@@ -163,6 +167,60 @@ bedrock-runtime_Converse_AmazonTitanText:
163167
services:
164168
bedrock-runtime: {Converse}
165169

170+
bedrock-runtime_Scenario_ToolUse:
171+
title: "A tool use example illustrating how to connect AI models on &BR; with a custom tool or API"
172+
title_abbrev: "Tool use with the Converse API"
173+
synopsis: "build a typical interaction between an application, a generative AI model, and connected tools or APIs to mediate interactions between the AI and the outside world. It uses the example of connecting an external weather API to the AI model so it can provide real-time weather information based on user input."
174+
category: Scenarios
175+
languages:
176+
.NET:
177+
versions:
178+
- sdk_version: 3
179+
github: dotnetv3/Bedrock-runtime/Scenarios/ConverseToolScenario
180+
excerpts:
181+
- description: "The primary execution of the scenario flow. This scenario orchestrates the conversation between the user, the &BR; Converse API, and a weather tool."
182+
genai: some
183+
snippet_tags:
184+
- Bedrock.ConverseTool.dotnetv3.Scenario
185+
- description: "The weather tool used by the demo. This file defines the tool specification and implements the logic to retrieve weather data using from the Open-Meteo API."
186+
genai: some
187+
snippet_tags:
188+
- Bedrock.ConverseTool.dotnetv3.WeatherTool
189+
- description: "The Converse API action with a tool configuration."
190+
genai: some
191+
snippet_tags:
192+
- Bedrock.ConverseTool.dotnetv3.SendConverseRequest
193+
Python:
194+
versions:
195+
- sdk_version: 3
196+
github: python/example_code/bedrock-runtime
197+
excerpts:
198+
- description: "The primary execution script of the demo. This script orchestrates the conversation between the user, the &BR; Converse API, and a weather tool."
199+
snippet_files:
200+
- python/example_code/bedrock-runtime/cross-model-scenarios/tool_use_demo/tool_use_demo.py
201+
- description: "The weather tool used by the demo. This script defines the tool specification and implements the logic to retrieve weather data using from the Open-Meteo API."
202+
snippet_files:
203+
- python/example_code/bedrock-runtime/cross-model-scenarios/tool_use_demo/weather_tool.py
204+
Rust:
205+
versions:
206+
- sdk_version: 1
207+
github: rustv1/examples/bedrock-runtime
208+
excerpts:
209+
- description: "The primary scenario and logic for the demo. This orchestrates the conversation between the user, the &BR; Converse API, and a weather tool."
210+
snippet_tags:
211+
- rust.bedrock-runtime.Converse_AnthropicClaude.tool-use
212+
- description: "The weather tool used by the demo. This script defines the tool specification and implements the logic to retrieve weather data using from the Open-Meteo API."
213+
snippet_tags:
214+
- rust.bedrock-runtime.Converse_AnthropicClaude.tool-use.weather-tool
215+
- description: "Utilities to print the Message Content Blocks."
216+
snippet_tags:
217+
- rust.bedrock-runtime.Converse_AnthropicClaude.tool-use.user-interface
218+
- description: "Use statements, Error utility, and constants."
219+
snippet_tags:
220+
- rust.bedrock-runtime.Converse_AnthropicClaude.tool-use.supporting
221+
services:
222+
bedrock-runtime: {Converse}
223+
166224
bedrock-runtime_Converse_AnthropicClaude:
167225
title: Invoke Anthropic Claude on &BR; using Bedrock's Converse API
168226
title_abbrev: "Converse"
@@ -1315,6 +1373,32 @@ bedrock-runtime_InvokeModelWithResponseStream_TitanTextEmbeddings:
13151373
bedrock-runtime: {InvokeModel}
13161374

13171375
# Tool use scenarios
1376+
bedrock-runtime_Scenario_ToolUseDemo_AmazonNova:
1377+
title: "A tool use demo illustrating how to connect AI models on &BR; with a custom tool or API"
1378+
title_abbrev: "Scenario: Tool use with the Converse API"
1379+
synopsis: "build a typical interaction between an application, a generative AI model, and connected tools or APIs to mediate interactions between the AI and the outside world. It uses the example of connecting an external weather API to the AI model so it can provide real-time weather information based on user input."
1380+
category: Amazon Nova
1381+
languages:
1382+
.NET:
1383+
versions:
1384+
- sdk_version: 3
1385+
github: dotnetv3/Bedrock-runtime/Scenarios/ConverseToolScenario
1386+
excerpts:
1387+
- description: "The primary execution of the scenario flow. This scenario orchestrates the conversation between the user, the &BR; Converse API, and a weather tool."
1388+
genai: some
1389+
snippet_tags:
1390+
- Bedrock.ConverseTool.dotnetv3.Scenario
1391+
- description: "The weather tool used by the demo. This file defines the tool specification and implements the logic to retrieve weather data using from the Open-Meteo API."
1392+
genai: some
1393+
snippet_tags:
1394+
- Bedrock.ConverseTool.dotnetv3.WeatherTool
1395+
- description: "The Converse API action with a tool configuration."
1396+
genai: some
1397+
snippet_tags:
1398+
- Bedrock.ConverseTool.dotnetv3.SendConverseRequest
1399+
services:
1400+
bedrock-runtime: {Converse}
1401+
13181402
bedrock-runtime_Scenario_ToolUseDemo_AnthropicClaude:
13191403
title: "A tool use demo illustrating how to connect AI models on &BR; with a custom tool or API"
13201404
title_abbrev: "Scenario: Tool use with the Converse API"
@@ -1349,7 +1433,6 @@ bedrock-runtime_Scenario_ToolUseDemo_AnthropicClaude:
13491433
- description: "Use statements, Error utility, and constants."
13501434
snippet_tags:
13511435
- rust.bedrock-runtime.Converse_AnthropicClaude.tool-use.supporting
1352-
13531436
services:
13541437
bedrock-runtime: {Converse}
13551438

dotnetv3/Bedrock-runtime/BedrockRuntimeExamples.sln

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,14 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "AmazonNovaCanvas", "AmazonN
106106
EndProject
107107
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "InvokeModel", "Models\AmazonNova\AmazonNovaCanvas\InvokeModel\InvokeModel.csproj", "{2B39D4E2-C6B6-4340-A9AD-5F5C25CA8C1D}"
108108
EndProject
109+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Actions", "Actions", "{FDC95D1E-41C6-45A5-BF29-F76FCC3DAEF9}"
110+
EndProject
111+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BedrockRuntimeActions", "Actions\BedrockRuntimeActions.csproj", "{ABA0C307-C7A1-4BBE-A7E2-4BA7163559FC}"
112+
EndProject
113+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Scenarios", "Scenarios", "{045D214B-6181-43B0-ABFE-246675F4D967}"
114+
EndProject
115+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConverseToolScenario", "Scenarios\ConverseToolScenario\ConverseToolScenario.csproj", "{C0A5B872-03F5-4865-9349-7A403591C50E}"
116+
EndProject
109117
Global
110118
GlobalSection(SolutionConfigurationPlatforms) = preSolution
111119
Debug|Any CPU = Debug|Any CPU
@@ -224,6 +232,14 @@ Global
224232
{2B39D4E2-C6B6-4340-A9AD-5F5C25CA8C1D}.Debug|Any CPU.Build.0 = Debug|Any CPU
225233
{2B39D4E2-C6B6-4340-A9AD-5F5C25CA8C1D}.Release|Any CPU.ActiveCfg = Release|Any CPU
226234
{2B39D4E2-C6B6-4340-A9AD-5F5C25CA8C1D}.Release|Any CPU.Build.0 = Release|Any CPU
235+
{ABA0C307-C7A1-4BBE-A7E2-4BA7163559FC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
236+
{ABA0C307-C7A1-4BBE-A7E2-4BA7163559FC}.Debug|Any CPU.Build.0 = Debug|Any CPU
237+
{ABA0C307-C7A1-4BBE-A7E2-4BA7163559FC}.Release|Any CPU.ActiveCfg = Release|Any CPU
238+
{ABA0C307-C7A1-4BBE-A7E2-4BA7163559FC}.Release|Any CPU.Build.0 = Release|Any CPU
239+
{C0A5B872-03F5-4865-9349-7A403591C50E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
240+
{C0A5B872-03F5-4865-9349-7A403591C50E}.Debug|Any CPU.Build.0 = Debug|Any CPU
241+
{C0A5B872-03F5-4865-9349-7A403591C50E}.Release|Any CPU.ActiveCfg = Release|Any CPU
242+
{C0A5B872-03F5-4865-9349-7A403591C50E}.Release|Any CPU.Build.0 = Release|Any CPU
227243
EndGlobalSection
228244
GlobalSection(SolutionProperties) = preSolution
229245
HideSolutionNode = FALSE
@@ -266,6 +282,8 @@ Global
266282
{E144492A-337A-0755-EAB4-DA083C3A2DDB} = {02EA681E-C7D8-13C7-8484-4AC65E1B71E8}
267283
{4D3E429C-CCAE-42DE-A062-4717E71D8403} = {3AF63EC9-2EB0-4A0B-8C3B-0CA3595080F6}
268284
{2B39D4E2-C6B6-4340-A9AD-5F5C25CA8C1D} = {4D3E429C-CCAE-42DE-A062-4717E71D8403}
285+
{ABA0C307-C7A1-4BBE-A7E2-4BA7163559FC} = {FDC95D1E-41C6-45A5-BF29-F76FCC3DAEF9}
286+
{C0A5B872-03F5-4865-9349-7A403591C50E} = {045D214B-6181-43B0-ABFE-246675F4D967}
269287
EndGlobalSection
270288
GlobalSection(ExtensibilityGlobals) = postSolution
271289
SolutionGuid = {E48A5088-1BBB-4A8B-9AB2-CC5CE0482466}

dotnetv3/Bedrock-runtime/README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ For prerequisites, see the [README](../README.md#Prerequisites) in the `dotnetv3
2828

2929
<!--custom.prerequisites.start-->
3030
<!--custom.prerequisites.end-->
31+
### Scenarios
32+
33+
Code examples that show you how to accomplish a specific task by calling multiple
34+
functions within the same service.
35+
36+
- [Tool use with the Converse API](Scenarios/ConverseToolScenario/ConverseToolScenario.cs)
37+
3138
### AI21 Labs Jurassic-2
3239

3340
- [Converse](Models/Ai21LabsJurassic2/Converse/Converse.cs#L4)
@@ -37,6 +44,7 @@ For prerequisites, see the [README](../README.md#Prerequisites) in the `dotnetv3
3744

3845
- [Converse](Models/AmazonNova/AmazonNovaText/Converse/Converse.cs#L4)
3946
- [ConverseStream](Models/AmazonNova/AmazonNovaText/ConverseStream/ConverseStream.cs#L4)
47+
- [Scenario: Tool use with the Converse API](Scenarios/ConverseToolScenario/ConverseToolScenario.cs#L4)
4048

4149
### Amazon Nova Canvas
4250

@@ -110,6 +118,18 @@ Alternatively, you can run the example from within your IDE.
110118

111119

112120

121+
#### Tool use with the Converse API
122+
123+
This example shows you how to build a typical interaction between an application, a generative AI model, and connected tools or APIs to mediate interactions between the AI and the outside world. It uses the example of connecting an external weather API to the AI model so it can provide real-time weather information based on user input.
124+
125+
126+
<!--custom.scenario_prereqs.bedrock-runtime_Scenario_ToolUse.start-->
127+
<!--custom.scenario_prereqs.bedrock-runtime_Scenario_ToolUse.end-->
128+
129+
130+
<!--custom.scenarios.bedrock-runtime_Scenario_ToolUse.start-->
131+
<!--custom.scenarios.bedrock-runtime_Scenario_ToolUse.end-->
132+
113133
### Tests
114134

115135
⚠ Running tests might result in charges to your AWS account.
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
using Amazon.BedrockRuntime;
5+
using Amazon.BedrockRuntime.Model;
6+
using Microsoft.Extensions.Logging;
7+
8+
namespace ConverseToolScenario;
9+
10+
// snippet-start:[Bedrock.ConverseTool.dotnetv3.SendConverseRequest]
11+
12+
/// <summary>
13+
/// Wrapper class for interacting with the Amazon Bedrock Converse API.
14+
/// </summary>
15+
public class BedrockActionsWrapper
16+
{
17+
private readonly IAmazonBedrockRuntime _bedrockClient;
18+
private readonly ILogger<BedrockActionsWrapper> _logger;
19+
20+
/// <summary>
21+
/// Initializes a new instance of the <see cref="BedrockActionsWrapper"/> class.
22+
/// </summary>
23+
/// <param name="bedrockClient">The Bedrock Converse API client.</param>
24+
/// <param name="logger">The logger instance.</param>
25+
public BedrockActionsWrapper(IAmazonBedrockRuntime bedrockClient, ILogger<BedrockActionsWrapper> logger)
26+
{
27+
_bedrockClient = bedrockClient;
28+
_logger = logger;
29+
}
30+
31+
/// <summary>
32+
/// Sends a Converse request to the Amazon Bedrock Converse API.
33+
/// </summary>
34+
/// <param name="modelId">The Bedrock Model Id.</param>
35+
/// <param name="systemPrompt">A system prompt instruction.</param>
36+
/// <param name="conversation">The array of messages in the conversation.</param>
37+
/// <param name="toolSpec">The specification for a tool.</param>
38+
/// <returns>The response of the model.</returns>
39+
public async Task<ConverseResponse> SendConverseRequestAsync(string modelId, string systemPrompt, List<Message> conversation, ToolSpecification toolSpec)
40+
{
41+
try
42+
{
43+
var request = new ConverseRequest()
44+
{
45+
ModelId = modelId,
46+
System = new List<SystemContentBlock>()
47+
{
48+
new SystemContentBlock()
49+
{
50+
Text = systemPrompt
51+
}
52+
},
53+
Messages = conversation,
54+
ToolConfig = new ToolConfiguration()
55+
{
56+
Tools = new List<Tool>()
57+
{
58+
new Tool()
59+
{
60+
ToolSpec = toolSpec
61+
}
62+
}
63+
}
64+
};
65+
66+
var response = await _bedrockClient.ConverseAsync(request);
67+
68+
return response;
69+
}
70+
catch (ModelNotReadyException ex)
71+
{
72+
_logger.LogError(ex, "Model not ready, please wait and try again.");
73+
throw;
74+
}
75+
catch (AmazonBedrockRuntimeException ex)
76+
{
77+
_logger.LogError(ex, "Error occurred while sending Converse request.");
78+
throw;
79+
}
80+
}
81+
}
82+
// snippet-end:[Bedrock.ConverseTool.dotnetv3.SendConverseRequest]

0 commit comments

Comments
 (0)