Update app.py
Browse files
app.py
CHANGED
@@ -2,28 +2,28 @@ 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, HumanMessage
|
6 |
from langchain.memory import ConversationBufferMemory
|
7 |
-
from langchain.
|
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 |
-
# β
Initialize
|
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 |
content=(
|
28 |
"You are Gremmy, a helpful, friendly AI chatbot created by Frederick using Google's Gemma model. "
|
29 |
"You are smart, polite, and informative.\n"
|
@@ -31,18 +31,12 @@ if "intro_done" not in st.session_state:
|
|
31 |
"- Speak in a concise and helpful tone.\n"
|
32 |
"- For more information about your creator, visit: https://www.linkedin.com/in/j-frederick-paul-35801a179/"
|
33 |
)
|
34 |
-
)
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
# β
Setup the conversation chain
|
39 |
-
chat = ConversationChain(
|
40 |
-
llm=llm,
|
41 |
-
memory=st.session_state.memory,
|
42 |
-
verbose=False
|
43 |
-
)
|
44 |
|
45 |
-
# β
Streamlit
|
46 |
st.set_page_config(page_title="Chatbot", layout="centered")
|
47 |
st.title("π¬ Chat with Gremmy")
|
48 |
|
@@ -54,19 +48,23 @@ if "history" not in st.session_state:
|
|
54 |
for sender, msg in st.session_state.history:
|
55 |
st.markdown(f"**{sender}:** {msg}")
|
56 |
|
57 |
-
# β
Input
|
58 |
with st.form(key="chat_form", clear_on_submit=True):
|
59 |
user_input = st.text_input("Talk to me", key="user_message")
|
60 |
submitted = st.form_submit_button("Send")
|
61 |
|
62 |
-
# β
|
63 |
if submitted and user_input:
|
64 |
-
# Add
|
65 |
st.session_state.history.append(("You", user_input))
|
|
|
66 |
|
67 |
-
#
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
|
|
|
|
|
|
72 |
st.rerun()
|
|
|
2 |
from dotenv import load_dotenv
|
3 |
import os
|
4 |
from langchain_openai import ChatOpenAI
|
5 |
+
from langchain.schema import SystemMessage, HumanMessage, AIMessage
|
6 |
from langchain.memory import ConversationBufferMemory
|
7 |
+
from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder
|
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 |
+
# β
Initialize LLM
|
15 |
llm = ChatOpenAI(
|
16 |
model_name="google/gemma-3n-e2b-it:free",
|
17 |
temperature=0.7
|
18 |
)
|
19 |
|
20 |
+
# β
Setup memory
|
21 |
if "memory" not in st.session_state:
|
22 |
st.session_state.memory = ConversationBufferMemory(return_messages=True)
|
23 |
|
24 |
+
# β
Setup prompt with system message only once
|
25 |
+
prompt = ChatPromptTemplate.from_messages([
|
26 |
+
SystemMessage(
|
27 |
content=(
|
28 |
"You are Gremmy, a helpful, friendly AI chatbot created by Frederick using Google's Gemma model. "
|
29 |
"You are smart, polite, and informative.\n"
|
|
|
31 |
"- Speak in a concise and helpful tone.\n"
|
32 |
"- For more information about your creator, visit: https://www.linkedin.com/in/j-frederick-paul-35801a179/"
|
33 |
)
|
34 |
+
),
|
35 |
+
MessagesPlaceholder(variable_name="chat_history"),
|
36 |
+
HumanMessage(content="{input}")
|
37 |
+
])
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
+
# β
Streamlit UI
|
40 |
st.set_page_config(page_title="Chatbot", layout="centered")
|
41 |
st.title("π¬ Chat with Gremmy")
|
42 |
|
|
|
48 |
for sender, msg in st.session_state.history:
|
49 |
st.markdown(f"**{sender}:** {msg}")
|
50 |
|
51 |
+
# β
Input form
|
52 |
with st.form(key="chat_form", clear_on_submit=True):
|
53 |
user_input = st.text_input("Talk to me", key="user_message")
|
54 |
submitted = st.form_submit_button("Send")
|
55 |
|
56 |
+
# β
Generate response
|
57 |
if submitted and user_input:
|
58 |
+
# Add to history
|
59 |
st.session_state.history.append(("You", user_input))
|
60 |
+
st.session_state.memory.chat_memory.add_message(HumanMessage(content=user_input))
|
61 |
|
62 |
+
# Format and run the prompt
|
63 |
+
full_prompt = prompt.format_messages(
|
64 |
+
chat_history=st.session_state.memory.chat_memory.messages,
|
65 |
+
input=user_input
|
66 |
+
)
|
67 |
+
response = llm(full_prompt)
|
68 |
+
st.session_state.history.append(("Gremmy", response.content))
|
69 |
+
st.session_state.memory.chat_memory.add_message(AIMessage(content=response.content))
|
70 |
st.rerun()
|