Notebooks
E
Elastic
02 Bit Vectors

02 Bit Vectors

openai-chatgptlangchain-pythonchatgptgenaielasticsearchelasticopenaiAIcolpalichatlogvectordatabasePythonsearchgenaistacksupporting-blog-contentvectorelasticsearch-labslangchainapplications

Scalable late interaction vectors in Elasticsearch: Bit Vectors

In this notebook, we will be looking at how to convert late interaction vectors to bit vectors to

  1. Save siginificant disk space
  2. Lower query latency

We will also look at how we can use hamming distance to speed our queries up even further.
This notebook builds on part 1 where we downloaded the images, created ColPali vectors and saved them to disk. Please execute this notebook before trying the techniques in this notebook.

Also check out our accompanying blog post on Scaling Late Interaction Models for more context on this notebook.

This is the key part of this notebook. We use the to_bit_vectors() function to convert our vectors into bit vectors.
The function is simple in essence. Values > 0 are converted to 1, values < 0 are converted to 0. We then convert our array of 0s and 1s to a hex string, that represents our bit vector.
So don't be surprised that the values that we will be indexing look like strings and not arrays as before. This is intended!

Learn more about bit vectors and hamming distance in our blog about this topic.

[1]

Here we are defining our mapping for our Elasticsearch index. Note how we set the element_type parameter to bit to inform Elasticsearch that we will be indexing bit vectors in this field.

[2]
[INFO] Creating index: searchlabs-colpali-hamming
[3]
Indexing documents:   0%|          | 0/500 [00:00<?, ?it/s]
Completed indexing 500 documents
[4]
Loading checkpoint shards:   0%|          | 0/2 [00:00<?, ?it/s]
[5]
{"_source": false, "query": {"script_score": {"query": {"match_all": {}}, "script": {"source": "maxSimInvHamming(params.query_vector, 'col_pali_vectors')", "params": {"query_vector": ["7747bcd9732859c3645aa81036f5c960", "729b3c418ba8594a67daa042eca1c961", "609e3d8a2ac379c2204aa0cfa8345bdc", "30bf378a2ac279da245aa8dfa83c3bdc", "64af77ea2acdf9c28c0aa5df863677f4", "686f3fce2ac871c26e6aaddf023455ec", "383f31a8e8c0f8ca2c4ab54f047c7dec", "203b33caaac279da0acaa54f8a3c6bcc", "319a63eba8d279ca30dbbccf8f757b8e", "203b73ca28d2798a325bb44f8c3c5bce", "203bb7caa8d2718a1a4bb14f8a3c5bdc", "203bb7caa8d2798a1a6aa14f8a3c5fdc", "303b33caa8d2798a0a4aa14f8a3c5bdc", "303b33caaad379ca0e4aa14f8a3c5bdc", "709b33caaac379ca0c4aa14f8a3c5fdc", "708e37eaaac779ca2c4aa1df863c1fdc", "648e77ea6acd79caac4ae1df86363ffc", "648e77ea6acdf9caac4ae5df06363ffc", "608f37ea2ac579ca2c4ea1df063c3ffc", "709f37c8aac379ca2c4ea1df863c1fdc", "70af31c82ac671ce2c6ab14fc43c1bfc"]}}}}, "size": 5}

Above we have seen how to query our data using the maxSimInvHamming(...) function.
We can also just pass the full fidelity col pali vector and use the maxSimDotProduct(...) function for asymmetric similarity between the vectors.

[6]
[ ]