python/example_code/bedrock-runtime/cross-model-scenarios/tool_use_demo/README.md
This project demonstrates 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 to retrieve current weather data for a given location.
This directory contains the primary script, utility scripts, and the configuration required to demonstrate the weather assistant's functionality.
tool_use_demo.pyThis primary execution script manages a conversational application using the Amazon Bedrock Converse API integrated with a weather tool.
The application will interactively request weather information for locations specified by the user, demonstrating how to build a chatbot that provides real-time weather information based on user queries.
python tool_use_demo.pyweather_tool.py: Provides functionality to fetch weather data from the Open-Meteo API based on geographical
coordinates. This script defines the tool specification and implements the logic to retrieve weather data using
latitude and longitude.utils/tool_use_print_utils.py: Contains utility functions for printing headers, footers, and other formatted
messages to the console, enhancing the readability of logs or messages displayed during the interaction.requirements.txt: Lists all Python packages that need to be installed to run the project. This file is used
with pip install -r requirements.txt to ensure all dependencies are met.pyproject.toml: A configuration file that specifies project metadata and dependencies. It is compatible with various
Python packaging tools such as pip, setuptools, pipenv, and poetry.For general prerequisites, see the README in the python folder.
Note: You must request access to an AI model on Amazon Bedrock before you can use it. If you try to use the model (with the API or console) before you have requested access to it, you will receive an error message. For more information, see Model access.
Depending on your operating system and how you have Python installed, the commands to install and run might vary slightly. For example, you may have to use
pyorpython3in place ofpython.
To get started, you can choose one of the following options to set up the environment:
requirements.txt1. Create a virtual environment:
python -m venv .venv
A virtual environment is a self-contained directory that contains a Python installation and its associated packages, ensuring that your project's dependencies are isolated from other projects.
2. Activate the virtual environment:
On Linux/macOS
source .venv/bin/activate
On Windows
.\.venv\Scripts\activate
Activating the virtual environment ensures that the Python interpreter and packages used in your shell session are those from the virtual environment.
3. Install dependencies:
pip install -r requirements.txt
This command installs all the packages listed in the requirements.txt file, ensuring your project has all the necessary dependencies.
pyproject.tomlAlternatively, you can use the pyproject.toml file with various package managers like Poetry, PDM, or Hatch. It
includes metadata about the project and a list of dependencies required for the project. Ensure you have the necessary
tool installed and follow its documentation for setting up the project environment.
1. Run the primary script:
python tool_use_demo.py
2. Follow the prompts:
Note: You're not limited to single locations, or even to using English! Feel free to experiment with the app.
This demo 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 flow follows these steps:
Step 1: 🛠️ Initialization
Step 2: 💬 User interaction
Step 3: 🤖 Request processing
Step 4: 📡 Tool invocation
Step 5: 🤖 Response handling
Step 6. ✅ Response output
This flow exemplifies how generative AI models can be integrated with applications and external tools or APIs to effectively solve user problems and achieve specific goals.
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0