Spaces:
Runtime error
Runtime error
Update main.py
Browse files
main.py
CHANGED
|
@@ -66,12 +66,22 @@ if "current_page" not in st.session_state:
|
|
| 66 |
# result = rag_pipeline.query(prompt, run_evaluation=False)
|
| 67 |
# # append result to messages and st.rerun()
|
| 68 |
# --- MONGODB CONNECTION ---
|
| 69 |
-
@st.cache_resource
|
| 70 |
def init_connection():
|
| 71 |
if not MONGO_URL:
|
| 72 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
return None
|
| 74 |
-
return MongoClient(MONGO_URL, tlsCAFile=certifi.where())
|
| 75 |
|
| 76 |
client = init_connection()
|
| 77 |
if client:
|
|
@@ -80,10 +90,14 @@ if client:
|
|
| 80 |
|
| 81 |
# --- HELPER FUNCTIONS FOR MEMORY ---
|
| 82 |
def get_all_past_chats():
|
| 83 |
-
if not client:
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
def save_chat(chat_id, messages):
|
| 88 |
if not client: return
|
| 89 |
collection.update_one(
|
|
|
|
| 66 |
# result = rag_pipeline.query(prompt, run_evaluation=False)
|
| 67 |
# # append result to messages and st.rerun()
|
| 68 |
# --- MONGODB CONNECTION ---
|
| 69 |
+
@st.cache_resource
|
| 70 |
def init_connection():
|
| 71 |
if not MONGO_URL:
|
| 72 |
+
st.warning("MONGO_URL not set. Running without chat history.")
|
| 73 |
+
return None
|
| 74 |
+
try:
|
| 75 |
+
client = MongoClient(
|
| 76 |
+
MONGO_URL,
|
| 77 |
+
tlsCAFile=certifi.where(),
|
| 78 |
+
serverSelectionTimeoutMS=5000,
|
| 79 |
+
)
|
| 80 |
+
client.admin.command("ping")
|
| 81 |
+
return client
|
| 82 |
+
except Exception as e:
|
| 83 |
+
st.warning(f"MongoDB unavailable. Running without chat history. ({e})")
|
| 84 |
return None
|
|
|
|
| 85 |
|
| 86 |
client = init_connection()
|
| 87 |
if client:
|
|
|
|
| 90 |
|
| 91 |
# --- HELPER FUNCTIONS FOR MEMORY ---
|
| 92 |
def get_all_past_chats():
|
| 93 |
+
if not client:
|
| 94 |
+
return {}
|
| 95 |
+
try:
|
| 96 |
+
cursor = collection.find({}, {"chat_id": 1, "messages": 1})
|
| 97 |
+
return {doc["chat_id"]: doc["messages"] for doc in cursor}
|
| 98 |
+
except Exception as e:
|
| 99 |
+
st.warning(f"Could not load chat history. ({e})")
|
| 100 |
+
return {}
|
| 101 |
def save_chat(chat_id, messages):
|
| 102 |
if not client: return
|
| 103 |
collection.update_one(
|