- Notifications
You must be signed in to change notification settings - Fork1
pgvector/pgvector-swift
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
pgvector support for Swift
SupportsPostgresNIO andPostgresClientKit
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’sPackage.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 Pgvectorimport 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)")// ortryawait client.query("CREATE INDEX ON items USING ivfflat (embedding vector_l2_ops) WITH (lists = 100)")
Usevector_ip_ops
for inner product andvector_cosine_ops
for cosine distance
See afull example
Add to your application’sPackage.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 Pgvectorimport 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().columnsletid=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)"// orlettext="CREATE INDEX ON items USING ivfflat (embedding vector_l2_ops) WITH (lists = 100)"letstatement=try connection.prepareStatement(text: text)try statement.execute()
Usevector_ip_ops
for inner product andvector_cosine_ops
for cosine distance
See afull 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.dim
Get the indices and values of non-zero elements
letindices= vec.indicesletvalues= vec.values
View thechangelog
Everyone is encouraged to help improve this project. Here are a few ways you can help:
- Report bugs
- Fix bugs andsubmit 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.gitcd pgvector-swiftcreatedb pgvector_swift_testswifttest
To run an example:
cd Examples/Ollamacreatedb pgvector_exampleswift run
About
pgvector support for Swift
Resources
License
Security policy
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.