|
import os |
|
from dotenv import find_dotenv, load_dotenv |
|
import streamlit as st |
|
from typing import Generator |
|
from groq import Groq |
|
|
|
|
|
_ = load_dotenv(find_dotenv()) |
|
|
|
|
|
st.set_page_config(page_icon="📃", layout="wide", page_title="Groq & LLaMA3.1 Chat Bot...") |
|
|
|
|
|
st.markdown( |
|
""" |
|
<style> |
|
.menu-container { |
|
padding: 20px; |
|
background-color: #f4f4f4; |
|
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 MARKETING DE CONTENIDOS de productos.</p> |
|
<p class="menu-description"> |
|
Herramienta de apoyo para crear MARKETING DE CONTENIDOS para medios Electrónicos.<br><br> |
|
Si desea usar otro BOT de I.A. escoja:<br> |
|
<a href='https://sentrycom-bot-mc.hf.space'>Marketing de Contenidos |</a> |
|
<a href='https://sentrycom-bot-tit.hf.space'> Creacion de TITULOS |</a> |
|
<a href='https://sentrycom-bot-dp.hf.space'> Descripcion de Productos |</a> |
|
<a href='https://sentrycom-bot-cp.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 |
|
) |
|
|
|
def icon(emoji: str): |
|
"""Muestra un emoji como ícono de página estilo Notion.""" |
|
st.write( |
|
f'<span style="font-size: 78px; line-height: 1">{emoji}</span>', |
|
unsafe_allow_html=True, |
|
) |
|
|
|
|
|
st.subheader("Groq Chat with LLaMA3.1 App", divider="rainbow", anchor=False) |
|
|
|
|
|
client = Groq( |
|
api_key=os.environ['GROQ_API_KEY'], |
|
) |
|
|
|
|
|
if "messages" not in st.session_state: |
|
st.session_state.messages = [] |
|
|
|
if "selected_model" not in st.session_state: |
|
st.session_state.selected_model = None |
|
|
|
|
|
models = { |
|
"llama-3.1-70b-versatile": {"name": "LLaMA3.1-70b", "tokens": 4096, "developer": "Meta"}, |
|
"llama-3.1-8b-instant": {"name": "LLaMA3.1-8b", "tokens": 4096, "developer": "Meta"}, |
|
"llama3-70b-8192": {"name": "Meta Llama 3 70B", "tokens": 4096, "developer": "Meta"}, |
|
"llama3-8b-8192": {"name": "Meta Llama 3 8B", "tokens": 4096, "developer": "Meta"}, |
|
"llama3-groq-70b-8192-tool-use-preview": {"name": "Llama 3 Groq 70B Tool Use (Preview)", "tokens": 4096, "developer": "Groq"}, |
|
"gemma-7b-it": {"name": "Gemma-7b-it", "tokens": 4096, "developer": "Google"}, |
|
"mixtral-8x7b-32768": { |
|
"name": "Mixtral-8x7b-Instruct-v0.1", |
|
"tokens": 32768, |
|
"developer": "Mistral", |
|
}, |
|
} |
|
|
|
|
|
col1, col2 = st.columns([1, 3]) |
|
|
|
with col1: |
|
model_option = st.selectbox( |
|
"Choose a model:", |
|
options=list(models.keys()), |
|
format_func=lambda x: models[x]["name"], |
|
index=0, |
|
) |
|
max_tokens_range = models[model_option]["tokens"] |
|
max_tokens = st.slider( |
|
"Max Tokens:", |
|
min_value=512, |
|
max_value=max_tokens_range, |
|
value=min(32768, max_tokens_range), |
|
step=512, |
|
help=f"Adjust the maximum number of tokens (words) for the model's response. Max for selected model: {max_tokens_range}", |
|
) |
|
|
|
|
|
if st.session_state.selected_model != model_option: |
|
st.session_state.messages = [] |
|
st.session_state.selected_model = model_option |
|
|
|
|
|
if st.button("Clear Chat"): |
|
st.session_state.messages = [] |
|
|
|
|
|
for message in st.session_state.messages: |
|
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 |
|
|
|
|
|
private_instruction = ( |
|
"# I want you to act as a content marketing consultant. # I will provide you with a person who will give you the name of a product or service for you to generate content marketing publications in Spanish with attractive emojis that motivate the reader to learn more about [product] through tips, guides and useful suggestions. # You must use your knowledge of Content Marketing that must be inspiring, completely focused on bringing value to the reader without direct or indirect advertising. # Generate long content, at least 5 short relevant paragraphs. Check that the previous content is not repeated. # Generate content with paragraphs between 10 and 20 words. Check that previous content is not repeated. # Use attractive emojis and titles such as: \"The 5 best tricks for [action]\". \"The ultimate beginner\'s guide to [topic].\" \"Want [result]? I show you how to achieve it in 5 steps.\" # Use practical tips such as: \"With these 5 tips you\'ll get [result].\" \"Five innovative ways to use [product] in your daily life.\" # Educational content: \"The most common mistakes and how to avoid them.\" \"Myths and truths about [topic].\" \"The latest trends you need to know about.\" # Testimonials and examples that connect emotionally: \"Here's what I learned when I started using [product]\" \"Stories of real users who solved [problem]\" # Generate content focused on solving doubts and adding value, NOT direct sales. Surprise me with your best ideas! # Always answers in AMERICAN SPANISH. Stop after finish the first content marketing genreated." |
|
) |
|
|
|
|
|
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) |
|
|
|
|
|
messages_for_api = [ |
|
{"role": "system", "content": private_instruction}, |
|
] + [ |
|
{"role": m["role"], "content": m["content"]} |
|
for m in st.session_state.messages |
|
] |
|
|
|
|
|
try: |
|
chat_completion = client.chat.completions.create( |
|
model=model_option, |
|
messages=messages_for_api, |
|
max_tokens=max_tokens, |
|
stream=True, |
|
) |
|
|
|
|
|
with st.chat_message("assistant", avatar="🔋"): |
|
chat_responses_generator = generate_chat_responses(chat_completion) |
|
full_response = st.write_stream(chat_responses_generator) |
|
|
|
|
|
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="❌") |
|
|