| 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_dotenv("apiroute.env") |
|
|
| |
| llm = ChatGroq( |
| model="meta-llama/llama-4-scout-17b-16e-instruct", |
| temperature=0.7, |
| groq_api_key=os.getenv("GROQ_API_KEY") |
| ) |
|
|
| |
| if "memory" not in st.session_state: |
| st.session_state.memory = ConversationBufferMemory(return_messages=True) |
|
|
| |
| 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/" |
| ) |
|
|
| |
| 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 |
|
|
| |
| chat = ConversationChain( |
| llm=llm, |
| memory=st.session_state.memory, |
| verbose=False |
| ) |
|
|
| |
| st.set_page_config(page_title="Chatbot", layout="centered") |
| st.title("Chat with Gremmy") |
|
|
| |
| if "history" not in st.session_state: |
| st.session_state.history = [] |
|
|
| for sender, msg in st.session_state.history: |
| st.markdown(f"**{sender}:** {msg}") |
|
|
| |
| 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") |
|
|
| |
| 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() |
|
|