View on GitHub

Dynamic Embeddings Model for different Domains

by Atharva K

Download this project as a .zip file Download this project as a tar.gz file

Dynamic Embedding Model for Retrieval-Augmented Generation (RAG)

This project implements a dynamic embedding system that classifies incoming queries into domains (e.g., Medical, Mathematics, Programming, Science, or General-Knowledge), re-embeds them using domain-specific models (when applicable), and indexes both query and document embeddings into ChromaDB for efficient retrieval. It demonstrates the complete workflow for a RAG pipeline—from dynamic embedding selection to document retrieval.

Table of Contents

Overview

In modern retrieval-augmented generation (RAG) pipelines, accurately fetching the most relevant context is critical for generating precise answers. To achieve this, our system:

Architecture and Workflow

High-Level Workflow

  1. Query Classification:
    • Input query is embedded using a universal model.
    • A Random Forest classifier predicts the query’s domain (e.g., Medical, Programming, etc.).
  2. Dynamic Query Re-embedding:
    • If the domain is not “General-Knowledge,” the query (and the related documents) are re-embedded using a domain-specific model.
  3. Document Indexing:
    • The system loads document data (via DuckDuckGo web loader) as JSON.
    • Each document’s content is preprocessed, embedded, and stored in ChromaDB along with metadata and the original text.
  4. Retrieval:
    • The query embedding is used to perform a similarity search in ChromaDB.
    • The top result (or results) are retrieved.
  5. Context Construction and Final Prompt:
    • The retrieved document’s text is concatenated with the original query to create a final prompt.
    • This prompt can then be passed to a language model to generate an answer, thereby completing the RAG pipeline.

Diagram

flowchart TD
  A[User Query] --> B[Universal Embedding]
  B --> C[RF Classifier]
  C -->|General-Knowledge| D[Use Universal Model for Q & Docs]
  C -->|Other Domains| E[Re-embed Query & Docs via Domain-Specific Model]
  D --> F[Index Query & Docs in ChromaDB]
  E --> F
  F --> G[Query ChromaDB for Similarity]
  G --> H[Retrieve Top Document]
  H --> I[Construct Final Prompt]
  I --> J[Pass Prompt to LLM for Generation]

##This final prompt is printed to the console, ready to be passed on to an LLM for answer generation

Features

Installation

First, clone this repository and install the required dependencies.

Jupyter Notebooks

Future Work

License

This project is licensed under the MIT License. See the LICENSE file for details.