Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Doesn't work after sometime #1

Open
Dhairya-Dudhatra opened this issue Feb 8, 2024 · 3 comments
Open

Doesn't work after sometime #1

Dhairya-Dudhatra opened this issue Feb 8, 2024 · 3 comments

Comments

@Dhairya-Dudhatra
Copy link

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.

@Dhairya-Dudhatra
Copy link
Author

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.

@jossai87
Copy link
Contributor

jossai87 commented Feb 16, 2024

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.

@Dhairya-Dudhatra
Copy link
Author

Dhairya-Dudhatra commented Feb 19, 2024

Modifications I have did in my application:

  1. Changed UI
  2. Removed the EndSession button and always keeping the endSession = False
  3. I am running this in AWS ECS which has bedrock Full access.
  4. 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 ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants