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.
- Running this code might result in charges to your AWS account. For more details, see AWS Pricing and Free Tier.
- Running the tests might result in charges to your AWS account.
- 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.
- This code is not tested in every AWS Region. For more information, see AWS Regional Services.
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:
- Set up the system prompt and tool configuration.
- Specify the AI model to be used (e.g., Anthropic Claude 3 Sonnet).
- Create a client to interact with Amazon Bedrock.
- Prompt the user for their weather request.
- Send the user input including the conversation history to the model.
- 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.
- The scenario application invokes the tool to fetch weather data, and append the response and tool use ID to the conversation.
- The model uses the tool response to generate a final response. If additional tool requests are needed, the process is repeated.
- Once the final response is received and printed, the application returns to the prompt.
For prerequisites, see the README in the javascriptv3 folder.
node converse-tool-scenario.js⚠ Running tests might result in charges to your AWS account.
To find instructions for running these tests, see the README in the javascriptv3 folder.
- Documentation: The Amazon Bedrock User Guide
- Tutorials: A developer's guide to Bedrock's new Converse API
- More examples: Amazon Bedrock code examples and scenarios in multiple programming languages
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0