File size: 2,067 Bytes
5ed2318 dbbb4e2 5ed2318 0da0ed7 dbbb4e2 5ed2318 0da0ed7 dbbb4e2 |
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 |
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)
|