Skip to main content

A lightweight semantic search engine using transformer embeddings.

Reason this release was yanked:

wrong author name

Project description

Search Engine - README

Overview

This Search Engine class provides a simple way to store, search, and remove sentences/phrases using embeddings generated from a pre-trained transformer model (all-MiniLM-L6-v2). It utilizes the Hugging Face AutoModel and AutoTokenizer for encoding sentences into dense vector embeddings, then allows for cosine similarity-based searches.

Features

  • Add Sentence: Add a sentence to the dataset if it's not already present.
  • Search: Find similar sentences based on cosine similarity.
  • Remove Sentence: Remove a sentence by exact match if present.
  • Clear All: Remove all stored data (vectors and sentences).

Requirements

  • Python >= 3.7
  • transformers library (for Hugging Face models)
  • torch (PyTorch)
  • scikit-learn (for cosine similarity)

Installation & Setup

  1. Clone the Repository: First, clone the repository to your local machine.

    git clone <repository_url>
    cd <repository_name>
    
  2. Install Required Dependencies: Install the necessary Python dependencies via pip:

    pip install -r requirements.txt
    

    Where requirements.txt includes:

    transformers==4.x
    torch==1.x
    scikit-learn==0.24
    
  3. Download Pre-trained Model: You can either download the model from Hugging Face and save it locally or use a pre-downloaded model. Make sure the model is located in ./my_local_model/all-MiniLM-L6-v2.

    • To download the model directly using transformers, run:
    python -c "from transformers import AutoModel, AutoTokenizer; AutoModel.from_pretrained('sentence-transformers/all-MiniLM-L6-v2'); AutoTokenizer.from_pretrained('sentence-transformers/all-MiniLM-L6-v2')"
    
  4. Run the Python Code: After setup, you can start using the search engine class by running the Python script that contains the Search_engine class.

    Example:

    python search_engine.py
    

Bash Script for Setup and Execution

#!/bin/bash

# Step 1: Install Python and required packages
echo "Checking Python version..."
python3 --version
if [ $? -ne 0 ]; then
    echo "Python3 is not installed. Installing Python3..."
    sudo apt update
    sudo apt install python3 python3-pip -y
fi

# Install dependencies
echo "Installing required dependencies..."
pip install -r requirements.txt

# Step 2: Download Pre-trained Model (if not already downloaded)
MODEL_DIR="./my_local_model/all-MiniLM-L6-v2"
if [ ! -d "$MODEL_DIR" ]; then
    echo "Model not found, downloading..."
    python -c "from transformers import AutoModel, AutoTokenizer; AutoModel.from_pretrained('sentence-transformers/all-MiniLM-L6-v2'); AutoTokenizer.from_pretrained('sentence-transformers/all-MiniLM-L6-v2')"
else
    echo "Model already downloaded at $MODEL_DIR."
fi

# Step 3: Run the Python script (assumes the script is named search_engine.py)
echo "Running the Search Engine..."
python search_engine.py

Code Walkthrough

Search_engine Class:

  1. Initialization (__init__):

    • Loads the model and tokenizer from the specified directory.
    • Initializes empty lists to store sentence vectors (vector_data) and sentences (base_data).
    • Sets a similarity threshold (threshold).
  2. get_cls_vector:

    • Converts a sentence to its vector representation using the pre-trained model.
    • Returns the embedding corresponding to the [CLS] token.
  3. similarity:

    • Calculates the cosine similarity between two vectors X and Y.
  4. add_one:

    • Converts a sentence to its vector and checks if a similar sentence already exists. If not, it stores the sentence and its vector.
  5. add_more:

    • Adds multiple sentences to the search engine.
  6. search_one:

    • Searches for the most similar sentences to a given input sentence, returning results that exceed the defined similarity threshold.
  7. remove_one:

    • Removes a sentence if it has an exact match based on similarity.
  8. remove_all:

    • Clears all stored sentences and vectors.

Example Usage

# Initialize the Search Engine
search_engine = Search_engine()

# Add sentences
search_engine.add_one("I love machine learning.")
search_engine.add_one("Artificial Intelligence is fascinating.")

# Search for similar sentences
results = search_engine.search_one("I enjoy AI.", metadata=True)
for score, sentence, index in results:
    print(f"Score: {score}, Sentence: {sentence}, Index: {index}")

# Remove a sentence
search_engine.remove_one("I love machine learning.")

# Clear all data
search_engine.remove_all()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

searchengine_v1-0.1.tar.gz (4.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

searchengine_v1-0.1-py3-none-any.whl (4.1 kB view details)

Uploaded Python 3

File details

Details for the file searchengine_v1-0.1.tar.gz.

File metadata

  • Download URL: searchengine_v1-0.1.tar.gz
  • Upload date:
  • Size: 4.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.12

File hashes

Hashes for searchengine_v1-0.1.tar.gz
Algorithm Hash digest
SHA256 2c447629e0e57a37c9b098ee1aaba6e83f0778425a1384869c70a86aceee1724
MD5 ddc2d10fa6b5997edf5d8689827d786b
BLAKE2b-256 a9ea3cce221c6d161ae890243827f68e3a442c508c040d0fdd45c744f9a3e8b0

See more details on using hashes here.

File details

Details for the file searchengine_v1-0.1-py3-none-any.whl.

File metadata

  • Download URL: searchengine_v1-0.1-py3-none-any.whl
  • Upload date:
  • Size: 4.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.12

File hashes

Hashes for searchengine_v1-0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6c99e7f899a739cd2b6de8f0a0b97642e5bff6455fe0fca3142c1761888cde5b
MD5 27b48d3cdc90d23d813a38678a4b1abf
BLAKE2b-256 6dd0300954f62f14a4020b300a6d60c7449577219a8fd5e00db916b053f0ca9c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page