Skip to content

Instantly share code, notes, and snippets.

@hocyadav
Last active July 11, 2024 07:20
Show Gist options
  • Save hocyadav/512c0d3d1e083ecca77cbfd63035e246 to your computer and use it in GitHub Desktop.
Save hocyadav/512c0d3d1e083ecca77cbfd63035e246 to your computer and use it in GitHub Desktop.

Your thinking is mostly correct. Here are some popular examples and their approaches:

  1. Google Search:

    • Approach: Semantic Search
    • Explanation: Uses natural language processing to understand the meaning of the query and find relevant documents, even if they don't contain the exact keywords.
    • Vector Database: Yes
  2. Google Image Search:

    • Approach: Similarity Search
    • Explanation: Finds images similar to the query image by comparing vector representations of images.
    • Vector Database: Yes
  3. Netflix Recommendations:

    • Approach: Semantic Search and Collaborative Filtering
    • Explanation: Uses user preferences and viewing history to recommend content that is similar to what users have previously enjoyed.
    • Vector Database: Yes

Other Examples:

  1. Spotify Song Recommendations:

    • Approach: Collaborative Filtering and Content-Based Filtering
    • Explanation: Suggests songs by analyzing users' listening habits and the characteristics of the songs, comparing vectors of audio features.
    • Vector Database: Yes
  2. Facebook Friend Suggestions:

    • Approach: Graph-Based Recommendations
    • Explanation: Uses social network analysis to suggest friends by finding connections and similarities between users.
    • Vector Database: Typically involves graph databases, but can also use vector databases for analyzing user connections and similarities.

These examples illustrate how different applications use various approaches to solve their specific problems.


Yes, you can think of Google's ScaNN (Scalable Nearest Neighbors) as somewhat similar to a RAG (retrieval-augmented generation) based approach in the context of using vector databases for semantic and similarity search. Both approaches involve:

  1. Vector Representations: Converting data into high-dimensional vectors.
  2. Similarity Search: Using these vectors to find the most relevant or similar items.

ScaNN specifically optimizes the process of finding similar vectors efficiently, which can be a part of a RAG system where you retrieve relevant documents (using similarity search) before generating an answer (using semantic understanding).


  • how embedding model works or create our own

Sure, here’s a simplified approach to training a new model for generating embeddings from a large document:

Steps to Train an Embedding Model:

  1. Prepare Data: Split your large document into sentences or smaller chunks. Clean the text by removing punctuation, converting to lowercase, etc.

  2. Tokenize: Break down the text into individual words or tokens.

  3. Build Vocabulary: Create a list of unique words (vocabulary) from your text.

  4. Initialize Model: Choose an embedding model, like Word2Vec. Initialize it with your vocabulary.

  5. Training: Train the model on your text. The model will learn word relationships by predicting a word from its context (neighboring words) or vice versa. This involves adjusting word vectors to minimize prediction errors.

  6. Generate Embeddings: After training, the model can convert any word into its corresponding vector (embedding).

Example:

  1. Input Document: "The cat sat on the mat. The dog lay beside the cat."
  2. Tokenize: ["the", "cat", "sat", "on", "the", "mat", "the", "dog", "lay", "beside", "the", "cat"]
  3. Vocabulary: {"the", "cat", "sat", "on", "mat", "dog", "lay", "beside"}
  4. Initialize Model: Word2Vec(vocabulary size=8, embedding size=100)
  5. Training: The model learns that "cat" and "mat" are often close together.
  6. Embeddings: "cat" -> [0.12, -0.33, 0.45, ...], "dog" -> [-0.22, 0.87, -0.41, ...]

By following these steps, you create a custom embedding model that can convert words from your document into numerical vectors, capturing their meanings and relationships.


In a retrieval-augmented generation (RAG) based architecture, where you store company documents and then ask questions, it involves both semantic search and similarity search:

  • Semantic Search: To understand the meaning of the question and find relevant documents that contain the answer. Similarity Search: To compare the vector representations of the question and documents to find the most similar content. Both approaches work together to retrieve and generate accurate and contextually relevant answers from your document repository.

RAG: Retrieval-Augmented Generation

Full Form: Retrieval-Augmented Generation

High-Level Architecture

  1. Document Retrieval:

    • Role: Fetch relevant documents from a database based on the input query.
    • Example: Using a search engine to find articles related to the query.
  2. Query Augmentation:

    • Role: Enhance the original query with information from the retrieved documents.
    • Example: Combining the query with key points from the fetched articles.
  3. Answer Generation:

    • Role: Use a generative model to produce a final answer using the augmented query.
    • Example: An AI model generates a coherent response incorporating the augmented information.

This architecture leverages external knowledge to improve the relevance, accuracy, and context of the generated answers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment