### Backend Development Setup Source: https://github.com/aws-samples/amazon-bedrock-samples/blob/main/responsible_ai/automated-reasoning-rewriting-chatbot/DESIGN.md Instructions for setting up the backend development environment, including creating a virtual environment and installing dependencies. ```bash # Backend cd backend python -m venv venv source venv/bin/activate pip install -r requirements.txt ``` -------------------------------- ### Install setuptools and chess Source: https://github.com/aws-samples/amazon-bedrock-samples/blob/main/custom-models/import_models/llama-3.1-jumpstart-chess/llama-3.1-8b-jumpstart-chess-finetuning.ipynb Installs specific versions of setuptools and the chess library, required for game logic and environment setup. ```python !pip install setuptools==69.0.2 !pip install chess ``` -------------------------------- ### Frontend Development Setup Source: https://github.com/aws-samples/amazon-bedrock-samples/blob/main/responsible_ai/automated-reasoning-rewriting-chatbot/DESIGN.md Instructions for setting up the frontend development environment by installing Node.js dependencies. ```bash # Frontend cd frontend npm install ``` -------------------------------- ### Set Up Streamlit Environment Source: https://github.com/aws-samples/amazon-bedrock-samples/blob/main/agents-and-function-calling/bedrock-agents/use-case-examples/insurance-claim-lifecycle-automation/documentation/testing-and-validation.md Navigate to the agent's Streamlit directory, make the setup script executable, and then run it to install dependencies and activate the virtual environment. ```shell # If not already cloned, clone the remote repository (https://github.com/aws-samples/amazon-bedrock-samples) and change working directory to insurance agent shell folder cd amazon-bedrock-samples/agents-and-function-calling/bedrock-agents/use-case-examples/insurance-claim-lifecycle-automation/agent/streamlit/ chmod u+x setup-streamlit-env.sh ``` ```shell source ./setup-streamlit-env.sh ``` -------------------------------- ### Install AWS CDK and Python Packages Source: https://github.com/aws-samples/amazon-bedrock-samples/blob/main/agents-and-function-calling/bedrock-agents/use-case-examples/text-2-sql-agent-cdk-enhanced/Readme.md Clone the repository, set up a virtual environment, and install the AWS CDK and required Python packages using the provided setup script. ```bash git clone https://github.com/aws-samples/amazon-bedrock-samples.git cd ./amazon-bedrock-samples/agents-and-function-calling-for-bedrock/use-case-examples/text-2-sql-agent-cdk-enhanced export AWS_PROFILE=XXX python3.9 -m venv .venv source .venv/bin/activate aws ecr-public get-login-password --region us-east-1 | docker login --username AWS --password-stdin public.ecr.aws chmod +x setup.sh ./setup.sh ``` -------------------------------- ### Setup and Utility Functions Source: https://github.com/aws-samples/amazon-bedrock-samples/blob/main/custom-models/import_models/perf-test-imported_model.ipynb Imports necessary libraries and defines utility functions for printing wrapped text and creating Bedrock clients. Ensure boto3 is installed before running. ```python import warnings from io import StringIO import sys import textwrap import os from typing import Optional # External Dependencies: import boto3 from botocore.config import Config warnings.filterwarnings('ignore') def print_ww(*args, width: int = 100, **kwargs): """Like print(), but wraps output to `width` characters (default 100)""" buffer = StringIO() try: _stdout = sys.stdout sys.stdout = buffer print(*args, **kwargs) output = buffer.getvalue() finally: sys.stdout = _stdout for line in output.splitlines(): print("\n".join(textwrap.wrap(line, width=width))) def get_boto_client_tmp_cred( retry_config = None, target_region: Optional[str] = None, runtime: Optional[bool] = True, service_name: Optional[str] = None, ): if not service_name: if runtime: service_name='bedrock-runtime' else: service_name='bedrock' bedrock_client = boto3.client( service_name=service_name, config=retry_config, aws_access_key_id=os.getenv("AWS_ACCESS_KEY_ID"), aws_secret_access_key=os.getenv("AWS_SECRET_ACCESS_KEY"), aws_session_token=os.getenv('AWS_SESSION_TOKEN'," "), ) print("boto3 Bedrock client successfully created!") print(bedrock_client._endpoint) return bedrock_client def get_boto_client( assumed_role: Optional[str] = None, region: Optional[str] = None, runtime: Optional[bool] = True, service_name: Optional[str] = None, ): """Create a boto3 client for Amazon Bedrock, with optional configuration overrides Parameters ---------- assumed_role : Optional ARN of an AWS IAM role to assume for calling the Bedrock service. If not specified, the current active credentials will be used. region : Optional name of the AWS Region in which the service should be called (e.g. "us-east-1"). If not specified, AWS_REGION or AWS_DEFAULT_REGION environment variable will be used. runtime : Optional choice of getting different client to perform operations with the Amazon Bedrock service. """ if region is None: target_region = os.environ.get("AWS_REGION", os.environ.get("AWS_DEFAULT_REGION")) else: target_region = region print(f"Create new client\n Using region: {target_region}") session_kwargs = {"region_name": target_region} client_kwargs = {**session_kwargs} profile_name = os.environ.get("AWS_PROFILE", None) retry_config = Config( region_name=target_region, signature_version = 'v4', retries={ "max_attempts": 10, "mode": "standard", }, ) if profile_name: print(f" Using profile: {profile_name}") session_kwargs["profile_name"] = profile_name else: # use temp credentials -- add to the client kwargs print(f" Using temp credentials") return get_boto_client_tmp_cred(retry_config=retry_config,target_region=target_region, runtime=runtime, service_name=service_name) session = boto3.Session(**session_kwargs) if assumed_role: print(f" Using role: {assumed_role}", end='') sts = session.client("sts") response = sts.assume_role( RoleArn=str(assumed_role), RoleSessionName="cmi-llm-1" ) print(" ... successful!") client_kwargs["aws_access_key_id"] = response["Credentials"]["AccessKeyId"] client_kwargs["aws_secret_access_key"] = response["Credentials"]["SecretAccessKey"] client_kwargs["aws_session_token"] = response["Credentials"]["SessionToken"] if not service_name: if runtime: service_name='bedrock-runtime' else: service_name='bedrock' bedrock_client = session.client( service_name=service_name, config=retry_config, **client_kwargs ) print("boto3 Bedrock client successfully created!") print(bedrock_client._endpoint) return bedrock_client ``` -------------------------------- ### Navigate to Project Directory Source: https://github.com/aws-samples/amazon-bedrock-samples/blob/main/agents-and-function-calling/bedrock-agents/use-case-examples/fine-grained-access-permissions-agent/README.md Changes the current directory to the specific agent example within the cloned repository. This is necessary to run subsequent setup commands. ```bash cd amazon-bedrock-samples/agents-and-function-calling/bedrock-agents/use-case-examples/fine-grained-access-permissions-agent ``` -------------------------------- ### Example Bedrock RFT Workflow Setup Source: https://github.com/aws-samples/amazon-bedrock-samples/blob/main/custom-models/bedrock-reinforcement-fine-tuning/helpers/README.md Demonstrates a complete workflow for setting up Bedrock RFT, including creating necessary IAM roles, packaging the reward function, deploying it as a Lambda, and creating the Bedrock RFT role. ```python import boto3 from helpers import ( create_lambda_execution_role, create_bedrock_rft_role, create_lambda_deployment_package, deploy_lambda_function ) REGION = "us-east-1" S3_BUCKET = "my-bucket" ACCOUNT_ID = boto3.client('sts').get_caller_identity()['Account'] # 1. Create Lambda role lambda_role_arn = create_lambda_execution_role( role_name="MyLambdaRole", region=REGION ) # 2. Create deployment package zip_content = create_lambda_deployment_package("my_reward_function.py") # 3. Deploy Lambda lambda_arn = deploy_lambda_function( function_name="my-reward-function", zip_content=zip_content, role_arn=lambda_role_arn, handler="my_reward_function.lambda_handler", region=REGION ) # 4. Create Bedrock role bedrock_role_arn = create_bedrock_rft_role( role_name="MyBedrockRole", region=REGION, account_id=ACCOUNT_ID, s3_bucket=S3_BUCKET, lambda_function_name="my-reward-function" ) ``` -------------------------------- ### Fine-tuning Script Setup Source: https://github.com/aws-samples/amazon-bedrock-samples/blob/main/custom-models/import_models/llama-3/customized-text-to-sql-model.ipynb Initializes the fine-tuning script by installing flash-attn, importing necessary libraries from PyTorch, Hugging Face, TRL, and PEFT, and setting up logging and random seeds. ```python import logging from dataclasses import dataclass, field import os try: os.system("pip install flash-attn --no-build-isolation --upgrade") except: print("flash-attn failed to install") import random import torch from datasets import load_dataset from tqdm import tqdm from trl.commands.cli_utils import TrlParser from transformers import ( AutoModelForCausalLM, AutoTokenizer, TrainingArguments, HfArgumentParser, BitsAndBytesConfig, set_seed, ) from trl import setup_chat_format from peft import LoraConfig from trl import ( SFTTrainer) # Anthropic/Vicuna like template without the need for special tokens ``` -------------------------------- ### Install Prerequisites Source: https://github.com/aws-samples/amazon-bedrock-samples/blob/main/responsible_ai/bedrock-automated-reasoning-checks/lounge_access_agent_demo/launch-demo-app.ipynb Installs the necessary dependencies for the demo application by running 'pip install -r requirements.txt'. ```python # installing pre-requisites !pip install -r requirements.txt ``` -------------------------------- ### Define Instructions and Document for Prompt Caching Example Source: https://github.com/aws-samples/amazon-bedrock-samples/blob/main/introduction-to-bedrock/prompt-caching/converse_api/notebooks/05_tenant_isolation.ipynb Defines the detailed instructions for a prompt caching task and the document content to be used. This setup is crucial for demonstrating prompt caching's effectiveness with large contexts. ```python INSTRUCTIONS = ( "I will provide you with a document, followed by a question about its content. " "Your task is to analyze the document, extract relevant information, and provide " "a comprehensive answer to the question. Please follow these detailed instructions:" "\n\n1. Identifying Relevant Quotes:" "\n - Carefully read through the entire document." "\n - Identify sections of the text that are directly relevant to answering the question." "\n - Select quotes that provide key information, context, or support for the answer." "\n - Quotes should be concise and to the point, typically no more than 2-3 sentences each." "\n - Choose a diverse range of quotes if multiple aspects of the question need to be addressed." "\n - Aim to select between 2 to 5 quotes, depending on the complexity of the question." "\n2. Presenting the Quotes:" "\n - List the selected quotes under the heading 'Relevant quotes:'" "\n - Number each quote sequentially, starting from [1]." "\n - Present each quote exactly as it appears in the original text, enclosed in quotation marks." "\n - If no relevant quotes can be found, write 'No relevant quotes' instead." "\n3. Formulating the Answer:" "\n - Begin your answer with the heading 'Answer:' on a new line after the quotes." "\n - Provide a clear, concise, and accurate answer to the question based on the information in the document." "\n - Ensure your answer is comprehensive and addresses all aspects of the question." "\n - Use information from the quotes to support your answer, but do not repeat them verbatim." "\n - Maintain a logical flow and structure in your response." "\n4. Referencing Quotes in the Answer:" "\n - Do not explicitly mention or introduce quotes in your answer." "\n - Instead, add the bracketed number of the relevant quote at the end of each sentence." "\n - If a sentence is supported by multiple quotes, include all relevant quote numbers." "\n5. Handling Uncertainty:" "\n - If the document does not contain enough information, clearly state this." "\n - Provide any partial information that is available." "\n6. Maintaining Objectivity:" "\n - Stick to the facts presented in the document." "\n - Do not include personal opinions or external information." "\n7. Formatting:" "\n - Use clear paragraph breaks to separate different points." "\n - Employ bullet points or numbered lists if it helps organize information." "\n - Ensure proper grammar, punctuation, and spelling." "\n8. Length and Depth:" "\n - Provide an answer that is sufficiently detailed to address the question comprehensively." "\n - Avoid unnecessary verbosity. Aim for clarity and conciseness." "\n9. Dealing with Complex Questions:" "\n - For questions with multiple parts, address each part separately." "\n - Use subheadings or numbered points to break down your answer." "\n10. Concluding the Answer:" "\n - If appropriate, provide a brief conclusion summarizing the key points." ) DOCUMENT = """ Prompt caching is a feature in Amazon Bedrock that stores portions of conversation context for reuse. When a cache checkpoint is hit on subsequent requests, the model skips reprocessing the cached tokens, resulting in lower Time-To-First-Token (TTFT) and reduced input token costs. Supported models include Anthropic Claude (Sonnet 4.6, Opus 4.6, Haiku 4.5), Amazon Nova, and select open-source models. Each model family has a minimum token threshold per cache checkpoint — for example, Claude Sonnet 4.6 requires at least 2,048 tokens. Cache entries are scoped per account and region. They expire based on the TTL specified in the request (default: 5 minutes, maximum: 1 hour for Converse API). The Converse API uses a cachePoint content block, while the InvokeModel API uses cache_control inside content blocks. Prompt caching is useful for chat-with-document workflows, coding assistants, agentic workflows with complex tool definitions, and few-shot learning scenarios with many examples. """ QUESTIONS = [ "What is prompt caching?", "What are the use cases for prompt caching?", ] ``` -------------------------------- ### Agent Initialization and Tool Use Source: https://github.com/aws-samples/amazon-bedrock-samples/blob/main/docs/workshop/open-source-l400/03_travel_agent_with_tools.md Initializes an agent and demonstrates its ability to use tools for a specific task. This example shows how to set up the agent and invoke its tool-using capabilities. ```python from langchain_aws import Chatgrimas from langchain_core.tools import tool from langchain_core.utils.function_calling import convert_to_openai_function @tool def get_flight_information(destination: str, date: str) -> str: """Gets information about flights to a specific destination on a given date.""" return f"There are flights to {destination} on {date}." @tool def book_flight(destination: str, date: str) -> str: """Books a flight to a specific destination on a given date.""" return f"Flight to {destination} on {date} booked." agent = Chatgrimas(model_id="anthropic-claude-3-sonnet-20240229-v1:0") agent_with_tools = agent.bind_tools([get_flight_information, book_flight]) result = agent_with_tools.invoke("I want to fly to London tomorrow.") print(result.tool_calls) print(result.content) result = agent_with_tools.invoke("Book me a flight to Paris on December 25th.") print(result.tool_calls) print(result.content) ``` -------------------------------- ### Install Langchain-AWS from source Source: https://github.com/aws-samples/amazon-bedrock-samples/blob/main/docs/agents-and-function-calling/open-source-agents/langgraph/03_langgraph_agents_of_agent.md Installs the langchain-aws library by cloning the repository and installing it locally. ```bash cd ~ mkdir temp_t cd temp_t git clone https://github.com/langchain-ai/langchain-aws/ pip install ./langchain-aws/libs/aws/ ``` -------------------------------- ### Read Examples from File Source: https://github.com/aws-samples/amazon-bedrock-samples/blob/main/docs/agents/open-source-l400/00_Lab_Intro to Use-Case.md Reads a list of examples from a text file. Each line in the file is treated as a separate example. ```python with open("./data/examples.txt", "r") as file: examples = file.read().splitlines() ``` -------------------------------- ### Example User Query and Agent Response Source: https://github.com/aws-samples/amazon-bedrock-samples/blob/main/docs/workshop/open-source-l400/03_travel_agent_with_tools.md This example shows a user's query and a potential agent response after it has processed the query and decided to use a tool. The agent's response includes information obtained from a tool. ```python # Assuming the agent is already initialized with tools as shown above # User's query user_query = {"role": "user", "content": "I want to search for flights from New York to Paris on December 25th, 2024."} # Agent's response (simulated based on the placeholder logic) # In a real LangGraph setup, this would be the output of compiled_graph.invoke() # For demonstration, we manually call the agent's invoke method with the query # Placeholder for the actual TravelAgent class class TravelAgent: def __init__(self, tools): self.tools = tools print("TravelAgent initialized with tools:", [tool.__name__ for tool in tools]) def invoke(self, messages): print("Agent received messages:", messages) if "search flights to paris" in messages[-1]['content'].lower(): # Simulate tool call and response flight_info = {"flights": [{"airline": "ExampleAir", "flight_number": "EA123", "departure_time": "10:00", "arrival_time": "22:00"}]} return {"messages": [{"role": "assistant", "content": f"Okay, I found some flights for you: {flight_info}"}] } return {"messages": [{"role": "assistant", "content": "I can help with travel plans."}] } # Re-initialize agent for this example agent = TravelAgent(tools=[search_flights, book_hotel, get_weather]) # Simulate the agent processing the user query agent_response = agent.invoke({"messages": [user_query]}) print("Agent Response:", agent_response) ``` -------------------------------- ### Install Required Python Packages Source: https://github.com/aws-samples/amazon-bedrock-samples/blob/main/docs/workshop/open-source-l400/03_travel_agent_with_tools.md Installs necessary libraries for LangChain, LangGraph, AWS integration, RAG, and data handling. Ensure your environment meets the specified version constraints. ```python # %pip install -U --no-cache-dir \ # "langchain==0.3.7" \ # "langchain-aws==0.2.6" \ # "langchain-community==0.3.5" \ # "langchain-text-splitters==0.3.2" \ # "langchainhub==0.1.20" \ # "langgraph==0.2.45" \ # "langgraph-checkpoint==2.0.2" \ # "langgraph-sdk==0.1.35" \ # "langsmith==0.1.140" \ # "pypdf==3.8,<4" \ # "ipywidgets>=7,<8" \ # "matplotlib==3.9.0" \ # "faiss-cpu==1.8.0" \ # "pandas==2.2.3" ``` -------------------------------- ### Example: Get LLM's Next Move for Initial Board State Source: https://github.com/aws-samples/amazon-bedrock-samples/blob/main/custom-models/import_models/llama-3.1-jumpstart-chess/test_chess_model.ipynb Demonstrates calling the `get_llm_next_move` function with a standard starting FEN and specifying 'WHITE' as the next turn. This is a basic test case. ```python get_llm_next_move("rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq - 0 1", "WHITE", None) ``` -------------------------------- ### Open a Tutorial Notebook Source: https://github.com/aws-samples/amazon-bedrock-samples/blob/main/advanced-prompt-optimization/README.md Launches a Jupyter Notebook server to access and run the tutorial notebooks, starting with '01_lambda_metric.ipynb'. ```bash jupyter notebook 01_lambda_metric.ipynb ``` -------------------------------- ### Initialize the Travel Agent Source: https://github.com/aws-samples/amazon-bedrock-samples/blob/main/docs/workshop/open-source-l400/03_travel_agent_with_tools.md Initializes the travel agent with a language model and the defined tools. This setup is crucial for enabling the agent's capabilities. ```python from langgraph.graph import StateGraph, END from typing import TypedDict, Annotated, List from langchain_openai import ChatOpenAI class AgentState(TypedDict): messages: Annotated[List[BaseMessage], operator.add] def initialize_agent(): llm = ChatOpenAI(model="gpt-4o", temperature=0) agent = create_tool_calling_agent(llm, tools, prompt) return agent ``` -------------------------------- ### Install LangChain Dependencies Source: https://github.com/aws-samples/amazon-bedrock-samples/blob/main/introduction-to-bedrock/cross-region/Getting_started_with_Cross-region_Inference.ipynb Installs the necessary LangChain packages for AWS integration. Run this command in your environment before using the LangChain examples. ```python #!pip install --quiet langchain_aws langchain_community ``` -------------------------------- ### Running the Travel Agent Source: https://github.com/aws-samples/amazon-bedrock-samples/blob/main/docs/workshop/open-source-l400/03_travel_agent_with_tools.md This Python code demonstrates how to invoke the compiled LangGraph application with an initial message to start the travel agent's process. ```python from langchain_core.messages import HumanMessage inputs = {"messages": [HumanMessage(content="Hi, I'd like to find a flight from New York to London tomorrow.")]} for s in app.stream(inputs): print(s) ``` -------------------------------- ### Example of a tool definition for car rental Source: https://github.com/aws-samples/amazon-bedrock-samples/blob/main/docs/workshop/open-source-l400/03_travel_agent_with_tools.md This snippet defines a tool for car rental searches, specifying parameters for pickup and return locations, dates, and times. The docstring explains its purpose. ```python @tool def get_car_rental_information(pickup_city: str, return_city: str, pickup_date: str, return_date: str, pickup_time: str, return_time: str) -> str: """Gets the car rental information between two cities on given dates and times.""" return f"Car rentals from {pickup_city} to {return_city} from {pickup_date} {pickup_time} to {return_date} {return_time}" ``` -------------------------------- ### Install Required Python Packages Source: https://github.com/aws-samples/amazon-bedrock-samples/blob/main/docs/workshop/open-source-l400/07_dynamic_tooling_agents.md Installs necessary libraries for LangChain, LangGraph, and other dependencies. Ensure your AWS setup is compatible with these versions. ```python # %pip install -U --no-cache-dir "langchain==0.3.7" "langchain-aws==0.2.6" "langchain-community==0.3.5" "langchain-text-splitters==0.3.2" "langchainhub==0.1.20" "langgraph==0.2.45" "langgraph-checkpoint==2.0.2" "langgraph-sdk==0.1.35" "langsmith==0.1.140" "pypdf==3.8,<4" "ipywidgets>=7,<8" "matplotlib==3.9.0" "faiss-cpu==1.8.0" "pandas==2.2.3" ``` -------------------------------- ### Example of a tool definition for a travel agent Source: https://github.com/aws-samples/amazon-bedrock-samples/blob/main/docs/workshop/open-source-l400/03_travel_agent_with_tools.md This snippet shows how to define a tool, likely for a language model, to interact with a travel booking system. It specifies the function name, description, and parameters. ```python from langchain_core.tools import tool @tool def get_flight_information(departure_city: str, arrival_city: str, date: str) -> str: """Gets the flight information between two cities on a given date.""" return f"Flights from {departure_city} to {arrival_city} on {date}" ``` -------------------------------- ### ToolMessage with Travel Guide Content Source: https://github.com/aws-samples/amazon-bedrock-samples/blob/main/docs/workshop/open-source-l400/03_travel_agent_with_tools.md This snippet represents a ToolMessage containing a detailed travel guide for New York City, generated by a language model. ```python ToolMessage(content='Travel Guide: New York\nGenerated by Llama3.1 405B\n \nNew York City, the iconic metropolis that never sleeps, is a captivating destination that offers an\nunparalleled blend of history, culture, and endless possibilities. From the towering skyscrapers of\nManhattan to the vibrant neighborhoods that pulse with energy, this city is a true melting pot, drawing\nvisitors from around the world to experience its unique charm.\n \nIconic landmarks such as the Statue of Liberty, the Empire State Building, and Central Park stand as\ntestaments to the city\'s grandeur, each offering a unique perspective on the city\'s rich history and\narchitectural prowess. The Metropolitan Museum of Art, one of the world\'s greatest art institutions,\nhouses an unrivaled collection that spans centuries and cultures, while the bright lights of Broadway\nbeckon theater enthusiasts to immerse themselves in the magic of live performances.\n \nBeyond the city\'s well-known attractions, New York is a haven for foodies, with a diverse array of\ncuisines and dining experiences to satisfy every palate. From the iconic New York-style pizza to the\ncutting-edge culinary creations of the city\'s renowned chefs, the gastronomic delights of New York are\nsure to leave a lasting impression.\n \nWhether you\'re seeking the hustle and bustle of the city streets or the tranquility of its green spaces,\nNew York offers an unparalleled experience that will captivate and inspire travelers from all walks of\nlife.\n \nPage 1\n\n\nTravel Guide: Philadelphia\nGenerated by Llama3.1 405B\n \nPhiladelphia, the City of Brotherly Love, is a captivating destination that seamlessly blends rich history,\nvibrant culture, and modern innovation. As the birthplace of American democracy, this iconic city invites\nvisitors to immerse themselves in the legacy of the Founding Fathers, exploring iconic landmarks like\nthe Liberty Bell and Independence Hall. Beyond its historical significance, Philadelphia boasts a thriving\narts scene, with world-class museums, performing arts venues, and a bustling culinary landscape that\ncelebrates diverse flavors from around the globe. From the iconic Rocky steps at the Philadelphia\nMuseum of Art to the serene beauty of the Fairmount Park system, this dynamic city offers endless\nopportunities for exploration and discovery, making it a must-visit destination for any traveler seeking to\nexperience the heart and soul of America.\n \nPage 1\n\n\nTravel Guide: Atlanta\nGenerated by Llama3.1 405B\n \nAtlanta, the vibrant capital of Georgia, is a city that seamlessly blends its rich history with a dynamic,\nmodern spirit. As the birthplace of the civil rights movement and the home of iconic landmarks like the\nMartin Luther King Jr. National Historic Site, Atlanta offers visitors a profound connection to America\'s\npast. Towering skyscrapers and bustling streets give way to the serene greenery of Piedmont Park,\nwhere locals and tourists alike gather to enjoy the city\'s natural beauty. From the world-class exhibits at\nthe Georgia Aquarium to the thrilling rides at Six Flags Over Georgia, Atlanta provides endless\nopportunities for adventure and exploration. Foodies will delight in the city\'s diverse culinary scene, with\neverything from classic Southern cuisine to innovative global flavors. Whether you\'re drawn to the city\'s\nstoried past or its exciting present, Atlanta is a destination that will captivate and inspire.\n \nPage 1\n\n\nTravel Guide: Austin\nGenerated by Llama3.1 405B\n \nAustin, the vibrant capital of Texas, is a city that effortlessly blends its rich history with a thriving\nmodern culture. Known as the \"Live Music Capital of the World,\" Austin\'s legendary music scene\npulsates through its countless bars, clubs, and outdoor stages, where both up-and-coming and\nrenowned artists take the stage nightly. Explore the iconic Congress Avenue Bridge, home to the\nworld\'s largest urban bat colony, or stroll through the serene trails of Barton Creek Greenbelt, a natural\noasis in the heart of the city. Foodies will delight in the ``` -------------------------------- ### Install Langchain AWS Source: https://github.com/aws-samples/amazon-bedrock-samples/blob/main/docs/agents/open-source-l400/00_Lab_Intro to Use-Case.md Installs the necessary langchain-aws library for interacting with Amazon Bedrock. Use this command in your environment before running other setup steps. ```bash !pip3 install langchain-aws --quiet ``` -------------------------------- ### Travel Agent Tool Usage Example Source: https://github.com/aws-samples/amazon-bedrock-samples/blob/main/docs/workshop/open-source-l400/03_travel_agent_with_tools.md This Python code demonstrates how a travel agent agent can utilize tools to find flights and hotels. It shows the agent's thought process and the tool calls made to fulfill the user's request. ```python from langgraph.graph import StateGraph, END from typing import TypedDict, Annotated, List from langgraph.checkpoint.memory import MemorySaver class AgentState(TypedDict): messages: Annotated[List[dict], lambda x, y: y.append(x) and y[-1]] # The following are used by the tools flights: List[str] hotels: List[str] def agent_node(state: AgentState): messages = state['messages'] # In a real agent, you would use a language model to # determine which tool to use based on the messages. # For this example, we'll hardcode the tool calls. # The agent's thought process would be something like: # "I need to find flights and hotels for the user's request." # Then it would call the appropriate tools. return { "messages": [ { "role": "assistant", "content": "Finding flights and hotels..." } ] } def call_flights_tool(state: AgentState): # In a real agent, this would call an external API. # For this example, we'll return dummy data. return { "flights": [ "Flight 1: LAX to JFK, $300", "Flight 2: LAX to JFK, $350" ] } def call_hotels_tool(state: AgentState): # In a real agent, this would call an external API. # For this example, we'll return dummy data. return { "hotels": [ "Hotel 1: JFK Airport Hotel, $150/night", "Hotel 2: Near JFK, $120/night" ] } def route_tool(state: AgentState) -> dict: # This is a simplified router. In a real agent, a language model # would determine which tool to use based on the user's intent. # For this example, we'll assume the user wants flights and hotels. messages = state['messages'] user_message = messages[-1]['content'] if "flight" in user_message.lower(): return {"next": "call_flights_tool"} elif "hotel" in user_message.lower(): return {"next": "call_hotels_tool"} else: # Default to agent node if no specific tool is identified return {"next": "agent"} workflow = StateGraph(AgentState) # Define the nodes workflow.add_node("agent", agent_node) workflow.add_node("call_flights_tool", call_flights_tool) workflow.add_node("call_hotels_tool", call_hotels_tool) # Define the edges workflow.add_edge("agent", "route_tool") workflow.add_edge("call_flights_tool", "agent") workflow.add_edge("call_hotels_tool", "agent") # Add a conditional edge for routing workflow.add_conditional_edges( "agent", route_tool, { "call_flights_tool": "call_flights_tool", "call_hotels_tool": "call_hotels_tool", "agent": "agent" # This would loop back to the agent if no tool is found } ) # Set the entry point workflow.set_entry_point("agent") # Compile the graph app = workflow.compile() # Example usage: inputs = {"messages": [{"role": "user", "content": "I want to find flights and hotels to New York"}]} # To use memory, uncomment the following lines: # saver = MemorySaver() # app = workflow.compile(checkpointer=saver) # Run the graph for step in app.stream(inputs): for key, value in step.items(): print(f"\n-> {key}") print(value) ``` -------------------------------- ### Install Python Libraries Source: https://github.com/aws-samples/amazon-bedrock-samples/blob/main/custom-models/import_models/qwen-3/CMI-Qwen3-HF-Download.ipynb Installs necessary Python libraries including boto3, huggingface_hub, and sagemaker. Huggingface_hub and sagemaker are optional depending on your setup. ```python %pip install boto3 "huggingface_hub>=1.2,<2" sagemaker ``` -------------------------------- ### Example of a tool definition for hotel booking Source: https://github.com/aws-samples/amazon-bedrock-samples/blob/main/docs/workshop/open-source-l400/03_travel_agent_with_tools.md This Python code defines a tool for searching hotel availability. It includes parameters for city, check-in date, and check-out date, along with a descriptive docstring. ```python @tool def get_hotel_information(city: str, check_in_date: str, check_out_date: str) -> str: """Gets the hotel information for a city between two dates.""" return f"Hotels in {city} from {check_in_date} to {check_out_date}" ``` -------------------------------- ### Install Dependencies and Deploy Source: https://github.com/aws-samples/amazon-bedrock-samples/blob/main/cost-reporting/converse-metadata-cost-reporting/README.md Install project dependencies using uv sync and make the deploy script executable. This prepares the environment for deployment. ```shell # Install dependencies with uv uv sync # Make the script executable chmod +x deploy.sh ``` -------------------------------- ### Install Required Python Packages Source: https://github.com/aws-samples/amazon-bedrock-samples/blob/main/custom-models/import_models/llama-3/benchmark-deepseek-r1-distill-llama-llmperf.ipynb Installs the necessary libraries for SageMaker, boto3, and Hugging Face interactions. Use this at the beginning of your environment setup. ```python !pip install transformers sagemaker --quiet !pip install boto3 huggingface huggingface_hub hf_transfer --upgrade --quiet ``` -------------------------------- ### Install Dependencies (Simplified) Source: https://github.com/aws-samples/amazon-bedrock-samples/blob/main/workshops/open-source-l400/05_dream_destination_with_crewai.ipynb Installs core CrewAI and related packages with simplified versioning. Use this for a quicker setup if specific version compatibility is not critical. ```python __!pip install boto3 botocore crewai crewai_tools duckduckgo-search langchain-community -q__ ``` -------------------------------- ### Start Web Server Source: https://github.com/aws-samples/amazon-bedrock-samples/blob/main/responsible_ai/bedrock-guardrails-optimizer/README.md Launches the web server for the visual interface. Access it via http://localhost:8080. ```bash cd src python api_server.py ``` -------------------------------- ### Install Chess and Stockfish Libraries Source: https://github.com/aws-samples/amazon-bedrock-samples/blob/main/custom-models/import_models/llama-3.1-jumpstart-chess/test_chess_model.ipynb Installs the necessary Python libraries for chess game logic and the Stockfish engine. Use this at the beginning of your environment setup. ```python !pip install chess stockfish --upgrade --quiet ``` -------------------------------- ### Initialize Amplify Project Source: https://github.com/aws-samples/amazon-bedrock-samples/blob/main/agents-and-function-calling/bedrock-agents/use-case-examples/fine-grained-access-permissions-agent/README.md Use 'amplify init' to set up a new Amplify project. Follow prompts for project name, environment, and framework configuration. ```bash amplify init ``` ```text Enter a name for the project: bedrocksecurity The following configuration will be applied: Project information | Name: bedrocksecurity | Environment: dev | Default editor: Visual Studio Code | App type: javascript | Javascript framework: react | Source Directory Path: src | Distribution Directory Path: build | Build Command: npm run-script build | Start Command: npm run-script start Initialize the project with the above configuration? Yes Using default provider awscloudformation Select the authentication method you want to use: AWS profile For more information on AWS Profiles, see: https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-profiles.html Please choose the profile you want to use oktank Adding backend environment dev to AWS Amplify app: d5np40989zxhn ``` ```text Deployment completed. Deploying root stack bedrocksecurity [ ---------------------------------------- ] 0/4 amplify-bedrocksecurity-dev-1… AWS::CloudFormation::Stack CREATE_IN_PROGRESS Sat May 18 2024 15:04:42… AuthRole AWS::IAM::Role CREATE_IN_PROGRESS Sat May 18 2024 15:04:44… DeploymentBucket AWS::S3::Bucket CREATE_IN_PROGRESS Sat May 18 2024 15:04:44… UnauthRole AWS::IAM::Role CREATE_IN_PROGRESS Sat May 18 2024 15:04:44… ``` ```text ✔ Help improve Amplify CLI by sharing non-sensitive project configurations on failures (y/N) · no You can always opt-in by running "amplify configure --share-project-config-on" ``` ```text Deployment state saved successfully. ✔ Initialized provider successfully. ✅ Initialized your environment successfully. ✅ Your project has been successfully initialized and connected to the cloud! Some next steps: "amplify status" will show you what you've added already and if it's locally configured or deployed "amplify add " will allow you to add features like user login or a backend API "amplify push" will build all your local backend resources and provision it in the cloud "amplify console" to open the Amplify Console and view your project status "amplify publish" will build all your local backend and frontend resources (if you have hosting category added) and provision it in the cloud ``` -------------------------------- ### Example: Calling Functions to Get Weather Source: https://github.com/aws-samples/amazon-bedrock-samples/blob/main/articles-guides/prompt-engineering/session-4/Function_agent.ipynb Demonstrates how to use the `call_function` utility to sequentially call `get_lat_long` and `get_weather` to determine the weather for a specified place. ```python place = 'Las Vegas' lat_long_response = call_function('get_lat_long', {'place' : place}) weather_response = call_function('get_weather', lat_long_response) print(f'Weather in {place} is...') weather_response ``` -------------------------------- ### Invoke Llama Model for Function Calling Source: https://github.com/aws-samples/amazon-bedrock-samples/blob/main/docs/agents-and-function-calling/function-calling/function_calling_with_invoke/function_calling_model_with_invoke.md Example of invoking a Llama model to process a message and print the generated response. This is a basic setup for model interaction. ```python message = f"""{system_prompt} Question: Who is the president of the US?<|eot_id|><|start_header_id|>assistant<|end_header_id|> " response = invoke_llama_model(bedrock, messages=message) print(response['generation']) ``` -------------------------------- ### Non-interactive Setup with Defaults Source: https://github.com/aws-samples/amazon-bedrock-samples/blob/main/advanced-prompt-optimization/README.md Executes the setup script non-interactively, accepting all default configurations. ```bash python setup.py -y ``` -------------------------------- ### Initialize AWS Clients and Configuration Source: https://github.com/aws-samples/amazon-bedrock-samples/blob/main/rag/knowledge-bases/features-examples/06-multi-modal-rag/audio-video-rag-using-kb/01_data_prep_using_bda.ipynb Sets up necessary AWS clients (STS, S3, BDA, Bedrock Agent) and defines bucket names and region for the project. ```python suffix = random.randrange(200, 900) sts_client = boto3.client('sts') account_id = sts_client.get_caller_identity()["Account"] bucket_name_kb = f'bedrock-kb-{suffix}-1' # replace it with your first bucket name. region_name = "us-west-2" # can be removed ones BDA is GA and available in other regions. region = region_name s3_client = boto3.client('s3', region_name=region_name) bda_client = boto3.client('bedrock-data-automation', region_name=region_name) bda_runtime_client = boto3.client('bedrock-data-automation-runtime', region_name=region_name) bedrock_agent_client = boto3.client('bedrock-agent') bedrock_agent_runtime_client = boto3.client('bedrock-agent-runtime') logging.basicConfig(format='[%(asctime)s] p%(process)s {%(filename)s:%(lineno)d} %(levelname)s - %(message)s', level=logging.INFO) logger = logging.getLogger(__name__) ``` -------------------------------- ### Get Inference Profile by ARN Source: https://github.com/aws-samples/amazon-bedrock-samples/blob/main/docs/poc-to-prod/inference-profiles/inference-profile-basics.md Retrieves metadata for an application inference profile using its ARN. This allows verification and inspection of the profile's properties and setup. ```python def get_inference_profile(inference_profile_arn): """Get Inference Profile by ARN""" response = bedrock.get_inference_profile( inferenceProfileIdentifier=inference_profile_arn ) print("GetInferenceProfile Response:", response['ResponseMetadata']['HTTPStatusCode']), print(response) return response print("Testing GetInferenceProfile...") profile_response = get_inference_profile(claims_dept_claude_3_sonnet_profile_arn) ``` -------------------------------- ### Initialize Bank Database Source: https://github.com/aws-samples/amazon-bedrock-samples/blob/main/agents-and-function-calling/agentic-guardrails/crewai_guardrails.ipynb Sets up the bank database by calling the setup function and defines the path to the SQLite database file. ```python setup_bank_database() DB_PATH = 'data/bank_data.db' ``` -------------------------------- ### Initialize Stateful Graph Components Source: https://github.com/aws-samples/amazon-bedrock-samples/blob/main/docs/workshop/open-source-l400/07_dynamic_tooling_agents.md Initializes components for building a stateful graph, including `StateGraph`, `START`, `add_messages`, `ToolNode`, and `tools_condition`. This setup is for multi-turn and multi-step workflows. ```python from typing import Annotated from typing_extensions import TypedDict from langgraph.graph import StateGraph, START from langgraph.graph.message import add_messages from langgraph.prebuilt import ToolNode, tools_condition from IPython.display import Image, display # Define the state structure using TypedDict. # It includes a list of messages (processed by add_messages) ``` -------------------------------- ### Creating a Vector Store for Prompt Examples Source: https://github.com/aws-samples/amazon-bedrock-samples/blob/main/agents-and-function-calling/open-source-agents/LangChain/00_medibot_V3_prompts.ipynb Defines example input-output pairs, initializes AWS Bedrock embeddings, vectorizes the examples, and creates a FAISS vector store for semantic similarity search. ```python examples = [ {"input": "What is the weather in New York city?", "output": "Respond using professional weatherman terminology:\n"}, {"input": "Tell me a joke", "output": "Respond as if you were a famous comedian that likes to joke about farm animals:\n"}, {"input": "Give me book recommendations", "output": "Respond as if you are a mistery novels fan:\n"}, {"input": "There is something wrong with my computer", "output": "Respond as if you are a former pirate turned IT repair expert:\n"}, ] br_embeddings = BedrockEmbeddings(model_id="amazon.titan-embed-text-v1", client=boto3_bedrock) to_vectorize = [" ".join(example.values()) for example in examples] embeddings = br_embeddings vectorstore = FAISS.from_texts(to_vectorize, embeddings, metadatas=examples) ``` -------------------------------- ### Environment Setup and AWS Client Initialization Source: https://github.com/aws-samples/amazon-bedrock-samples/blob/main/evaluation-observe/bedrock-llm-as-judge-evaluation/model-as-a-judge.ipynb Sets up the AWS environment by defining configuration variables and initializing Boto3 clients for Bedrock and S3. Ensure you replace placeholder values with your specific AWS details. ```python import boto3 import json import random from datetime import datetime from typing import List, Dict, Any, Optional # AWS Configuration REGION = "" ROLE_ARN = "arn:aws:iam:::role/" BUCKET_NAME = "" PREFIX = "" dataset_custom_name = "dummy-data" # Initialize AWS clients bedrock_client = boto3.client('bedrock', region_name=REGION) s3_client = boto3.client('s3', region_name=REGION) ``` -------------------------------- ### Setting Up Conversational Memory and LLM Source: https://github.com/aws-samples/amazon-bedrock-samples/blob/main/docs/workshop/open-source-l200/04_retrieval_based_chat_application.md Initializes the ChatBedrock LLM and sets up conversational memory, including adding example messages to guide the AI assistant's behavior. ```python llm = ChatBedrock( client=boto3_bedrock, model_id="anthropic.claude-instant-v1", model_kwargs={ "temperature": 0.9} ) memory_chain = ChatMessageHistory() memory_chain.add_user_message( 'Hello, what are you able to do?' ) memory_chain.add_ai_message( 'Hi! I am a help chat assistant which can answer questions about Amazon SageMaker.' ) ``` -------------------------------- ### Non-interactive Setup with Replay Mode and Environment Reuse Source: https://github.com/aws-samples/amazon-bedrock-samples/blob/main/advanced-prompt-optimization/README.md Configures the setup script to use replay mode and reuses existing environment settings without re-prompting. ```bash python setup.py --mode replay --reuse-env ``` -------------------------------- ### Generate Answers with System Prompt Source: https://github.com/aws-samples/amazon-bedrock-samples/blob/main/docs/agents/open-source-l400/00_Lab_Intro to Use-Case.md Generates model answers for a single example using a specific system prompt to guide the model's behavior, in this case, as a text summarizer. ```python one_example = examples[:1] output = generate_answers( one_example, SystemMessage(content="You are a text summarizer for travelers who are on the go. Generate your summary in a single sentence.")) ``` -------------------------------- ### Start Conversation with Empty Memory Source: https://github.com/aws-samples/amazon-bedrock-samples/blob/main/docs/agents-and-function-calling/bedrock-agents/features-examples/09-create-agent-with-memory/09-create-agent-with-memory.md Initiates a conversation with a Bedrock agent using a new session ID and a specified memory ID. This example demonstrates the initial query to book a trip. ```python session_id:str = str(uuid.uuid1()) memory_id:str = 'TST_MEM_ID' enable_trace:bool = False end_session:bool = False query = "Hi, my name is Anna. I would like to book a trip from Boston to NYC departuring on July 11th 2024 and returning on July 22nd 2024. Via AIR." invoke_agent_helper(query, session_id, agent_id, agent_alias_id, enable_trace=enable_trace, memory_id=memory_id) ``` -------------------------------- ### Import Libraries and Setup Logging Source: https://github.com/aws-samples/amazon-bedrock-samples/blob/main/model-latency-benchmarking/latency-benchmarking-tool.ipynb Imports all necessary Python libraries for the benchmarking tool and configures logging to a file, including creating output directories. ```python import subprocess import sys import boto3 import botocore import random import pprint import time import json import argparse import pandas as pd import numpy as np import random from datetime import datetime, timedelta import pytz import os import logging from botocore.config import Config from botocore.exceptions import ClientError import concurrent.futures from concurrent.futures import ThreadPoolExecutor from threading import Lock from typing import List, Dict logging_lock = Lock() os.makedirs(f"{directory}", exist_ok=True) os.makedirs(f"{directory}-analysis", exist_ok=True) # Configure logging logging.basicConfig(filename=f"latency-benchmarking-experiment-{datetime.now().strftime('%Y%m%d_%H%M%S')}.log", level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') ``` -------------------------------- ### Invoke Agent with Prompt Attributes Source: https://github.com/aws-samples/amazon-bedrock-samples/blob/main/docs/agents-and-function-calling/bedrock-agents/features-examples/08-create-agent-with-guardrails/08-create-agent-with-guardrails.md Demonstrates how to provide session context, such as user name, directly to the agent's prompt using `promptSessionAttributes`. A new session ID is started for this example. ```python # This code block is intentionally left blank as per the source. ```