Cookie の設定を選択する

当社は、当社のサイトおよびサービスを提供するために必要な必須 Cookie および類似のツールを使用しています。当社は、パフォーマンス Cookie を使用して匿名の統計情報を収集することで、お客様が当社のサイトをどのように利用しているかを把握し、改善に役立てています。必須 Cookie は無効化できませんが、[カスタマイズ] または [拒否] をクリックしてパフォーマンス Cookie を拒否することはできます。

お客様が同意した場合、AWS および承認された第三者は、Cookie を使用して便利なサイト機能を提供したり、お客様の選択を記憶したり、関連する広告を含む関連コンテンツを表示したりします。すべての必須ではない Cookie を受け入れるか拒否するには、[受け入れる] または [拒否] をクリックしてください。より詳細な選択を行うには、[カスタマイズ] をクリックしてください。

Amazon Bedrock の AI モデルをカスタムツールまたは API に接続する方法を示すツールの使用例

フォーカスモード
Amazon Bedrock の AI モデルをカスタムツールまたは API に接続する方法を示すツールの使用例 - Amazon Bedrock

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

次のコード例は、アプリケーション、生成 AI モデル、接続されたツールまたは API 間の一般的なインタラクションを構築し、AI と外部世界のインタラクションを仲介する方法を示しています。外部気象 API を AI モデルに接続する例を使用して、ユーザー入力に基づいてリアルタイムの気象情報を提供します。

.NET
SDK for .NET
注記

GitHub には、その他のリソースもあります。AWS コード例リポジトリ で全く同じ例を見つけて、設定と実行の方法を確認してください。

シナリオフローのプライマリ実行。このシナリオでは、ユーザー、Amazon Bedrock Converse API、および気象ツール間の会話を調整します。

using Amazon; using Amazon.BedrockRuntime; using Amazon.BedrockRuntime.Model; using Amazon.Runtime.Documents; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Http; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Console; namespace ConverseToolScenario; public static class ConverseToolScenario { /* Before running this .NET code example, set up your development environment, including your credentials. This demo illustrates a tool use scenario using Amazon Bedrock's Converse API and a weather tool. The script interacts with a foundation model on Amazon Bedrock to provide weather information based on user input. It uses the Open-Meteo API (https://open-meteo.com) to retrieve current weather data for a given location. */ public static BedrockActionsWrapper _bedrockActionsWrapper = null!; public static WeatherTool _weatherTool = null!; public static bool _interactive = true; // Change this string to use a different model with Converse API. private static string model_id = "amazon.nova-lite-v1:0"; private static string system_prompt = @" You are a weather assistant that provides current weather data for user-specified locations using only the Weather_Tool, which expects latitude and longitude. Infer the coordinates from the location yourself. If the user specifies a state, country, or region, infer the locations of cities within that state. If the user provides coordinates, infer the approximate location and refer to it in your response. To use the tool, you strictly apply the provided tool specification. - Explain your step-by-step process, and give brief updates before each step. - Only use the Weather_Tool for data. Never guess or make up information. - Repeat the tool use for subsequent requests if necessary. - If the tool errors, apologize, explain weather is unavailable, and suggest other options. - Report temperatures in °C (°F) and wind in km/h (mph). Keep weather reports concise. Sparingly use emojis where appropriate. - Only respond to weather queries. Remind off-topic users of your purpose. - Never claim to search online, access external data, or use tools besides Weather_Tool. - Complete the entire process until you have all required data before sending the complete response. " ; private static string default_prompt = "What is the weather like in Seattle?"; // The maximum number of recursive calls allowed in the tool use function. // This helps prevent infinite loops and potential performance issues. private static int max_recursions = 5; public static async Task Main(string[] args) { // Set up dependency injection for the Amazon service. using var host = Host.CreateDefaultBuilder(args) .ConfigureLogging(logging => logging.AddFilter("System", LogLevel.Error) .AddFilter<ConsoleLoggerProvider>("Microsoft", LogLevel.Trace)) .ConfigureServices((_, services) => services.AddHttpClient() .AddSingleton<IAmazonBedrockRuntime>(_ => new AmazonBedrockRuntimeClient(RegionEndpoint.USEast1)) // Specify a region that has access to the chosen model. .AddTransient<BedrockActionsWrapper>() .AddTransient<WeatherTool>() .RemoveAll<IHttpMessageHandlerBuilderFilter>() ) .Build(); ServicesSetup(host); try { await RunConversationAsync(); } catch (Exception ex) { Console.WriteLine(new string('-', 80)); Console.WriteLine($"There was a problem running the scenario: {ex.Message}"); Console.WriteLine(new string('-', 80)); } finally { Console.WriteLine( "Amazon Bedrock Converse API with Tool Use Feature Scenario is complete."); Console.WriteLine(new string('-', 80)); } } /// <summary> /// Populate the services for use within the console application. /// </summary> /// <param name="host">The services host.</param> private static void ServicesSetup(IHost host) { _bedrockActionsWrapper = host.Services.GetRequiredService<BedrockActionsWrapper>(); _weatherTool = host.Services.GetRequiredService<WeatherTool>(); } /// <summary> /// Starts the conversation with the user and handles the interaction with Bedrock. /// </summary> /// <returns>The conversation array.</returns> public static async Task<List<Message>> RunConversationAsync() { // Print the greeting and a short user guide PrintHeader(); // Start with an empty conversation var conversation = new List<Message>(); // Get the first user input var userInput = await GetUserInputAsync(); while (userInput != null) { // Create a new message with the user input and append it to the conversation var message = new Message { Role = ConversationRole.User, Content = new List<ContentBlock> { new ContentBlock { Text = userInput } } }; conversation.Add(message); // Send the conversation to Amazon Bedrock var bedrockResponse = await SendConversationToBedrock(conversation); // Recursively handle the model's response until the model has returned its final response or the recursion counter has reached 0 await ProcessModelResponseAsync(bedrockResponse, conversation, max_recursions); // Repeat the loop until the user decides to exit the application userInput = await GetUserInputAsync(); } PrintFooter(); return conversation; } /// <summary> /// Sends the conversation, the system prompt, and the tool spec to Amazon Bedrock, and returns the response. /// </summary> /// <param name="conversation">The conversation history including the next message to send.</param> /// <returns>The response from Amazon Bedrock.</returns> private static async Task<ConverseResponse> SendConversationToBedrock(List<Message> conversation) { Console.WriteLine("\tCalling Bedrock..."); // Send the conversation, system prompt, and tool configuration, and return the response return await _bedrockActionsWrapper.SendConverseRequestAsync(model_id, system_prompt, conversation, _weatherTool.GetToolSpec()); } /// <summary> /// Processes the response received via Amazon Bedrock and performs the necessary actions based on the stop reason. /// </summary> /// <param name="modelResponse">The model's response returned via Amazon Bedrock.</param> /// <param name="conversation">The conversation history.</param> /// <param name="maxRecursion">The maximum number of recursive calls allowed.</param> private static async Task ProcessModelResponseAsync(ConverseResponse modelResponse, List<Message> conversation, int maxRecursion) { if (maxRecursion <= 0) { // Stop the process, the number of recursive calls could indicate an infinite loop Console.WriteLine("\tWarning: Maximum number of recursions reached. Please try again."); } // Append the model's response to the ongoing conversation conversation.Add(modelResponse.Output.Message); if (modelResponse.StopReason == "tool_use") { // If the stop reason is "tool_use", forward everything to the tool use handler await HandleToolUseAsync(modelResponse.Output, conversation, maxRecursion - 1); } if (modelResponse.StopReason == "end_turn") { // If the stop reason is "end_turn", print the model's response text, and finish the process PrintModelResponse(modelResponse.Output.Message.Content[0].Text); if (!_interactive) { default_prompt = "x"; } } } /// <summary> /// Handles the tool use case by invoking the specified tool and sending the tool's response back to Bedrock. /// The tool response is appended to the conversation, and the conversation is sent back to Amazon Bedrock for further processing. /// </summary> /// <param name="modelResponse">The model's response containing the tool use request.</param> /// <param name="conversation">The conversation history.</param> /// <param name="maxRecursion">The maximum number of recursive calls allowed.</param> public static async Task HandleToolUseAsync(ConverseOutput modelResponse, List<Message> conversation, int maxRecursion) { // Initialize an empty list of tool results var toolResults = new List<ContentBlock>(); // The model's response can consist of multiple content blocks foreach (var contentBlock in modelResponse.Message.Content) { if (!String.IsNullOrEmpty(contentBlock.Text)) { // If the content block contains text, print it to the console PrintModelResponse(contentBlock.Text); } if (contentBlock.ToolUse != null) { // If the content block is a tool use request, forward it to the tool var toolResponse = await InvokeTool(contentBlock.ToolUse); // Add the tool use ID and the tool's response to the list of results toolResults.Add(new ContentBlock { ToolResult = new ToolResultBlock() { ToolUseId = toolResponse.ToolUseId, Content = new List<ToolResultContentBlock>() { new ToolResultContentBlock { Json = toolResponse.Content } } } }); } } // Embed the tool results in a new user message var message = new Message() { Role = ConversationRole.User, Content = toolResults }; // Append the new message to the ongoing conversation conversation.Add(message); // Send the conversation to Amazon Bedrock var response = await SendConversationToBedrock(conversation); // Recursively handle the model's response until the model has returned its final response or the recursion counter has reached 0 await ProcessModelResponseAsync(response, conversation, maxRecursion); } /// <summary> /// Invokes the specified tool with the given payload and returns the tool's response. /// If the requested tool does not exist, an error message is returned. /// </summary> /// <param name="payload">The payload containing the tool name and input data.</param> /// <returns>The tool's response or an error message.</returns> public static async Task<ToolResponse> InvokeTool(ToolUseBlock payload) { var toolName = payload.Name; if (toolName == "Weather_Tool") { var inputData = payload.Input.AsDictionary(); PrintToolUse(toolName, inputData); // Invoke the weather tool with the input data provided var weatherResponse = await _weatherTool.FetchWeatherDataAsync(inputData["latitude"].ToString(), inputData["longitude"].ToString()); return new ToolResponse { ToolUseId = payload.ToolUseId, Content = weatherResponse }; } else { var errorMessage = $"\tThe requested tool with name '{toolName}' does not exist."; return new ToolResponse { ToolUseId = payload.ToolUseId, Content = new { error = true, message = errorMessage } }; } } /// <summary> /// Prompts the user for input and returns the user's response. /// Returns null if the user enters 'x' to exit. /// </summary> /// <param name="prompt">The prompt to display to the user.</param> /// <returns>The user's input or null if the user chooses to exit.</returns> private static async Task<string?> GetUserInputAsync(string prompt = "\tYour weather info request:") { var userInput = default_prompt; if (_interactive) { Console.WriteLine(new string('*', 80)); Console.WriteLine($"{prompt} (x to exit): \n\t"); userInput = Console.ReadLine(); } if (string.IsNullOrWhiteSpace(userInput)) { prompt = "\tPlease enter your weather info request, e.g. the name of a city"; return await GetUserInputAsync(prompt); } if (userInput.ToLowerInvariant() == "x") { return null; } return userInput; } /// <summary> /// Logs the welcome message and usage guide for the tool use demo. /// </summary> public static void PrintHeader() { Console.WriteLine(@" ================================================= Welcome to the Amazon Bedrock Tool Use demo! ================================================= This assistant provides current weather information for user-specified locations. You can ask for weather details by providing the location name or coordinates. Weather information will be provided using a custom Tool and open-meteo API. Example queries: - What's the weather like in New York? - Current weather for latitude 40.70, longitude -74.01 - Is it warmer in Rome or Barcelona today? To exit the program, simply type 'x' and press Enter. P.S.: You're not limited to single locations, or even to using English! Have fun and experiment with the app! "); } /// <summary> /// Logs the footer information for the tool use demo. /// </summary> public static void PrintFooter() { Console.WriteLine(@" ================================================= Thank you for checking out the Amazon Bedrock Tool Use demo. We hope you learned something new, or got some inspiration for your own apps today! For more Bedrock examples in different programming languages, have a look at: https://docs.aws.amazon.com/bedrock/latest/userguide/service_code_examples.html ================================================= "); } /// <summary> /// Logs information about the tool use. /// </summary> /// <param name="toolName">The name of the tool being used.</param> /// <param name="inputData">The input data for the tool.</param> public static void PrintToolUse(string toolName, Dictionary<string, Document> inputData) { Console.WriteLine($"\n\tInvoking tool: {toolName} with input: {inputData["latitude"].ToString()}, {inputData["longitude"].ToString()}...\n"); } /// <summary> /// Logs the model's response. /// </summary> /// <param name="message">The model's response message.</param> public static void PrintModelResponse(string message) { Console.WriteLine("\tThe model's response:\n"); Console.WriteLine(message); Console.WriteLine(); } }

デモで使用される気象ツール。このファイルは、ツール仕様を定義し、Open-Meteo API から を使用して気象データを取得するロジックを実装します。

using Amazon.BedrockRuntime.Model; using Amazon.Runtime.Documents; using Microsoft.Extensions.Logging; namespace ConverseToolScenario; /// <summary> /// Weather tool that will be invoked when requested by the Bedrock response. /// </summary> public class WeatherTool { private readonly ILogger<WeatherTool> _logger; private readonly IHttpClientFactory _httpClientFactory; public WeatherTool(ILogger<WeatherTool> logger, IHttpClientFactory httpClientFactory) { _logger = logger; _httpClientFactory = httpClientFactory; } /// <summary> /// Returns the JSON Schema specification for the Weather tool. The tool specification /// defines the input schema and describes the tool's functionality. /// For more information, see https://json-schema.org/understanding-json-schema/reference. /// </summary> /// <returns>The tool specification for the Weather tool.</returns> public ToolSpecification GetToolSpec() { ToolSpecification toolSpecification = new ToolSpecification(); toolSpecification.Name = "Weather_Tool"; toolSpecification.Description = "Get the current weather for a given location, based on its WGS84 coordinates."; Document toolSpecDocument = Document.FromObject( new { type = "object", properties = new { latitude = new { type = "string", description = "Geographical WGS84 latitude of the location." }, longitude = new { type = "string", description = "Geographical WGS84 longitude of the location." } }, required = new[] { "latitude", "longitude" } }); toolSpecification.InputSchema = new ToolInputSchema() { Json = toolSpecDocument }; return toolSpecification; } /// <summary> /// Fetches weather data for the given latitude and longitude using the Open-Meteo API. /// Returns the weather data or an error message if the request fails. /// </summary> /// <param name="latitude">The latitude of the location.</param> /// <param name="longitude">The longitude of the location.</param> /// <returns>The weather data or an error message.</returns> public async Task<Document> FetchWeatherDataAsync(string latitude, string longitude) { string endpoint = "https://api.open-meteo.com/v1/forecast"; try { var httpClient = _httpClientFactory.CreateClient(); var response = await httpClient.GetAsync($"{endpoint}?latitude={latitude}&longitude={longitude}&current_weather=True"); response.EnsureSuccessStatusCode(); var weatherData = await response.Content.ReadAsStringAsync(); Document weatherDocument = Document.FromObject( new { weather_data = weatherData }); return weatherDocument; } catch (HttpRequestException e) { _logger.LogError(e, "Error fetching weather data: {Message}", e.Message); throw; } catch (Exception e) { _logger.LogError(e, "Unexpected error fetching weather data: {Message}", e.Message); throw; } } }

ツール設定を使用した Converse API アクション。

/// <summary> /// Wrapper class for interacting with the Amazon Bedrock Converse API. /// </summary> public class BedrockActionsWrapper { private readonly IAmazonBedrockRuntime _bedrockClient; private readonly ILogger<BedrockActionsWrapper> _logger; /// <summary> /// Initializes a new instance of the <see cref="BedrockActionsWrapper"/> class. /// </summary> /// <param name="bedrockClient">The Bedrock Converse API client.</param> /// <param name="logger">The logger instance.</param> public BedrockActionsWrapper(IAmazonBedrockRuntime bedrockClient, ILogger<BedrockActionsWrapper> logger) { _bedrockClient = bedrockClient; _logger = logger; } /// <summary> /// Sends a Converse request to the Amazon Bedrock Converse API. /// </summary> /// <param name="modelId">The Bedrock Model Id.</param> /// <param name="systemPrompt">A system prompt instruction.</param> /// <param name="conversation">The array of messages in the conversation.</param> /// <param name="toolSpec">The specification for a tool.</param> /// <returns>The response of the model.</returns> public async Task<ConverseResponse> SendConverseRequestAsync(string modelId, string systemPrompt, List<Message> conversation, ToolSpecification toolSpec) { try { var request = new ConverseRequest() { ModelId = modelId, System = new List<SystemContentBlock>() { new SystemContentBlock() { Text = systemPrompt } }, Messages = conversation, ToolConfig = new ToolConfiguration() { Tools = new List<Tool>() { new Tool() { ToolSpec = toolSpec } } } }; var response = await _bedrockClient.ConverseAsync(request); return response; } catch (ModelNotReadyException ex) { _logger.LogError(ex, "Model not ready, please wait and try again."); throw; } catch (AmazonBedrockRuntimeException ex) { _logger.LogError(ex, "Error occurred while sending Converse request."); throw; } } }
  • API の詳細については、「AWS SDK for .NET API リファレンス」の「Converse」を参照してください。

Java
SDK for Java 2.x
注記

GitHub には、その他のリソースもあります。AWS コード例リポジトリ で全く同じ例を見つけて、設定と実行の方法を確認してください。

シナリオフローのプライマリ実行。このシナリオでは、ユーザー、Amazon Bedrock Converse API、および気象ツール間の会話を調整します。

/* This demo illustrates a tool use scenario using Amazon Bedrock's Converse API and a weather tool. The program interacts with a foundation model on Amazon Bedrock to provide weather information based on user input. It uses the Open-Meteo API (https://open-meteo.com) to retrieve current weather data for a given location. */ public class BedrockScenario { public static final String DASHES = new String(new char[80]).replace("\0", "-"); private static String modelId = "amazon.nova-lite-v1:0"; private static String defaultPrompt = "What is the weather like in Seattle?"; private static WeatherTool weatherTool = new WeatherTool(); // The maximum number of recursive calls allowed in the tool use function. // This helps prevent infinite loops and potential performance issues. private static int maxRecursions = 5; static BedrockActions bedrockActions = new BedrockActions(); public static boolean interactive = true; private static final String systemPrompt = """ You are a weather assistant that provides current weather data for user-specified locations using only the Weather_Tool, which expects latitude and longitude. Infer the coordinates from the location yourself. If the user provides coordinates, infer the approximate location and refer to it in your response. To use the tool, you strictly apply the provided tool specification. - Explain your step-by-step process, and give brief updates before each step. - Only use the Weather_Tool for data. Never guess or make up information. - Repeat the tool use for subsequent requests if necessary. - If the tool errors, apologize, explain weather is unavailable, and suggest other options. - Report temperatures in °C (°F) and wind in km/h (mph). Keep weather reports concise. Sparingly use emojis where appropriate. - Only respond to weather queries. Remind off-topic users of your purpose. - Never claim to search online, access external data, or use tools besides Weather_Tool. - Complete the entire process until you have all required data before sending the complete response. """; public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println(""" ================================================= Welcome to the Amazon Bedrock Tool Use demo! ================================================= This assistant provides current weather information for user-specified locations. You can ask for weather details by providing the location name or coordinates. Example queries: - What's the weather like in New York? - Current weather for latitude 40.70, longitude -74.01 - Is it warmer in Rome or Barcelona today? To exit the program, simply type 'x' and press Enter. P.S.: You're not limited to single locations, or even to using English! Have fun and experiment with the app! """); System.out.println(DASHES); try { runConversation(scanner); } catch (Exception ex) { System.out.println("There was a problem running the scenario: " + ex.getMessage()); } waitForInputToContinue(scanner); System.out.println(DASHES); System.out.println("Amazon Bedrock Converse API with Tool Use Feature Scenario is complete."); System.out.println(DASHES); } /** * Starts the conversation with the user and handles the interaction with Bedrock. */ private static List<Message> runConversation(Scanner scanner) { List<Message> conversation = new ArrayList<>(); // Get the first user input String userInput = getUserInput("Your weather info request:", scanner); System.out.println(userInput); while (userInput != null) { ContentBlock block = ContentBlock.builder() .text(userInput) .build(); List<ContentBlock> blockList = new ArrayList<>(); blockList.add(block); Message message = Message.builder() .role(ConversationRole.USER) .content(blockList) .build(); conversation.add(message); // Send the conversation to Amazon Bedrock. ConverseResponse bedrockResponse = sendConversationToBedrock(conversation); // Recursively handle the model's response until the model has returned its final response or the recursion counter has reached 0. processModelResponse(bedrockResponse, conversation, maxRecursions); // Repeat the loop until the user decides to exit the application. userInput = getUserInput("Your weather info request:", scanner); } printFooter(); return conversation; } /** * Processes the response from the model and updates the conversation accordingly. * * @param modelResponse the response from the model * @param conversation the ongoing conversation * @param maxRecursion the maximum number of recursions allowed */ private static void processModelResponse(ConverseResponse modelResponse, List<Message> conversation, int maxRecursion) { if (maxRecursion <= 0) { // Stop the process, the number of recursive calls could indicate an infinite loop System.out.println("\tWarning: Maximum number of recursions reached. Please try again."); } // Append the model's response to the ongoing conversation conversation.add(modelResponse.output().message()); String modelResponseVal = modelResponse.stopReasonAsString(); if (modelResponseVal.compareTo("tool_use") == 0) { // If the stop reason is "tool_use", forward everything to the tool use handler handleToolUse(modelResponse.output(), conversation, maxRecursion - 1); } if (modelResponseVal.compareTo("end_turn") == 0) { // If the stop reason is "end_turn", print the model's response text, and finish the process PrintModelResponse(modelResponse.output().message().content().get(0).text()); if (!interactive) { defaultPrompt = "x"; } } } /** * Handles the use of a tool by the model in a conversation. * * @param modelResponse the response from the model, which may include a tool use request * @param conversation the current conversation, which will be updated with the tool use results * @param maxRecursion the maximum number of recursive calls allowed to handle the model's response */ private static void handleToolUse(ConverseOutput modelResponse, List<Message> conversation, int maxRecursion) { List<ContentBlock> toolResults = new ArrayList<>(); // The model's response can consist of multiple content blocks for (ContentBlock contentBlock : modelResponse.message().content()) { if (contentBlock.text() != null && !contentBlock.text().isEmpty()) { // If the content block contains text, print it to the console PrintModelResponse(contentBlock.text()); } if (contentBlock.toolUse() != null) { ToolResponse toolResponse = invokeTool(contentBlock.toolUse()); // Add the tool use ID and the tool's response to the list of results List<ToolResultContentBlock> contentBlockList = new ArrayList<>(); ToolResultContentBlock block = ToolResultContentBlock.builder() .json(toolResponse.getContent()) .build(); contentBlockList.add(block); ToolResultBlock toolResultBlock = ToolResultBlock.builder() .toolUseId(toolResponse.getToolUseId()) .content(contentBlockList) .build(); ContentBlock contentBlock1 = ContentBlock.builder() .toolResult(toolResultBlock) .build(); toolResults.add(contentBlock1); } } // Embed the tool results in a new user message Message message = Message.builder() .role(ConversationRole.USER) .content(toolResults) .build(); // Append the new message to the ongoing conversation //conversation.add(message); conversation.add(message); // Send the conversation to Amazon Bedrock var response = sendConversationToBedrock(conversation); // Recursively handle the model's response until the model has returned its final response or the recursion counter has reached 0 processModelResponse(response, conversation, maxRecursion); } // Invokes the specified tool with the given payload and returns the tool's response. // If the requested tool does not exist, an error message is returned. private static ToolResponse invokeTool(ToolUseBlock payload) { String toolName = payload.name(); if (Objects.equals(toolName, "Weather_Tool")) { Map<String, Document> inputData = payload.input().asMap(); printToolUse(toolName, inputData); // Invoke the weather tool with the input data provided Document weatherResponse = weatherTool.fetchWeatherData(inputData.get("latitude").toString(), inputData.get("longitude").toString()); ToolResponse toolResponse = new ToolResponse(); toolResponse.setContent(weatherResponse); toolResponse.setToolUseId(payload.toolUseId()); return toolResponse; } else { String errorMessage = "The requested tool with name " + toolName + " does not exist."; System.out.println(errorMessage); return null; } } public static void printToolUse(String toolName, Map<String, Document> inputData) { System.out.println("Invoking tool: " + toolName + " with input: " + inputData.get("latitude").toString() + ", " + inputData.get("longitude").toString() + "..."); } private static void PrintModelResponse(String message) { System.out.println("\tThe model's response:\n"); System.out.println(message); System.out.println(""); } private static ConverseResponse sendConversationToBedrock(List<Message> conversation) { System.out.println("Calling Bedrock..."); try { return bedrockActions.sendConverseRequestAsync(modelId, systemPrompt, conversation, weatherTool.getToolSpec()); } catch (ModelNotReadyException ex) { System.err.println("Model is not ready. Please try again later: " + ex.getMessage()); throw ex; } catch (BedrockRuntimeException ex) { System.err.println("Bedrock service error: " + ex.getMessage()); throw ex; } catch (RuntimeException ex) { System.err.println("Unexpected error occurred: " + ex.getMessage()); throw ex; } } private static ConverseResponse sendConversationToBedrockwithSpec(List<Message> conversation, ToolSpecification toolSpec) { System.out.println("Calling Bedrock..."); // Send the conversation, system prompt, and tool configuration, and return the response return bedrockActions.sendConverseRequestAsync(modelId, systemPrompt, conversation, toolSpec); } public static String getUserInput(String prompt, Scanner scanner) { String userInput = defaultPrompt; if (interactive) { System.out.println("*".repeat(80)); System.out.println(prompt + " (x to exit): \n\t"); userInput = scanner.nextLine(); } if (userInput == null || userInput.trim().isEmpty()) { return getUserInput("\tPlease enter your weather info request, e.g., the name of a city", scanner); } if (userInput.equalsIgnoreCase("x")) { return null; } return userInput; } private static void waitForInputToContinue(Scanner scanner) { while (true) { System.out.println(""); System.out.println("Enter 'c' followed by <ENTER> to continue:"); String input = scanner.nextLine(); if (input.trim().equalsIgnoreCase("c")) { System.out.println("Continuing with the program..."); System.out.println(""); break; } else { // Handle invalid input. System.out.println("Invalid input. Please try again."); } } } public static void printFooter() { System.out.println(""" ================================================= Thank you for checking out the Amazon Bedrock Tool Use demo. We hope you learned something new, or got some inspiration for your own apps today! For more Bedrock examples in different programming languages, have a look at: https://docs.aws.amazon.com/bedrock/latest/userguide/service_code_examples.html ================================================= """); } }

デモで使用される気象ツール。このファイルは、ツール仕様を定義し、Open-Meteo API から を使用して気象データを取得するロジックを実装します。

public class WeatherTool { private static final Logger logger = LoggerFactory.getLogger(WeatherTool.class); private static java.net.http.HttpClient httpClient = null; /** * Returns the JSON Schema specification for the Weather tool. The tool specification * defines the input schema and describes the tool's functionality. * For more information, see https://json-schema.org/understanding-json-schema/reference. * * @return The tool specification for the Weather tool. */ public ToolSpecification getToolSpec() { Map<String, Document> latitudeMap = new HashMap<>(); latitudeMap.put("type", Document.fromString("string")); latitudeMap.put("description", Document.fromString("Geographical WGS84 latitude of the location.")); // Create the nested "longitude" object Map<String, Document> longitudeMap = new HashMap<>(); longitudeMap.put("type", Document.fromString("string")); longitudeMap.put("description", Document.fromString("Geographical WGS84 longitude of the location.")); // Create the "properties" object Map<String, Document> propertiesMap = new HashMap<>(); propertiesMap.put("latitude", Document.fromMap(latitudeMap)); propertiesMap.put("longitude", Document.fromMap(longitudeMap)); // Create the "required" array List<Document> requiredList = new ArrayList<>(); requiredList.add(Document.fromString("latitude")); requiredList.add(Document.fromString("longitude")); // Create the root object Map<String, Document> rootMap = new HashMap<>(); rootMap.put("type", Document.fromString("object")); rootMap.put("properties", Document.fromMap(propertiesMap)); rootMap.put("required", Document.fromList(requiredList)); // Now create the Document representing the JSON schema Document document = Document.fromMap(rootMap); ToolSpecification specification = ToolSpecification.builder() .name("Weather_Tool") .description("Get the current weather for a given location, based on its WGS84 coordinates.") .inputSchema(ToolInputSchema.builder() .json(document) .build()) .build(); return specification; } /** * Fetches weather data for the given latitude and longitude. * * @param latitude the latitude coordinate * @param longitude the longitude coordinate * @return a {@link CompletableFuture} containing the weather data as a JSON string */ public Document fetchWeatherData(String latitude, String longitude) { HttpClient httpClient = HttpClient.newHttpClient(); // Ensure no extra double quotes latitude = latitude.replace("\"", ""); longitude = longitude.replace("\"", ""); String endpoint = "https://api.open-meteo.com/v1/forecast"; String url = String.format("%s?latitude=%s&longitude=%s&current_weather=True", endpoint, latitude, longitude); HttpRequest request = HttpRequest.newBuilder() .uri(URI.create(url)) .build(); try { HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString()); if (response.statusCode() == 200) { String weatherJson = response.body(); System.out.println(weatherJson); ObjectMapper objectMapper = new ObjectMapper(); Map<String, Object> rawMap = objectMapper.readValue(weatherJson, new TypeReference<Map<String, Object>>() {}); Map<String, Document> documentMap = convertToDocumentMap(rawMap); Document weatherDocument = Document.fromMap(documentMap); System.out.println(weatherDocument); return weatherDocument; } else { throw new RuntimeException("Error fetching weather data: " + response.statusCode()); } } catch (Exception e) { System.out.println("Error fetching weather data: " + e.getMessage()); throw new RuntimeException("Error fetching weather data", e); } } private static Map<String, Document> convertToDocumentMap(Map<String, Object> inputMap) { Map<String, Document> result = new HashMap<>(); for (Map.Entry<String, Object> entry : inputMap.entrySet()) { result.put(entry.getKey(), convertToDocument(entry.getValue())); } return result; } // Convert different types of Objects to Document private static Document convertToDocument(Object value) { if (value instanceof Map) { return Document.fromMap(convertToDocumentMap((Map<String, Object>) value)); } else if (value instanceof Integer) { return Document.fromNumber(SdkNumber.fromInteger((Integer) value)); } else if (value instanceof Double) { // return Document.fromNumber(SdkNumber.fromDouble((Double) value)); } else if (value instanceof Boolean) { return Document.fromBoolean((Boolean) value); } else if (value instanceof String) { return Document.fromString((String) value); } return Document.fromNull(); // Handle null values safely } }

ツール設定を使用した Converse API アクション。

/** * Sends an asynchronous converse request to the AI model. * * @param modelId the unique identifier of the AI model to be used for the converse request * @param systemPrompt the system prompt to be included in the converse request * @param conversation a list of messages representing the conversation history * @param toolSpec the specification of the tool to be used in the converse request * @return the converse response received from the AI model */ public ConverseResponse sendConverseRequestAsync(String modelId, String systemPrompt, List<Message> conversation, ToolSpecification toolSpec) { List<Tool> toolList = new ArrayList<>(); Tool tool = Tool.builder() .toolSpec(toolSpec) .build(); toolList.add(tool); ToolConfiguration configuration = ToolConfiguration.builder() .tools(toolList) .build(); SystemContentBlock block = SystemContentBlock.builder() .text(systemPrompt) .build(); ConverseRequest request = ConverseRequest.builder() .modelId(modelId) .system(block) .messages(conversation) .toolConfig(configuration) .build(); try { ConverseResponse response = getClient().converse(request).join(); return response; } catch (ModelNotReadyException ex) { throw new RuntimeException("Model is not ready: " + ex.getMessage(), ex); } catch (BedrockRuntimeException ex) { throw new RuntimeException("Failed to converse with Bedrock model: " + ex.getMessage(), ex); } }
  • API の詳細については、「AWS SDK for Java 2.x API リファレンス」の「Converse」を参照してください。

JavaScript
SDK for JavaScript (v3)
注記

GitHub には、その他のリソースもあります。AWS コード例リポジトリ で全く同じ例を見つけて、設定と実行の方法を確認してください。

シナリオフローのプライマリ実行。このシナリオでは、ユーザー、Amazon Bedrock Converse API、および気象ツール間の会話を調整します。

/* Before running this JavaScript code example, set up your development environment, including your credentials. This demo illustrates a tool use scenario using Amazon Bedrock's Converse API and a weather tool. The script interacts with a foundation model on Amazon Bedrock to provide weather information based on user input. It uses the Open-Meteo API (https://open-meteo.com) to retrieve current weather data for a given location.*/ import { Scenario, ScenarioAction, ScenarioInput, ScenarioOutput, } from "@aws-doc-sdk-examples/lib/scenario/index.js"; import { BedrockRuntimeClient, ConverseCommand, } from "@aws-sdk/client-bedrock-runtime"; import { parseArgs } from "node:util"; import { fileURLToPath } from "node:url"; import { dirname } from "node:path"; const __filename = fileURLToPath(import.meta.url); import data from "./questions.json" with { type: "json" }; import toolConfig from "./tool_config.json" with { type: "json" }; const systemPrompt = [ { text: "You are a weather assistant that provides current weather data for user-specified locations using only\n" + "the Weather_Tool, which expects latitude and longitude. Infer the coordinates from the location yourself.\n" + "If the user provides coordinates, infer the approximate location and refer to it in your response.\n" + "To use the tool, you strictly apply the provided tool specification.\n" + "If the user specifies a state, country, or region, infer the locations of cities within that state.\n" + "\n" + "- Explain your step-by-step process, and give brief updates before each step.\n" + "- Only use the Weather_Tool for data. Never guess or make up information. \n" + "- Repeat the tool use for subsequent requests if necessary.\n" + "- If the tool errors, apologize, explain weather is unavailable, and suggest other options.\n" + "- Report temperatures in °C (°F) and wind in km/h (mph). Keep weather reports concise. Sparingly use\n" + " emojis where appropriate.\n" + "- Only respond to weather queries. Remind off-topic users of your purpose. \n" + "- Never claim to search online, access external data, or use tools besides Weather_Tool.\n" + "- Complete the entire process until you have all required data before sending the complete response.", }, ]; const tools_config = toolConfig; /// Starts the conversation with the user and handles the interaction with Bedrock. async function askQuestion(userMessage) { // The maximum number of recursive calls allowed in the tool use function. // This helps prevent infinite loops and potential performance issues. const max_recursions = 5; const messages = [ { role: "user", content: [{ text: userMessage }], }, ]; try { const response = await SendConversationtoBedrock(messages); await ProcessModelResponseAsync(response, messages, max_recursions); } catch (error) { console.log("error ", error); } } // Sends the conversation, the system prompt, and the tool spec to Amazon Bedrock, and returns the response. // param "messages" - The conversation history including the next message to send. // return - The response from Amazon Bedrock. async function SendConversationtoBedrock(messages) { const bedRockRuntimeClient = new BedrockRuntimeClient({ region: "us-east-1", }); try { const modelId = "amazon.nova-lite-v1:0"; const response = await bedRockRuntimeClient.send( new ConverseCommand({ modelId: modelId, messages: messages, system: systemPrompt, toolConfig: tools_config, }), ); return response; } catch (caught) { if (caught.name === "ModelNotReady") { console.log( "`${caught.name}` - Model not ready, please wait and try again.", ); throw caught; } if (caught.name === "BedrockRuntimeException") { console.log( '`${caught.name}` - "Error occurred while sending Converse request.', ); throw caught; } } } // Processes the response received via Amazon Bedrock and performs the necessary actions based on the stop reason. // param "response" - The model's response returned via Amazon Bedrock. // param "messages" - The conversation history. // param "max_recursions" - The maximum number of recursive calls allowed. async function ProcessModelResponseAsync(response, messages, max_recursions) { if (max_recursions <= 0) { await HandleToolUseAsync(response, messages); } if (response.stopReason === "tool_use") { await HandleToolUseAsync(response, messages, max_recursions - 1); } if (response.stopReason === "end_turn") { const messageToPrint = response.output.message.content[0].text; console.log(messageToPrint.replace(/<[^>]+>/g, "")); } } // Handles the tool use case by invoking the specified tool and sending the tool's response back to Bedrock. // The tool response is appended to the conversation, and the conversation is sent back to Amazon Bedrock for further processing. // param "response" - the model's response containing the tool use request. // param "messages" - the conversation history. // param "max_recursions" - The maximum number of recursive calls allowed. async function HandleToolUseAsync(response, messages, max_recursions) { const toolResultFinal = []; try { const output_message = response.output.message; messages.push(output_message); const toolRequests = output_message.content; const toolMessage = toolRequests[0].text; console.log(toolMessage.replace(/<[^>]+>/g, "")); for (const toolRequest of toolRequests) { if (Object.hasOwn(toolRequest, "toolUse")) { const toolUse = toolRequest.toolUse; const latitude = toolUse.input.latitude; const longitude = toolUse.input.longitude; const toolUseID = toolUse.toolUseId; console.log( `Requesting tool ${toolUse.name}, Tool use id ${toolUseID}`, ); if (toolUse.name === "Weather_Tool") { try { const current_weather = await callWeatherTool( longitude, latitude, ).then((current_weather) => current_weather); const currentWeather = current_weather; const toolResult = { toolResult: { toolUseId: toolUseID, content: [{ json: currentWeather }], }, }; toolResultFinal.push(toolResult); } catch (err) { console.log("An error occurred. ", err); } } } } const toolResultMessage = { role: "user", content: toolResultFinal, }; messages.push(toolResultMessage); // Send the conversation to Amazon Bedrock await ProcessModelResponseAsync( await SendConversationtoBedrock(messages), messages, ); } catch (error) { console.log("An error occurred. ", error); } } // Call the Weathertool. // param = longitude of location // param = latitude of location async function callWeatherTool(longitude, latitude) { // Open-Meteo API endpoint const apiUrl = `https://api.open-meteo.com/v1/forecast?latitude=${latitude}&longitude=${longitude}&current_weather=true`; // Fetch the weather data. return fetch(apiUrl) .then((response) => { return response.json().then((current_weather) => { return current_weather; }); }) .catch((error) => { console.error("Error fetching weather data:", error); }); } /** * Used repeatedly to have the user press enter. * @type {ScenarioInput} */ const pressEnter = new ScenarioInput("continue", "Press Enter to continue", { type: "input", }); const greet = new ScenarioOutput( "greet", "Welcome to the Amazon Bedrock Tool Use demo! \n" + "This assistant provides current weather information for user-specified locations. " + "You can ask for weather details by providing the location name or coordinates." + "Weather information will be provided using a custom Tool and open-meteo API." + "For the purposes of this example, we'll use in order the questions in ./questions.json :\n" + "What's the weather like in Seattle? " + "What's the best kind of cat? " + "Where is the warmest city in Washington State right now? " + "What's the warmest city in California right now?\n" + "To exit the program, simply type 'x' and press Enter.\n" + "Have fun and experiment with the app by editing the questions in ./questions.json! " + "P.S.: You're not limited to single locations, or even to using English! ", { header: true }, ); const displayAskQuestion1 = new ScenarioOutput( "displayAskQuestion1", "Press enter to ask question number 1 (default is 'What's the weather like in Seattle?')", ); const askQuestion1 = new ScenarioAction( "askQuestion1", async (/** @type {State} */ state) => { const userMessage1 = data.questions["question-1"]; await askQuestion(userMessage1); }, ); const displayAskQuestion2 = new ScenarioOutput( "displayAskQuestion2", "Press enter to ask question number 2 (default is 'What's the best kind of cat?')", ); const askQuestion2 = new ScenarioAction( "askQuestion2", async (/** @type {State} */ state) => { const userMessage2 = data.questions["question-2"]; await askQuestion(userMessage2); }, ); const displayAskQuestion3 = new ScenarioOutput( "displayAskQuestion3", "Press enter to ask question number 3 (default is 'Where is the warmest city in Washington State right now?')", ); const askQuestion3 = new ScenarioAction( "askQuestion3", async (/** @type {State} */ state) => { const userMessage3 = data.questions["question-3"]; await askQuestion(userMessage3); }, ); const displayAskQuestion4 = new ScenarioOutput( "displayAskQuestion4", "Press enter to ask question number 4 (default is 'What's the warmest city in California right now?')", ); const askQuestion4 = new ScenarioAction( "askQuestion4", async (/** @type {State} */ state) => { const userMessage4 = data.questions["question-4"]; await askQuestion(userMessage4); }, ); const goodbye = new ScenarioOutput( "goodbye", "Thank you for checking out the Amazon Bedrock Tool Use demo. We hope you\n" + "learned something new, or got some inspiration for your own apps today!\n" + "For more Bedrock examples in different programming languages, have a look at:\n" + "https://docs.aws.amazon.com/bedrock/latest/userguide/service_code_examples.html", ); const myScenario = new Scenario("Converse Tool Scenario", [ greet, pressEnter, displayAskQuestion1, askQuestion1, pressEnter, displayAskQuestion2, askQuestion2, pressEnter, displayAskQuestion3, askQuestion3, pressEnter, displayAskQuestion4, askQuestion4, pressEnter, goodbye, ]); /** @type {{ stepHandlerOptions: StepHandlerOptions }} */ export const main = async (stepHandlerOptions) => { await myScenario.run(stepHandlerOptions); }; // Invoke main function if this file was run directly. if (process.argv[1] === fileURLToPath(import.meta.url)) { const { values } = parseArgs({ options: { yes: { type: "boolean", short: "y", }, }, }); main({ confirmAll: values.yes }); }
  • API の詳細については、「AWS SDK for JavaScript API リファレンス」の「Converse」を参照してください。

Python
SDK for Python (Boto3)
注記

GitHub には、その他のリソースもあります。AWS コード例リポジトリ で全く同じ例を見つけて、設定と実行の方法を確認してください。

デモのプライマリ実行スクリプト。このスクリプトは、ユーザー、Amazon Bedrock Converse API、および気象ツール間の会話を調整します。

""" This demo illustrates a tool use scenario using Amazon Bedrock's Converse API and a weather tool. The script interacts with a foundation model on Amazon Bedrock to provide weather information based on user input. It uses the Open-Meteo API (https://open-meteo.com) to retrieve current weather data for a given location. """ import boto3 import logging from enum import Enum import utils.tool_use_print_utils as output import weather_tool logging.basicConfig(level=logging.INFO, format="%(message)s") AWS_REGION = "us-east-1" # For the most recent list of models supported by the Converse API's tool use functionality, visit: # https://docs.aws.amazon.com/bedrock/latest/userguide/conversation-inference.html class SupportedModels(Enum): CLAUDE_OPUS = "anthropic.claude-3-opus-20240229-v1:0" CLAUDE_SONNET = "anthropic.claude-3-sonnet-20240229-v1:0" CLAUDE_HAIKU = "anthropic.claude-3-haiku-20240307-v1:0" COHERE_COMMAND_R = "cohere.command-r-v1:0" COHERE_COMMAND_R_PLUS = "cohere.command-r-plus-v1:0" # Set the model ID, e.g., Claude 3 Haiku. MODEL_ID = SupportedModels.CLAUDE_HAIKU.value SYSTEM_PROMPT = """ You are a weather assistant that provides current weather data for user-specified locations using only the Weather_Tool, which expects latitude and longitude. Infer the coordinates from the location yourself. If the user provides coordinates, infer the approximate location and refer to it in your response. To use the tool, you strictly apply the provided tool specification. - Explain your step-by-step process, and give brief updates before each step. - Only use the Weather_Tool for data. Never guess or make up information. - Repeat the tool use for subsequent requests if necessary. - If the tool errors, apologize, explain weather is unavailable, and suggest other options. - Report temperatures in °C (°F) and wind in km/h (mph). Keep weather reports concise. Sparingly use emojis where appropriate. - Only respond to weather queries. Remind off-topic users of your purpose. - Never claim to search online, access external data, or use tools besides Weather_Tool. - Complete the entire process until you have all required data before sending the complete response. """ # The maximum number of recursive calls allowed in the tool_use_demo function. # This helps prevent infinite loops and potential performance issues. MAX_RECURSIONS = 5 class ToolUseDemo: """ Demonstrates the tool use feature with the Amazon Bedrock Converse API. """ def __init__(self): # Prepare the system prompt self.system_prompt = [{"text": SYSTEM_PROMPT}] # Prepare the tool configuration with the weather tool's specification self.tool_config = {"tools": [weather_tool.get_tool_spec()]} # Create a Bedrock Runtime client in the specified AWS Region. self.bedrockRuntimeClient = boto3.client( "bedrock-runtime", region_name=AWS_REGION ) def run(self): """ Starts the conversation with the user and handles the interaction with Bedrock. """ # Print the greeting and a short user guide output.header() # Start with an emtpy conversation conversation = [] # Get the first user input user_input = self._get_user_input() while user_input is not None: # Create a new message with the user input and append it to the conversation message = {"role": "user", "content": [{"text": user_input}]} conversation.append(message) # Send the conversation to Amazon Bedrock bedrock_response = self._send_conversation_to_bedrock(conversation) # Recursively handle the model's response until the model has returned # its final response or the recursion counter has reached 0 self._process_model_response( bedrock_response, conversation, max_recursion=MAX_RECURSIONS ) # Repeat the loop until the user decides to exit the application user_input = self._get_user_input() output.footer() def _send_conversation_to_bedrock(self, conversation): """ Sends the conversation, the system prompt, and the tool spec to Amazon Bedrock, and returns the response. :param conversation: The conversation history including the next message to send. :return: The response from Amazon Bedrock. """ output.call_to_bedrock(conversation) # Send the conversation, system prompt, and tool configuration, and return the response return self.bedrockRuntimeClient.converse( modelId=MODEL_ID, messages=conversation, system=self.system_prompt, toolConfig=self.tool_config, ) def _process_model_response( self, model_response, conversation, max_recursion=MAX_RECURSIONS ): """ Processes the response received via Amazon Bedrock and performs the necessary actions based on the stop reason. :param model_response: The model's response returned via Amazon Bedrock. :param conversation: The conversation history. :param max_recursion: The maximum number of recursive calls allowed. """ if max_recursion <= 0: # Stop the process, the number of recursive calls could indicate an infinite loop logging.warning( "Warning: Maximum number of recursions reached. Please try again." ) exit(1) # Append the model's response to the ongoing conversation message = model_response["output"]["message"] conversation.append(message) if model_response["stopReason"] == "tool_use": # If the stop reason is "tool_use", forward everything to the tool use handler self._handle_tool_use(message, conversation, max_recursion) if model_response["stopReason"] == "end_turn": # If the stop reason is "end_turn", print the model's response text, and finish the process output.model_response(message["content"][0]["text"]) return def _handle_tool_use( self, model_response, conversation, max_recursion=MAX_RECURSIONS ): """ Handles the tool use case by invoking the specified tool and sending the tool's response back to Bedrock. The tool response is appended to the conversation, and the conversation is sent back to Amazon Bedrock for further processing. :param model_response: The model's response containing the tool use request. :param conversation: The conversation history. :param max_recursion: The maximum number of recursive calls allowed. """ # Initialize an empty list of tool results tool_results = [] # The model's response can consist of multiple content blocks for content_block in model_response["content"]: if "text" in content_block: # If the content block contains text, print it to the console output.model_response(content_block["text"]) if "toolUse" in content_block: # If the content block is a tool use request, forward it to the tool tool_response = self._invoke_tool(content_block["toolUse"]) # Add the tool use ID and the tool's response to the list of results tool_results.append( { "toolResult": { "toolUseId": (tool_response["toolUseId"]), "content": [{"json": tool_response["content"]}], } } ) # Embed the tool results in a new user message message = {"role": "user", "content": tool_results} # Append the new message to the ongoing conversation conversation.append(message) # Send the conversation to Amazon Bedrock response = self._send_conversation_to_bedrock(conversation) # Recursively handle the model's response until the model has returned # its final response or the recursion counter has reached 0 self._process_model_response(response, conversation, max_recursion - 1) def _invoke_tool(self, payload): """ Invokes the specified tool with the given payload and returns the tool's response. If the requested tool does not exist, an error message is returned. :param payload: The payload containing the tool name and input data. :return: The tool's response or an error message. """ tool_name = payload["name"] if tool_name == "Weather_Tool": input_data = payload["input"] output.tool_use(tool_name, input_data) # Invoke the weather tool with the input data provided by response = weather_tool.fetch_weather_data(input_data) else: error_message = ( f"The requested tool with name '{tool_name}' does not exist." ) response = {"error": "true", "message": error_message} return {"toolUseId": payload["toolUseId"], "content": response} @staticmethod def _get_user_input(prompt="Your weather info request"): """ Prompts the user for input and returns the user's response. Returns None if the user enters 'x' to exit. :param prompt: The prompt to display to the user. :return: The user's input or None if the user chooses to exit. """ output.separator() user_input = input(f"{prompt} (x to exit): ") if user_input == "": prompt = "Please enter your weather info request, e.g. the name of a city" return ToolUseDemo._get_user_input(prompt) elif user_input.lower() == "x": return None else: return user_input if __name__ == "__main__": tool_use_demo = ToolUseDemo() tool_use_demo.run()

デモで使用される気象ツール。このスクリプトは、ツールの仕様を定義し、Open-Meteo API を使用して気象データを取得するロジックを実装します。

import requests from requests.exceptions import RequestException def get_tool_spec(): """ Returns the JSON Schema specification for the Weather tool. The tool specification defines the input schema and describes the tool's functionality. For more information, see https://json-schema.org/understanding-json-schema/reference. :return: The tool specification for the Weather tool. """ return { "toolSpec": { "name": "Weather_Tool", "description": "Get the current weather for a given location, based on its WGS84 coordinates.", "inputSchema": { "json": { "type": "object", "properties": { "latitude": { "type": "string", "description": "Geographical WGS84 latitude of the location.", }, "longitude": { "type": "string", "description": "Geographical WGS84 longitude of the location.", }, }, "required": ["latitude", "longitude"], } }, } } def fetch_weather_data(input_data): """ Fetches weather data for the given latitude and longitude using the Open-Meteo API. Returns the weather data or an error message if the request fails. :param input_data: The input data containing the latitude and longitude. :return: The weather data or an error message. """ endpoint = "https://api.open-meteo.com/v1/forecast" latitude = input_data.get("latitude") longitude = input_data.get("longitude", "") params = {"latitude": latitude, "longitude": longitude, "current_weather": True} try: response = requests.get(endpoint, params=params) weather_data = {"weather_data": response.json()} response.raise_for_status() return weather_data except RequestException as e: return e.response.json() except Exception as e: return {"error": type(e), "message": str(e)}
  • API の詳細については、「AWS SDK for Python (Boto3) API リファレンス」の「Converse」を参照してください。

Rust
SDK for Rust
注記

GitHub には、その他のリソースもあります。AWS コード例リポジトリ で全く同じ例を見つけて、設定と実行の方法を確認してください。

デモのプライマリシナリオとロジック。これは、ユーザー、Amazon Bedrock Converse API、および気象ツール間の会話を調整します。

#[derive(Debug)] #[allow(dead_code)] struct InvokeToolResult(String, ToolResultBlock); struct ToolUseScenario { client: Client, conversation: Vec<Message>, system_prompt: SystemContentBlock, tool_config: ToolConfiguration, } impl ToolUseScenario { fn new(client: Client) -> Self { let system_prompt = SystemContentBlock::Text(SYSTEM_PROMPT.into()); let tool_config = ToolConfiguration::builder() .tools(Tool::ToolSpec( ToolSpecification::builder() .name(TOOL_NAME) .description(TOOL_DESCRIPTION) .input_schema(ToolInputSchema::Json(make_tool_schema())) .build() .unwrap(), )) .build() .unwrap(); ToolUseScenario { client, conversation: vec![], system_prompt, tool_config, } } async fn run(&mut self) -> Result<(), ToolUseScenarioError> { loop { let input = get_input().await?; if input.is_none() { break; } let message = Message::builder() .role(User) .content(ContentBlock::Text(input.unwrap())) .build() .map_err(ToolUseScenarioError::from)?; self.conversation.push(message); let response = self.send_to_bedrock().await?; self.process_model_response(response).await?; } Ok(()) } async fn send_to_bedrock(&mut self) -> Result<ConverseOutput, ToolUseScenarioError> { debug!("Sending conversation to bedrock"); self.client .converse() .model_id(MODEL_ID) .set_messages(Some(self.conversation.clone())) .system(self.system_prompt.clone()) .tool_config(self.tool_config.clone()) .send() .await .map_err(ToolUseScenarioError::from) } async fn process_model_response( &mut self, mut response: ConverseOutput, ) -> Result<(), ToolUseScenarioError> { let mut iteration = 0; while iteration < MAX_RECURSIONS { iteration += 1; let message = if let Some(ref output) = response.output { if output.is_message() { Ok(output.as_message().unwrap().clone()) } else { Err(ToolUseScenarioError( "Converse Output is not a message".into(), )) } } else { Err(ToolUseScenarioError("Missing Converse Output".into())) }?; self.conversation.push(message.clone()); match response.stop_reason { StopReason::ToolUse => { response = self.handle_tool_use(&message).await?; } StopReason::EndTurn => { print_model_response(&message.content[0])?; return Ok(()); } _ => (), } } Err(ToolUseScenarioError( "Exceeded MAX_ITERATIONS when calling tools".into(), )) } async fn handle_tool_use( &mut self, message: &Message, ) -> Result<ConverseOutput, ToolUseScenarioError> { let mut tool_results: Vec<ContentBlock> = vec![]; for block in &message.content { match block { ContentBlock::Text(_) => print_model_response(block)?, ContentBlock::ToolUse(tool) => { let tool_response = self.invoke_tool(tool).await?; tool_results.push(ContentBlock::ToolResult(tool_response.1)); } _ => (), }; } let message = Message::builder() .role(User) .set_content(Some(tool_results)) .build()?; self.conversation.push(message); self.send_to_bedrock().await } async fn invoke_tool( &mut self, tool: &ToolUseBlock, ) -> Result<InvokeToolResult, ToolUseScenarioError> { match tool.name() { TOOL_NAME => { println!( "\x1b[0;90mExecuting tool: {TOOL_NAME} with input: {:?}...\x1b[0m", tool.input() ); let content = fetch_weather_data(tool).await?; println!( "\x1b[0;90mTool responded with {:?}\x1b[0m", content.content() ); Ok(InvokeToolResult(tool.tool_use_id.clone(), content)) } _ => Err(ToolUseScenarioError(format!( "The requested tool with name {} does not exist", tool.name() ))), } } } #[tokio::main] async fn main() { tracing_subscriber::fmt::init(); let sdk_config = aws_config::defaults(BehaviorVersion::latest()) .region(CLAUDE_REGION) .load() .await; let client = Client::new(&sdk_config); let mut scenario = ToolUseScenario::new(client); header(); if let Err(err) = scenario.run().await { println!("There was an error running the scenario! {}", err.0) } footer(); }

デモで使用される気象ツール。このスクリプトは、ツールの仕様を定義し、Open-Meteo API を使用して気象データを取得するロジックを実装します。

const ENDPOINT: &str = "https://api.open-meteo.com/v1/forecast"; async fn fetch_weather_data( tool_use: &ToolUseBlock, ) -> Result<ToolResultBlock, ToolUseScenarioError> { let input = tool_use.input(); let latitude = input .as_object() .unwrap() .get("latitude") .unwrap() .as_string() .unwrap(); let longitude = input .as_object() .unwrap() .get("longitude") .unwrap() .as_string() .unwrap(); let params = [ ("latitude", latitude), ("longitude", longitude), ("current_weather", "true"), ]; debug!("Calling {ENDPOINT} with {params:?}"); let response = reqwest::Client::new() .get(ENDPOINT) .query(&params) .send() .await .map_err(|e| ToolUseScenarioError(format!("Error requesting weather: {e:?}")))? .error_for_status() .map_err(|e| ToolUseScenarioError(format!("Failed to request weather: {e:?}")))?; debug!("Response: {response:?}"); let bytes = response .bytes() .await .map_err(|e| ToolUseScenarioError(format!("Error reading response: {e:?}")))?; let result = String::from_utf8(bytes.to_vec()) .map_err(|_| ToolUseScenarioError("Response was not utf8".into()))?; Ok(ToolResultBlock::builder() .tool_use_id(tool_use.tool_use_id()) .content(ToolResultContentBlock::Text(result)) .build()?) }

メッセージコンテンツブロックを印刷するユーティリティ。

fn print_model_response(block: &ContentBlock) -> Result<(), ToolUseScenarioError> { if block.is_text() { let text = block.as_text().unwrap(); println!("\x1b[0;90mThe model's response:\x1b[0m\n{text}"); Ok(()) } else { Err(ToolUseScenarioError(format!( "Content block is not text ({block:?})" ))) } }

ステートメント、エラーユーティリティ、定数を使用します。

use std::{collections::HashMap, io::stdin}; use aws_config::BehaviorVersion; use aws_sdk_bedrockruntime::{ error::{BuildError, SdkError}, operation::converse::{ConverseError, ConverseOutput}, types::{ ContentBlock, ConversationRole::User, Message, StopReason, SystemContentBlock, Tool, ToolConfiguration, ToolInputSchema, ToolResultBlock, ToolResultContentBlock, ToolSpecification, ToolUseBlock, }, Client, }; use aws_smithy_runtime_api::http::Response; use aws_smithy_types::Document; use tracing::debug; // Set the model ID, e.g., Claude 3 Haiku. const MODEL_ID: &str = "anthropic.claude-3-haiku-20240307-v1:0"; const CLAUDE_REGION: &str = "us-east-1"; const SYSTEM_PROMPT: &str = "You are a weather assistant that provides current weather data for user-specified locations using only the Weather_Tool, which expects latitude and longitude. Infer the coordinates from the location yourself. If the user provides coordinates, infer the approximate location and refer to it in your response. To use the tool, you strictly apply the provided tool specification. - Explain your step-by-step process, and give brief updates before each step. - Only use the Weather_Tool for data. Never guess or make up information. - Repeat the tool use for subsequent requests if necessary. - If the tool errors, apologize, explain weather is unavailable, and suggest other options. - Report temperatures in °C (°F) and wind in km/h (mph). Keep weather reports concise. Sparingly use emojis where appropriate. - Only respond to weather queries. Remind off-topic users of your purpose. - Never claim to search online, access external data, or use tools besides Weather_Tool. - Complete the entire process until you have all required data before sending the complete response. "; // The maximum number of recursive calls allowed in the tool_use_demo function. // This helps prevent infinite loops and potential performance issues. const MAX_RECURSIONS: i8 = 5; const TOOL_NAME: &str = "Weather_Tool"; const TOOL_DESCRIPTION: &str = "Get the current weather for a given location, based on its WGS84 coordinates."; fn make_tool_schema() -> Document { Document::Object(HashMap::<String, Document>::from([ ("type".into(), Document::String("object".into())), ( "properties".into(), Document::Object(HashMap::from([ ( "latitude".into(), Document::Object(HashMap::from([ ("type".into(), Document::String("string".into())), ( "description".into(), Document::String("Geographical WGS84 latitude of the location.".into()), ), ])), ), ( "longitude".into(), Document::Object(HashMap::from([ ("type".into(), Document::String("string".into())), ( "description".into(), Document::String( "Geographical WGS84 longitude of the location.".into(), ), ), ])), ), ])), ), ( "required".into(), Document::Array(vec![ Document::String("latitude".into()), Document::String("longitude".into()), ]), ), ])) } #[derive(Debug)] struct ToolUseScenarioError(String); impl std::fmt::Display for ToolUseScenarioError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "Tool use error with '{}'. Reason: {}", MODEL_ID, self.0) } } impl From<&str> for ToolUseScenarioError { fn from(value: &str) -> Self { ToolUseScenarioError(value.into()) } } impl From<BuildError> for ToolUseScenarioError { fn from(value: BuildError) -> Self { ToolUseScenarioError(value.to_string().clone()) } } impl From<SdkError<ConverseError, Response>> for ToolUseScenarioError { fn from(value: SdkError<ConverseError, Response>) -> Self { ToolUseScenarioError(match value.as_service_error() { Some(value) => value.meta().message().unwrap_or("Unknown").into(), None => "Unknown".into(), }) } }
  • API の詳細については、「AWS SDK for Rust API リファレンス」の「Converse」を参照してください。

SDK for .NET
注記

GitHub には、その他のリソースもあります。AWS コード例リポジトリ で全く同じ例を見つけて、設定と実行の方法を確認してください。

シナリオフローのプライマリ実行。このシナリオでは、ユーザー、Amazon Bedrock Converse API、および気象ツール間の会話を調整します。

using Amazon; using Amazon.BedrockRuntime; using Amazon.BedrockRuntime.Model; using Amazon.Runtime.Documents; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Http; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Console; namespace ConverseToolScenario; public static class ConverseToolScenario { /* Before running this .NET code example, set up your development environment, including your credentials. This demo illustrates a tool use scenario using Amazon Bedrock's Converse API and a weather tool. The script interacts with a foundation model on Amazon Bedrock to provide weather information based on user input. It uses the Open-Meteo API (https://open-meteo.com) to retrieve current weather data for a given location. */ public static BedrockActionsWrapper _bedrockActionsWrapper = null!; public static WeatherTool _weatherTool = null!; public static bool _interactive = true; // Change this string to use a different model with Converse API. private static string model_id = "amazon.nova-lite-v1:0"; private static string system_prompt = @" You are a weather assistant that provides current weather data for user-specified locations using only the Weather_Tool, which expects latitude and longitude. Infer the coordinates from the location yourself. If the user specifies a state, country, or region, infer the locations of cities within that state. If the user provides coordinates, infer the approximate location and refer to it in your response. To use the tool, you strictly apply the provided tool specification. - Explain your step-by-step process, and give brief updates before each step. - Only use the Weather_Tool for data. Never guess or make up information. - Repeat the tool use for subsequent requests if necessary. - If the tool errors, apologize, explain weather is unavailable, and suggest other options. - Report temperatures in °C (°F) and wind in km/h (mph). Keep weather reports concise. Sparingly use emojis where appropriate. - Only respond to weather queries. Remind off-topic users of your purpose. - Never claim to search online, access external data, or use tools besides Weather_Tool. - Complete the entire process until you have all required data before sending the complete response. " ; private static string default_prompt = "What is the weather like in Seattle?"; // The maximum number of recursive calls allowed in the tool use function. // This helps prevent infinite loops and potential performance issues. private static int max_recursions = 5; public static async Task Main(string[] args) { // Set up dependency injection for the Amazon service. using var host = Host.CreateDefaultBuilder(args) .ConfigureLogging(logging => logging.AddFilter("System", LogLevel.Error) .AddFilter<ConsoleLoggerProvider>("Microsoft", LogLevel.Trace)) .ConfigureServices((_, services) => services.AddHttpClient() .AddSingleton<IAmazonBedrockRuntime>(_ => new AmazonBedrockRuntimeClient(RegionEndpoint.USEast1)) // Specify a region that has access to the chosen model. .AddTransient<BedrockActionsWrapper>() .AddTransient<WeatherTool>() .RemoveAll<IHttpMessageHandlerBuilderFilter>() ) .Build(); ServicesSetup(host); try { await RunConversationAsync(); } catch (Exception ex) { Console.WriteLine(new string('-', 80)); Console.WriteLine($"There was a problem running the scenario: {ex.Message}"); Console.WriteLine(new string('-', 80)); } finally { Console.WriteLine( "Amazon Bedrock Converse API with Tool Use Feature Scenario is complete."); Console.WriteLine(new string('-', 80)); } } /// <summary> /// Populate the services for use within the console application. /// </summary> /// <param name="host">The services host.</param> private static void ServicesSetup(IHost host) { _bedrockActionsWrapper = host.Services.GetRequiredService<BedrockActionsWrapper>(); _weatherTool = host.Services.GetRequiredService<WeatherTool>(); } /// <summary> /// Starts the conversation with the user and handles the interaction with Bedrock. /// </summary> /// <returns>The conversation array.</returns> public static async Task<List<Message>> RunConversationAsync() { // Print the greeting and a short user guide PrintHeader(); // Start with an empty conversation var conversation = new List<Message>(); // Get the first user input var userInput = await GetUserInputAsync(); while (userInput != null) { // Create a new message with the user input and append it to the conversation var message = new Message { Role = ConversationRole.User, Content = new List<ContentBlock> { new ContentBlock { Text = userInput } } }; conversation.Add(message); // Send the conversation to Amazon Bedrock var bedrockResponse = await SendConversationToBedrock(conversation); // Recursively handle the model's response until the model has returned its final response or the recursion counter has reached 0 await ProcessModelResponseAsync(bedrockResponse, conversation, max_recursions); // Repeat the loop until the user decides to exit the application userInput = await GetUserInputAsync(); } PrintFooter(); return conversation; } /// <summary> /// Sends the conversation, the system prompt, and the tool spec to Amazon Bedrock, and returns the response. /// </summary> /// <param name="conversation">The conversation history including the next message to send.</param> /// <returns>The response from Amazon Bedrock.</returns> private static async Task<ConverseResponse> SendConversationToBedrock(List<Message> conversation) { Console.WriteLine("\tCalling Bedrock..."); // Send the conversation, system prompt, and tool configuration, and return the response return await _bedrockActionsWrapper.SendConverseRequestAsync(model_id, system_prompt, conversation, _weatherTool.GetToolSpec()); } /// <summary> /// Processes the response received via Amazon Bedrock and performs the necessary actions based on the stop reason. /// </summary> /// <param name="modelResponse">The model's response returned via Amazon Bedrock.</param> /// <param name="conversation">The conversation history.</param> /// <param name="maxRecursion">The maximum number of recursive calls allowed.</param> private static async Task ProcessModelResponseAsync(ConverseResponse modelResponse, List<Message> conversation, int maxRecursion) { if (maxRecursion <= 0) { // Stop the process, the number of recursive calls could indicate an infinite loop Console.WriteLine("\tWarning: Maximum number of recursions reached. Please try again."); } // Append the model's response to the ongoing conversation conversation.Add(modelResponse.Output.Message); if (modelResponse.StopReason == "tool_use") { // If the stop reason is "tool_use", forward everything to the tool use handler await HandleToolUseAsync(modelResponse.Output, conversation, maxRecursion - 1); } if (modelResponse.StopReason == "end_turn") { // If the stop reason is "end_turn", print the model's response text, and finish the process PrintModelResponse(modelResponse.Output.Message.Content[0].Text); if (!_interactive) { default_prompt = "x"; } } } /// <summary> /// Handles the tool use case by invoking the specified tool and sending the tool's response back to Bedrock. /// The tool response is appended to the conversation, and the conversation is sent back to Amazon Bedrock for further processing. /// </summary> /// <param name="modelResponse">The model's response containing the tool use request.</param> /// <param name="conversation">The conversation history.</param> /// <param name="maxRecursion">The maximum number of recursive calls allowed.</param> public static async Task HandleToolUseAsync(ConverseOutput modelResponse, List<Message> conversation, int maxRecursion) { // Initialize an empty list of tool results var toolResults = new List<ContentBlock>(); // The model's response can consist of multiple content blocks foreach (var contentBlock in modelResponse.Message.Content) { if (!String.IsNullOrEmpty(contentBlock.Text)) { // If the content block contains text, print it to the console PrintModelResponse(contentBlock.Text); } if (contentBlock.ToolUse != null) { // If the content block is a tool use request, forward it to the tool var toolResponse = await InvokeTool(contentBlock.ToolUse); // Add the tool use ID and the tool's response to the list of results toolResults.Add(new ContentBlock { ToolResult = new ToolResultBlock() { ToolUseId = toolResponse.ToolUseId, Content = new List<ToolResultContentBlock>() { new ToolResultContentBlock { Json = toolResponse.Content } } } }); } } // Embed the tool results in a new user message var message = new Message() { Role = ConversationRole.User, Content = toolResults }; // Append the new message to the ongoing conversation conversation.Add(message); // Send the conversation to Amazon Bedrock var response = await SendConversationToBedrock(conversation); // Recursively handle the model's response until the model has returned its final response or the recursion counter has reached 0 await ProcessModelResponseAsync(response, conversation, maxRecursion); } /// <summary> /// Invokes the specified tool with the given payload and returns the tool's response. /// If the requested tool does not exist, an error message is returned. /// </summary> /// <param name="payload">The payload containing the tool name and input data.</param> /// <returns>The tool's response or an error message.</returns> public static async Task<ToolResponse> InvokeTool(ToolUseBlock payload) { var toolName = payload.Name; if (toolName == "Weather_Tool") { var inputData = payload.Input.AsDictionary(); PrintToolUse(toolName, inputData); // Invoke the weather tool with the input data provided var weatherResponse = await _weatherTool.FetchWeatherDataAsync(inputData["latitude"].ToString(), inputData["longitude"].ToString()); return new ToolResponse { ToolUseId = payload.ToolUseId, Content = weatherResponse }; } else { var errorMessage = $"\tThe requested tool with name '{toolName}' does not exist."; return new ToolResponse { ToolUseId = payload.ToolUseId, Content = new { error = true, message = errorMessage } }; } } /// <summary> /// Prompts the user for input and returns the user's response. /// Returns null if the user enters 'x' to exit. /// </summary> /// <param name="prompt">The prompt to display to the user.</param> /// <returns>The user's input or null if the user chooses to exit.</returns> private static async Task<string?> GetUserInputAsync(string prompt = "\tYour weather info request:") { var userInput = default_prompt; if (_interactive) { Console.WriteLine(new string('*', 80)); Console.WriteLine($"{prompt} (x to exit): \n\t"); userInput = Console.ReadLine(); } if (string.IsNullOrWhiteSpace(userInput)) { prompt = "\tPlease enter your weather info request, e.g. the name of a city"; return await GetUserInputAsync(prompt); } if (userInput.ToLowerInvariant() == "x") { return null; } return userInput; } /// <summary> /// Logs the welcome message and usage guide for the tool use demo. /// </summary> public static void PrintHeader() { Console.WriteLine(@" ================================================= Welcome to the Amazon Bedrock Tool Use demo! ================================================= This assistant provides current weather information for user-specified locations. You can ask for weather details by providing the location name or coordinates. Weather information will be provided using a custom Tool and open-meteo API. Example queries: - What's the weather like in New York? - Current weather for latitude 40.70, longitude -74.01 - Is it warmer in Rome or Barcelona today? To exit the program, simply type 'x' and press Enter. P.S.: You're not limited to single locations, or even to using English! Have fun and experiment with the app! "); } /// <summary> /// Logs the footer information for the tool use demo. /// </summary> public static void PrintFooter() { Console.WriteLine(@" ================================================= Thank you for checking out the Amazon Bedrock Tool Use demo. We hope you learned something new, or got some inspiration for your own apps today! For more Bedrock examples in different programming languages, have a look at: https://docs.aws.amazon.com/bedrock/latest/userguide/service_code_examples.html ================================================= "); } /// <summary> /// Logs information about the tool use. /// </summary> /// <param name="toolName">The name of the tool being used.</param> /// <param name="inputData">The input data for the tool.</param> public static void PrintToolUse(string toolName, Dictionary<string, Document> inputData) { Console.WriteLine($"\n\tInvoking tool: {toolName} with input: {inputData["latitude"].ToString()}, {inputData["longitude"].ToString()}...\n"); } /// <summary> /// Logs the model's response. /// </summary> /// <param name="message">The model's response message.</param> public static void PrintModelResponse(string message) { Console.WriteLine("\tThe model's response:\n"); Console.WriteLine(message); Console.WriteLine(); } }

デモで使用される気象ツール。このファイルは、ツール仕様を定義し、Open-Meteo API から を使用して気象データを取得するロジックを実装します。

using Amazon.BedrockRuntime.Model; using Amazon.Runtime.Documents; using Microsoft.Extensions.Logging; namespace ConverseToolScenario; /// <summary> /// Weather tool that will be invoked when requested by the Bedrock response. /// </summary> public class WeatherTool { private readonly ILogger<WeatherTool> _logger; private readonly IHttpClientFactory _httpClientFactory; public WeatherTool(ILogger<WeatherTool> logger, IHttpClientFactory httpClientFactory) { _logger = logger; _httpClientFactory = httpClientFactory; } /// <summary> /// Returns the JSON Schema specification for the Weather tool. The tool specification /// defines the input schema and describes the tool's functionality. /// For more information, see https://json-schema.org/understanding-json-schema/reference. /// </summary> /// <returns>The tool specification for the Weather tool.</returns> public ToolSpecification GetToolSpec() { ToolSpecification toolSpecification = new ToolSpecification(); toolSpecification.Name = "Weather_Tool"; toolSpecification.Description = "Get the current weather for a given location, based on its WGS84 coordinates."; Document toolSpecDocument = Document.FromObject( new { type = "object", properties = new { latitude = new { type = "string", description = "Geographical WGS84 latitude of the location." }, longitude = new { type = "string", description = "Geographical WGS84 longitude of the location." } }, required = new[] { "latitude", "longitude" } }); toolSpecification.InputSchema = new ToolInputSchema() { Json = toolSpecDocument }; return toolSpecification; } /// <summary> /// Fetches weather data for the given latitude and longitude using the Open-Meteo API. /// Returns the weather data or an error message if the request fails. /// </summary> /// <param name="latitude">The latitude of the location.</param> /// <param name="longitude">The longitude of the location.</param> /// <returns>The weather data or an error message.</returns> public async Task<Document> FetchWeatherDataAsync(string latitude, string longitude) { string endpoint = "https://api.open-meteo.com/v1/forecast"; try { var httpClient = _httpClientFactory.CreateClient(); var response = await httpClient.GetAsync($"{endpoint}?latitude={latitude}&longitude={longitude}&current_weather=True"); response.EnsureSuccessStatusCode(); var weatherData = await response.Content.ReadAsStringAsync(); Document weatherDocument = Document.FromObject( new { weather_data = weatherData }); return weatherDocument; } catch (HttpRequestException e) { _logger.LogError(e, "Error fetching weather data: {Message}", e.Message); throw; } catch (Exception e) { _logger.LogError(e, "Unexpected error fetching weather data: {Message}", e.Message); throw; } } }

ツール設定を使用した Converse API アクション。

/// <summary> /// Wrapper class for interacting with the Amazon Bedrock Converse API. /// </summary> public class BedrockActionsWrapper { private readonly IAmazonBedrockRuntime _bedrockClient; private readonly ILogger<BedrockActionsWrapper> _logger; /// <summary> /// Initializes a new instance of the <see cref="BedrockActionsWrapper"/> class. /// </summary> /// <param name="bedrockClient">The Bedrock Converse API client.</param> /// <param name="logger">The logger instance.</param> public BedrockActionsWrapper(IAmazonBedrockRuntime bedrockClient, ILogger<BedrockActionsWrapper> logger) { _bedrockClient = bedrockClient; _logger = logger; } /// <summary> /// Sends a Converse request to the Amazon Bedrock Converse API. /// </summary> /// <param name="modelId">The Bedrock Model Id.</param> /// <param name="systemPrompt">A system prompt instruction.</param> /// <param name="conversation">The array of messages in the conversation.</param> /// <param name="toolSpec">The specification for a tool.</param> /// <returns>The response of the model.</returns> public async Task<ConverseResponse> SendConverseRequestAsync(string modelId, string systemPrompt, List<Message> conversation, ToolSpecification toolSpec) { try { var request = new ConverseRequest() { ModelId = modelId, System = new List<SystemContentBlock>() { new SystemContentBlock() { Text = systemPrompt } }, Messages = conversation, ToolConfig = new ToolConfiguration() { Tools = new List<Tool>() { new Tool() { ToolSpec = toolSpec } } } }; var response = await _bedrockClient.ConverseAsync(request); return response; } catch (ModelNotReadyException ex) { _logger.LogError(ex, "Model not ready, please wait and try again."); throw; } catch (AmazonBedrockRuntimeException ex) { _logger.LogError(ex, "Error occurred while sending Converse request."); throw; } } }
  • API の詳細については、「AWS SDK for .NET API リファレンス」の「Converse」を参照してください。

AWS SDK 開発者ガイドとコード例の完全なリストについては、「」を参照してくださいAWS SDK での Amazon Bedrock の使用。このトピックには、使用開始方法に関する情報と、以前の SDK バージョンの詳細も含まれています。

プライバシーサイト規約Cookie の設定
© 2025, Amazon Web Services, Inc. or its affiliates.All rights reserved.