Enoch1359 commited on
Commit
8e6bbab
Β·
verified Β·
1 Parent(s): 4076854

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -31
app.py CHANGED
@@ -2,11 +2,11 @@ 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, 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")
@@ -21,50 +21,58 @@ llm = ChatOpenAI(
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"
30
- "- Introduce yourself as Gremmy when needed.\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
 
43
- # βœ… Initialize chat history
44
  if "history" not in st.session_state:
45
  st.session_state.history = []
46
 
47
- # βœ… Display chat history
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()
 
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.chains import ConversationChain
8
 
9
+ # βœ… Load API keys
10
  load_dotenv("apiroute.env")
11
  api_key = os.getenv("OPENAI_API_KEY")
12
  api_base = os.getenv("OPENAI_API_BASE")
 
21
  if "memory" not in st.session_state:
22
  st.session_state.memory = ConversationBufferMemory(return_messages=True)
23
 
24
+ # βœ… Setup flag to inject system prompt only once
25
+ if "intro_done" not in st.session_state:
26
+ st.session_state.intro_done = False
27
+
28
+ # βœ… Create system message describing the assistant
29
+ system_message = SystemMessage(
30
+ content=(
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
+ # βœ… Setup ConversationChain (adds memory)
40
+ chat = ConversationChain(
41
+ llm=llm,
42
+ memory=st.session_state.memory,
43
+ verbose=False
44
+ )
45
 
46
  # βœ… Streamlit UI
47
  st.set_page_config(page_title="Chatbot", layout="centered")
48
  st.title("πŸ’¬ Chat with Gremmy")
49
 
50
+ # βœ… Initialize history
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 box
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 message
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()