This repository contains the simplest way to setup and interact with a vector database in Azure PostgreSQL using the pgvector
extension. It's designed to essentially mimic Pinecone's Vector Database but with Azure PostgreSQL.
- Create Vector Tables: Easily create tables with vector and metadata columns.
- Upsert Vectors: Insert or update vectors with associated metadata.
- Query Vectors: Perform efficient nearest neighbor searches on vectors.
- Azure PostgreSQL database with
pgvector
extension enabled. - Python environment with the required packages installed.
pip install -r requirements.txt
- Open the
vector_database.py
file. - Fill in the
DB_PARAMS
dictionary with your database credentials.
Creates an index for storing vectors and metadata.
from vector_database import create_index
# Create an index with dimension of 1536
table_name = create_index(name='my_vector_index', dimension_size=1536)
Inserts or updates vectors with associated metadata.
from vector_database import upsert_vectors
# Upsert vectors to simulate OpenAI's embedding vectors
example_vectors = [
(1, [0.1] * 1536, json.dumps({"description": "Vector 1"})),
(2, [0.2] * 1536, json.dumps({"description": "Vector 2"})),
(3, [0.3] * 1536, json.dumps({"description": "Vector 3"})),
(4, [0.4] * 1536, json.dumps({"description": "Vector 4"}))
]
upsert_vectors(table_name, example_vectors)
from vector_database import query_vectors
# Query vectors
query_results = query_vector(table_name, [0.15] * 1536, limit=1)