-
Notifications
You must be signed in to change notification settings - Fork 9
/
chat_with_pdf.py
318 lines (256 loc) · 11.1 KB
/
chat_with_pdf.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
import tempfile
from langchain_couchbase.vectorstores import CouchbaseVectorStore
from langchain_openai import OpenAIEmbeddings
from langchain_community.document_loaders import PyPDFLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
import os
import streamlit as st
from langchain_core.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI
from langchain_core.runnables import RunnablePassthrough
from langchain_core.output_parsers import StrOutputParser
from langchain_core.globals import set_llm_cache
from langchain_couchbase.cache import CouchbaseCache
import time
def parse_bool(value: str):
"""Parse boolean values from environment variables"""
return value.lower() in ("yes", "true", "t", "1")
def check_environment_variable(variable_name):
"""Check if environment variable is set"""
if variable_name not in os.environ:
st.error(
f"{variable_name} environment variable is not set. Please add it to the secrets.toml file"
)
st.stop()
def save_to_vector_store(uploaded_file, vector_store):
"""Chunk the PDF & store it in Couchbase Vector Store"""
if uploaded_file is not None:
temp_dir = tempfile.TemporaryDirectory()
temp_file_path = os.path.join(temp_dir.name, uploaded_file.name)
with open(temp_file_path, "wb") as f:
f.write(uploaded_file.getvalue())
loader = PyPDFLoader(temp_file_path)
docs = loader.load()
text_splitter = RecursiveCharacterTextSplitter(
chunk_size=1500, chunk_overlap=150
)
doc_pages = text_splitter.split_documents(docs)
vector_store.add_documents(doc_pages)
st.info(f"PDF loaded into vector store in {len(doc_pages)} documents")
@st.cache_resource(show_spinner="Connecting to Vector Store")
def get_vector_store(
_cluster,
db_bucket,
db_scope,
db_collection,
_embedding,
index_name,
):
"""Return the Couchbase vector store"""
vector_store = CouchbaseVectorStore(
cluster=_cluster,
bucket_name=db_bucket,
scope_name=db_scope,
collection_name=db_collection,
embedding=_embedding,
index_name=index_name,
)
return vector_store
@st.cache_resource(show_spinner="Connecting to Cache")
def get_cache(_cluster, db_bucket, db_scope, cache_collection):
"""Return the Couchbase cache"""
cache = CouchbaseCache(
cluster=_cluster,
bucket_name=db_bucket,
scope_name=db_scope,
collection_name=cache_collection,
)
return cache
@st.cache_resource(show_spinner="Connecting to Couchbase")
def connect_to_couchbase(connection_string, db_username, db_password):
"""Connect to couchbase"""
from couchbase.cluster import Cluster
from couchbase.auth import PasswordAuthenticator
from couchbase.options import ClusterOptions
from datetime import timedelta
auth = PasswordAuthenticator(db_username, db_password)
options = ClusterOptions(auth)
connect_string = connection_string
cluster = Cluster(connect_string, options)
# Wait until the cluster is ready for use.
cluster.wait_until_ready(timedelta(seconds=5))
return cluster
def stream_string(s, chunk_size=10):
"""Stream a string with a delay to simulate streaming"""
for i in range(0, len(s), chunk_size):
yield s[i : i + chunk_size]
time.sleep(0.02)
if __name__ == "__main__":
st.set_page_config(
page_title="Chat with your PDF using Langchain, Couchbase & OpenAI",
page_icon="🤖",
layout="centered",
initial_sidebar_state="auto",
menu_items=None,
)
AUTH_ENABLED = parse_bool(os.getenv("AUTH_ENABLED", "False"))
if not AUTH_ENABLED:
st.session_state.auth = True
else:
# Authorization
if "auth" not in st.session_state:
st.session_state.auth = False
AUTH = os.getenv("LOGIN_PASSWORD")
check_environment_variable("LOGIN_PASSWORD")
# Authentication
user_pwd = st.text_input("Enter password", type="password")
pwd_submit = st.button("Submit")
if pwd_submit and user_pwd == AUTH:
st.session_state.auth = True
elif pwd_submit and user_pwd != AUTH:
st.error("Incorrect password")
if st.session_state.auth:
# Load environment variables
DB_CONN_STR = os.getenv("DB_CONN_STR")
DB_USERNAME = os.getenv("DB_USERNAME")
DB_PASSWORD = os.getenv("DB_PASSWORD")
DB_BUCKET = os.getenv("DB_BUCKET")
DB_SCOPE = os.getenv("DB_SCOPE")
DB_COLLECTION = os.getenv("DB_COLLECTION")
INDEX_NAME = os.getenv("INDEX_NAME")
CACHE_COLLECTION = os.getenv("CACHE_COLLECTION")
# Ensure that all environment variables are set
check_environment_variable("OPENAI_API_KEY")
check_environment_variable("DB_CONN_STR")
check_environment_variable("DB_USERNAME")
check_environment_variable("DB_PASSWORD")
check_environment_variable("DB_BUCKET")
check_environment_variable("DB_SCOPE")
check_environment_variable("DB_COLLECTION")
check_environment_variable("INDEX_NAME")
check_environment_variable("CACHE_COLLECTION")
# Use OpenAI Embeddings
embedding = OpenAIEmbeddings()
# Connect to Couchbase Vector Store
cluster = connect_to_couchbase(DB_CONN_STR, DB_USERNAME, DB_PASSWORD)
vector_store = get_vector_store(
cluster,
DB_BUCKET,
DB_SCOPE,
DB_COLLECTION,
embedding,
INDEX_NAME,
)
# Use couchbase vector store as a retriever for RAG
retriever = vector_store.as_retriever()
# Set the LLM cache
cache = get_cache(cluster, DB_BUCKET, DB_SCOPE, CACHE_COLLECTION)
set_llm_cache(cache)
# Build the prompt for the RAG
template = """You are a helpful bot. If you cannot answer based on the context provided, respond with a generic answer. Answer the question as truthfully as possible using the context below:
{context}
Question: {question}"""
prompt = ChatPromptTemplate.from_template(template)
# Use OpenAI GPT 4 as the LLM for the RAG
llm = ChatOpenAI(temperature=0, model="gpt-4-1106-preview", streaming=True)
# RAG chain
chain = (
{"context": retriever, "question": RunnablePassthrough()}
| prompt
| llm
| StrOutputParser()
)
# Pure OpenAI output without RAG
template_without_rag = """You are a helpful bot. Answer the question as truthfully as possible.
Question: {question}"""
prompt_without_rag = ChatPromptTemplate.from_template(template_without_rag)
llm_without_rag = ChatOpenAI(model="gpt-4-1106-preview", streaming=True)
chain_without_rag = (
{"question": RunnablePassthrough()}
| prompt_without_rag
| llm_without_rag
| StrOutputParser()
)
# Frontend
couchbase_logo = (
"https://emoji.slack-edge.com/T024FJS4M/couchbase/4a361e948b15ed91.png"
)
st.title("Chat with PDF")
st.markdown(
"Answers with [Couchbase logo](https://emoji.slack-edge.com/T024FJS4M/couchbase/4a361e948b15ed91.png) are generated using *RAG* while 🤖 are generated by pure *LLM (ChatGPT)*"
)
with st.sidebar:
st.header("Upload your PDF")
with st.form("upload pdf"):
uploaded_file = st.file_uploader(
"Choose a PDF.",
help="The document will be deleted after one hour of inactivity (TTL).",
type="pdf",
)
submitted = st.form_submit_button("Upload")
if submitted:
# store the PDF in the vector store after chunking
save_to_vector_store(uploaded_file, vector_store)
st.subheader("How does it work?")
st.markdown(
"""
For each question, you will get two answers:
* one using RAG ([Couchbase logo](https://emoji.slack-edge.com/T024FJS4M/couchbase/4a361e948b15ed91.png))
* one using pure LLM - OpenAI (🤖).
"""
)
st.markdown(
"For RAG, we are using [Langchain](https://langchain.com/), [Couchbase Vector Search](https://couchbase.com/) & [OpenAI](https://openai.com/). We fetch parts of the PDF relevant to the question using Vector search & add it as the context to the LLM. The LLM is instructed to answer based on the context from the Vector Store."
)
# View Code
if st.checkbox("View Code"):
st.write(
"View the code here: [Github](https://github.com/couchbase-examples/rag-demo/blob/main/chat_with_pdf.py)"
)
if "messages" not in st.session_state:
st.session_state.messages = []
st.session_state.messages.append(
{
"role": "assistant",
"content": "Hi, I'm a chatbot who can chat with the PDF. How can I help you?",
"avatar": "🤖",
}
)
# Display chat messages from history on app rerun
for message in st.session_state.messages:
with st.chat_message(message["role"], avatar=message["avatar"]):
st.markdown(message["content"])
# React to user input
if question := st.chat_input("Ask a question based on the PDF"):
# Display user message in chat message container
st.chat_message("user").markdown(question)
# Add user message to chat history
st.session_state.messages.append(
{"role": "user", "content": question, "avatar": "👤"}
)
# Add placeholder for streaming the response
with st.chat_message("assistant", avatar=couchbase_logo):
# Get the response from the RAG & stream it
# In order to cache the response, we need to invoke the chain and cache the response locally as OpenAI does not support it yet
# Ref: https://github.com/langchain-ai/langchain/issues/9762
rag_response = chain.invoke(question)
st.write_stream(stream_string(rag_response))
st.session_state.messages.append(
{
"role": "assistant",
"content": rag_response,
"avatar": couchbase_logo,
}
)
# Get the response from the pure LLM & stream it
pure_llm_response = chain_without_rag.invoke(question)
# Add placeholder for streaming the response
with st.chat_message("ai", avatar="🤖"):
st.write_stream(stream_string(pure_llm_response))
st.session_state.messages.append(
{
"role": "assistant",
"content": pure_llm_response,
"avatar": "🤖",
}
)