This repository is a Crystal port of the rig-postgres Rust crate.
PostgreSQL vector store for Crystal with pgvector support, providing vector similarity search powered by the pgvector extension.
- Repository: https://github.com/0xPlaygrounds/rig.git
- Subdirectory: rig-integrations/rig-postgres
- Pinned ref: Same as parent crig project
-
Add the dependency to your
shard.yml:dependencies: crig-postgres: github: dsisnero/crig-postgres
-
Run
shards install
- PostgreSQL with pgvector extension installed
- Create extension in your database:
CREATE EXTENSION vector;
require "crig-postgres"
# Connect to PostgreSQL
db = DB.open("postgresql://localhost/mydb")
# Create a mock embedding model (or use a real one from crig)
class MyEmbeddingModel
include CrigPostgres::EmbeddingModel
def max_documents : Int32
100
end
def ndims : Int32
1536
end
def embed_texts(texts : Enumerable(String)) : Array(CrigPostgres::Embedding)
texts.map do |text|
vec = Array.new(ndims) { rand }
CrigPostgres::Embedding.new(text, vec)
end
end
end
model = MyEmbeddingModel.new
# Create vector store
store = CrigPostgres::PostgresVectorStore.new(
embedding_model: model,
db: db,
documents_table: "documents",
distance_function: CrigPostgres::PgVectorDistanceFunction::Cosine
)
# Create table with vector support
store.create_table(dims: 1536)
# Insert documents
documents = [{my_doc, [embedding]}]
store.insert_documents(documents)
# Search for similar documents
req = CrigPostgres::VectorSearchRequest(CrigPostgres::PgSearchFilter).builder
.query("search query")
.samples(5)
.build
results = store.top_n(req, MyDocumentType)The original Rust implementation provides:
PostgresVectorStorestruct with generic embedding model support- Multiple distance functions (L2, InnerProduct, Cosine, L1, Hamming, Jaccard)
PgSearchFilterfor composing SQL WHERE clauses- Integration with
pgvectorfor vector similarity search - Support for both full document and ID-only search results
make install # Install dependencies
make format # Format code
make lint # Run linter
make test # Run tests
make clean # Clean build artifacts- Fork it (https://github.com/dsisnero/crig-postgres/fork)
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create a new Pull Request
- Dominic Sisneros - creator and maintainer