Deep Lake Vector Store API
Running Vector Search in the Deep Lake Vector Store module.
Search Options for Deep Lake Vector Stores in the Deep Lake API
This tutorial requires installation of:
Vector Search on the Client
Let's load the same vector store used in the Quickstart and run embeddings search based on a user prompt using the Deep Lake Vector Store module. (Note: DeepLakeVectorStore
class is deprecated, but you can still use it. The new API for calling Deep Lake's Vector Store is: VectorStore
)
Next, let's define an embedding function using OpenAI. It must work for a single string and a list of strings so that it can be used to embed a prompt and a batch of texts.
Simple Vector Search
Let's run a simple vector search using default options, which performs a simple cosine similarity search in Python on the client.
The search_results
is a dictionary with keys for the text
, score
, id
, and metadata
, with data ordered by score. By default, it returns 4 samples ordered by similarity score, and if we examine the first returned text, it appears to contain the text about trust and safety models that is relevant to the prompt.
Returns:
Hybrid Search Using UDFs
Vector search can be combined with other search logic for performing more advanced queries. Let's define a function compatible with deeplake.filter for filtering data before the vector search. The function below will filter samples that contain the word "program"
in the text
tensor.
Let's run the vector search with the filter above, and return more samples (k = 10
), and perform similarity search using L2 metric (distance_metric = "l2"
):
We can verity that the word "program"
is present in all of the results:
UDFs are only supported with query execution using the Python engine, so in the search above, exec_option = "python"
should be specified.
Hybrid Search Using Metadata Filters
Instead of using UDFs, a filter can be specified using dictionary syntax. For json tensors, the syntax is filter = {"tensor_name": {"key": "value"}}
. For text tensors, it is filter = {"tensor": "value"}
. In all cases, an exact match is performed.
Hybrid Search using TQL Vector Search
Deep Lake offers advanced search that executes queries with higher performance in C++, and offers querying using Deep Lake's Tensor Query Language (TQL).
In order to use Compute Engine, Deep Lake data must be stored in Deep Lake Storage, or in the user's cloud while being connected to Deep Lake using Managed Credentials.
Let's load a larger Vector Store for running more interesting queries:
NOTE: this Vector Store is stored in us-east
, and query performance may vary significantly depending on your location. In real-world use-cases, users would store their Vector Stores in regions optimized for their use case.
Now let's run a search that includes filtering of text
, metadata
, and embedding
tensors. We do this using TQL by combining embedding search syntax (cosine_similarity(embedding, ...)
) and filtering syntax (where ....
).
We are interested in answering a prompt based on the question:
Therefore, we apply a filter to only search for text
that contains the word "python"
and metadata
where the source
key contains ".py"
.
Vector Search Using the Managed Tensor Database (Server-Side)
For Vector Stored in the Managed Tensor Database, queries will automatically execute on the database (instead of the client). Vector Stores are created in the Managed Tensor Database by specifying vector_store_path = hub://org_id/dataset_name
and runtime = {"tensor_db": True}
during Vector Store creation.
If Vector Stores are not in the Managed Tensor Database, they can be migrated using these steps:
Last updated