Representing multi-modal data as vector embeddings is becoming a standard practice. Embedding functions can themselves be thought of as key part of the data processing pipeline that each request has to be passed through. The assumption here is: after initial setup, these components and the underlying methodology are not expected to change for a particular project.
For this purpose, LanceDB introduces an embedding functions API, that allow you simply set up once, during the configuration stage of your project. After this, the table remembers it, effectively making the embedding functions disappear in the background so you don't have to worry about manually passing callables, and instead, simply focus on the rest of your data engineering pipeline.
Using the embedding function registry means that you don't have to explicitly generate the embeddings yourself. However, if your embedding function changes, you'll have to re-configure your table with the new embedding function and regenerate the embeddings. In the future, we plan to support the ability to change the embedding function via table metadata and have LanceDB automatically take care of regenerating the embeddings.
1. Define the embedding function
In the LanceDB python SDK, we define a global embedding function registry with many different embedding models and even more coming soon. Here's let's an implementation of CLIP as example.
You can also define your own embedding function by implementing the
abstract base interface. It subclasses Pydantic Model which can be utilized to write complex schemas simply as we'll see next!
In the TypeScript SDK, the choices are more limited. For now, only the OpenAI embedding function is available.
2. Define the data model or schema
The embedding function defined above abstracts away all the details about the models and dimensions required to define the schema. You can simply set a field as source or vector column. Here's how:
VectorField tells LanceDB to use the clip embedding function to generate query embeddings for the
vector column and
SourceField ensures that when adding data, we automatically use the specified embedding function to encode
For the TypeScript SDK, a schema can be inferred from input data, or an explicit Arrow schema can be provided.
3. Create table and add data
Now that we have chosen/defined our embedding function and the schema, we can create the table and ingest data without needing to explicitly generate the embeddings at all:
4. Querying your table
Not only can you forget about the embeddings during ingestion, you also don't need to worry about it when you query the table:
Our OpenCLIP query embedding function supports querying via both text and images:
Or we can search using an image:
Both of the above snippet returns a pandas DataFrame with the 10 closest vectors to the query.
Rate limit Handling
EmbeddingFunction class wraps the calls for source and query embedding generation inside a rate limit handler that retries the requests with exponential backoff after successive failures. By default, the maximum retires is set to 7. You can tune it by setting it to a different number, or disable it by setting it to 0.
An example of how to do this is shown below:
Embedding functions can also fail due to other errors that have nothing to do with rate limits. This is why the error is also logged.
Some fun with Pydantic
LanceDB is integrated with Pydantic, which was used in the example above to define the schema in Python. It's also used behind the scenes by the embedding function API to ingest useful information as table metadata.
You can also use the integration for adding utility operations in the schema. For example, in our multi-modal example, you can search images using text or another image. Let's define a utility function to plot the image.
Now that you have the basic idea about LanceDB embedding functions and the embedding function registry, let's dive deeper into defining your own custom functions.