Featured image of post Setup for Db2 vector for similarity search

Setup for Db2 vector for similarity search

Db2 features new vector capabilities that support use cases like in-database similarity search or RAG close to production data. Here are the setup instructions for my tests.

In my recent blog post Db2 vector for similarity search, I discussed a demo that I built, Jupyter Notebook to showcase Db2 VECTOR data type and similarity search. In that blog post I promised to follow up with a write-up on the setup. Here we go…

Components for Db2 vector tests

For my tests of the Db2 vector data type and its vector-related functionality, I used the following components:

The notebook acts as database application and runs SQL statements against my Db2 test database. The app utilizes an ollama service to turn product features (selected parts of a data record) into a vector, a so-called embedding.

Diagram showing the components like Jupyter Notebook, ollama, Db2 database

It is important to note that all components run locally, but could be deployed to a (public or private) cloud or run on separate machines. I use the free Db2 Community Edition for Docker, it is important to have Db2 12.1.2 or higher.

Setup instructions

To set up the environment, I recommend these steps:

  • Make sure to have Db2 12.1.2 (mod pack 2) or higher accessible.
  • Download ollama
  • Use ollama to download (pull) the IBM Granite Embedding model: ollama pull hf.co/ibm/granite-embedding-107m-multilingual
  • Install Python packages: pip install -r requirements.txt. I tested with Python 3.13. Consider a virtual Python environment.
  • Configure access to Db2 using the file .db2conn. The values are used to point to your Db2 instance and database, and to connect with the specified username and password.
  • Configure .env by copying over .env-sample, then adjusting the values as needed.

Configuration variables and defaults in .env:

EMBEDDING_MODEL='granite-embedding'  # Which embedding model to use
IMPORT_DATA=False                    # Generate or import the data? We generate by default
EXPORT_DATA=False                    # By default, no data is exported
KEEP_DATA=True                       # By default, the table is kept, so you could run your own queries against Db2

Once finished, you can run the Jupyter Notebook. I utilized the Visual Studio Code extension for Jupyter Notebooks. In that case, you need to point VS Code to your Python environment (see the docs).

Conclusions

Db2 has a built-in vector data type since version 12.1.2. You can use my and other existing code and reading material to get started. In this blog post, I provided setup instructions. For updates, you might want to check the README for the demo. It also has links for more background reading and demos.

If you are stuck or have ideas, open an issue for that GitHub repository or contact me through the usual channels, including Mastodon (@data_henrik@mastodon.social) or LinkedIn.