-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
59 lines (44 loc) · 1.78 KB
/
app.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
import streamlit as st
from llm_chain import load_normal_chain
from langchain.memory import StreamlitChatMessageHistory
import yaml
import os
with open("config.yaml","r") as f:
config = yaml.safe_load(f)
#for llm chain
def load_chain(chat_history):
return load_normal_chain(chat_history)
def clear_input_field():
st.session_state.user_question = st.session_state.user_input
st.session_state.user_input = ""
def set_send_input():
st.session_state.send_input = True
clear_input_field()
#main Client
def main():
st.title("VAGBHAT AI")
chat_container = st.container()
st.sidebar.title("Chat Sessions")
chat_sessions = ["new_session"] + os.listdir(config["chat_history_path"])
#session state definitions
if "send_input" not in st.session_state:
st.session_state.send_input = False
st.session_state.user_question = ""
st.sidebar.selectbox("Select a chat session", chat_sessions, key="session_key")
chat_history = StreamlitChatMessageHistory(key="history")
llm_chain = load_chain(chat_history)
user_input = st.text_input("Ask Query here", key="user_input", on_change=set_send_input)
send_button = st.button("Send", key="send_button")
if send_button or st.session_state.send_input:
if st.session_state.user_question != "":
with chat_container:
st.chat_message("user").write(st.session_state.user_question)
llm_response = llm_chain.run(st.session_state.user_question)
st.session_state.user_question = ""
if chat_history.messages !=[]:
with chat_container:
st.write("Chat History")
for message in chat_history.messages:
st.chat_message(message.type).write(message.content)
if __name__ == "__main__":
main()