Spaces:
Running
Running
[email protected]
commited on
Commit
·
87fc54f
1
Parent(s):
271c7bb
Fix stream response in multiple chatbots
Browse files- pages/chatbot.py +41 -22
pages/chatbot.py
CHANGED
@@ -34,21 +34,6 @@ def display_messages():
|
|
34 |
with st.chat_message("System"):
|
35 |
st.write(message.content)
|
36 |
|
37 |
-
def launchQuery(query: str = None):
|
38 |
-
|
39 |
-
# Initialize the assistant's response
|
40 |
-
full_response = st.write_stream(
|
41 |
-
st.session_state["assistant"].ask(
|
42 |
-
query,
|
43 |
-
prompt_system=st.session_state.prompt_system,
|
44 |
-
messages=st.session_state[chapter_session_key]["messages"] if "messages" in st.session_state[chapter_session_key] else [],
|
45 |
-
# variables=st.session_state["data_dict"],
|
46 |
-
# metadata=metadata
|
47 |
-
))
|
48 |
-
|
49 |
-
# Temporary placeholder AI message in chat history
|
50 |
-
st.session_state[chapter_session_key]["messages"].append(AIMessage(content=full_response, kwargs={"model": st.session_state["assistant"].getReadableModel()}))
|
51 |
-
st.rerun()
|
52 |
|
53 |
|
54 |
def show_prompts(prompts: list[str]):
|
@@ -59,6 +44,15 @@ def show_prompts(prompts: list[str]):
|
|
59 |
if expander.button(p, key=f"button_{chapter_num}_{i}_{uuid.uuid4()}"):
|
60 |
launchQuery(p)
|
61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
|
63 |
def page():
|
64 |
|
@@ -113,15 +107,40 @@ def page():
|
|
113 |
# Displaying messages
|
114 |
display_messages()
|
115 |
|
|
|
116 |
|
117 |
-
|
118 |
-
|
119 |
-
|
|
|
|
|
|
|
120 |
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
125 |
|
126 |
|
127 |
page()
|
|
|
34 |
with st.chat_message("System"):
|
35 |
st.write(message.content)
|
36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
|
39 |
def show_prompts(prompts: list[str]):
|
|
|
44 |
if expander.button(p, key=f"button_{chapter_num}_{i}_{uuid.uuid4()}"):
|
45 |
launchQuery(p)
|
46 |
|
47 |
+
def inputSubmut():
|
48 |
+
user_query = st.session_state[f"input_{chapter_session_key}"]
|
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 |
+
|
54 |
+
# Stream and display response
|
55 |
+
launchQuery(user_query)
|
56 |
|
57 |
def page():
|
58 |
|
|
|
107 |
# Displaying messages
|
108 |
display_messages()
|
109 |
|
110 |
+
chat_input_key = f"input_{chapter_session_key}"
|
111 |
|
112 |
+
# Réinitialisation si la clé existe déjà (quand on change de page)
|
113 |
+
if chat_input_key in st.session_state:
|
114 |
+
del st.session_state[chat_input_key]
|
115 |
+
|
116 |
+
|
117 |
+
st.chat_input("", key=chat_input_key, on_submit=inputSubmut)
|
118 |
|
119 |
+
|
120 |
+
global response_placeholder
|
121 |
+
response_placeholder = st.empty()
|
122 |
+
|
123 |
+
|
124 |
+
# Check if we need to rerun the app
|
125 |
+
if st.session_state.get('rerun', False):
|
126 |
+
st.session_state['rerun'] = False
|
127 |
+
st.rerun()
|
128 |
+
|
129 |
+
|
130 |
+
def launchQuery(query: str = None):
|
131 |
+
|
132 |
+
# Initialize the assistant's response
|
133 |
+
full_response = response_placeholder.write_stream(
|
134 |
+
st.session_state["assistant"].ask(
|
135 |
+
query,
|
136 |
+
prompt_system=st.session_state.prompt_system,
|
137 |
+
messages=st.session_state[chapter_session_key]["messages"] if "messages" in st.session_state[chapter_session_key] else [],
|
138 |
+
)
|
139 |
+
)
|
140 |
+
|
141 |
+
# Temporary placeholder AI message in chat history
|
142 |
+
st.session_state[chapter_session_key]["messages"].append(AIMessage(content=full_response, kwargs={"model": st.session_state["assistant"].getReadableModel()}))
|
143 |
+
st.session_state['rerun'] = True
|
144 |
|
145 |
|
146 |
page()
|