Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,137 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
## RAG Q&A Conversation With PDF Including Chat History
|
2 |
+
import streamlit as st
|
3 |
+
from langchain.chains import create_history_aware_retriever, create_retrieval_chain
|
4 |
+
from langchain.chains.combine_documents import create_stuff_documents_chain
|
5 |
+
from langchain_community.vectorstores import FAISS
|
6 |
+
from langchain_community.chat_message_histories import ChatMessageHistory
|
7 |
+
from langchain_core.chat_history import BaseChatMessageHistory
|
8 |
+
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
|
9 |
+
from langchain_groq import ChatGroq
|
10 |
+
from langchain_core.runnables.history import RunnableWithMessageHistory
|
11 |
+
from langchain_huggingface import HuggingFaceEmbeddings
|
12 |
+
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
13 |
+
from langchain_community.document_loaders import PyPDFLoader
|
14 |
+
import os
|
15 |
+
|
16 |
+
from dotenv import load_dotenv
|
17 |
+
load_dotenv()
|
18 |
+
|
19 |
+
os.environ['HF_TOKEN']=os.getenv("HF_TOKEN")
|
20 |
+
embeddings=HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
|
21 |
+
|
22 |
+
|
23 |
+
## set up Streamlit
|
24 |
+
st.title("Conversational RAG With PDF uploads and chat history")
|
25 |
+
st.write("Upload Pdf's and chat with their content")
|
26 |
+
|
27 |
+
## Input the Groq API Key
|
28 |
+
api_key=st.text_input("Enter your Groq API key:",type="password")
|
29 |
+
|
30 |
+
## Check if groq api key is provided
|
31 |
+
if api_key:
|
32 |
+
llm=ChatGroq(groq_api_key=api_key,model_name="Gemma2-9b-It")
|
33 |
+
|
34 |
+
## chat interface
|
35 |
+
|
36 |
+
session_id=st.text_input("Session ID",value="default_session")
|
37 |
+
## statefully manage chat history
|
38 |
+
|
39 |
+
if 'store' not in st.session_state:
|
40 |
+
st.session_state.store={}
|
41 |
+
|
42 |
+
uploaded_files=st.file_uploader("Choose A PDf file",type="pdf",accept_multiple_files=True)
|
43 |
+
## Process uploaded PDF's
|
44 |
+
if uploaded_files:
|
45 |
+
documents=[]
|
46 |
+
for uploaded_file in uploaded_files:
|
47 |
+
temppdf=f"./temp.pdf"
|
48 |
+
with open(temppdf,"wb") as file:
|
49 |
+
file.write(uploaded_file.getvalue())
|
50 |
+
file_name=uploaded_file.name
|
51 |
+
|
52 |
+
loader=PyPDFLoader(temppdf)
|
53 |
+
docs=loader.load()
|
54 |
+
documents.extend(docs)
|
55 |
+
|
56 |
+
# Split and create embeddings for the documents
|
57 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=5000, chunk_overlap=500)
|
58 |
+
splits = text_splitter.split_documents(documents)
|
59 |
+
vectorstore = FAISS.from_documents(documents=splits, embedding=embeddings)
|
60 |
+
retriever = vectorstore.as_retriever()
|
61 |
+
|
62 |
+
contextualize_q_system_prompt=(
|
63 |
+
"Given a chat history and the latest user question"
|
64 |
+
"which might reference context in the chat history, "
|
65 |
+
"formulate a standalone question which can be understood "
|
66 |
+
"without the chat history. Do NOT answer the question, "
|
67 |
+
"just reformulate it if needed and otherwise return it as is."
|
68 |
+
)
|
69 |
+
contextualize_q_prompt = ChatPromptTemplate.from_messages(
|
70 |
+
[
|
71 |
+
("system", contextualize_q_system_prompt),
|
72 |
+
MessagesPlaceholder("chat_history"),
|
73 |
+
("human", "{input}"),
|
74 |
+
]
|
75 |
+
)
|
76 |
+
|
77 |
+
history_aware_retriever=create_history_aware_retriever(llm,retriever,contextualize_q_prompt)
|
78 |
+
|
79 |
+
## Answer question
|
80 |
+
|
81 |
+
# Answer question
|
82 |
+
system_prompt = (
|
83 |
+
"You are an assistant for question-answering tasks. "
|
84 |
+
"Use the following pieces of retrieved context to answer "
|
85 |
+
"the question. If you don't know the answer, say that you "
|
86 |
+
"don't know. Use three sentences maximum and keep the "
|
87 |
+
"answer concise."
|
88 |
+
"\n\n"
|
89 |
+
"{context}"
|
90 |
+
)
|
91 |
+
qa_prompt = ChatPromptTemplate.from_messages(
|
92 |
+
[
|
93 |
+
("system", system_prompt),
|
94 |
+
MessagesPlaceholder("chat_history"),
|
95 |
+
("human", "{input}"),
|
96 |
+
]
|
97 |
+
)
|
98 |
+
|
99 |
+
question_answer_chain=create_stuff_documents_chain(llm,qa_prompt)
|
100 |
+
rag_chain=create_retrieval_chain(history_aware_retriever,question_answer_chain)
|
101 |
+
|
102 |
+
def get_session_history(session:str)->BaseChatMessageHistory:
|
103 |
+
if session_id not in st.session_state.store:
|
104 |
+
st.session_state.store[session_id]=ChatMessageHistory()
|
105 |
+
return st.session_state.store[session_id]
|
106 |
+
|
107 |
+
conversational_rag_chain=RunnableWithMessageHistory(
|
108 |
+
rag_chain,get_session_history,
|
109 |
+
input_messages_key="input",
|
110 |
+
history_messages_key="chat_history",
|
111 |
+
output_messages_key="answer"
|
112 |
+
)
|
113 |
+
|
114 |
+
user_input = st.text_input("Your question:")
|
115 |
+
if user_input:
|
116 |
+
session_history=get_session_history(session_id)
|
117 |
+
response = conversational_rag_chain.invoke(
|
118 |
+
{"input": user_input},
|
119 |
+
config={
|
120 |
+
"configurable": {"session_id":session_id}
|
121 |
+
}, # constructs a key "abc123" in `store`.
|
122 |
+
)
|
123 |
+
st.write(st.session_state.store)
|
124 |
+
st.write("Assistant:", response['answer'])
|
125 |
+
st.write("Chat History:", session_history.messages)
|
126 |
+
else:
|
127 |
+
st.warning("Please enter the GRoq API Key")
|
128 |
+
|
129 |
+
|
130 |
+
|
131 |
+
|
132 |
+
|
133 |
+
|
134 |
+
|
135 |
+
|
136 |
+
|
137 |
+
|