A powerful Retrieval-Augmented Generation (RAG) application that allows you to upload PDF documents and ask natural language questions about their content. The application uses Mistral AI models for both embeddings and question answering.
- Upload and process multiple PDF documents
- Extract and chunk text from documents for efficient retrieval
- Generate vector embeddings using Mistral AI
- Ask natural language questions about document content
- Get accurate, context-specific responses from Mistral Large LLM
- View source references to verify information
- Export conversation history as text files
- Clean and intuitive user interface
This application implements a Retrieval-Augmented Generation (RAG) pipeline with the following components:
-
Document Processing:
- PDF text extraction using PyPDF2
- Text chunking with RecursiveCharacterTextSplitter (1000 token chunks with 200 token overlap)
- Embedding generation using Mistral AI's embedding model
- Vector storage in FAISS for efficient similarity search
-
Question Answering:
- User question is embedded using the same model
- Similar chunks are retrieved from the vector store (top 4 most relevant)
- Retrieved context is sent to Mistral Large LLM with a specialized prompt
- Response is generated using only the provided context
-
User Experience:
- Two-column layout with document management in sidebar
- Chat interface in main content area
- Source references for transparency
- Conversation history tracking and export
- Detailed error handling
- Clone this repository:
git clone <repository-url>
cd ask-docs- Install the required dependencies:
pip install -r requirements.txt- Create a
.envfile in the project root with your Mistral AI API key:
MISTRAL_API_KEY=your_mistral_api_key_here
You can obtain a Mistral AI API key from the Mistral AI Platform.
This application can be run using Docker, which simplifies setup and deployment across different environments.
- Docker and Docker Compose installed on your system
- A Mistral AI API key
- Create a
.envfile in the project root with your Mistral AI API key:
MISTRAL_API_KEY=your_mistral_api_key_here
- Build and start the application:
docker-compose up -d-
Access the application at http://localhost:8501
-
To stop the application:
docker-compose down- Build the Docker image:
docker build -t askdocs .- Run the container:
docker run -p 8501:8501 -e MISTRAL_API_KEY=your_api_key_here -v $(pwd)/faiss_index:/app/faiss_index askdocs- Access the application at http://localhost:8501
The Docker setup includes a volume to persist the FAISS index between container restarts. This means you only need to process your documents once, and they'll remain available even if you restart the container.
- Start the application:
streamlit run app.py-
The application will open in your default web browser (typically at http://localhost:8501)
-
Upload PDF documents:
- Use the file uploader in the sidebar
- Click "Process Documents" to extract text and generate embeddings
- Wait for processing to complete
-
Ask questions about your documents:
- Type your question in the text input field
- View the AI-generated answer based on your documents
- Toggle "Show Source Documents" to see which parts of the document were used
-
Manage your conversation:
- Use "Clear Chat History" to start a new conversation
- Download chat history using the provided link
The application is built with the following components:
- Frontend: Streamlit for the web interface
- Document Processing: PyPDF2 for PDF parsing
- Text Chunking: LangChain's RecursiveCharacterTextSplitter
- Embeddings: Mistral AI's embedding model (mistral-embed)
- Vector Database: Facebook AI Similarity Search (FAISS)
- Question Answering: Mistral Large LLM via LangChain
- The application uses
allow_dangerous_deserialization=Truewhen loading the FAISS index, which is safe in this context as the index is created within the application. - Your Mistral AI API key is loaded from the
.envfile and is not exposed in the UI. - For deployment, ensure you set up proper environment variable management for your API key.
You can modify the following parameters in the code:
chunk_sizeandchunk_overlapin theget_text_chunksfunction- Number of retrieved chunks (
k) in theanswer_questionfunction - Temperature and max tokens in the
get_qa_chainfunction - Prompt template for the LLM in the same function
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.
- LangChain for the RAG components
- Mistral AI for the AI models
- Streamlit for the web framework
- FAISS for the vector database