pgvector support for Swift
Supports PostgresNIO and PostgresClientKit
Follow the instructions for your database library:
Or check out an example:
- Embeddings with OpenAI
- Binary embeddings with Cohere
- Hybrid search with Ollama
- Sparse search with Text Embeddings Inference
Add to your application’s Package.swift
dependencies: [ + .package(url: "https://github.com/pgvector/pgvector-swift", from: "0.1.0") ], targets: [ .executableTarget(name: "App", dependencies: [ + .product(name: "Pgvector", package: "pgvector-swift"),+ .product(name: "PgvectorNIO", package: "pgvector-swift") ]) ]Import the packages
import Pgvector import PgvectorNIOEnable the extension
tryawait client.query("CREATE EXTENSION IF NOT EXISTS vector")Register the types
tryawaitPgvectorNIO.registerTypes(client)Create a table
tryawait client.query("CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3))")Insert vectors
letembedding1=Vector([1,1,1])letembedding2=Vector([2,2,2])letembedding3=Vector([1,1,2])tryawait client.query("INSERT INTO items (embedding) VALUES (\(embedding1)), (\(embedding2)), (\(embedding3))")Get the nearest neighbors
letembedding=Vector([1,1,1])letrows=tryawait client.query("SELECT id, embedding::text FROM items ORDER BY embedding <-> \(embedding) LIMIT 5")fortryawaitrowin rows {print(row)}Add an approximate index
tryawait client.query("CREATE INDEX ON items USING hnsw (embedding vector_l2_ops)") // or tryawait client.query("CREATE INDEX ON items USING ivfflat (embedding vector_l2_ops) WITH (lists = 100)")Use vector_ip_ops for inner product and vector_cosine_ops for cosine distance
See a full example
Add to your application’s Package.swift
dependencies: [ + .package(url: "https://github.com/pgvector/pgvector-swift", from: "0.1.0") ], targets: [ .executableTarget(name: "App", dependencies: [ + .product(name: "Pgvector", package: "pgvector-swift"),+ .product(name: "PgvectorClientKit", package: "pgvector-swift") ]) ]Import the packages
import Pgvector import PgvectorClientKitEnable the extension
lettext="CREATE EXTENSION IF NOT EXISTS vector"letstatement=try connection.prepareStatement(text: text)try statement.execute()Create a table
lettext="CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3))"letstatement=try connection.prepareStatement(text: text)try statement.execute()Insert vectors
lettext="INSERT INTO items (embedding) VALUES ($1), ($2), ($3)"letstatement=try connection.prepareStatement(text: text)try statement.execute(parameterValues:[Vector([1,1,1]),Vector([2,2,2]),Vector([1,1,2])])Get the nearest neighbors
lettext="SELECT * FROM items ORDER BY embedding <-> $1 LIMIT 5"letstatement=try connection.prepareStatement(text: text)letcursor=try statement.execute(parameterValues:[Vector([1,1,1])])forrowin cursor {letcolumns=try row.get().columns letid=trycolumns[0].int()letembedding=trycolumns[1].vector()print(id, embedding)}Add an approximate index
lettext="CREATE INDEX ON items USING hnsw (embedding vector_l2_ops)" // or lettext="CREATE INDEX ON items USING ivfflat (embedding vector_l2_ops) WITH (lists = 100)"letstatement=try connection.prepareStatement(text: text)try statement.execute()Use vector_ip_ops for inner product and vector_cosine_ops for cosine distance
See a full example
Create a vector from an array
letvec=Vector([1,2,3])Create a half vector from an array
letvec=HalfVector([1,2,3])Create a sparse vector from an array
letvec=SparseVector([1,0,2,0,3,0])Or a dictionary of non-zero elements
letvec=SparseVector([0:1,2:2,4:3], dim:6)!Note: Indices start at 0
Get the number of dimensions
letdim= vec.dimGet the indices and values of non-zero elements
letindices= vec.indices letvalues= vec.valuesView the changelog
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-swift.git cd pgvector-swift createdb pgvector_swift_test swift testTo run an example:
cd Examples/Ollama createdb pgvector_example swift run