Skip to content

pgvector/pgvector-swift

Repository files navigation

pgvector-swift

pgvector support for Swift

Supports PostgresNIO and PostgresClientKit

Build Status

Getting Started

Follow the instructions for your database library:

Or check out an example:

PostgresNIO

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 PgvectorNIO

Enable 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

PostgresClientKit

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 PgvectorClientKit

Enable 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

Reference

Vectors

Create a vector from an array

letvec=Vector([1,2,3])

Half Vectors

Create a half vector from an array

letvec=HalfVector([1,2,3])

Sparse Vectors

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.dim

Get the indices and values of non-zero elements

letindices= vec.indices letvalues= vec.values

History

View the changelog

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

To run an example:

cd Examples/Ollama createdb pgvector_example swift run

About

pgvector support for Swift

Resources

License

Security policy

Stars

Watchers

Forks

Packages

No packages published

Languages