From 5e971ecedd041b58c801fff853790fa601ee4e26 Mon Sep 17 00:00:00 2001 From: Doruk Ozturk Date: Thu, 26 Sep 2024 22:32:28 -0400 Subject: [PATCH] Separate caching logic per page --- .../web-app/pages/foundation_model.py | 12 ++++++------ .../web-app/pages/rag_integration.py | 12 ++++++------ 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/cdk/examples/generative_ai_rag/web-app/pages/foundation_model.py b/cdk/examples/generative_ai_rag/web-app/pages/foundation_model.py index 990792aa..84fe2ad5 100644 --- a/cdk/examples/generative_ai_rag/web-app/pages/foundation_model.py +++ b/cdk/examples/generative_ai_rag/web-app/pages/foundation_model.py @@ -17,15 +17,15 @@ def get_parameter(name): st.title("💬 Chat") -if "messages" not in st.session_state: - st.session_state["messages"] = [{"role": "assistant", "content": "Ask me something about reinvent 2024?"}] +if "foundation_messages" not in st.session_state: + st.session_state["foundation_messages"] = [{"role": "assistant", "content": "Ask me something about reinvent 2024?"}] -for msg in st.session_state.messages: +for msg in st.session_state.foundation_messages: st.chat_message(msg["role"]).write(msg["content"]) if prompt := st.chat_input(): st.chat_message("user").write(prompt) - st.session_state.messages.append( + st.session_state.foundation_messages.append( { "role": "user", "content": prompt @@ -62,7 +62,7 @@ def get_parameter(name): except (ClientError, Exception) as e: msg = f"ERROR: Can't invoke '{model_id}'. Reason: {e}" - st.session_state.messages.append( + st.session_state.foundation_messages.append( { "role": "assistant", "content": msg @@ -75,7 +75,7 @@ def get_parameter(name): # Extract and print the response text. msg = model_response["content"][0]["text"] - st.session_state.messages.append( + st.session_state.foundation_messages.append( { "role": "assistant", "content": msg diff --git a/cdk/examples/generative_ai_rag/web-app/pages/rag_integration.py b/cdk/examples/generative_ai_rag/web-app/pages/rag_integration.py index 4b323d40..a507c802 100644 --- a/cdk/examples/generative_ai_rag/web-app/pages/rag_integration.py +++ b/cdk/examples/generative_ai_rag/web-app/pages/rag_integration.py @@ -16,15 +16,15 @@ def get_parameter(name): st.title("💬 Chat") -if "messages" not in st.session_state: - st.session_state["messages"] = [{"role": "assistant", "content": "Ask me something about reinvent 2024?"}] +if "rag_messages" not in st.session_state: + st.session_state["rag_messages"] = [{"role": "assistant", "content": "Ask me something about reinvent 2024?"}] -for msg in st.session_state.messages: +for msg in st.session_state.rag_messages: st.chat_message(msg["role"]).write(msg["content"]) if prompt := st.chat_input(): st.chat_message("user").write(prompt) - st.session_state.messages.append( + st.session_state.rag_messages.append( { "role": "user", "content": prompt @@ -49,7 +49,7 @@ def get_parameter(name): chunks = event.get('chunk').get('attribution').get('citations') for chunk in chunks: msg = chunk['generatedResponsePart']['textResponsePart']['text'] - st.session_state.messages.append( + st.session_state.rag_messages.append( { "role": "assistant", "content": msg @@ -58,7 +58,7 @@ def get_parameter(name): st.chat_message("assistant").write(msg) except: msg = "Please give me a better prompt" - st.session_state.messages.append( + st.session_state.rag_messages.append( { "role": "assistant", "content": msg