We use handcrafted 3-dimensional vectors here for clarity. In production, you would typically generate higher-dimensional embeddings using a machine learning model (for example, sentence or image encoders).
1) Setup: imports, paths, and model/table definition
Run the cell below to import dependencies, create a local directory for the DB, connect to LanceDB, and define a Pydantic-backed LanceModel schema for our fruit vectors.fruit_vectors in the local directory fruits_lancedb. If you point lancedb.connect() to a remote LanceDB instance, the DB files will be stored remotely and won’t appear in the local directory.
2) Insert handcrafted fruit vectors
Define each fruit as a 3-dimensional NumPy vector and add the records to the LanceDB table. We convert vectors to plain Python lists (vector.tolist()) before insertion because LanceDB stores JSON-like serializable data.
| Fruit | Vector (sweetness, sourness, texture) |
|---|---|
| papaya | [0.88, 0.35, 0.05] |
| peach | [0.81, 0.32, 0.12] |
| pear | [0.62, 0.51, 0.38] |
| pineapple | [0.15, 0.85, 0.79] |
| apple | [0.28, 0.42, 0.69] |
| apricot | [0.77, 0.44, 0.21] |
| guava | [0.10, 0.74, 0.58] |
| nectarine | [0.83, 0.30, 0.11] |
A local directory named
fruits_lancedb will be created and populated. If you run this demo multiple times, the table is dropped and recreated by prepare_table() to ensure a clean state.- Converting NumPy arrays via
vector.tolist()ensures the data is JSON-serializable for LanceDB. - Use
table.to_pandas()to inspect stored records as a pandas DataFrame.
3) Prepare the query vector (mango)
Construct the mango query vector. This vector is used only for querying and is not inserted into the table in this demo (unless you intentionally add it).- A LanceDB table
fruit_vectorscontaining handcrafted 3-dimensional fruit vectors. - A query vector
mango_contextready for similarity comparisons.
Next steps — similarity search examples
You can now run similarity searches (for example, cosine similarity, Euclidean distance, or dot product) against the stored vectors to see how the mango ranks among the fruits under different metrics. Typical steps:- Normalize vectors (if using cosine similarity).
- Run a nearest-neighbors query in LanceDB using the mango vector.
- Compare top-k results across different metrics to see how rankings change.
- Replace handcrafted vectors with model-generated embeddings (for example, text or image embeddings).
- Scale the table to higher-dimensional embeddings and larger datasets.
- Benchmark different similarity metrics (cosine vs. Euclidean vs. dot product) for your application.