Skip to content

pgvector/pgvector-swift

Repository files navigation

pgvector-swift

pgvector examples for Swift

Supports PostgresNIO and PostgresClientKit

Build Status

Getting Started

Follow the instructions for your database library:

PostgresNIO

Enable the extension

try await connection.query("CREATE EXTENSION IF NOT EXISTS vector", logger: logger)

Create a table

try await connection.query("CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3))", logger: logger)

Insert vectors

let embedding1 = "[1,1,1]"
let embedding2 = "[2,2,2]"
let embedding3 = "[1,1,2]"
try await connection.query("INSERT INTO items (embedding) VALUES (\(embedding1)::vector), (\(embedding2)::vector), (\(embedding3)::vector)", logger: logger)

Get the nearest neighbors

let embedding = "[1,1,1]"
let rows = try await connection.query("SELECT id, embedding::text FROM items ORDER BY embedding <-> \(embedding)::vector LIMIT 5", logger: logger)
for try await row in rows {
    print(row)
}

Add an approximate index

try await connection.query("CREATE INDEX ON items USING hnsw (embedding vector_l2_ops)", logger: logger)
// or
try await connection.query("CREATE INDEX ON items USING ivfflat (embedding vector_l2_ops) WITH (lists = 100)", logger: logger)

Use vector_ip_ops for inner product and vector_cosine_ops for cosine distance

See a full example

PostgresClientKit

Enable the extension

let text = "CREATE EXTENSION IF NOT EXISTS vector"
let statement = try connection.prepareStatement(text: text)
try statement.execute()

Create a table

let text = "CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3))"
let statement = try connection.prepareStatement(text: text)
try statement.execute()

Insert vectors

let text = "INSERT INTO items (embedding) VALUES ($1), ($2), ($3)"
let statement = try connection.prepareStatement(text: text)
try statement.execute(parameterValues: [ "[1,1,1]", "[2,2,2]", "[1,1,2]" ])

Get the nearest neighbors

let text = "SELECT * FROM items ORDER BY embedding <-> $1 LIMIT 5"
let statement = try connection.prepareStatement(text: text)
let cursor = try statement.execute(parameterValues: [ "[1,1,1]" ])

for row in cursor {
    let columns = try row.get().columns
    let id = try columns[0].int()
    let embedding = try columns[1].string()
    print(id, embedding)
}

Add an approximate index

let text = "CREATE INDEX ON items USING hnsw (embedding vector_l2_ops)"
// or
let text = "CREATE INDEX ON items USING ivfflat (embedding vector_l2_ops) WITH (lists = 100)"
let statement = 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

Contributing

Everyone is encouraged to help improve this project. Here are a few ways you can help:

To get started with development:

git clone https://github.com/pgvector/pgvector-swift.git
cd pgvector-swift
createdb pgvector_swift_test
swift test

About

pgvector examples for Swift

Resources

License

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages