Spaces:
Sleeping
Sleeping
cd@bziiit.com commited on
Commit ·
d1ca5e8
1
Parent(s): 8dda261
refactor: Enhance prompt handling and session state management in chatbot
Browse files- pages/chatbot.py +15 -6
pages/chatbot.py
CHANGED
|
@@ -41,13 +41,22 @@ def show_prompts(prompts: list[str]):
|
|
| 41 |
expander = st.expander("Prompts pré-définis")
|
| 42 |
|
| 43 |
for i, p in enumerate(prompts):
|
| 44 |
-
|
| 45 |
-
launchQuery(p)
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
-
|
| 51 |
if user_query is not None and user_query != "":
|
| 52 |
st.session_state[chapter_session_key]["messages"].append(HumanMessage(content=user_query))
|
| 53 |
|
|
@@ -99,7 +108,7 @@ def page():
|
|
| 99 |
st.markdown("<style>iframe{height:50px;}</style>", unsafe_allow_html=True)
|
| 100 |
|
| 101 |
# Collpase for default prompts
|
| 102 |
-
|
| 103 |
|
| 104 |
# Models selector
|
| 105 |
selector.ModelSelector()
|
|
|
|
| 41 |
expander = st.expander("Prompts pré-définis")
|
| 42 |
|
| 43 |
for i, p in enumerate(prompts):
|
| 44 |
+
button_key = f"button_{chapter_session_key}_{i}"
|
|
|
|
| 45 |
|
| 46 |
+
# Réinitialisation si la clé existe déjà (quand on change de page)
|
| 47 |
+
if button_key in st.session_state:
|
| 48 |
+
del st.session_state[button_key]
|
| 49 |
+
|
| 50 |
+
expander.button(p, key=button_key, on_click=promptSubmit, args=(p,))
|
| 51 |
+
|
| 52 |
+
def promptSubmit(key,):
|
| 53 |
+
inputSubmut(key)
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
def inputSubmut(query: str = None):
|
| 57 |
+
# Get query or find in session state
|
| 58 |
+
user_query = query if query is not None else st.session_state[f"input_{chapter_session_key}"]
|
| 59 |
|
|
|
|
| 60 |
if user_query is not None and user_query != "":
|
| 61 |
st.session_state[chapter_session_key]["messages"].append(HumanMessage(content=user_query))
|
| 62 |
|
|
|
|
| 108 |
st.markdown("<style>iframe{height:50px;}</style>", unsafe_allow_html=True)
|
| 109 |
|
| 110 |
# Collpase for default prompts
|
| 111 |
+
show_prompts(chapter['prompts'])
|
| 112 |
|
| 113 |
# Models selector
|
| 114 |
selector.ModelSelector()
|