-
Notifications
You must be signed in to change notification settings - Fork 18
/
app.py
executable file
·132 lines (110 loc) · 3.92 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
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
125
126
127
128
129
130
131
132
import asyncio
import random
import streamlit as st
from dotenv import load_dotenv
from ragbase.chain import ask_question, create_chain
from ragbase.config import Config
from ragbase.ingestor import Ingestor
from ragbase.model import create_llm
from ragbase.retriever import create_retriever
from ragbase.uploader import upload_files
load_dotenv()
LOADING_MESSAGES = [
"Calculating your answer through multiverse...",
"Adjusting quantum entanglement...",
"Summoning star wisdom... almost there!",
"Consulting Schrödinger's cat...",
"Warping spacetime for your response...",
"Balancing neutron star equations...",
"Analyzing dark matter... please wait...",
"Engaging hyperdrive... en route!",
"Gathering photons from a galaxy...",
"Beaming data from Andromeda... stand by!",
]
@st.cache_resource(show_spinner=False)
def build_qa_chain(files):
file_paths = upload_files(files)
vector_store = Ingestor().ingest(file_paths)
llm = create_llm()
retriever = create_retriever(llm, vector_store=vector_store)
return create_chain(llm, retriever)
async def ask_chain(question: str, chain):
full_response = ""
assistant = st.chat_message(
"assistant", avatar=str(Config.Path.IMAGES_DIR / "assistant-avatar.png")
)
with assistant:
message_placeholder = st.empty()
message_placeholder.status(random.choice(LOADING_MESSAGES), state="running")
documents = []
async for event in ask_question(chain, question, session_id="session-id-42"):
if type(event) is str:
full_response += event
message_placeholder.markdown(full_response)
if type(event) is list:
documents.extend(event)
for i, doc in enumerate(documents):
with st.expander(f"Source #{i+1}"):
st.write(doc.page_content)
st.session_state.messages.append({"role": "assistant", "content": full_response})
def show_upload_documents():
holder = st.empty()
with holder.container():
st.header("RagBase")
st.subheader("Get answers from your documents")
uploaded_files = st.file_uploader(
label="Upload PDF files", type=["pdf"], accept_multiple_files=True
)
if not uploaded_files:
st.warning("Please upload PDF documents to continue!")
st.stop()
with st.spinner("Analyzing your document(s)..."):
holder.empty()
return build_qa_chain(uploaded_files)
def show_message_history():
for message in st.session_state.messages:
role = message["role"]
avatar_path = (
Config.Path.IMAGES_DIR / "assistant-avatar.png"
if role == "assistant"
else Config.Path.IMAGES_DIR / "user-avatar.png"
)
with st.chat_message(role, avatar=str(avatar_path)):
st.markdown(message["content"])
def show_chat_input(chain):
if prompt := st.chat_input("Ask your question here"):
st.session_state.messages.append({"role": "user", "content": prompt})
with st.chat_message(
"user",
avatar=str(Config.Path.IMAGES_DIR / "user-avatar.png"),
):
st.markdown(prompt)
asyncio.run(ask_chain(prompt, chain))
st.set_page_config(page_title="RagBase", page_icon="🐧")
st.html(
"""
<style>
.st-emotion-cache-p4micv {
width: 2.75rem;
height: 2.75rem;
}
</style>
"""
)
if "messages" not in st.session_state:
st.session_state.messages = [
{
"role": "assistant",
"content": "Hi! What do you want to know about your documents?",
}
]
if Config.CONVERSATION_MESSAGES_LIMIT > 0 and Config.CONVERSATION_MESSAGES_LIMIT <= len(
st.session_state.messages
):
st.warning(
"You have reached the conversation limit. Refresh the page to start a new conversation."
)
st.stop()
chain = show_upload_documents()
show_message_history()
show_chat_input(chain)