Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,36 +9,100 @@ from functions import *
|
|
| 9 |
from config import *
|
| 10 |
from dotenv import load_dotenv
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
try:
|
| 15 |
-
#
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
| 20 |
except Exception as e:
|
| 21 |
-
st.error(f"Erreur lors de la
|
| 22 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
def main():
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
if not
|
| 29 |
-
st.error("Impossible de se connecter aux index Pinecone. Vérifiez votre configuration.")
|
| 30 |
return
|
| 31 |
|
| 32 |
-
# Initialiser l'historique de chat s'il n'existe pas
|
| 33 |
-
if "chat_history" not in st.session_state:
|
| 34 |
-
st.session_state.chat_history = []
|
| 35 |
-
|
| 36 |
st.title("Architecture A et B")
|
| 37 |
use_architecture_B = st.checkbox("Utiliser l'architecture B (avancée)")
|
| 38 |
|
| 39 |
-
# Initialiser l'indexation en fonction de l'architecture sélectionnée
|
| 40 |
-
initialize_pdf_indexing(use_architecture_B=use_architecture_B)
|
| 41 |
-
|
| 42 |
display_sidebar()
|
| 43 |
display_chat_history()
|
| 44 |
|
|
@@ -48,4 +112,7 @@ def main():
|
|
| 48 |
process_query_B(query)
|
| 49 |
else:
|
| 50 |
process_query_A(query)
|
| 51 |
-
st.rerun()
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
from config import *
|
| 10 |
from dotenv import load_dotenv
|
| 11 |
|
| 12 |
+
|
| 13 |
+
# Charger les variables d'environnement
|
| 14 |
+
load_dotenv()
|
| 15 |
+
|
| 16 |
+
# Initialiser l'état de chat globalement
|
| 17 |
+
if "chat_history" not in st.session_state:
|
| 18 |
+
st.session_state.chat_history = []
|
| 19 |
+
|
| 20 |
+
def check_indexes_ready():
|
| 21 |
+
"""Vérifie que les index Pinecone sont prêts."""
|
| 22 |
try:
|
| 23 |
+
# Vérifier que les index existent
|
| 24 |
+
existing_indexes = pc.list_indexes().names()
|
| 25 |
+
if sparse_index_name not in existing_indexes or dense_index_name not in existing_indexes:
|
| 26 |
+
st.error("Les index Pinecone ne sont pas prêts. Veuillez exécuter 'initialize_indexes.py'.")
|
| 27 |
+
return False
|
| 28 |
+
return True
|
| 29 |
except Exception as e:
|
| 30 |
+
st.error(f"Erreur lors de la vérification des index Pinecone : {e}")
|
| 31 |
+
return False
|
| 32 |
+
|
| 33 |
+
def process_query_A(query):
|
| 34 |
+
"""Traite la requête de l'utilisateur avec l'architecture A."""
|
| 35 |
+
st.session_state.chat_history.append({"role": "user", "content": query})
|
| 36 |
+
with st.spinner("Recherche en cours..."):
|
| 37 |
+
initial_state = {"query": query, "messages": [], "relevant_docs": [], "response": ""}
|
| 38 |
+
result = agent_A.invoke(initial_state)
|
| 39 |
+
response = result["response"]
|
| 40 |
+
st.session_state.chat_history.append({"role": "assistant", "content": response})
|
| 41 |
+
client.create_run(
|
| 42 |
+
name="RAG_Query",
|
| 43 |
+
run_type="chain",
|
| 44 |
+
project_name=langsmith_project,
|
| 45 |
+
inputs={"query": query},
|
| 46 |
+
outputs={"response": response},
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
def process_query_B(query):
|
| 50 |
+
"""Traite la requête de l'utilisateur avec l'architecture B."""
|
| 51 |
+
st.session_state.chat_history.append({"role": "user", "content": query})
|
| 52 |
+
with st.spinner("Recherche en cours..."):
|
| 53 |
+
# Récupérer les documents pertinents en utilisant la recherche hybride
|
| 54 |
+
relevant_docs = retrieve_documents_B(query)
|
| 55 |
+
|
| 56 |
+
# Préparer l'état initial pour l'agent
|
| 57 |
+
initial_state = {
|
| 58 |
+
"query": query,
|
| 59 |
+
"messages": [],
|
| 60 |
+
"relevant_docs": relevant_docs,
|
| 61 |
+
"response": ""
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
# Exécuter l'agent
|
| 65 |
+
result = agent_B.invoke(initial_state)
|
| 66 |
+
response = result["response"]
|
| 67 |
|
| 68 |
+
# Ajouter la réponse à l'historique de chat
|
| 69 |
+
st.session_state.chat_history.append({"role": "assistant", "content": response})
|
| 70 |
+
|
| 71 |
+
# Enregistrer la requête et la réponse dans LangSmith
|
| 72 |
+
client.create_run(
|
| 73 |
+
name="RAG_Query",
|
| 74 |
+
run_type="chain",
|
| 75 |
+
project_name=langsmith_project,
|
| 76 |
+
inputs={"query": query, "relevant_docs": relevant_docs},
|
| 77 |
+
outputs={"response": response},
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
def display_sidebar():
|
| 81 |
+
"""Affiche la barre latérale."""
|
| 82 |
+
with st.sidebar:
|
| 83 |
+
st.title("📄 La confession muette")
|
| 84 |
+
st.write("Posez vos questions sur le document.")
|
| 85 |
+
st.image(agent_B.get_graph().draw_mermaid_png(), caption="Workflow Graph (Architecture B)")
|
| 86 |
+
|
| 87 |
+
def display_chat_history():
|
| 88 |
+
"""Affiche l'historique de chat."""
|
| 89 |
+
for message in st.session_state.chat_history:
|
| 90 |
+
if message["role"] == "user":
|
| 91 |
+
with st.chat_message("user"):
|
| 92 |
+
st.markdown(message["content"])
|
| 93 |
+
elif message["role"] == "assistant":
|
| 94 |
+
with st.chat_message("assistant"):
|
| 95 |
+
st.markdown(message["content"])
|
| 96 |
+
|
| 97 |
def main():
|
| 98 |
+
"""Point d'entrée de l'application."""
|
| 99 |
+
# Vérifier que les index Pinecone sont prêts
|
| 100 |
+
if not check_indexes_ready():
|
|
|
|
| 101 |
return
|
| 102 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
st.title("Architecture A et B")
|
| 104 |
use_architecture_B = st.checkbox("Utiliser l'architecture B (avancée)")
|
| 105 |
|
|
|
|
|
|
|
|
|
|
| 106 |
display_sidebar()
|
| 107 |
display_chat_history()
|
| 108 |
|
|
|
|
| 112 |
process_query_B(query)
|
| 113 |
else:
|
| 114 |
process_query_A(query)
|
| 115 |
+
st.rerun()
|
| 116 |
+
|
| 117 |
+
if __name__ == "__main__":
|
| 118 |
+
main()
|