Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

pgvector support for Swift

License

NotificationsYou must be signed in to change notification settings

pgvector/pgvector-swift

Repository files navigation

pgvector support for Swift

SupportsPostgresNIO andPostgresClientKit

Build Status

Getting Started

Follow the instructions for your database library:

Or check out an example:

PostgresNIO

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

PostgresClientKit

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

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.indicesletvalues= vec.values

History

View thechangelog

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

Stars

Watchers

Forks

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp