From 25d5dda6b30af32f391654f804a721f557b45c6c Mon Sep 17 00:00:00 2001 From: Andrew Kane Date: Sun, 16 Feb 2025 14:48:56 -0800 Subject: [PATCH] Started package --- CHANGELOG.md | 3 +++ Sources/Pgvector/HalfVector.swift | 23 +++++++++++++++++++ Sources/Pgvector/Pgvector.swift | 0 Sources/Pgvector/Vector.swift | 23 +++++++++++++++++++ Tests/PgvectorTests/HalfVectorTests.swift | 14 +++++++++++ .../PostgresClientKitTests.swift | 5 ++-- Tests/PgvectorTests/PostgresNIOTests.swift | 1 + Tests/PgvectorTests/VectorTests.swift | 14 +++++++++++ 8 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 CHANGELOG.md create mode 100644 Sources/Pgvector/HalfVector.swift delete mode 100644 Sources/Pgvector/Pgvector.swift create mode 100644 Sources/Pgvector/Vector.swift create mode 100644 Tests/PgvectorTests/HalfVectorTests.swift create mode 100644 Tests/PgvectorTests/VectorTests.swift diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..fdbb576 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,3 @@ +## 0.1.0 (unreleased) + +- First release diff --git a/Sources/Pgvector/HalfVector.swift b/Sources/Pgvector/HalfVector.swift new file mode 100644 index 0000000..07d7fc1 --- /dev/null +++ b/Sources/Pgvector/HalfVector.swift @@ -0,0 +1,23 @@ +#if canImport(PostgresClientKit) +import PostgresClientKit +#endif + +struct HalfVector: Equatable { + var value: [Float16] + + init(_ value: [Float16]) { + self.value = value + } + + static func == (lhs: HalfVector, rhs: HalfVector) -> Bool { + return lhs.value == rhs.value + } +} + +#if canImport(PostgresClientKit) +extension HalfVector: PostgresValueConvertible { + public var postgresValue: PostgresValue { + return PostgresValue(String(describing: value)) + } +} +#endif diff --git a/Sources/Pgvector/Pgvector.swift b/Sources/Pgvector/Pgvector.swift deleted file mode 100644 index e69de29..0000000 diff --git a/Sources/Pgvector/Vector.swift b/Sources/Pgvector/Vector.swift new file mode 100644 index 0000000..a8d9b0c --- /dev/null +++ b/Sources/Pgvector/Vector.swift @@ -0,0 +1,23 @@ +#if canImport(PostgresClientKit) +import PostgresClientKit +#endif + +struct Vector: Equatable { + var value: [Float] + + init(_ value: [Float]) { + self.value = value + } + + static func == (lhs: Vector, rhs: Vector) -> Bool { + return lhs.value == rhs.value + } +} + +#if canImport(PostgresClientKit) +extension Vector: PostgresValueConvertible { + public var postgresValue: PostgresValue { + return PostgresValue(String(describing: value)) + } +} +#endif diff --git a/Tests/PgvectorTests/HalfVectorTests.swift b/Tests/PgvectorTests/HalfVectorTests.swift new file mode 100644 index 0000000..5e53790 --- /dev/null +++ b/Tests/PgvectorTests/HalfVectorTests.swift @@ -0,0 +1,14 @@ +import Foundation +import Testing +@testable import Pgvector + +final class HalfVectorTests { + @Test func equatable() { + let a = HalfVector([1, 2, 3]) + let b = HalfVector([1, 2, 3]) + let c = HalfVector([1, 2, 4]) + #expect(a == a) + #expect(a == b) + #expect(a != c) + } +} diff --git a/Tests/PgvectorTests/PostgresClientKitTests.swift b/Tests/PgvectorTests/PostgresClientKitTests.swift index 0f7dc3e..a99141a 100644 --- a/Tests/PgvectorTests/PostgresClientKitTests.swift +++ b/Tests/PgvectorTests/PostgresClientKitTests.swift @@ -1,6 +1,7 @@ import Foundation import PostgresClientKit import Testing +@testable import Pgvector final class PostgresClientKitTests { @Test func example() throws { @@ -26,11 +27,11 @@ final class PostgresClientKitTests { text = "INSERT INTO items (embedding) VALUES ($1), ($2), ($3)" statement = try connection.prepareStatement(text: text) - try statement.execute(parameterValues: ["[1,1,1]", "[2,2,2]", "[1,1,2]"]) + try statement.execute(parameterValues: [Vector([1, 1, 1]), Vector([2, 2, 2]), Vector([1, 1, 2])]) text = "SELECT * FROM items ORDER BY embedding <-> $1 LIMIT 5" statement = try connection.prepareStatement(text: text) - let cursor = try statement.execute(parameterValues: ["[1,1,1]"]) + let cursor = try statement.execute(parameterValues: [Vector([1, 1, 1])]) for row in cursor { let columns = try row.get().columns diff --git a/Tests/PgvectorTests/PostgresNIOTests.swift b/Tests/PgvectorTests/PostgresNIOTests.swift index 36916e0..6ca644e 100644 --- a/Tests/PgvectorTests/PostgresNIOTests.swift +++ b/Tests/PgvectorTests/PostgresNIOTests.swift @@ -1,6 +1,7 @@ import Foundation import PostgresNIO import Testing +@testable import Pgvector final class PostgresNIOTests { @Test func example() async throws { diff --git a/Tests/PgvectorTests/VectorTests.swift b/Tests/PgvectorTests/VectorTests.swift new file mode 100644 index 0000000..d2dc832 --- /dev/null +++ b/Tests/PgvectorTests/VectorTests.swift @@ -0,0 +1,14 @@ +import Foundation +import Testing +@testable import Pgvector + +final class VectorTests { + @Test func equatable() { + let a = Vector([1, 2, 3]) + let b = Vector([1, 2, 3]) + let c = Vector([1, 2, 4]) + #expect(a == a) + #expect(a == b) + #expect(a != c) + } +}