Quickly build Generative AI applications with Amazon Bedrock
Learn how to build using foundation models quickly with Streamlit, Python, Claude, Stable Diffusion, and Amazon Bedrock.
Step 1: Subscribe to Models in the Amazon Bedrock Console
Step 2: Check your Python version
Step 3: Install the essentials
Creating stunning images with Stable Diffusion
Step 1: Initialize Amazon Bedrock
Versatile Text Tasks with Foundation Models
Retrieval Augmented Generation (RAG)
The Essentials: Embeddings, Vector Databases, and LangChain
Step 2: The Power of LangChain
Step 3: Add context to a Prompt
Create a Chatbot with Amazon Bedrock and Streamlit
1
python --version
1
2
3
git clone https://github.com/build-on-aws/amazon-bedrock-quick-start
cd amazon-bedrock-quick-start
pip install -U boto3 langchain streamlit pillow faiss-cpu
- Streamlit: This open-source Python library is a game-changer for building interactive web apps. Think of it as your go-to tool for quick and easy front-end development.
- Stable Diffusion: Released in 2022, this text-to-image model is a proficient at generating detailed visuals from textual descriptions. Imagine having a virtual artist at your command.
1
2
3
4
5
# Define Amazon Bedrock
bedrock_runtime = boto3.client(
service_name='bedrock-runtime',
region_name='us-east-1',
)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# Amazon Bedrock api call to stable diffusion
def generate_image(text, style):
"""
Purpose:
Uses Bedrock API to generate an Image
Args/Requests:
text: Prompt
style: style for image
Return:
image: base64 string of image
"""
body = {
"text_prompts": [{"text": text}],
"cfg_scale": 10,
"seed": 0,
"steps": 50,
"style_preset": style,
}
if style == "None":
del body["style_preset"]
body = json.dumps(body)
modelId = "stability.stable-diffusion-xl"
accept = "application/json"
contentType = "application/json"
response = bedrock_runtime.invoke_model(
body=body, modelId=modelId, accept=accept, contentType=contentType
)
response_body = json.loads(response.get("body").read())
results = response_body.get("artifacts")[0].get("base64")
return results
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# Turn base64 string to image with PIL
def base64_to_pil(base64_string):
"""
Purpose:
Turn base64 string to image with PIL
Args/Requests:
base64_string: base64 string of image
Return:
image: PIL image
"""
import base64
imgdata = base64.b64decode(base64_string)
image = Image.open(io.BytesIO(imgdata))
return image
1
2
3
4
5
6
7
8
9
10
11
12
# List of Stable Diffusion Preset Styles
#sd_presets = ["None","3d-model","analog-film","anime",...
# select box for styles
style = st.selectbox("Select Style", sd_presets)
# text input
prompt = st.text_input("Enter prompt")
# Generate image from prompt,
if st.button("Generate Image"):
image = base64_to_pil(generate_image(prompt, style))
st.image(image)
- Summarization: Boil down lengthy articles into crisp summaries.
- Code Generation: Automate the creation of functions, modules, or even whole applications.
- Q&A: Design intelligent systems capable of answering queries based on specific data sets or documents.
- Embeddings: These are vectors of floating-point numbers that help measure the relatedness between items. You've likely seen them in action in semantic search, classification, and recommendation systems.
- Vector Databases: These specialized databases manage vector embeddings. They simplify data management, offer real-time updates, and come with built-in security features, making them a preferred choice for developers.
- LangChain: A framework that supercharges applications with language model capabilities. LangChain makes your applications data-aware and interactive, offering modular components and off-the-shelf chains for various tasks.
1
2
3
4
5
6
7
8
9
10
11
12
13
def get_embedding(body, modelId, accept, contentType):
response = bedrock_runtime.invoke_model(body=body, modelId=modelId, accept=accept, contentType=contentType)
response_body = json.loads(response.get('body').read())
embedding = response_body.get('embedding')
return embedding
body = json.dumps({"inputText": "explain black holes to 8th graders"})
modelId = 'amazon.titan-embed-text-v1'
accept = 'application/json'
contentType = 'application/json'
embedding = get_embedding(body, modelId, accept, contentType)
print(embedding)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
from langchain.embeddings import BedrockEmbeddings
from langchain.vectorstores import FAISS
import boto3
import json
sentences = [
# Pets
"Your dog is so cute.",
"How cute your dog is!",
"You have such a cute dog!",
# Cities in the US
"New York City is the place where I work.",
"I work in New York City.",
# Color
"What color do you like the most?",
"What is your favorite color?",
]
embeddings = BedrockEmbeddings()
local_vector_store = FAISS.from_texts(sentences, embeddings)
query = "What type of pet do I have?"
docs = local_vector_store.similarity_search(query)
context = ""
for doc in docs:
context += doc.page_content
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
prompt = f"""Use the following pieces of context to answer the question at the end.
{context}
Question: {query}
Answer:"""
bedrock = boto3.client(
service_name="bedrock-runtime",
region_name="us-east-1",
endpoint_url="https://bedrock.us-east-1.amazonaws.com",
)
def call_bedrock(prompt):
prompt_config = {
"prompt": prompt,
"max_tokens_to_sample": 4096,
"temperature": 0.5,
"top_k": 250,
"top_p": 0.5,
"stop_sequences": [],
}
body = json.dumps(prompt_config)
modelId = "anthropic.claude-v2"
accept = "application/json"
contentType = "application/json"
response = bedrock_runtime.invoke_model(
body=body, modelId=modelId, accept=accept, contentType=contentType
)
response_body = json.loads(response.get("body").read())
results = response_body.get("results")[0].get("outputText")
return results
@st.cache_resource
decorator to cache the LangChain Language Model (LLM) instance. This saves us from recreating it every time we run the app.1
2
3
4
5
6
7
8
9
10
11
12
def load_llm():
llm = Bedrock(model_id="anthropic.claude-v2")
llm.model_kwargs = {"temperature": 0.7, "max_tokens_to_sample": 2048}
model = ConversationChain(
llm=llm, verbose=True, memory=ConversationBufferMemory()
)
return model
model = load_llm()
- Two message containers for the user and the bot.
- A chat input widget for the user.
- A mechanism to store and display chat history.
1
2
3
4
5
6
7
8
# Initialize chat history
if "messages" not in st.session_state:
st.session_state.messages = []
# Display chat messages from history on app rerun
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])
1
2
3
4
5
6
# React to user input
if prompt := st.chat_input("What is up?"):
# Display user message in chat message container
st.chat_message("user").markdown(prompt)
# Add user message to chat history
st.session_state.messages.append({"role": "user", "content": prompt}
1
2
3
4
5
with st.chat_message("assistant"):
message_placeholder = st.empty()
full_response = ""
result = model.predict(input=prompt)
1
2
3
4
5
6
7
8
# Simulate stream of response with milliseconds delay
for chunk in result.split():
full_response += chunk + " "
time.sleep(0.05)
# Add a blinking cursor to simulate typing
message_placeholder.markdown(full_response + "▌")
message_placeholder.markdown(full_response)
streamlit run chat_bedrock_st.py
.- Experiment: Take the example codes, tweak them, break them, and make them your own.
- Explore: Dive deeper into the features and capabilities of Amazon Bedrock, LangChain, and more.
- Engage: Share your projects, and learn from others. Reach out to @awsdevelopers on X, Instagram, and LinkedIn
Any opinions in this post are those of the individual author and may not reflect the opinions of AWS.