LogoLogo
API ReferenceGitHubSlackService StatusLogin
v3.6.1
v3.6.1
  • Deep Lake Docs
  • Vector Store Quickstart
  • Deep Learning Quickstart
  • Storage & Credentials
    • Storage Options
    • User Authentication
    • Storing Deep Lake Data in Your Own Cloud
      • Microsoft Azure
        • Provisioning Federated Credentials
      • Amazon Web Services
        • Provisioning Role-Based Access
        • Enabling CORS
  • List of ML Datasets
  • API Reference
  • 🏢Enterprise Features
    • Compute Engine
      • Tensor Query Language (TQL)
        • TQL Syntax
        • Sampling Datasets
      • Performant Dataloader
    • Tensor Database
      • REST API
      • Migrating Datasets to the Tensor Database
  • 📚EXAMPLE CODE
  • Getting Started
    • Vector Store
      • Step 1: Hello World
      • Step 2: Creating Deep Lake Vector Stores
      • Step 3: Performing Search in the Vector Store
      • Step 4: Customizing Vector Stores
    • Deep Learning
      • Step 1: Hello World
      • Step 2: Creating Deep Lake Datasets
      • Step 3: Understanding Compression
      • Step 4: Accessing and Updating Data
      • Step 5: Visualizing Datasets
      • Step 6: Using Activeloop Storage
      • Step 7: Connecting Deep Lake Datasets to ML Frameworks
      • Step 8: Parallel Computing
      • Step 9: Dataset Version Control
      • Step 10: Dataset Filtering
  • Tutorials (w Colab)
    • Vector Store Tutorials
      • Vector Search Options
        • Deep Lake Vector Store API
        • REST API
        • LangChain API
      • Deep Lake Vector Store in LangChain
    • Creating Datasets
      • Creating Complex Datasets
      • Creating Object Detection Datasets
      • Creating Time-Series Datasets
      • Creating Datasets with Sequences
      • Creating Video Datasets
    • Training Models
      • Training an Image Classification Model in PyTorch
      • Training Models Using MMDetection
      • Training Models Using PyTorch Lightning
      • Training on AWS SageMaker
      • Training an Object Detection and Segmentation Model in PyTorch
    • Updating Datasets
    • Data Processing Using Parallel Computing
  • Playbooks
    • Querying, Training and Editing Datasets with Data Lineage
    • Evaluating Model Performance
    • Training Reproducibility Using Deep Lake and Weights & Biases
    • Working with Videos
  • API Summary
  • 🔬Technical Details
    • Best Practices
      • Creating Datasets at Scale
      • Training Models at Scale
      • Storage Synchronization and "with" Context
      • Restoring Corrupted Datasets
    • Data Layout
    • Version Control and Querying
    • Dataset Visualization
    • Tensor Relationships
    • Visualizer Integration
    • Shuffling in dataloaders
    • How to Contribute
Powered by GitBook
On this page
  • How to Search the Deep Lake Vector Store
  • Performing Vector Search
  • Customization of Vector Search
  • Full Customization of Vector Search

Was this helpful?

  1. Getting Started
  2. Vector Store

Step 3: Performing Search in the Vector Store

How to Search the Deep Lake Vector Store

Performing Vector Search

Deep Lake offers highly-flexible vector search and hybrid search options discussed in detail in this tutorial. Here we show a simple example of vector search using default options, which performs simple cosine similarity search in Python on the client (your machine).

prompt = "What do trust and safety models do?"

search_results = vector_store.search(embedding_data=prompt, embedding_function=embedding_function)

The search_results is a dictionary with keys for the text, score, id, and metadata, with data ordered by score. By default, the search returns the top 4 results which can be verified using:

len(search_results['text']) 

# Returns 4

If we examine the first returned text, it appears to contain the text about trust and safety models that is relevant to the prompt.

search_results['text'][0]

Returns:

Trust and Safety Models
=======================

We decided to open source the training code of the following models:
- pNSFWMedia: Model to detect tweets with NSFW images. This includes adult and porn content.
- pNSFWText: Model to detect tweets with NSFW text, adult/sexual topics.
- pToxicity: Model to detect toxic tweets. Toxicity includes marginal content like insults and certain types of harassment. Toxic content does not violate Twitter's terms of service.
- pAbuse: Model to detect abusive content. This includes violations of Twitter's terms of service, including hate speech, targeted harassment and abusive behavior.

We have several more models and rules that we are not going to open source at this time because of the adversarial nature of this area. The team is considering open sourcing more models going forward and will keep the community posted accordingly.

We can also retrieve the corresponding filename from the metadata, which shows the top result came from the README.

search_results['metadata'][0]

# Returns: {'filepath': '/the-algorithm/trust_and_safety_models/README.md'}

Customization of Vector Search

You can customize your vector search with simple parameters, such as selecting the distance_metric and top k results:

search_results = vector_store.search(embedding_data=prompt, 
                                     embedding_function=embedding_functiondding, 
                                     k=10,
                                     distance_metric='l2')

The search now returns 10 search results:

len(search_results['text']) 

# Returns: 10

The first search result with the L2 distance metric returns the same text as the previous Cos search:

search_results['text'][0]

Returns:

Trust and Safety Models
=======================

We decided to open source the training code of the following models:
- pNSFWMedia: Model to detect tweets with NSFW images. This includes adult and porn content.
- pNSFWText: Model to detect tweets with NSFW text, adult/sexual topics.
- pToxicity: Model to detect toxic tweets. Toxicity includes marginal content like insults and certain types of harassment. Toxic content does not violate Twitter's terms of service.
- pAbuse: Model to detect abusive content. This includes violations of Twitter's terms of service, including hate speech, targeted harassment and abusive behavior.

We have several more models and rules that we are not going to open source at this time because of the adversarial nature of this area. The team is considering open sourcing more models going forward and will keep the community posted accordingly.

Full Customization of Vector Search

Deep Lake's Compute Engine can be used to rapidly execute a variety of different search logic. It is available with !pip install "deeplake[enterprise]" (Make sure to restart your kernel after installation), and it is only available for data stored in or connected to Deep Lake.

Let's load a representative Vector Store that is already stored in Deep Lake's Managed Tensor Database. If data is not being written, is advisable to use read_only = True.

vector_store = DeepLakeVectorStore(
    path = "hub://activeloop/twitter-algorithm",
    read_only=True
)

The query should be constructed using the Tensor Query Language (TQL) syntax.

prompt = "What do trust and safety models do?"

embedding = embedding_function(prompt)[0]

# Format the embedding array or list as a string, so it can be passed in the REST API request.
embedding_string = ",".join([str(item) for item in embedding])

tql_query = f"select * from (select text, cosine_similarity(embedding, ARRAY[{embedding_string}]) as score) order by score desc limit 5"

Let's run the query in the Deep Lake Tensor Database (not on the client), by specifying the query parameter to the Vector Store search method and specifying exec_option = "tensor_db".

search_results = vector_store.search(query=tql_query, exec_option="tensor_db")

If we examine the first returned text, it appears to contain the same text about trust and safety models that is relevant to the prompt.

search_results['text'][0]

Returns:

Trust and Safety Models
=======================

We decided to open source the training code of the following models:
- pNSFWMedia: Model to detect tweets with NSFW images. This includes adult and porn content.
- pNSFWText: Model to detect tweets with NSFW text, adult/sexual topics.
- pToxicity: Model to detect toxic tweets. Toxicity includes marginal content like insults and certain types of harassment. Toxic content does not violate Twitter's terms of service.
- pAbuse: Model to detect abusive content. This includes violations of Twitter's terms of service, including hate speech, targeted harassment and abusive behavior.

We have several more models and rules that we are not going to open source at this time because of the adversarial nature of this area. The team is considering open sourcing more models going forward and will keep the community posted accordingly.

We can also retrieve the corresponding filename from the metadata, which shows the top result came from the README.

print(search_results['metadata'][0])

# Returns {'filepath': '/Users/istranic/ActiveloopCode/the-algorithm/trust_and_safety_models/README.md', 'extension': '.md'}

Deep Lake also offers a variety of search options depending on where data is stored (load, cloud, Deep Lake storage, etc.) and where query execution should take place (client side or server side)

PreviousStep 2: Creating Deep Lake Vector StoresNextStep 4: Customizing Vector Stores

Last updated 2 years ago

Was this helpful?