LLm_file / app.py
Enoch1359's picture
Update app.py
b911bd0 verified
import os
import streamlit as st
from dotenv import load_dotenv
from langchain_groq import ChatGroq
from langchain.chains import ConversationChain
from langchain.memory import ConversationBufferMemory
from langchain.schema import SystemMessage
# Load env
load_dotenv("apiroute.env")
# βœ… Setup LLM (Groq)
llm = ChatGroq(
model="meta-llama/llama-4-scout-17b-16e-instruct",
temperature=0.7,
groq_api_key=os.getenv("GROQ_API_KEY")
)
# βœ… Memory setup
if "memory" not in st.session_state:
st.session_state.memory = ConversationBufferMemory(return_messages=True)
# βœ… System prompt
system_prompt = (
"You are Gremmy, a helpful, friendly AI chatbot created by Frederick An AI Engineer with Mechanical Background.\n"
"- Introduce yourself **only once**, unless asked.\n"
"- Be concise, polite, and informative.\n"
"- Avoid repeating your background repeatedly.\n"
"- For more about your creator: https://www.linkedin.com/in/j-frederick-paul-35801a179/"
)
# βœ… Inject system message only once
if "system_added" not in st.session_state:
st.session_state.memory.chat_memory.add_message(
SystemMessage(content=system_prompt)
)
st.session_state.system_added = True
# βœ… Setup conversation chain
chat = ConversationChain(
llm=llm,
memory=st.session_state.memory,
verbose=False
)
# βœ… Page config
st.set_page_config(page_title="Chatbot", layout="centered")
st.title("Chat with Gremmy")
# βœ… Track and display history
if "history" not in st.session_state:
st.session_state.history = []
for sender, msg in st.session_state.history:
st.markdown(f"**{sender}:** {msg}")
# βœ… Input UI
with st.form(key="chat_form", clear_on_submit=True):
user_input = st.text_input("Talk to me", key="user_message")
submitted = st.form_submit_button("Send")
# βœ… Handle submission
if submitted and user_input:
st.session_state.history.append(("You", user_input))
response = chat.run(user_input)
st.session_state.history.append(("Gremmy", response.strip()))
st.rerun()