|
import os |
|
import streamlit as st |
|
from dotenv import load_dotenv |
|
from langchain_community.embeddings import HuggingFaceEmbeddings |
|
from langchain_community.llms import llamacpp |
|
from langchain_core.runnables.history import RunnableWithMessageHistory |
|
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder |
|
from langchain_core.callbacks import CallbackManager, StreamingStdOutCallbackHandler |
|
from langchain.chains import create_history_aware_retriever, create_retrieval_chain, ConversationalRetrievalChain |
|
from langchain.document_loaders import TextLoader |
|
from langchain.chains.combine_documents import create_stuff_documents_chain |
|
from langchain_community.chat_message_histories.streamlit import StreamlitChatMessageHistory |
|
from langchain.prompts import PromptTemplate |
|
from langchain.vectorstores import Chroma |
|
from utills import load_txt_documents, split_docs, load_uploaded_documents, retriever_from_chroma |
|
from langchain.text_splitter import TokenTextSplitter, RecursiveCharacterTextSplitter |
|
from langchain_community.document_loaders.directory import DirectoryLoader |
|
from HTML_templates import css, bot_template, user_template |
|
|
|
|
|
data_path = "data" |
|
|
|
def create_retriever_from_chroma(data_path, vectorstore_path="docs/chroma/", search_type='mmr', k=7, chunk_size=250, chunk_overlap=20): |
|
model_name = "sentence-transformers/all-mpnet-base-v2" |
|
model_kwargs = {'device': 'cpu'} |
|
encode_kwargs = {'normalize_embeddings': True} |
|
|
|
|
|
embeddings = HuggingFaceEmbeddings( |
|
model_name=model_name, |
|
model_kwargs=model_kwargs, |
|
encode_kwargs=encode_kwargs |
|
) |
|
|
|
|
|
if os.path.exists(vectorstore_path) and os.listdir(vectorstore_path): |
|
|
|
vectorstore = Chroma(persist_directory=vectorstore_path,embedding_function=embeddings) |
|
else: |
|
|
|
documents = [] |
|
for filename in os.listdir(data_path): |
|
if filename.endswith('.txt'): |
|
file_path = os.path.join(data_path, filename) |
|
loaded_docs = TextLoader(file_path).load() |
|
documents.extend(loaded_docs) |
|
|
|
|
|
text_splitter = RecursiveCharacterTextSplitter(chunk_size=chunk_size, chunk_overlap=chunk_overlap) |
|
split_docs = text_splitter.split_documents(documents) |
|
|
|
|
|
if not os.path.exists(vectorstore_path): |
|
os.makedirs(vectorstore_path) |
|
|
|
|
|
vectorstore = Chroma.from_documents( |
|
documents=split_docs, embedding=embeddings, persist_directory=vectorstore_path |
|
) |
|
|
|
|
|
retriever = vectorstore.as_retriever(search_type=search_type, search_kwargs={"k": k}) |
|
return retriever |
|
|
|
|
|
|
|
|
|
|
|
|
|
def main(): |
|
|
|
st.set_page_config(page_title="Chat with multiple PDFs", |
|
page_icon=":books:") |
|
st.write(css, unsafe_allow_html=True) |
|
|
|
if "conversation" not in st.session_state: |
|
st.session_state.conversation = None |
|
if "chat_history" not in st.session_state: |
|
st.session_state.chat_history = None |
|
|
|
st.header("Chat with multiple PDFs :books:") |
|
|
|
with st.chat_message("Assistant"): |
|
st.write("Hello my name is Robert, how can i help you? ") |
|
user_question = st.text_input("Ask a question about your documents:") |
|
with st.chat_message("User"): |
|
st.write(user_question) |
|
if user_question: |
|
handle_userinput(user_question) |
|
|
|
|
|
def handle_userinput(user_question): |
|
retriever = create_retriever_from_chroma(data_path, vectorstore_path="docs/chroma/", search_type='mmr', k=7, chunk_size=250, chunk_overlap=20) |
|
docs = retriever.invoke(user_question) |
|
|
|
doc_txt = [doc.page_content for doc in docs] |
|
|
|
Rag_chain = create_conversational_rag_chain(retriever) |
|
response = rag_chain.invoke({"context": doc_txt, "question": user_question}) |
|
with st.chat_message("Assistant"): |
|
st.write(response) |
|
|
|
|
|
def create_conversational_rag_chain(retriever): |
|
|
|
model_path = ('qwen2-0_5b-instruct-q4_0.gguf') |
|
|
|
callback_manager = CallbackManager([StreamingStdOutCallbackHandler()]) |
|
|
|
llm = llamacpp.LlamaCpp( |
|
model_path=model_path, |
|
n_gpu_layers=1, |
|
temperature=0.1, |
|
top_p=0.9, |
|
n_ctx=22000, |
|
max_tokens=200, |
|
repeat_penalty=1.7, |
|
|
|
verbose=False, |
|
) |
|
|
|
template = """Answer the question based only on the following context: |
|
{context} |
|
|
|
Question: {question} |
|
""" |
|
prompt = ChatPromptTemplate.from_template(template) |
|
|
|
rag_chain = prompt | llm | StrOutputParser() |
|
|
|
|
|
return rag_chain |
|
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
main() |