File size: 5,878 Bytes
846e270 9cf8e68 fd9c480 846e270 9cf8e68 df8d05e 9cf8e68 994b773 d2f5f9c 994b773 d2f5f9c 9984f20 d2f5f9c 9984f20 d2f5f9c 9984f20 c3711a7 8a9091b 1405e50 d2f5f9c fd9c480 9cf8e68 846e270 9cf8e68 fd9c480 9cf8e68 abe3e7a 9cf8e68 c2de6b7 abe3e7a c2de6b7 9cf8e68 0b37f7d 3293bbd fd9c480 e82df87 fd9c480 950ecdd 277522e fd9c480 68a28db fd9c480 9cf8e68 68a28db 9cf8e68 fd9c480 9cf8e68 5f6daba 9f89f20 5f6daba 9cf8e68 68a28db 9cf8e68 5f6daba 7954eab 5f6daba fd9c480 9cf8e68 5f6daba 0b37f7d 9cf8e68 fd9c480 68a28db 9cf8e68 fd9c480 9cf8e68 4bfb0ed |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
import os
from dotenv import find_dotenv, load_dotenv
import streamlit as st
from typing import Generator
from groq import Groq
# Cargar variables de entorno
_ = load_dotenv(find_dotenv())
# Configurar la página de Streamlit
st.set_page_config(page_icon="📃", layout="wide", page_title="Groq & LLaMA3.1 Chat Bot...")
# Menú superior con fondo transparente
st.markdown(
"""
<style>
.menu-container {
padding: 20px;
background-color: transparent; /* Fondo transparente */
border-bottom: 1px solid #e1e1e1;
}
.menu-title {
font-size: 24px;
font-weight: bold;
margin-bottom: 10px;
}
.menu-description {
line-height: 1.5;
}
.menu-description a {
color: #1f77b4;
text-decoration: none;
}
.menu-description a:hover {
text-decoration: underline;
}
</style>
<div class="menu-container">
<p class="menu-title">Bot con I.A. para crear BENEFICIOS de productos.</p>
<p class="menu-description">
Estos BENEFICIOS van en la descripcion LARGA de producto (En la parte de ARRIBA).<br><br>
Si desea usar otro BOT de I.A. escoja:<br>
<a href='https://sentrycom-mc-bot.hf.space'>Marketing de Contenidos |</a>
<a href='https://sentrycom-tit-bot.hf.space'> Creacion de TITULOS |</a>
<a href='https://sentrycom-dp-bot.hf.space'> Descripcion de Productos |</a>
<a href='https://sentrycom-cp-bot.hf.space'> Caracteristicas de Productos |</a>
<a href='https://wa.me/51927929109'> Desarrollado por MAGNET IMPACT - Agencia de Marketing Digital</a>
</p>
</div>
""",
unsafe_allow_html=True
)
# Inicializar cliente Groq
client = Groq(
api_key=os.environ['GROQ_API_KEY'],
)
# Inicializar historial de chat y modelo seleccionado
if "messages" not in st.session_state:
st.session_state.messages = []
if "selected_model" not in st.session_state:
st.session_state.selected_model = "mixtral-8x7b-32768"
# Detalles del modelo
models = {
"mixtral-8x7b-32768": {
"name": "Mixtral-8x7b-Instruct-v0.1",
"tokens": 32768,
"developer": "Mistral",
},
}
# Configurar el modelo y tokens
model_option = "mixtral-8x7b-32768"
max_tokens_range = models[model_option]["tokens"]
# No mostrar la selección del modelo ni la barra de tokens
st.session_state.max_tokens = max_tokens_range
# Detectar cambio de modelo y limpiar historial de chat si el modelo ha cambiado
if st.session_state.selected_model != model_option:
st.session_state.messages = []
st.session_state.selected_model = model_option
# Añadir un botón para "Limpiar Chat"
if st.button("Limpiar Chat"):
st.session_state.messages = []
# Cargar la imagen del avatar del asistente
assistant_avatar = "botm.png"
# Mostrar mensajes de chat del historial en la aplicación
for message in st.session_state.messages:
avatar = assistant_avatar if message["role"] == "assistant" else "🧑💻"
with st.chat_message(message["role"], avatar=avatar):
st.markdown(message["content"])
def generate_chat_responses(chat_completion) -> Generator[str, None, None]:
"""Generar contenido de respuesta del chat a partir de la respuesta de la API de Groq."""
for chunk in chat_completion:
if chunk.choices[0].delta.content:
yield chunk.choices[0].delta.content
# Instrucción privada que se aplicará a cada mensaje
private_instruction = (
"# Extract the benefits of the product, not the features. # You should be as brief as possible. # Omit the price, if any. # Do not mention the name of the product. # Use 3 paragraphs. # Try to synthesize or summarize. # Focus only on the benefits. # Highlight how this product helps the customer. # Always respond in Spanish. # The text you create will be used in an e-commerce product sales page through the Internet, so it must be persuasive, attractive, and above all very short and summarized. # Remember to keep the text short, summarized, synthesized in three paragraphs. # Surprise me with your best ideas! # Always answers in AMERICAN SPANISH. Stop after finish the first content genreated."
)
# Manejar la entrada del chat del usuario
if prompt := st.chat_input("Escribe tu mensaje aquí..."):
st.session_state.messages.append({"role": "user", "content": prompt})
with st.chat_message("user", avatar="🧑💻"):
st.markdown(prompt)
# Preparar los mensajes para la API, incluyendo la instrucción privada
messages_for_api = [
{"role": "system", "content": private_instruction},
] + [
{"role": m["role"], "content": m["content"]}
for m in st.session_state.messages
]
# Obtener respuesta de la API de Groq
try:
chat_completion = client.chat.completions.create(
model=model_option,
messages=messages_for_api,
max_tokens=max_tokens_range,
stream=True,
)
# Usar la función generadora con st.write_stream
with st.chat_message("assistant", avatar=assistant_avatar):
chat_responses_generator = generate_chat_responses(chat_completion)
full_response = st.write_stream(chat_responses_generator)
# Añadir la respuesta completa al historial de mensajes
if isinstance(full_response, str):
st.session_state.messages.append(
{"role": "assistant", "content": full_response}
)
else:
combined_response = "\n".join(str(item) for item in full_response)
st.session_state.messages.append(
{"role": "assistant", "content": combined_response}
)
except Exception as e:
st.error(e, icon="❌")
|