Secure HyDe RAG is an in-house Retrieval-Augmented Generation (RAG) system designed to query complex technical documentation such as:
- SOPs (Standard Operating Procedures)
- RCA-CAPA reports
- Engineering Manuals
- Internal technical knowledge bases
Unlike traditional RAG systems that directly search documents using the user query, this project implements HyDe (Hypothetical Document Embeddings).
Instead of embedding the raw query, the system:
- Generates a hypothetical answer to the question.
- Embeds that hypothetical document.
- Uses it to retrieve semantically similar real documents.
This technique significantly improves retrieval accuracy when dealing with:
- Technical terminology
- Domain-specific language
- Short or ambiguous user queries
The system is built entirely with open-source models via Hugging Face APIs, making it suitable for secure internal deployments without relying on closed-source AI platforms.
Implements Hypothetical Document Embeddings (HyDe) to improve semantic search for technical documentation.
Runs on open-source LLMs such as:
google/flan-t5-largeMistral-7B
This ensures data control and internal security.
Uses FAISS (Facebook AI Similarity Search) for efficient local vector retrieval.
Built with Streamlit to provide:
- PDF upload capability
- Interactive querying
- Visualization of retrieval steps
Users can view:
- The AI-generated hypothetical document
- The actual retrieved source text
This helps understand how the system arrives at answers.
Traditional RAG systems perform:
Query → Embedding → Document Similarity Search
HyDe RAG performs:
Query → Hypothetical Answer → Embedding → Document Similarity Search
User Query
How do I recalibrate the pressure sensor?
The LLM generates a hypothetical answer:
"To recalibrate the pressure sensor, access the maintenance menu, select calibration mode, and apply zero-reference gas..."
This step helps create a context-rich representation of the user's intent.
The hypothetical document is embedded into a vector representation.
That vector is used to search the real document database using FAISS.
This retrieves the most semantically similar sections from the uploaded PDF.
The retrieved documents are passed back to the LLM to generate a factually grounded final response.
User Query
↓
Generate Hypothetical Answer
↓
Embed Hypothesis
↓
Vector Search (FAISS)
↓
Retrieve Relevant Documents
↓
Generate Final Answer
| Component | Technology |
|---|---|
| Language | Python |
| Framework | LangChain |
| Frontend | Streamlit |
| LLM Provider | Hugging Face Inference API |
| Generation Models | google/flan-t5-large, Mistral-7B |
| Embedding Model | sentence-transformers/all-MiniLM-L6-v2 |
| Vector Database | FAISS (Facebook AI Similarity Search) |
git clone https://github.com/yourusername/secure-hyde-rag.git
cd secure-hyde-ragpip install -r requirements.txtGet a free API access token from:
https://huggingface.co/settings/tokens
Then open app.py and replace the placeholder:
hf_api_key = "hf_xxxxxxxxxxxxxxxxxxxxxxxx"streamlit run app.py1️⃣ Open the local URL shown in the terminal (usually)
http://localhost:8501
2️⃣ Upload a PDF document
Examples:
- Technical manuals
- SOP documentation
- Research papers
3️⃣ Wait for the Processed! message.
4️⃣ Ask a question about the document.
5️⃣ Expand:
View Generated Hypothetical Document
to see the HyDe retrieval process in action.
Contributions are welcome!
If you'd like to improve the project:
- Fork the repository
- Create a feature branch
- Submit a Pull Request