Skip to content

Commit e65586c

Browse files
committed
Adding scenario files.
1 parent c7af517 commit e65586c

File tree

12 files changed

+297
-381
lines changed

12 files changed

+297
-381
lines changed

.doc_gen/metadata/bedrock-runtime_metadata.yaml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,9 @@ bedrock-runtime_Converse_AnthropicClaude:
138138
- description: Send a text message to Anthropic Claude, using Bedrock's Converse API.
139139
snippet_tags:
140140
- BedrockRuntime.dotnetv3.Converse_AnthropicClaude
141+
- description: Send a conversation of messages to Anthropic Claude, using Bedrock's Converse API with a tool configuration.
142+
snippet_tags:
143+
- Bedrock.ConverseTool.dotnetv3.SendConverseRequest
141144
Python:
142145
versions:
143146
- sdk_version: 3
@@ -1197,6 +1200,20 @@ bedrock-runtime_Scenario_ToolUseDemo_AnthropicClaude:
11971200
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."
11981201
category: Anthropic Claude
11991202
languages:
1203+
.NET:
1204+
versions:
1205+
- sdk_version: 3
1206+
github: dotnetv3/Bedrock-runtime/Scenarios/ConverseToolScenario
1207+
excerpts:
1208+
- description: "The primary execution of the scenario flow. This scenario orchestrates the conversation between the user, the &BR; Converse API, and a weather tool."
1209+
snippet_tags:
1210+
- Bedrock.ConverseTool.dotnetv3.Scenario
1211+
- 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."
1212+
snippet_tags:
1213+
- Bedrock.ConverseTool.dotnetv3.WeatherTool
1214+
- description: "The Converse API action with a tool configuration."
1215+
snippet_tags:
1216+
- Bedrock.ConverseTool.dotnetv3.SendConverseRequest
12001217
Python:
12011218
versions:
12021219
- sdk_version: 3

dotnetv3/Bedrock-runtime/Scenarios/ConverseToolScenario/BedrockActionsWrapper.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
namespace ConverseToolScenario;
99

10+
// snippet-start:[Bedrock.ConverseTool.dotnetv3.SendConverseRequest]
11+
1012
/// <summary>
1113
/// Wrapper class for interacting with the Amazon Bedrock Converse API.
1214
/// </summary>
@@ -26,7 +28,6 @@ public BedrockActionsWrapper(IAmazonBedrockRuntime bedrockClient, ILogger<Bedroc
2628
_logger = logger;
2729
}
2830

29-
3031
/// <summary>
3132
/// Sends a Converse request to the Amazon Bedrock Converse API.
3233
/// </summary>
@@ -66,10 +67,16 @@ public async Task<ConverseResponse> SendConverseRequestAsync(string modelId, str
6667

6768
return response;
6869
}
70+
catch (ModelNotReadyException ex)
71+
{
72+
_logger.LogError(ex, "Model not ready, please wait and try again.");
73+
throw;
74+
}
6975
catch (AmazonBedrockRuntimeException ex)
7076
{
7177
_logger.LogError(ex, "Error occurred while sending Converse request.");
7278
throw;
7379
}
7480
}
75-
}
81+
}
82+
// snippet-end:[Bedrock.ConverseTool.dotnetv3.SendConverseRequest]

dotnetv3/Bedrock-runtime/Scenarios/ConverseToolScenario/ConverseToolScenario.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4+
// snippet-start:[Bedrock.ConverseTool.dotnetv3.Scenario]
5+
46
using Amazon.BedrockRuntime;
57
using Amazon.BedrockRuntime.Model;
68
using Amazon.Runtime.Documents;
@@ -352,5 +354,5 @@ public static void PrintModelResponse(string message)
352354
Console.WriteLine(message);
353355
Console.WriteLine();
354356
}
355-
356-
}
357+
}
358+
// snippet-end:[Bedrock.ConverseTool.dotnetv3.Scenario]
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Bedrock Runtime Converse API with Tool Feature Scenario
2+
3+
## Overview
4+
5+
This example shows how to use AWS SDKs and the Amazon Bedrock Converse API to call a custom tool from a large language model (LLM) as part of a multistep conversation. The example creates a weather tool that leverages the Open-Meteo API to retrieve current weather information based on user input.
6+
7+
[Bedrock Converse API with Tool Definition](https://docs.aws.amazon.com/bedrock/latest/userguide/tool-use-inference-call.html).
8+
9+
## ⚠ Important
10+
11+
* Running this code might result in charges to your AWS account.
12+
* Running the tests might result in charges to your AWS account.
13+
* We recommend that you grant your code least privilege. At most, grant only the minimum permissions required to perform the task. For more information, see [Grant least privilege](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#grant-least-privilege).
14+
* This code is not tested in every AWS Region. For more information, see [AWS Regional Services](https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services).
15+
16+
## Scenario
17+
18+
This example illustrates a typical interaction between a generative AI model, an application, and connected tools or APIs to solve a problem or achieve a specific goal. The scenario follows these steps:
19+
20+
1. Set up the system prompt and tool configuration.
21+
2. Specify the AI model to be used (e.g., Anthropic Claude 3 Sonnet).
22+
3. Create a client to interact with Amazon Bedrock.
23+
4. Prompt the user for their weather request.
24+
5. Send the user input including the conversation history to the model.
25+
6. The model processes the input and determines if a connected tool or API needs to be used. If this is the case, the model returns a tool use request with specific parameters needed to invoke the tool, and a unique tool use ID to correlate tool responses to the request.
26+
7. The scenario application invokes the tool to fetch weather data, and append the response and tool use ID to the conversation.
27+
8. The model uses the tool response to generate a final response. If additional tool requests are needed, the process is repeated.
28+
9. Once the final response is received and printed, the application returns to the prompt.
29+
30+
### Prerequisites
31+
32+
For general prerequisites, see the [README](../../../README.md) in the `dotnetv3` folder.
33+
34+
### Resources
35+
36+
No additional resources are needed for this scenario.
37+
38+
### Instructions
39+
40+
After the example compiles, you can run it from the command line. To do so, navigate to
41+
the folder that contains the .sln file and run the following command:
42+
43+
```
44+
dotnet run
45+
```
46+
47+
Alternatively, you can run the example from within your IDE.
48+
49+
This starts an interactive scenario that walks you through exploring conditional requests for read, write, and copy operations.
50+
51+
## Additional resources
52+
53+
- [Documentation: The Amazon Bedrock User Guide](https://docs.aws.amazon.com/bedrock/latest/userguide/what-is-bedrock.html)
54+
- [Tutorials: A developer's guide to Bedrock's new Converse API](https://community.aws/content/2dtauBCeDa703x7fDS9Q30MJoBA/amazon-bedrock-converse-api-developer-guide)
55+
- [More examples: Amazon Bedrock code examples and scenarios in multiple programming languages](https://docs.aws.amazon.com/bedrock/latest/userguide/service_code_examples.html)
56+
57+
---
58+
59+
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. SPDX-License-Identifier: Apache-2.0

dotnetv3/Bedrock-runtime/Scenarios/ConverseToolScenario/ToolResponse.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4+
// snippet-start:[Bedrock.ConverseTool.dotnetv3.ToolResponse]
5+
46
namespace ConverseToolScenario;
57

68
/// <summary>
@@ -10,4 +12,5 @@ public class ToolResponse
1012
{
1113
public string ToolUseId { get; set; } = null!;
1214
public dynamic Content { get; set; } = null!;
13-
}
15+
}
16+
// snippet-end:[Bedrock.ConverseTool.dotnetv3.ToolResponse]

dotnetv3/Bedrock-runtime/Scenarios/ConverseToolScenario/ToolSpec.json

Lines changed: 0 additions & 22 deletions
This file was deleted.

dotnetv3/Bedrock-runtime/Scenarios/ConverseToolScenario/WeatherTool.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4+
// snippet-start:[Bedrock.ConverseTool.dotnetv3.WeatherTool]
5+
46
using Amazon.BedrockRuntime.Model;
57
using Amazon.Runtime.Documents;
68
using Microsoft.Extensions.Logging;
@@ -92,4 +94,5 @@ public async Task<Document> FetchWeatherDataAsync(string latitude, string longit
9294
throw;
9395
}
9496
}
95-
}
97+
}
98+
// snippet-end:[Bedrock.ConverseTool.dotnetv3.WeatherTool]

dotnetv4/Bedrock/BedrockExamples.sln

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{898AFE57
1111
EndProject
1212
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BedrockTests", "Tests\BedrockTests.csproj", "{5486426B-A8E8-4C6A-BEE2-83DD7CDB68A6}"
1313
EndProject
14-
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Scenarios", "Scenarios", "{A3F7526C-B026-497B-A9A5-D503BB53EF72}"
15-
EndProject
16-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConverseToolScenario", "Scenarios\ConverseToolScenario\ConverseToolScenario.csproj", "{1CF238B1-BCA5-4615-9F43-36BC2D61E7C6}"
17-
EndProject
1814
Global
1915
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2016
Debug|Any CPU = Debug|Any CPU
@@ -29,18 +25,13 @@ Global
2925
{5486426B-A8E8-4C6A-BEE2-83DD7CDB68A6}.Debug|Any CPU.Build.0 = Debug|Any CPU
3026
{5486426B-A8E8-4C6A-BEE2-83DD7CDB68A6}.Release|Any CPU.ActiveCfg = Release|Any CPU
3127
{5486426B-A8E8-4C6A-BEE2-83DD7CDB68A6}.Release|Any CPU.Build.0 = Release|Any CPU
32-
{1CF238B1-BCA5-4615-9F43-36BC2D61E7C6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
33-
{1CF238B1-BCA5-4615-9F43-36BC2D61E7C6}.Debug|Any CPU.Build.0 = Debug|Any CPU
34-
{1CF238B1-BCA5-4615-9F43-36BC2D61E7C6}.Release|Any CPU.ActiveCfg = Release|Any CPU
35-
{1CF238B1-BCA5-4615-9F43-36BC2D61E7C6}.Release|Any CPU.Build.0 = Release|Any CPU
3628
EndGlobalSection
3729
GlobalSection(SolutionProperties) = preSolution
3830
HideSolutionNode = FALSE
3931
EndGlobalSection
4032
GlobalSection(NestedProjects) = preSolution
4133
{C47E3B3E-0040-4CB6-AB92-EF4395C1EB83} = {0DD1E95E-9EF2-4E43-86B3-F636736BE054}
4234
{5486426B-A8E8-4C6A-BEE2-83DD7CDB68A6} = {898AFE57-24C6-4D79-81C2-614873B38F62}
43-
{1CF238B1-BCA5-4615-9F43-36BC2D61E7C6} = {A3F7526C-B026-497B-A9A5-D503BB53EF72}
4435
EndGlobalSection
4536
GlobalSection(ExtensibilityGlobals) = postSolution
4637
SolutionGuid = {3D82D9F6-BE3C-40F1-9224-B8E4D746FC2E}

dotnetv4/Bedrock/Scenarios/ConverseToolScenario/ConverseToolScenario.cs

Lines changed: 0 additions & 104 deletions
This file was deleted.

dotnetv4/Bedrock/Scenarios/ConverseToolScenario/ConverseToolScenario.csproj

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
 (0)