vatsal18 commited on
Commit
1cf865f
·
verified ·
1 Parent(s): c68aee3

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +137 -0
  2. requirements.txt +37 -3
app.py ADDED
@@ -0,0 +1,137 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import os
3
+ from dotenv import load_dotenv
4
+ load_dotenv()
5
+
6
+ os.environ["LANGCHAIN_API_KEY"] = os.getenv("LANGCHAIN_API_KEY")
7
+ os.environ["LANGCHAIN_TRACING_V2"] = "true"
8
+ os.environ["LANGCHAIN_PROJECT"]= "RAG Document Q&A"
9
+ os.environ["HUGGINGFACEHUB_API_TOKEN"] = os.getenv("HUGGINGFACEHUB_API_TOKEN")
10
+
11
+ from langchain_groq import ChatGroq
12
+ from langchain_openai import ChatOpenAI, OpenAIEmbeddings
13
+ from langchain_huggingface import HuggingFaceEmbeddings
14
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
15
+ from langchain_core.prompts import ChatPromptTemplate, PromptTemplate, MessagesPlaceholder
16
+ from langchain.chains.combine_documents import create_stuff_documents_chain
17
+ from langchain.document_loaders import PyPDFLoader
18
+
19
+ from langchain.vectorstores import FAISS
20
+ from langchain.chains import create_retrieval_chain, create_history_aware_retriever
21
+ from langchain_core.chat_history import BaseChatMessageHistory
22
+ from langchain_community.chat_message_histories import ChatMessageHistory
23
+ from langchain_core.runnables.history import RunnableWithMessageHistory
24
+
25
+
26
+
27
+ st.title("Conversational RAG With PDF uploads and chat history")
28
+ st.write("Upload PDFs and chat with their content")
29
+
30
+ api_key = st.text_input("Enter your Groq API key:", type="password")
31
+
32
+ if api_key:
33
+ llm=ChatGroq(groq_api_key=api_key, model_name="openai/gpt-oss-20b")
34
+
35
+ session_id= st.text_input("Session ID", value="default_session")
36
+
37
+ if 'store' not in st.session_state:
38
+ st.session_state.store={}
39
+
40
+ uploaded_files=st.file_uploader("Choose A PDF file", type="pdf", accept_multiple_files=True)
41
+
42
+ if uploaded_files:
43
+ documents=[]
44
+ for uploaded_file in uploaded_files:
45
+ tempdf=f"./temp.pdf"
46
+ with open(tempdf, "wb") as file:
47
+ file.write(uploaded_file.getvalue())
48
+ file_name = uploaded_file.name
49
+
50
+ loader= PyPDFLoader(tempdf)
51
+ docs = loader.load()
52
+ documents.extend(docs)
53
+
54
+ text_splitter = RecursiveCharacterTextSplitter(chunk_size=5000, chunk_overlap=500)
55
+ splits = text_splitter.split_documents(documents)
56
+ vectorstore = FAISS.from_documents(documents=splits, embedding=OpenAIEmbeddings())
57
+ retriever = vectorstore.as_retriever()
58
+
59
+ contextualize_q_system_prompt=(
60
+ "Given a chat history and the latest user question"
61
+ "which might reference context in the chat history, "
62
+ "formulate a standalone question which can be understood "
63
+ "without the chat history. Do NOT answer the question, "
64
+ "just reformulate it if needed and otherwise return it as is."
65
+ )
66
+
67
+ contextualize_q_prompt = ChatPromptTemplate.from_messages(
68
+ [
69
+ ("system", contextualize_q_system_prompt),
70
+ MessagesPlaceholder("chat_history"),
71
+ ("human", "{input}"),
72
+ ]
73
+ )
74
+
75
+ history_aware_retriever= create_history_aware_retriever(llm, retriever, contextualize_q_prompt)
76
+
77
+ ## Answer question
78
+
79
+ # Answer question
80
+ system_prompt = (
81
+ "You are an assistant for question-answering tasks. "
82
+ "Use the following pieces of retrieved context to answer "
83
+ "the question. If you don't know the answer, say that you "
84
+ "don't know. Use three sentences maximum and keep the "
85
+ "answer concise."
86
+ "\n\n"
87
+ "{context}"
88
+ )
89
+ qa_prompt = ChatPromptTemplate.from_messages(
90
+ [
91
+ ("system", system_prompt),
92
+ MessagesPlaceholder("chat_history"),
93
+ ("human", "{input}"),
94
+ ]
95
+ )
96
+
97
+ question_answer_chain = create_stuff_documents_chain(llm, qa_prompt)
98
+
99
+ rag_chain=create_retrieval_chain(history_aware_retriever, question_answer_chain)
100
+
101
+ def get_session_history(session:str)->BaseChatMessageHistory:
102
+ if session_id not in st.session_state.store:
103
+ st.session_state.store[session_id]=ChatMessageHistory()
104
+ return st.session_state.store[session_id]
105
+
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
+ user_input = st.text_input("Enter your questions:")
114
+ if user_input:
115
+ session_history=get_session_history(session_id)
116
+ response = conversational_rag_chain.invoke(
117
+ {"input": user_input},
118
+ config={
119
+ "configurable": {"session_id":session_id}
120
+ }, # constructs a key "abc123" in `store`.
121
+ )
122
+ #st.write(st.session_state.store)
123
+ st.write("Assistant:", response['answer'])
124
+ #st.write("Chat History:", session_history.messages)
125
+ else:
126
+ st.warning("Please enter the GRoq API Key")
127
+
128
+
129
+
130
+
131
+
132
+
133
+
134
+
135
+
136
+
137
+
requirements.txt CHANGED
@@ -1,3 +1,37 @@
1
- altair
2
- pandas
3
- streamlit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ langchain
2
+ python-dotenv
3
+ ipykernel
4
+ langchain-community
5
+ langchain_openai
6
+ dotenv
7
+ langchain_text_splitters
8
+ chromadb
9
+ sentence_transformers
10
+ langchain_huggingface
11
+ faiss-cpu
12
+ langchain_chroma
13
+ grandalf
14
+ huggingface_hub
15
+ streamlit
16
+ langchain_groq
17
+ fastapi
18
+ uvicorn
19
+ langserve
20
+ sse_starlette
21
+ bs4
22
+ pypdf
23
+ arxiv
24
+ wikipedia
25
+ duckduckgo-search
26
+ validators
27
+ youtube_transcript_api
28
+ unstructured
29
+ pytube
30
+ numexpr
31
+ mysql-connector-python
32
+ SQLAlchemy
33
+ langchain_nvidia_ai_endpoints
34
+ pinecone-client
35
+ pinecone-text
36
+ pinecone-notebooks
37
+ pinecone