cd@bziiit.com
commited on
Commit
·
72cdb72
1
Parent(s):
0c111b7
Refactor chatbot.py to use getYamlConfig for loading prompts and streamline input processing
Browse files- pages/chatbot.py +24 -27
pages/chatbot.py
CHANGED
|
@@ -1,8 +1,8 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import yaml
|
| 3 |
-
import os
|
| 4 |
from streamlit_chat import message
|
| 5 |
from model import selector
|
|
|
|
|
|
|
| 6 |
|
| 7 |
def display_messages():
|
| 8 |
for i, (msg, is_user) in enumerate(st.session_state["messages"]):
|
|
@@ -10,13 +10,6 @@ def display_messages():
|
|
| 10 |
st.session_state["thinking_spinner"] = st.empty()
|
| 11 |
|
| 12 |
|
| 13 |
-
# Charger les données YAML
|
| 14 |
-
def load_yaml(file_path):
|
| 15 |
-
with open(file_path, 'r', encoding='utf-8') as file:
|
| 16 |
-
data = yaml.safe_load(file)
|
| 17 |
-
return data
|
| 18 |
-
|
| 19 |
-
|
| 20 |
def process_input():
|
| 21 |
if "user_input" in st.session_state and st.session_state["user_input"] and len(st.session_state["user_input"].strip()) > 0:
|
| 22 |
user_text = st.session_state["user_input"].strip()
|
|
@@ -24,24 +17,26 @@ def process_input():
|
|
| 24 |
prompt_sys = st.session_state.prompt_system if 'prompt_system' in st.session_state and st.session_state.prompt_system != '' else ""
|
| 25 |
|
| 26 |
with st.session_state["thinking_spinner"], st.spinner(f"Je réfléchis"):
|
| 27 |
-
agent_text = st.session_state["assistant"].ask(user_text, prompt_sys, st.session_state["messages"] if "messages" in st.session_state else [], variables=st.session_state["data_dict"])
|
| 28 |
-
|
| 29 |
st.session_state["messages"].append((user_text, True))
|
| 30 |
st.session_state["messages"].append((agent_text, False))
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
@st.dialog("Choisissez votre prompt")
|
| 33 |
def show_prompts():
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
for categroy in yaml_data
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
for item in yaml_data[
|
| 42 |
-
if
|
| 43 |
st.session_state["user_input"] = item
|
| 44 |
-
|
|
|
|
| 45 |
|
| 46 |
def page():
|
| 47 |
st.subheader("Posez vos questions")
|
|
@@ -51,16 +46,18 @@ def page():
|
|
| 51 |
|
| 52 |
if "assistant" not in st.session_state:
|
| 53 |
st.text("Assistant non initialisé")
|
| 54 |
-
|
| 55 |
-
# Bouton pour ouvrir la modale
|
| 56 |
-
if st.button("Prompts pré-définis"):
|
| 57 |
-
show_prompts()
|
| 58 |
|
|
|
|
|
|
|
| 59 |
|
|
|
|
| 60 |
selector.ModelSelector()
|
| 61 |
-
|
|
|
|
| 62 |
display_messages()
|
| 63 |
|
|
|
|
| 64 |
st.text_input("Message", key="user_input", on_change=process_input)
|
| 65 |
|
|
|
|
| 66 |
page()
|
|
|
|
| 1 |
import streamlit as st
|
|
|
|
|
|
|
| 2 |
from streamlit_chat import message
|
| 3 |
from model import selector
|
| 4 |
+
from util import getYamlConfig
|
| 5 |
+
|
| 6 |
|
| 7 |
def display_messages():
|
| 8 |
for i, (msg, is_user) in enumerate(st.session_state["messages"]):
|
|
|
|
| 10 |
st.session_state["thinking_spinner"] = st.empty()
|
| 11 |
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
def process_input():
|
| 14 |
if "user_input" in st.session_state and st.session_state["user_input"] and len(st.session_state["user_input"].strip()) > 0:
|
| 15 |
user_text = st.session_state["user_input"].strip()
|
|
|
|
| 17 |
prompt_sys = st.session_state.prompt_system if 'prompt_system' in st.session_state and st.session_state.prompt_system != '' else ""
|
| 18 |
|
| 19 |
with st.session_state["thinking_spinner"], st.spinner(f"Je réfléchis"):
|
| 20 |
+
agent_text = st.session_state["assistant"].ask(user_text, prompt_system=prompt_sys, messages=st.session_state["messages"] if "messages" in st.session_state else [], variables=st.session_state["data_dict"])
|
| 21 |
+
|
| 22 |
st.session_state["messages"].append((user_text, True))
|
| 23 |
st.session_state["messages"].append((agent_text, False))
|
| 24 |
+
st.session_state["user_input"] = ""
|
| 25 |
+
|
| 26 |
|
|
|
|
| 27 |
def show_prompts():
|
| 28 |
+
yaml_data = getYamlConfig()["prompts"]
|
| 29 |
+
|
| 30 |
+
expander = st.expander("Prompts pré-définis")
|
| 31 |
+
|
| 32 |
+
for categroy in yaml_data:
|
| 33 |
+
expander.write(categroy.capitalize())
|
| 34 |
+
|
| 35 |
+
for item in yaml_data[categroy]:
|
| 36 |
+
if expander.button(item, key=f"button_{item}"):
|
| 37 |
st.session_state["user_input"] = item
|
| 38 |
+
process_input()
|
| 39 |
+
|
| 40 |
|
| 41 |
def page():
|
| 42 |
st.subheader("Posez vos questions")
|
|
|
|
| 46 |
|
| 47 |
if "assistant" not in st.session_state:
|
| 48 |
st.text("Assistant non initialisé")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
+
# Collpase for default prompts
|
| 51 |
+
show_prompts()
|
| 52 |
|
| 53 |
+
# Models selector
|
| 54 |
selector.ModelSelector()
|
| 55 |
+
|
| 56 |
+
# Displaying messages
|
| 57 |
display_messages()
|
| 58 |
|
| 59 |
+
# Input user query
|
| 60 |
st.text_input("Message", key="user_input", on_change=process_input)
|
| 61 |
|
| 62 |
+
|
| 63 |
page()
|