Upload chatbox_v1.py
Browse files- src/chatbox_v1.py +60 -0
src/chatbox_v1.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from Rag_milvus import query_qdrant, obtener_colecciones, query_qdrant_sinumbral
|
| 3 |
+
from Llm_local import get_response_from_mistral, generarPages
|
| 4 |
+
from sentence_transformers import SentenceTransformer
|
| 5 |
+
|
| 6 |
+
col1, col2 = st.columns([1, 4])
|
| 7 |
+
with col1:
|
| 8 |
+
st.image("Procuradurialogo.jpg", width=600)
|
| 9 |
+
|
| 10 |
+
with col2:
|
| 11 |
+
st.markdown("""
|
| 12 |
+
<div style='display: flex; align-items: center; height: 100%;'>
|
| 13 |
+
<h1 style='margin: 0; text-align: center;'>ProcurIA</h1>
|
| 14 |
+
</div>
|
| 15 |
+
""", unsafe_allow_html=True)
|
| 16 |
+
|
| 17 |
+
st.sidebar.title("Men煤 de Funciones")
|
| 18 |
+
generarPages()
|
| 19 |
+
#Inicializamos nuestro historial de chat
|
| 20 |
+
if "messages" not in st.session_state:
|
| 21 |
+
st.session_state.messages = [{"role": "assistant", "content": "Hola!, en que puedo ayudarte?"}]
|
| 22 |
+
|
| 23 |
+
#Definimos modelo
|
| 24 |
+
model = SentenceTransformer("all-MiniLM-L6-v2")
|
| 25 |
+
|
| 26 |
+
#Elegimos una coleccion
|
| 27 |
+
colecciones = obtener_colecciones()
|
| 28 |
+
coleccion_seleccionada = st.sidebar.selectbox("Selecciona una colecci贸n", colecciones)
|
| 29 |
+
|
| 30 |
+
# Mostrar el historial de mensajes
|
| 31 |
+
for message in st.session_state.messages:
|
| 32 |
+
with st.chat_message(message["role"]):
|
| 33 |
+
st.markdown(message["content"])
|
| 34 |
+
|
| 35 |
+
# Entrada del usuario
|
| 36 |
+
if prompt := st.chat_input("Escribe tus dudas"):
|
| 37 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 38 |
+
|
| 39 |
+
with st.chat_message("user"):
|
| 40 |
+
st.markdown(prompt)
|
| 41 |
+
|
| 42 |
+
with st.chat_message("assistant"):
|
| 43 |
+
if coleccion_seleccionada == "Todas las colecciones":
|
| 44 |
+
colecciones_disponibles = obtener_colecciones()
|
| 45 |
+
results = []
|
| 46 |
+
umbral=1
|
| 47 |
+
for coleccion in colecciones_disponibles[1:]:
|
| 48 |
+
coleccion_results = query_qdrant_sinumbral(prompt,model,coleccion)
|
| 49 |
+
results.extend(coleccion_results)
|
| 50 |
+
else:
|
| 51 |
+
umbral=0.56
|
| 52 |
+
results = query_qdrant(prompt, model, coleccion_seleccionada,5,umbral)
|
| 53 |
+
|
| 54 |
+
if not results:
|
| 55 |
+
response = "Disculpa, no tengo informaci贸n para responder esa pregunta."
|
| 56 |
+
else:
|
| 57 |
+
response = st.write_stream(get_response_from_mistral(prompt, results))
|
| 58 |
+
|
| 59 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|
| 60 |
+
st.write(results)
|