Update app.py
Browse files
app.py
CHANGED
|
@@ -2,77 +2,65 @@ import streamlit as st
|
|
| 2 |
from dotenv import load_dotenv
|
| 3 |
import os
|
| 4 |
from langchain_openai import ChatOpenAI
|
| 5 |
-
from langchain.schema import SystemMessage
|
| 6 |
from langchain.memory import ConversationBufferMemory
|
| 7 |
from langchain.chains import ConversationChain
|
| 8 |
|
| 9 |
-
# β
Load
|
| 10 |
load_dotenv("apiroute.env")
|
| 11 |
api_key = os.getenv("OPENAI_API_KEY")
|
| 12 |
api_base = os.getenv("OPENAI_API_BASE")
|
| 13 |
|
| 14 |
-
# β
|
| 15 |
llm = ChatOpenAI(
|
| 16 |
model_name="google/gemma-3n-e2b-it:free",
|
| 17 |
-
temperature=0.7
|
| 18 |
)
|
| 19 |
|
| 20 |
-
# β
|
| 21 |
if "memory" not in st.session_state:
|
| 22 |
st.session_state.memory = ConversationBufferMemory(return_messages=True)
|
| 23 |
|
| 24 |
-
# β
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
"You are Gremmy, a helpful, friendly AI chatbot created by Frederick using Google's Gemma model. "
|
| 32 |
-
"You are smart, polite, and informative.\n"
|
| 33 |
-
"- Introduce yourself as Gremmy when needed.\n"
|
| 34 |
-
"- Speak in a concise and helpful tone.\n"
|
| 35 |
-
"- For more information about your creator, visit: https://www.linkedin.com/in/j-frederick-paul-35801a179/"
|
| 36 |
-
)
|
| 37 |
)
|
| 38 |
|
| 39 |
-
# β
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
chat = ConversationChain(
|
| 41 |
llm=llm,
|
| 42 |
memory=st.session_state.memory,
|
| 43 |
verbose=False
|
| 44 |
)
|
| 45 |
|
| 46 |
-
# β
|
| 47 |
st.set_page_config(page_title="Chatbot", layout="centered")
|
| 48 |
st.title("π¬ Chat with Gremmy")
|
| 49 |
|
| 50 |
-
# β
|
| 51 |
if "history" not in st.session_state:
|
| 52 |
st.session_state.history = []
|
| 53 |
|
| 54 |
-
# β
Display history
|
| 55 |
for sender, msg in st.session_state.history:
|
| 56 |
st.markdown(f"**{sender}:** {msg}")
|
| 57 |
|
| 58 |
-
# β
Input
|
| 59 |
with st.form(key="chat_form", clear_on_submit=True):
|
| 60 |
user_input = st.text_input("Talk to me", key="user_message")
|
| 61 |
submitted = st.form_submit_button("Send")
|
| 62 |
|
| 63 |
-
# β
Handle
|
| 64 |
if submitted and user_input:
|
| 65 |
-
# Inject system message only once
|
| 66 |
-
if not st.session_state.intro_done:
|
| 67 |
-
st.session_state.memory.chat_memory.add_message(system_message)
|
| 68 |
-
st.session_state.intro_done = True
|
| 69 |
-
|
| 70 |
-
# Add user message
|
| 71 |
st.session_state.history.append(("You", user_input))
|
| 72 |
-
|
| 73 |
-
# Get response from Gremmy
|
| 74 |
response = chat.run(user_input)
|
| 75 |
-
|
| 76 |
-
# Add Gremmy response
|
| 77 |
st.session_state.history.append(("Gremmy", response.strip()))
|
| 78 |
st.rerun()
|
|
|
|
| 2 |
from dotenv import load_dotenv
|
| 3 |
import os
|
| 4 |
from langchain_openai import ChatOpenAI
|
| 5 |
+
from langchain.schema import SystemMessage
|
| 6 |
from langchain.memory import ConversationBufferMemory
|
| 7 |
from langchain.chains import ConversationChain
|
| 8 |
|
| 9 |
+
# β
Load environment variables
|
| 10 |
load_dotenv("apiroute.env")
|
| 11 |
api_key = os.getenv("OPENAI_API_KEY")
|
| 12 |
api_base = os.getenv("OPENAI_API_BASE")
|
| 13 |
|
| 14 |
+
# β
Setup LLM
|
| 15 |
llm = ChatOpenAI(
|
| 16 |
model_name="google/gemma-3n-e2b-it:free",
|
| 17 |
+
temperature=0.7,
|
| 18 |
)
|
| 19 |
|
| 20 |
+
# β
Memory setup
|
| 21 |
if "memory" not in st.session_state:
|
| 22 |
st.session_state.memory = ConversationBufferMemory(return_messages=True)
|
| 23 |
|
| 24 |
+
# β
System prompt
|
| 25 |
+
system_prompt = (
|
| 26 |
+
"You are Gremmy, a helpful, friendly AI chatbot created by Frederick using Google's Gemma model.\n"
|
| 27 |
+
"- Introduce yourself **only once**, unless asked.\n"
|
| 28 |
+
"- Be concise, polite, and informative.\n"
|
| 29 |
+
"- Avoid repeating your background repeatedly.\n"
|
| 30 |
+
"- For more about your creator: https://www.linkedin.com/in/j-frederick-paul-35801a179/"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
)
|
| 32 |
|
| 33 |
+
# β
Inject system message only once
|
| 34 |
+
if "system_added" not in st.session_state:
|
| 35 |
+
st.session_state.memory.chat_memory.add_message(SystemMessage(content=system_prompt))
|
| 36 |
+
st.session_state.system_added = True
|
| 37 |
+
|
| 38 |
+
# β
Setup conversation chain
|
| 39 |
chat = ConversationChain(
|
| 40 |
llm=llm,
|
| 41 |
memory=st.session_state.memory,
|
| 42 |
verbose=False
|
| 43 |
)
|
| 44 |
|
| 45 |
+
# β
Page config and title
|
| 46 |
st.set_page_config(page_title="Chatbot", layout="centered")
|
| 47 |
st.title("π¬ Chat with Gremmy")
|
| 48 |
|
| 49 |
+
# β
Track and display history
|
| 50 |
if "history" not in st.session_state:
|
| 51 |
st.session_state.history = []
|
| 52 |
|
|
|
|
| 53 |
for sender, msg in st.session_state.history:
|
| 54 |
st.markdown(f"**{sender}:** {msg}")
|
| 55 |
|
| 56 |
+
# β
Input UI
|
| 57 |
with st.form(key="chat_form", clear_on_submit=True):
|
| 58 |
user_input = st.text_input("Talk to me", key="user_message")
|
| 59 |
submitted = st.form_submit_button("Send")
|
| 60 |
|
| 61 |
+
# β
Handle submission
|
| 62 |
if submitted and user_input:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
st.session_state.history.append(("You", user_input))
|
|
|
|
|
|
|
| 64 |
response = chat.run(user_input)
|
|
|
|
|
|
|
| 65 |
st.session_state.history.append(("Gremmy", response.strip()))
|
| 66 |
st.rerun()
|