pgvector examples for Scala
Supports Slick
Follow the instructions for your database library:
Add a vector column
class Items(tag: Tag) extends Table[(String)](tag, "items") {
def embedding = column[String]("embedding", O.SqlType("vector(3)"))
def * = (embedding)
}Insert a vector
val embedding = "[1,1,1]"
db.run(sqlu"INSERT INTO items (embedding) VALUES ($embedding::vector)")Get the nearest neighbors
val embedding = "[1,1,1]"
db.run(sql"SELECT * FROM items ORDER BY embedding <-> $embedding::vector LIMIT 5".as[(String)])Add an approximate index
db.run(sqlu"CREATE INDEX my_index ON items USING ivfflat (embedding vector_l2_ops)")Use vector_ip_ops for inner product and vector_cosine_ops for cosine distance
See a full example
Everyone is encouraged to help improve this project. Here are a few ways you can help:
- Report bugs
- Fix bugs and submit pull requests
- Write, clarify, or fix documentation
- Suggest or add new features
To get started with development:
git clone https://github.com/pgvector/pgvector-scala.git
cd pgvector-scala
createdb pgvector_scala_test
sbt run