-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
124 lines (86 loc) · 3.23 KB
/
Copy pathapp.py
File metadata and controls
124 lines (86 loc) · 3.23 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import streamlit as st
from langchain_core.messages import HumanMessage, AIMessage
from main import get_answer
from memory import save_history, get_all_chats, load_chat,delete_chat
import uuid
if "chat_id" not in st.session_state:
st.session_state.chat_id = uuid.uuid4().hex
chats = get_all_chats()
# -------------------------------------------------
# Page Configuration
# -------------------------------------------------
st.set_page_config(
page_title="AI Chat Assistant",
page_icon="🤖",
layout="wide"
)
# -------------------------------------------------
# Header
# -------------------------------------------------
st.markdown(
"<h1 style='text-align: center;'>🤖 AI Chat Assistant</h1>",
unsafe_allow_html=True
)
st.markdown(
"<p style='text-align: center;'>A conversational AI assistant powered by Ollama and LangChain with intelligent web search and persistent chat history.</p>",
unsafe_allow_html=True
)
st.divider()
# -------------------------------------------------
# Sidebar
# -------------------------------------------------
with st.sidebar:
st.title("☰ Actions")
if st.button("➕ New Chat", use_container_width=True):
st.session_state.chat_history = []
st.session_state.chat_id = uuid.uuid4().hex
st.rerun()
if st.button("🗑 Delete Current Chat", use_container_width=True):
delete_chat(st.session_state.chat_id)
st.session_state.chat_history = []
st.session_state.chat_id = uuid.uuid4().hex
st.rerun()
st.divider()
chats = get_all_chats()
st.title("💬 Chats")
for chat in chats:
if st.button(chat["chat_name"], use_container_width=True):
st.session_state.chat_id = chat["chat_id"]
st.session_state.chat_history = load_chat(chat["chat_id"])
st.rerun()
# -------------------------------------------------
# Session State Initialization
# -------------------------------------------------
if "chat_history" not in st.session_state:
st.session_state.chat_history = []
# -------------------------------------------------
# Display Previous Messages
# -------------------------------------------------
for message in st.session_state.chat_history:
if isinstance(message, HumanMessage):
with st.chat_message("user"):
st.write(message.content)
elif isinstance(message, AIMessage):
with st.chat_message("assistant"):
st.write(message.content)
# -------------------------------------------------
# Input Box
# -------------------------------------------------
question = st.chat_input("Ask your research question...")
# -------------------------------------------------
# Generate Response
# -------------------------------------------------
if question:
with st.chat_message("user"):
st.write(question)
st.session_state.chat_history.append(
HumanMessage(content=question)
)
with st.chat_message("assistant"):
answer, source = get_answer(question,st.session_state.chat_history)
st.caption(f"Source: {source}")
st.write(answer)
st.session_state.chat_history.append(
AIMessage(content=answer)
)
save_history(st.session_state.chat_history,st.session_state.chat_id)