ballerinax/ai.pgvector Ballerina library

1.0.0

Overview

Pgvector is a PostgreSQL extension that introduces a vector data type and similarity search capabilities for working with embeddings.

The Ballerina pgvector module provides an API for integrating with the pgvector extension for PostgreSQL. Its implementation allows it to be used as a Ballerina AI ai:VectorStore, enabling users to store, retrieve, and search high-dimensional vectors.

Setup guide

You need a running PostgreSQL instance with the pgvector extension enabled. For that you can use the official pgvector Docker image.

Copy
docker run --name pgvector-db \
  -e POSTGRES_PASSWORD=mypassword \
  -e POSTGRES_DB=vector_db \
  -p 5432:5432 \
  -d pgvector/pgvector:pg17

To enable the pgvector extension, connect to the database and execute the following query.

Copy
CREATE EXTENSION IF NOT EXISTS vector;

Quick Start

To use the pgvector vector store in your Ballerina project, modify the .bal file as follows.

Step 1: Import the module

Copy
import ballerina/ai;
import ballerinax/ai.pgvector;

Step 2: Initialize the Pgvector vector store

Copy
ai:VectorStore vectorStore = check new(
   host,
   user,
   password,
   database,
   tableName,
   configs = {
      vectorDimension: 1536
   }
);

Step 3: Invoke the operations

Copy
ai:Error? result = vectorStore.add(
   [
      {
         id: "1",
         embedding: [1.0, 2.0, 3.0],
         chunk: {
               'type: "text", 
               content: "This is a chunk"
         }
      }
   ]
);

ai:VectorMatch[]|ai:Error matches = vectorStore.query({
   embedding: [1.0, 2.0, 3.0],
   filters: {
      // optional metadata filters
   }
});

Import

import ballerinax/ai.pgvector;Copy

Other versions

1.0.0

Metadata

Released date: 5 days ago

Version: 1.0.0

License: Apache-2.0


Compatibility

Platform: any

Ballerina version: 2201.12.0

GraalVM compatible: Yes


Pull count

Total: 13

Current verison: 13


Weekly downloads


Source repository


Keywords

pgvector

vector database

vector search


Contributors