-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot_without_memory.py
71 lines (55 loc) · 2.02 KB
/
bot_without_memory.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import streamlit as st
import os
from dotenv import load_dotenv
from langchain_groq import ChatGroq
from langchain_core.prompts import ChatPromptTemplate
# Load environment variables
load_dotenv()
# Initialize LLM and chat chain
@st.cache_resource
def init_chat_chain():
llm = ChatGroq(
model="gemma2-9b-it",
groq_api_key=os.getenv("GROQ_API_KEY")
)
prompt = ChatPromptTemplate.from_messages([
("system", "You are a helpful assistant please answer the question."),
("human", "{input}")
])
chain = prompt | llm
return chain
def main():
st.title("💬 Chatbot with Memory")
# Initialize session state for messages
if "messages" not in st.session_state:
st.session_state.messages = []
st.session_state.session_id = str(hash(str(st.session_state)))
# Initialize chat chain
chat_chain = init_chat_chain()
# Display chat messages
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])
# Chat input
if prompt := st.chat_input("What would you like to know?"):
# Add user message to chat history
st.session_state.messages.append({"role": "user", "content": prompt})
# Display user message
with st.chat_message("user"):
st.markdown(prompt)
# Get bot response
with st.chat_message("assistant"):
config = {"configurable": {"session_id": st.session_state.session_id}}
with st.spinner("Thinking..."):
response = chat_chain.invoke(
{"input": prompt},
config=config,
)
# Display bot response
st.markdown(response.content)
# Add bot response to chat history
st.session_state.messages.append(
{"role": "assistant", "content": response.content}
)
if __name__ == "__main__":
main()