Skip to main content
LangChain’s Db2 integration (langchain-db2) provides vector store and vector search capabilities for working with IBM relational database Db2 version v12.1.2 and above, distributed under the MIT license. Users can use the provided implementations as-is or customize them for specific needs. Key features include:
  • Vector storage with metadata
  • Vector similarity search and max marginal relevance search, with metadata filtering options
  • Support for dot production, cosine, and euclidean distance metrics
  • Performance optimization by index creation and Approximate nearest neighbors search. (Will be added shortly)

Setup

Install package langchain-db2 which is the integration package for the db2 LangChain Vector Store and Search. The installation of the package should also install its dependencies like langchain-core and ibm_db.

Connect to Db2 vector store

The following sample code will show how to connect to Db2 Database. Besides the dependencies above, you will need a Db2 database instance (with version v12.1.2+, which has the vector datatype support) running.

Import the required dependencies

The langchain-community package is no longer maintained. Examples that import from langchain_community may be outdated or broken. Use with caution.

Initialization

Create documents

Create vector stores with different distance metrics

First we will create three vector stores each with different distance strategies. (You can manually connect to the Db2 Database and will see three tables : Documents_DOT, Documents_COSINE and Documents_EUCLIDEAN. )

Manage vector store

Query vector store

Demonstrate advanced searches on vector stores, with and without attribute filtering

With filtering, we only select the document id 101 and nothing else