Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import os | |
| from dotenv import load_dotenv | |
| load_dotenv() | |
| os.environ["LANGCHAIN_API_KEY"] = os.getenv("LANGCHAIN_API_KEY") | |
| os.environ["LANGCHAIN_TRACING_V2"] = "true" | |
| os.environ["LANGCHAIN_PROJECT"]= "RAG Document Q&A" | |
| os.environ["HUGGINGFACEHUB_API_TOKEN"] = os.getenv("HUGGINGFACEHUB_API_TOKEN") | |
| from langchain_groq import ChatGroq | |
| from langchain_openai import ChatOpenAI, OpenAIEmbeddings | |
| from langchain_huggingface import HuggingFaceEmbeddings | |
| from langchain.text_splitter import RecursiveCharacterTextSplitter | |
| from langchain_core.prompts import ChatPromptTemplate, PromptTemplate, MessagesPlaceholder | |
| from langchain.chains.combine_documents import create_stuff_documents_chain | |
| from langchain.document_loaders import PyPDFLoader | |
| from langchain.vectorstores import FAISS | |
| from langchain.chains import create_retrieval_chain, create_history_aware_retriever | |
| from langchain_core.chat_history import BaseChatMessageHistory | |
| from langchain_community.chat_message_histories import ChatMessageHistory | |
| from langchain_core.runnables.history import RunnableWithMessageHistory | |
| st.title("Conversational RAG With PDF uploads and chat history") | |
| st.write("Upload PDFs and chat with their content") | |
| api_key = st.text_input("Enter your Groq API key:", type="password") | |
| if api_key: | |
| llm=ChatGroq(groq_api_key=api_key, model_name="openai/gpt-oss-20b") | |
| session_id= st.text_input("Session ID", value="default_session") | |
| if 'store' not in st.session_state: | |
| st.session_state.store={} | |
| uploaded_files=st.file_uploader("Choose A PDF file", type="pdf", accept_multiple_files=True) | |
| if uploaded_files: | |
| documents=[] | |
| for uploaded_file in uploaded_files: | |
| tempdf=f"./temp.pdf" | |
| with open(tempdf, "wb") as file: | |
| file.write(uploaded_file.getvalue()) | |
| file_name = uploaded_file.name | |
| loader= PyPDFLoader(tempdf) | |
| docs = loader.load() | |
| documents.extend(docs) | |
| text_splitter = RecursiveCharacterTextSplitter(chunk_size=5000, chunk_overlap=500) | |
| splits = text_splitter.split_documents(documents) | |
| vectorstore = FAISS.from_documents(documents=splits, embedding=OpenAIEmbeddings()) | |
| retriever = vectorstore.as_retriever() | |
| contextualize_q_system_prompt=( | |
| "Given a chat history and the latest user question" | |
| "which might reference context in the chat history, " | |
| "formulate a standalone question which can be understood " | |
| "without the chat history. Do NOT answer the question, " | |
| "just reformulate it if needed and otherwise return it as is." | |
| ) | |
| contextualize_q_prompt = ChatPromptTemplate.from_messages( | |
| [ | |
| ("system", contextualize_q_system_prompt), | |
| MessagesPlaceholder("chat_history"), | |
| ("human", "{input}"), | |
| ] | |
| ) | |
| history_aware_retriever= create_history_aware_retriever(llm, retriever, contextualize_q_prompt) | |
| ## Answer question | |
| # Answer question | |
| system_prompt = ( | |
| "You are an assistant for question-answering tasks. " | |
| "Use the following pieces of retrieved context to answer " | |
| "the question. If you don't know the answer, say that you " | |
| "don't know. Use three sentences maximum and keep the " | |
| "answer concise." | |
| "\n\n" | |
| "{context}" | |
| ) | |
| qa_prompt = ChatPromptTemplate.from_messages( | |
| [ | |
| ("system", system_prompt), | |
| MessagesPlaceholder("chat_history"), | |
| ("human", "{input}"), | |
| ] | |
| ) | |
| question_answer_chain = create_stuff_documents_chain(llm, qa_prompt) | |
| rag_chain=create_retrieval_chain(history_aware_retriever, question_answer_chain) | |
| def get_session_history(session:str)->BaseChatMessageHistory: | |
| if session_id not in st.session_state.store: | |
| st.session_state.store[session_id]=ChatMessageHistory() | |
| return st.session_state.store[session_id] | |
| conversational_rag_chain = RunnableWithMessageHistory( | |
| rag_chain, get_session_history, | |
| input_messages_key="input", | |
| history_messages_key="chat_history", | |
| output_messages_key="answer" | |
| ) | |
| user_input = st.text_input("Enter your questions:") | |
| if user_input: | |
| session_history=get_session_history(session_id) | |
| response = conversational_rag_chain.invoke( | |
| {"input": user_input}, | |
| config={ | |
| "configurable": {"session_id":session_id} | |
| }, # constructs a key "abc123" in `store`. | |
| ) | |
| #st.write(st.session_state.store) | |
| st.write("Assistant:", response['answer']) | |
| #st.write("Chat History:", session_history.messages) | |
| else: | |
| st.warning("Please enter the GRoq API Key") | |