This CLI app let's users upload, process it, and ask questions on the content using a conversational chat powered by Google's Gemini-pro.
- Python 3.10 or higher
- PyPDF2
- Langchain
- Google Generative AI
- Google Cloud SDK
- Clone this repository to your local machine.
- Create a virtual environment and activate it.
- Install the required packages by running
pip install langchain google-generativeai faiss-cpu python-dotenv PyPDF2 langchain_google_genai
- Create a
.env
file in the root directory of the project and add your Google API key:
GOOGLE_API_KEY=your_google_api_key
- Run the application by executing
python new_app [file_name]
- change the file_name with the pdf name that you want to work on.
- After processing the PDF, the application will enter an interactive mode where you can ask questions.
- Purpose: This function extracts text from the uploaded PDF files.
- How It Works: It takes a list of PDF file paths, reads each file using
PyPDF2
, and concatenates all the text from each page. The resulting string contains the complete content from all PDFs combined.
- Purpose: This function splits the extracted text into smaller, manageable chunks.
- How It Works: Using the
RecursiveCharacterTextSplitter
from Langchain, the function divides the text into chunks of 10,000 characters each, with a 1,000-character overlap between consecutive chunks. This ensures that context is preserved across chunks for better question answering.
- Purpose: This function creates a vector database for the text chunks.
- How It Works: It uses the embeddings from Google Generative AI to transform each text chunk into a vector representation. These vectors are then stored using FAISS, which is a library for efficient similarity search. The vector database is saved locally as
"faisss_index"
.
- Purpose: This function creates a conversational chain to be used for answering questions.
- How It Works: It defines a prompt template for how to respond to user queries. It uses the
ChatGoogleGenerativeAI
model (gemini-pro
) and creates a question-answering chain (load_qa_chain
) that combines context and user input to generate a detailed response.
- Purpose: This function processes user input, searches the vector store, and generates a response.
- How It Works: When the user provides a question, this function retrieves relevant chunks from the FAISS vector store by performing a similarity search. It then uses the conversational chain to generate an answer based on the user question and the most relevant parts of the extracted PDF text.
- Purpose: This is the main function that sets up the CLI program and runs the user input process.
- How It Works: It uses
argparse
to parse command-line arguments (the list of PDF file paths). After extracting text, splitting it into chunks, and storing the vectors, it enters a loop where the user can ask questions about the content of the processed PDF files. The loop will continue until the user types'exit'
.