What is Vertex AI Vector Search?

Vector Search helps find similar items from huge collections, even if the items are not exactly the same but are related in meaning. It’s like finding songs or movies that feel the same or have the same vibe, even if the titles or keywords are different.

Here are some real-world examples of where it’s useful:

  • Recommendation engines: Suggesting products, songs, or movies that are similar to the ones you’ve liked before.
  • Search engines: Finding results that match the meaning of what you’re looking for, not just the exact words.
  • Chatbots: Understanding the intent behind what you type and giving meaningful responses.
  • Text classification: Automatically sorting and tagging content based on its meaning.
  • Image similarity: Finding pictures that are visually similar to a given image, like when you search for similar products or items by uploading a picture.

In my case, I use Vector Search for checking image similarity. It helps me reduce duplicate customer images in my database, making it easier to manage and keep my data clean.

The Difference between Batch Mode and Stream Mode in Vertex AI Vector Search

When performing Vector Search, there are two primary ways to handle your data: Batch Mode and Stream Mode. Both have their own strengths, depending on the needs of your application.

Batch Mode

In Batch Mode, data is processed in large chunks or batches. This means you send a group of data points all at once for indexing or searching, and the system processes them together. It’s a good option when:

  • Efficiency is important for processing large datasets all at once.
  • You have scheduled data processing where data updates or changes are handled periodically.
  • The system can handle a delay in processing because the focus is on working through a large set of data in one go.

For example, you might use Batch Mode when performing nightly updates to your vector database, processing thousands or millions of new data points together.

Stream Mode

In Stream Mode, data is processed in real-time or near real-time as it becomes available. Instead of waiting to accumulate data in batches, each data point is handled as soon as it is received. Stream Mode is ideal when:

  • You need instantaneous or real-time results.
  • The application requires immediate updates, such as live recommendation systems or fraud detection.
  • Low latency is crucial, and you can’t afford delays in processing.

For example, in my case, I use Stream Mode for checking image similarity. Every time a new customer image is uploaded, it’s instantly checked against existing images in the database to prevent duplicates in real-time.

CRUD Operations for Vector Search on Stream Mode

With stream mode, you can update and query your index within seconds. However, streaming updates cannot be applied to an index created with batch mode. You’ll need to create a new index specifically for stream mode operations.

Insert

In Vector Search, adding or updating data is known as upsert datapoint. You can insert new datapoints using a curl request, like this:

curl -X POST -H “Content-Type: application/json” -H “Authorization: Bearer `gcloud auth print-access-token`” \

https://${REGION}-aiplatform.googleapis.com/v1/projects/${PROJECT_ID}/locations/${REGION}/indexes/${INDEX_ID}:upsertDatapoints \

-d ‘{datapoints: [{datapoint_id: “111”, feature_vector: [0.111, 0.111], “sparse_embedding”: {“values”: [111.0, 111.1, 111.2], “dimensions”: [10, 20, 30]}}]}’

If the upsert datapoint is successful, the response will be an empty object ({}) and status 200. So, it’s important to handle this response correctly in your application.

In my experience with this insert method, there are a few important things to keep in mind:

  • datapoint_id is the primary key for the embedding, and it accepts not just numbers but also characters since the input is treated as a string. I’ve tested sending datapoint IDs like “aksjdn” and “4a”, and the response was successful. To verify this, I performed a search for the inserted datapoint IDs (which I’ll explain how to do later), and the system returned the correct string. For consistency in my case, I decided to wrap the datapoint_id so it only accepts numbers.
  • This insert method does not check if the datapoint_id already exists. If you accidentally insert the same datapoint_id, it will simply overwrite the previous data with the new insert. In my setup, I perform a check on the datapoint_id before inserting to avoid unintentional overwrites. However, if your use case requires updating existing data, you can allow it to overwrite as needed.

Read

There are two methods to read the datapoints that you’ve inserted into the index in Vector Search. The first method is to search for similarity using the dense embedding, and the second is to directly search for the datapoint itself.

  1. Searching for Similarity

In this method, you provide a feature vector (embedding), and the system will search for the most similar datapoints in the index. This is useful when you want to find items that are closely related based on their vector representation. You can send a curl request similar to the insert operation but this time for similarity search:

curl -X POST -H “Authorization: Bearer $(gcloud auth print-access-token) \

“https://${PUBLIC-ENDPOINT-NAME}/v1/projects/${PROJECT-NUMBER}/locations/${REGION}/indexEndpoints/${ENDPOINT-ID}:findNeighbors” \

-d ‘{deployedIndexId:”<DEPLOYED-INDEX-ID>”, “queries”:[{datapoint:{“featureVector”:”<FEATURE_VECTOR>”}}], returnFullDatapoint:false}’

  1. Searching for the Datapoint

The second method is to search for a specific datapoint by its datapoint_id. This is handy if you want to verify whether a particular datapoint exists or retrieve its details. You can use a similar curl request to fetch the exact datapoint:

curl -X POST -H “Authorization: Bearer $(gcloud auth print-access-token) \

“https://${PUBLIC-ENDPOINT-NAME}/v1/projects/${PROJECT-NUMBER}/locations/${REGION}/indexEndpoints/${ENDPOINT-ID}:findNeighbors” \

-d ‘{deployedIndexId:”<DEPLOYED-INDEX-ID>”, “queries”:[{datapoint:{“datapointId”:”<DATA_POINT_ID>”}}], returnFullDatapoint:false}’

Delete

To remove datapoints from the vector search index, you can use the delete operation. This allows you to delete specific datapoints by their datapoint_id. Here’s the curl command for deleting one or more datapoints:

curl -X POST -H “Content-Type: application/json” -H “Authorization: Bearer `gcloud auth print-access-token`” \

https://${REGION}-aiplatform.googleapis.com/v1/projects/${PROJECT_ID}/locations/${REGION}/indexes/${INDEX_ID}:removeDatapoints \

-d ‘{

 “datapoint_ids”: [“‘{DATAPOINT_ID_1}‘”, “‘{DATAPOINT_ID_2}‘”]

}’

In this command:

  • datapoint_ids: You provide the IDs of the datapoints you want to delete. This can be one or multiple datapoints.
  • If the deletion is successful, the response will be an empty object ({}). You should handle this response properly in your code to confirm that the deletion was completed successfully.

In my case, I use this method to clean up duplicate or outdated data. However, it’s important to be cautious — once a datapoint is deleted, it’s permanently removed from the index. I recommend verifying the datapoints before deletion to avoid accidental data loss.

Conclusion

Implementing Vector Search CRUD operations in stream mode on GCP’s Vertex AI provides powerful and flexible tools for managing and querying large datasets in real time. By allowing you to insert, search, and delete datapoints efficiently, stream mode is perfect for applications where immediate updates and low latency are essential, such as recommendation systems, chatbots, and image similarity checks.

In my experience, I’ve found that stream mode simplifies operations like preventing duplicate entries and managing data integrity, especially when working with image similarity. However, it’s crucial to handle responses carefully, especially when overwriting data or managing deletions, to ensure you maintain data consistency.

I hope this guide helped you get a better understanding of how to use Vector Search in stream mode! If you have any feedback, questions, or tips from your own experience, feel free to drop them in the comments. I’d love to hear from you!


Leave a Comment

Your email address will not be published. Required fields are marked *