from langchain.document_loaders import PyPDFLoader from langchain.text_splitter import RecursiveCharacterTextSplitter from langchain_community.embeddings.huggingface import HuggingFaceEmbeddings from langchain_community.vectorstores import Chroma from langchain_google_genai import ChatGoogleGenerativeAI from langchain.prompts import ChatPromptTemplate from langchain_core.output_parsers import StrOutputParser from langchain_core.runnables import RunnablePassthrough import streamlit as st # Loading and spliting the document pdf = PyPDFLoader("quran-in-modern-english.pdf") data = pdf.load() rs = RecursiveCharacterTextSplitter(chunk_size=1313, chunk_overlap=200) splits = rs.split_documents(data) # Initializing the embedding model em = HuggingFaceEmbeddings() # Creating vector data base vectordb = Chroma.from_documents(documents=splits, embedding=em) # Initializizing the LLM llm = ChatGoogleGenerativeAI(model="gemini-pro", temperature=0, google_api_key="AIzaSyBG8UJFmZnGq417gnWyoA-5mrTKBn1D1r0") #prompt template template = """ You are a helpful assistant to answer the queries. If you don't get an answer from the context, generate it by yourself. Use this context to answer the user's question. Context: {context} Question: {question} """ prompt_template = ChatPromptTemplate.from_template(template) #retriever retriever = vectordb.as_retriever() # Creating RAG chain rag_chain = ( {"context": retriever, "question": RunnablePassthrough()} | prompt_template | llm | StrOutputParser() ) # Streamlit app st.title("Quran Query Answering Bot") st.write("Ask your query, you will get an answer from the context of the Quran.") user_query = st.text_input("Enter your query:") if user_query: answer = rag_chain.invoke({"question": user_query}) st.write("Answer:", answer)