-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmemory&response.py
45 lines (35 loc) · 1.46 KB
/
memory&response.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
conversational_memory_length = 10
memory = ConversationBufferWindowMemory(k=conversational_memory_length)
if 'chat_history' not in st.session_state:
st.session_state.chat_history = []
else:
for message in st.session_state.chat_history:
memory.save_context(
{'input': message['human']},
{'output': message['AI']}
)
# Initialize Groq Langchain chat object and conversation
groq_chat = ChatGroq(
groq_api_key=os.environ.get("GROQ_API_KEY"),
model_name="llama3-70b-8192"
)
conversation = ConversationChain(
llm=groq_chat,
memory=memory
)
st.markdown('<div class="chat-container">', unsafe_allow_html=True)
for message in st.session_state.chat_history:
if message['human']:
st.markdown(f'<div class="chat-message user-message">{message["human"]}</div>', unsafe_allow_html=True)
if message['AI']:
st.markdown(f'<div class="chat-message bot-message">{message["AI"]}</div>', unsafe_allow_html=True)
st.markdown('</div>', unsafe_allow_html=True)
st.markdown('<div class="input-container">', unsafe_allow_html=True)
user_question = st.text_input("", key="user_input", placeholder="Type your message here...")
if st.button("Send"):
if user_question:
response = conversation(user_question)
message = {'human': user_question, 'AI': response['response']}
st.session_state.chat_history.append(message)
st.experimental_rerun()
st.markdown('</div>', unsafe_allow_html=True)