|
|
|
"""RAG Conversational Chat Application using lanchain, Mistral 7B , Pinecone vector DB |
|
|
|
### Step-1: Upload Documents and Load with Langchain Document Loader |
|
- Upload the documents to Google Colab. |
|
- Use Langchain document loader to load the documents. |
|
|
|
### Step-2: Perform Chunking |
|
- Perform chunking on the loaded documents. |
|
|
|
### Step-3: Initialize LLM and Use Huggingface Embedding Model |
|
- Initialize a Large Language Model (LLM). |
|
- Use the Huggingface Embedding Model to convert the chunks into embeddings. |
|
|
|
### Step-4: Initialize Vector Database |
|
- Initialize a Vector Database to store the resulting embeddings. |
|
|
|
### Step-5: Upload Embeddings to Vector Database |
|
- Upload the embeddings to the Vector Database. |
|
|
|
### Step-6: Create Langchain Conversational Buffer Memory |
|
- Create a Langchain conversational buffer memory. |
|
|
|
### Step-7: Create Prompt Template |
|
- Create a prompt template for generating responses. |
|
|
|
### Step-8: Use Langchain RetreivalQA |
|
- Use Langchain RetreivalQA for creating the conversational chat. |
|
|
|
### Step-9: Create Front End with Streamlit |
|
- Create a front end for the application using Gradio. |
|
|
|
### Step-10: Upload Code to GitHub |
|
- Upload the code to a GitHub repository. |
|
|
|
### Step-11: Deploy App in Huggingface Spaces |
|
- Deploy the application in Huggingface Spaces. |
|
|
|
### Step-12: Create Documentation |
|
- Create documentation for the entire process followed. |
|
""" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from PyPDF2 import PdfReader |
|
from langchain.chains.question_answering import load_qa_chain |
|
from langchain.prompts import PromptTemplate |
|
from langchain.text_splitter import RecursiveCharacterTextSplitter |
|
from langchain.memory import ConversationBufferMemory |
|
from langchain.chains import ConversationalRetrievalChain |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import os |
|
huggingfacehub_api_token = os.environ("HF_API_TOKEN") |
|
|
|
def get_pdf_text(pdf_docs): |
|
text="" |
|
for pdf in pdf_docs: |
|
pdf_reader= PdfReader(pdf) |
|
for page in pdf_reader.pages: |
|
text+= page.extract_text() |
|
return text |
|
|
|
|
|
def get_text_chunks(text): |
|
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=100) |
|
chunks = text_splitter.split_text(text) |
|
return chunks |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import sentence_transformers |
|
from langchain.embeddings import HuggingFaceEmbeddings |
|
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2") |
|
|
|
|
|
|
|
PINECONE_API_KEY=os.environ.get('PINECONE_API_KEY', 'f7384d73-ea97-45ca-abaa-9b14327fd50f') |
|
PINECONE_API_ENV=os.environ.get('PINECONE_API_ENV', 'gcp-starter') |
|
|
|
import pinecone |
|
|
|
pinecone.init( |
|
api_key=PINECONE_API_KEY, |
|
environment=PINECONE_API_ENV |
|
) |
|
index_name = "pinecone-demo" |
|
|
|
from langchain.vectorstores import Pinecone |
|
|
|
|
|
def get_vector_store(text_chunks): |
|
|
|
docsearch = Pinecone.from_texts([t for t in text_chunks], embeddings, index_name=index_name) |
|
return docsearch |
|
|
|
|
|
|
|
|
|
|
|
|
|
from langchain import HuggingFaceHub |
|
|
|
llm=HuggingFaceHub(huggingfacehub_api_token=huggingfacehub_api_token,repo_id="mistralai/Mixtral-8x7B-Instruct-v0.1") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
memory = ConversationBufferMemory( |
|
memory_key="chat_history", |
|
return_messages=True, max_history_length=5 |
|
) |
|
|
|
|
|
import streamlit as st |
|
|
|
|
|
|
|
|
|
|
|
if 'chat_history' not in st.session_state: |
|
st.session_state['chat_history'] = [] |
|
|
|
def user_input(user_question): |
|
|
|
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2") |
|
|
|
|
|
docsearch = Pinecone.from_existing_index(index_name, embeddings) |
|
docs = docsearch.similarity_search(user_question) |
|
|
|
|
|
template = """Answer the question as detailed as possible from the provided context, make sure to provide all the details, |
|
if the answer is not available in the provided context just say, "answer is not available in the context", don't provide the wrong answer\n\n |
|
{context} |
|
Donot provide the Context , Provide the Answer only , to the question in the following format |
|
Question: {question} |
|
Helpful Answer:""" |
|
prompt = PromptTemplate(input_variables=["context", "question"], template=template) |
|
|
|
|
|
|
|
retriever = docsearch.as_retriever() |
|
qa_chain = ConversationalRetrievalChain.from_llm( |
|
llm, |
|
retriever=retriever, |
|
memory=memory |
|
) |
|
|
|
|
|
|
|
|
|
context = docs |
|
|
|
|
|
query = f"{template.format(context=context, question=user_question)}\nQuestion: {user_question}" |
|
|
|
|
|
response = qa_chain( |
|
{"question": query}, |
|
return_only_outputs=True |
|
) |
|
|
|
|
|
|
|
Ans = extract_helpful_answer(response) |
|
st.write(Ans) |
|
|
|
|
|
if st.button("Load Chat History"): |
|
|
|
st.session_state['chat_history'].append(("you",user_question)) |
|
|
|
|
|
|
|
st.session_state['chat_history'].append(("AI Assistant",Ans)) |
|
st.subheader("The chat history is ") |
|
for role,text in st.session_state['chat_history']: |
|
st.write(f"{role}: {text}") |
|
|
|
|
|
if st.button("Load Related Context from Your Document"): |
|
related_context = docs |
|
st.subheader("Related Context from Your Document:") |
|
for doc in related_context: |
|
st.write(f"Document: {doc}") |
|
st.write("\n") |
|
else: |
|
st.warning("Please enter a question before loading related context.") |
|
|
|
def extract_helpful_answer(response): |
|
|
|
parts = response["answer"].split("Helpful Answer:") |
|
|
|
|
|
return parts[2].strip() |
|
|
|
|
|
|
|
|
|
def main(): |
|
|
|
st.header("Chat with PDF using Mistral") |
|
|
|
user_question = st.text_input("Ask a Question from the PDF Files") |
|
|
|
if user_question: |
|
user_input(user_question) |
|
|
|
with st.sidebar: |
|
st.title("Menu:") |
|
pdf_docs = st.file_uploader("Upload your PDF Files and Click on the Submit & Process Button", accept_multiple_files=True) |
|
if st.button("Submit & Process"): |
|
with st.spinner("Processing..."): |
|
raw_text = get_pdf_text(pdf_docs) |
|
text_chunks = get_text_chunks(raw_text) |
|
get_vector_store(text_chunks) |
|
st.success("Done") |
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
main() |
|
|
|
|