AWS Builder Center

How I Built a Generative AI Chatbot in 15 Minutes Using Amazon Bedrock and Python

Generative AI doesn't have to be complex or require heavy infrastructure. In this step-by-step guide, I will show you how to quickly build a powerful serverless AI chatbot using Amazon Bedrock and Python (Boto3). You will learn how to set up model access, construct the API payload, and write clean code to generate responses using foundation models.

Generative AI is transforming how we build applications, but setting up the infrastructure to host Large Language Models (LLMs) can be daunting and expensive. As an AI and Data Science student, I wanted a way to experiment with powerful foundation models without worrying about server provisioning. Enter Amazon Bedrock. In this walkthrough, I will show you how to build a simple, serverless AI chatbot using Amazon Bedrock and Python (Boto3). What We Are Building We will create a Python script that communicates with Amazon Bedrock to generate text responses. We will use Anthropic's Claude model (available via Bedrock) because of its excellent conversational capabilities. Prerequisites An AWS Account. IAM User/Role with bedrock:InvokeModel permissions. Python installed with the boto3 library (pip install boto3). Step 1: Request Model Access in AWS Before you can use a model in Bedrock, you must explicitly enable it: Navigate to the Amazon Bedrock console. In the left navigation pane, select Model access. Click Manage model access and check the box next to Anthropic Claude (or Amazon Titan). Click Save changes. Step 2: The Python Implementation Once you have model access and your AWS credentials configured locally, building the chat interface requires just a few lines of Python. Here is the complete script: import boto3 import json
def generate_chat_response(prompt): # Initialize the Bedrock client bedrock = boto3.client(service_name='bedrock-runtime', region_name='us-east-1')

model_id = 'anthropic.claude-v2'
formatted_prompt = f"\n\nHuman: {prompt}\n\nAssistant:"

payload = {
"prompt": formatted_prompt,
"max_tokens_to_sample": 300,
"temperature": 0.7,
"top_p": 0.9,
}

body = json.dumps(payload)

try:
response = bedrock.invoke_model(
body=body,
modelId=model_id,
accept='application/json',
contentType='application/json'
)
response_body = json.loads(response.get('body').read())
return response_body.get('completion')
except Exception as e:
return f"Error invoking Bedrock: {e}"

Test the chatbot

if name == "main": user_input = "Explain how AWS Lambda works to a 5-year-old." print(f"User: {user_input}")
response = generate_chat_response(user_input)
print(f"Chatbot: {response}")
Breaking Down the Code The Client: We initialize the bedrock-runtime client, not just bedrock. The runtime client is specifically used for invoking models. Prompt Formatting: Different models require different prompt structures. Anthropic Claude expects prompts to be wrapped in \n\nHuman: ... \n\nAssistant:. Parameters: temperature: Controls the randomness (0.0 is deterministic, 1.0 is highly creative). max_tokens_to_sample: Limits the length of the generated response. Next Steps: Taking it to Production This script runs locally, but the real magic happens when you move this into the cloud. My next project is wrapping this Boto3 logic inside an AWS Lambda function and exposing it via Amazon API Gateway to create a fully serverless backend for a web chatbot! Over to you: Have you started experimenting with Amazon Bedrock yet? Which foundation model is your favorite to work with? Let me know in the comments below! 👇 #AWS #GenerativeAI #Python #MachineLearning #AmazonBedrock #Serverless
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article