bedrock-agents-streamlit
bedrock-agents-streamlit copied to clipboard
Doesn't work after sometime
Hello, I have tested this, after launching this streamlit app in Aws ECR, I tested this after a day. It doesn't work and returned an error. I have checked this multiple times. If I force update the ecr service, it works fine for a few hours.
import InvokeAgent as agenthelper
import streamlit as st
import json
import pandas as pd
from PIL import Image, ImageOps, ImageDraw
import string
import random
# Streamlit page configuration
st.set_page_config(page_title="JFK Airport Agent", page_icon=":robot_face:", layout="wide")
def format_response(response_body):
try:
# Try to load the response as JSON
data = json.loads(response_body)
# If it's a list, convert it to a DataFrame for better visualization
if isinstance(data, list):
return pd.DataFrame(data)
else:
return response_body
except json.JSONDecodeError:
# If response is not JSON, return as is
return response_body
def generate_random_string(length):
letters = string.ascii_letters
return ''.join(random.choice(letters) for _ in range(length))
# Display a text box for input
if prompt := st.chat_input("Ask any question regarding JFK Airport."):
# def filter_trace_data(trace_data, query):
# if query:
# # Filter lines that contain the query
# return "\n".join([line for line in trace_data.split('\n') if query.lower() in line.lower()])
# return trace_data
# Session State Management
if "history" not in st.session_state:
st.session_state.history = []
#random ID generator
st.session_state.session_id = generate_random_string(10)
event = {
"sessionId": st.session_state.session_id,
"question": prompt
}
response = agenthelper.lambda_handler(event, None)
try:
# Parse the JSON string
if response and 'body' in response and response['body']:
response_data = json.loads(response['body'])
print("TRACE & RESPONSE DATA -> ", response_data)
else:
print("Invalid or empty response received")
except json.JSONDecodeError as e:
print("JSON decoding error:", e)
response_data = None
# Parse the JSON string
# response_data = json.loads(response['body'])
#print("TRACE & RESPONSE DATA -> ", json.loads(response['body']))
# Extract the response and trace data
try:
# Extract the response and trace data
all_data = format_response(response_data['response'])
the_response = response_data['trace_data']
except:
all_data = "..."
the_response = "Apologies, but an error occurred. Please rerun the application"
# Use trace_data and formatted_response as needed
#st.sidebar.text_area("Trace Data", value=all_data, height=300)
st.session_state.history.append({"question": prompt, "answer": the_response})
st.session_state['trace_data'] = the_response
for chat in reversed(st.session_state['history']):
with st.chat_message("user"):
st.markdown(chat["question"])
#st.text_area("Query:", value=chat["question"], height=10, key=str(chat)+"q", disabled=True)
with st.chat_message("assistant"):
if isinstance(chat["answer"], pd.DataFrame):
st.dataframe(chat["answer"])
else:
st.markdown(chat["answer"])
#st.text_area("Answer:", value=chat["answer"], height=10, key=str(chat)+"a")
# Function to parse and format response
# Handling user input and responses
#submit_button and
# if end_session_button:
# st.session_state['history'].append({"question": "Session Ended", "answer": "Thank you for using JFK Support Agent!"})
# event = {
# "sessionId": "MYSESSION",
# "question": "placeholder to end session",
# "endSession": True
# }
# agenthelper.lambda_handler(event, None)
# st.session_state['history'].clear()
# Display conversation history
# st.write("## Chat History")
# for chat in reversed(st.session_state['history']):
# with st.chat_message("user"):
# st.markdown(chat["question"])
# #st.text_area("Query:", value=chat["question"], height=10, key=str(chat)+"q", disabled=True)
# with st.chat_message("assistant"):
# if isinstance(chat["answer"], pd.DataFrame):
# st.dataframe(chat["answer"])
# else:
# st.markdown(chat["answer"])
# #st.text_area("Answer:", value=chat["answer"], height=10, key=str(chat)+"a")
This is my app.py. Please let me know if I am doing something differently and that's why it's happening.
Can you please share the error you are getting after you try and run the app via containers? Also, is a Lambda log generated when this error occurs? or does an API call to the Bedrock agent even happen? If so, please share those details as well. With you running containers, I want to figure out if the error is occurring with the running app that could not be running anymore at the time, if the error is generating at the Bedrock agent level where we can look at the tracing to see what is occurring, or if the error is from the Lambda logs for the action group.
Modifications I have did in my application:
- Changed UI
- Removed the EndSession button and always keeping the endSession = False
- I am running this in AWS ECS which has bedrock Full access.
- Randomly generating a sessionID for every prompt .
Things I noticed. After I enabled the logs in AWS ECS, I saw that I am getting 403 status code on the request. This error usually comes after 5-8 hours from starting the container in ECS. 1 - I kept this application running in background in EC2 in terminal and in EC2 container at separate times. In the container, I faced the same error after a few hours. While terminal one was running smoothly.
Both EC2 instance and ECS had bedrock full access.
Can it be regarding getting frozen credentials ?