Chat_bot / main.py
Ezhil
TEST-2
dbbb4e2
import streamlit as st
from groq import Groq
# Set up the Groq client
client = Groq(api_key="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")
# Streamlit app UI
st.set_page_config(page_title="πŸ’° Finance & Banking Chatbot", layout="wide")
# System prompt to enforce finance-related responses
SYSTEM_PROMPT = (
"You are an expert financial assistant. Your role is to answer ONLY finance-related topics, including banking, investments, "
"loans, credit cards, budgeting, and economic trends. "
"If a user asks a question unrelated to finance, you MUST respond with: "
"'I'm here to assist with financial topics only. Please ask me something related to banking, investments, or finance. πŸ’°'"
)
# Initialize session state for chat history
if "messages" not in st.session_state:
st.session_state.messages = [
{"role": "assistant", "content": "Hello! I can help you with financial questions. How can I assist? πŸ’³"}
]
# Main chat interface
st.title("πŸ’³ Finance & Banking Chatbot 🀡")
# Display previous messages in the chat area
for msg in st.session_state.messages:
with st.chat_message(msg["role"], avatar=("πŸ‘¦πŸ»" if msg["role"] == "user" else "🀡")):
st.markdown(msg["content"])
# User input field
user_input = st.chat_input("Ask me about finance, banking, investments, etc. πŸ“ˆ")
if user_input:
# Add user message to history
st.session_state.messages.append({"role": "user", "content": user_input})
with st.chat_message("user", avatar="πŸ‘¦πŸ»"):
st.markdown(user_input)
# Get response from Groq API
response = client.chat.completions.create(
messages=[{"role": "system", "content": SYSTEM_PROMPT}] + st.session_state.messages,
model="llama-3.3-70b-versatile",
max_tokens=200,
)
bot_reply = response.choices[0].message.content
# Add bot response to history
st.session_state.messages.append({"role": "assistant", "content": bot_reply})
with st.chat_message("assistant", avatar="🀡"):
st.markdown(bot_reply)