Export Chonkie’s Chunks into a PostgreSQL database with pgvector.
The PgvectorHandshake class provides seamless integration between Chonkie’s chunking system and PostgreSQL with pgvector. It uses the vecs client library from Supabase underneath to provide a higher-level API with automatic indexing, metadata filtering, and simplified connection management.Store your Chonkie chunks in PostgreSQL with vector embeddings and perform semantic search without ever leaving the Chonkie SDK.
from chonkie import PgvectorHandshake# Initialize with individual connection parametershandshake = PgvectorHandshake( host="localhost", port=5432, database="your_database", user="your_user", password="your_password", collection_name="chonkie_chunks")# Or use a connection stringhandshake = PgvectorHandshake( connection_string="postgresql://user:password@localhost:5432/database")# Or use an existing vecs clientimport vecsclient = vecs.create_client("postgresql://user:password@localhost:5432/database")handshake = PgvectorHandshake(client=client, collection_name="my_collection")
Store your chunked text in PostgreSQL with vector embeddings
Copy
Ask AI
from chonkie import PgvectorHandshake, RecursiveChunker# Initialize the handshakehandshake = PgvectorHandshake( host="localhost", database="my_database", user="my_user", password="my_password")# Create some chunkschunker = RecursiveChunker(chunk_size=2048)chunks = chunker.chunk("Chonkie makes PostgreSQL vector search easy!")# Write chunks to PostgreSQLhandshake.write(chunks)
Searching Chunks
Find similar chunks using vector similarity search
Copy
Ask AI
# Search for similar chunksresults = handshake.search( query="PostgreSQL vector search", limit=5)for result in results: print(f"Text: {result['text']}") print(f"Similarity: {result['similarity']:.3f}") print("---")
Creating Indexes
Optimize search performance with vector indexes
Copy
Ask AI
# Create an HNSW index for better performancehandshake.create_index(method="hnsw")# Or create with custom parametershandshake.create_index( method="hnsw", m=16, ef_construction=64)