aboutsummaryrefslogtreecommitdiff
path: root/databases/py-sqlite-vec
diff options
context:
space:
mode:
Diffstat (limited to 'databases/py-sqlite-vec')
-rw-r--r--databases/py-sqlite-vec/Makefile40
-rw-r--r--databases/py-sqlite-vec/distinfo3
-rw-r--r--databases/py-sqlite-vec/files/__init__.py61
-rw-r--r--databases/py-sqlite-vec/files/pyproject.toml16
-rw-r--r--databases/py-sqlite-vec/files/sqlite-vec.h41
-rw-r--r--databases/py-sqlite-vec/pkg-descr5
6 files changed, 166 insertions, 0 deletions
diff --git a/databases/py-sqlite-vec/Makefile b/databases/py-sqlite-vec/Makefile
new file mode 100644
index 000000000000..3b6f6edaf10b
--- /dev/null
+++ b/databases/py-sqlite-vec/Makefile
@@ -0,0 +1,40 @@
+PORTNAME= sqlite-vec
+DISTVERSIONPREFIX= v
+DISTVERSION= 0.1.9
+CATEGORIES= databases python
+PKGNAMEPREFIX= ${PYTHON_PKGNAMEPREFIX}
+
+MAINTAINER= yuri@FreeBSD.org
+COMMENT= SQLite vector search extension with Python bindings
+WWW= https://github.com/asg017/sqlite-vec
+
+LICENSE= APACHE20 MIT
+LICENSE_COMB= dual
+LICENSE_FILE_APACHE20= ${WRKSRC}/LICENSE-APACHE
+LICENSE_FILE_MIT= ${WRKSRC}/LICENSE-MIT
+
+BUILD_DEPENDS= ${LOCALBASE}/include/sqlite3.h:databases/sqlite3 \
+ ${PYTHON_PKGNAMEPREFIX}setuptools>=61:devel/py-setuptools@${PY_FLAVOR} \
+ ${PYTHON_PKGNAMEPREFIX}wheel>0:devel/py-wheel@${PY_FLAVOR}
+
+USES= gmake python
+USE_PYTHON= pep517 autoplist pytest
+
+USE_GITHUB= yes
+GH_ACCOUNT= asg017
+
+post-extract:
+ @${MKDIR} ${WRKSRC}/sqlite_vec
+ @${INSTALL_DATA} ${FILESDIR}/__init__.py ${WRKSRC}/sqlite_vec/__init__.py
+ @${INSTALL_DATA} ${FILESDIR}/pyproject.toml ${WRKSRC}/pyproject.toml
+ @${INSTALL_DATA} ${FILESDIR}/sqlite-vec.h ${WRKSRC}/sqlite-vec.h
+
+pre-build:
+ @cd ${WRKSRC} && ${SETENV} ${MAKE_ENV} ${GMAKE} loadable \
+ VERSION=${DISTVERSION} \
+ CFLAGS="${CFLAGS} -I${LOCALBASE}/include -include sys/types.h"
+ @${CP} ${WRKSRC}/dist/vec0.so ${WRKSRC}/sqlite_vec/vec0.so
+
+# tests as of 0.1.9: 2 failed, 2 passed, 4 warnings, 74 errors in 4.30s: failures are due to sqlite3 missing enable_load_extension which needs to be fixed in the port
+
+.include <bsd.port.mk>
diff --git a/databases/py-sqlite-vec/distinfo b/databases/py-sqlite-vec/distinfo
new file mode 100644
index 000000000000..12083675ebf0
--- /dev/null
+++ b/databases/py-sqlite-vec/distinfo
@@ -0,0 +1,3 @@
+TIMESTAMP = 1777862627
+SHA256 (asg017-sqlite-vec-v0.1.9_GH0.tar.gz) = 9823e737d9934dcbe85dff75d3fca81018a9beee803d70fa77b16faab5d61dc9
+SIZE (asg017-sqlite-vec-v0.1.9_GH0.tar.gz) = 617543
diff --git a/databases/py-sqlite-vec/files/__init__.py b/databases/py-sqlite-vec/files/__init__.py
new file mode 100644
index 000000000000..e57188c623bb
--- /dev/null
+++ b/databases/py-sqlite-vec/files/__init__.py
@@ -0,0 +1,61 @@
+from os import path
+import sqlite3
+
+__version__ = "0.1.9"
+__version_info__ = tuple(__version__.split("."))
+
+
+def loadable_path():
+ """Returns the full path to the sqlite-vec loadable SQLite extension bundled with this package"""
+ lp = path.join(path.dirname(__file__), "vec0")
+ return path.normpath(lp)
+
+
+def load(conn: sqlite3.Connection) -> None:
+ """Load the sqlite-vec SQLite extension into the given database connection."""
+ conn.load_extension(loadable_path())
+
+
+from typing import List
+from struct import pack
+from sqlite3 import Connection
+
+
+def serialize_float32(vector: List[float]) -> bytes:
+ """Serializes a list of floats into the raw bytes format sqlite-vec expects"""
+ return pack("%sf" % len(vector), *vector)
+
+
+def serialize_int8(vector: List[int]) -> bytes:
+ """Serializes a list of integers into the raw bytes format sqlite-vec expects"""
+ return pack("%sb" % len(vector), *vector)
+
+
+try:
+ import numpy.typing as npt
+
+ def register_numpy(db: Connection, name: str, array: npt.NDArray):
+ ptr = array.__array_interface__["data"][0]
+ nvectors, dimensions = array.__array_interface__["shape"]
+ element_type = array.__array_interface__["typestr"]
+
+ assert element_type == "<f4"
+
+ name_escaped = db.execute("select printf('%w', ?)", [name]).fetchone()[0]
+
+ db.execute(
+ """
+ insert into temp.vec_static_blobs(name, data)
+ select ?, vec_static_blob_from_raw(?, ?, ?, ?)
+ """,
+ [name, ptr, element_type, dimensions, nvectors],
+ )
+
+ db.execute(
+ f'create virtual table "{name_escaped}" using vec_static_blob_entries({name_escaped})'
+ )
+
+except ImportError:
+
+ def register_numpy(db: Connection, name: str, array):
+ raise Exception("numpy package is required for register_numpy")
diff --git a/databases/py-sqlite-vec/files/pyproject.toml b/databases/py-sqlite-vec/files/pyproject.toml
new file mode 100644
index 000000000000..60a4f12110e5
--- /dev/null
+++ b/databases/py-sqlite-vec/files/pyproject.toml
@@ -0,0 +1,16 @@
+[build-system]
+requires = ["setuptools>=61.0"]
+build-backend = "setuptools.build_meta"
+
+[project]
+name = "sqlite-vec"
+version = "0.1.9"
+description = "A vector search SQLite extension"
+license = {text = "MIT OR Apache-2.0"}
+requires-python = ">=3.8"
+
+[tool.setuptools.packages.find]
+include = ["sqlite_vec*"]
+
+[tool.setuptools.package-data]
+sqlite_vec = ["vec0.so", "vec0.dylib", "vec0.dll"]
diff --git a/databases/py-sqlite-vec/files/sqlite-vec.h b/databases/py-sqlite-vec/files/sqlite-vec.h
new file mode 100644
index 000000000000..40d5405f0163
--- /dev/null
+++ b/databases/py-sqlite-vec/files/sqlite-vec.h
@@ -0,0 +1,41 @@
+#ifndef SQLITE_VEC_H
+#define SQLITE_VEC_H
+
+#ifndef SQLITE_CORE
+#include "sqlite3ext.h"
+#else
+#include "sqlite3.h"
+#endif
+
+#ifdef SQLITE_VEC_STATIC
+ #define SQLITE_VEC_API
+#else
+ #ifdef _WIN32
+ #define SQLITE_VEC_API __declspec(dllexport)
+ #else
+ #define SQLITE_VEC_API
+ #endif
+#endif
+
+#define SQLITE_VEC_VERSION "v0.1.9"
+// TODO rm
+#define SQLITE_VEC_DATE "2025-01-01T00:00:00Z+0000"
+#define SQLITE_VEC_SOURCE "v0.1.9"
+
+
+#define SQLITE_VEC_VERSION_MAJOR 0
+#define SQLITE_VEC_VERSION_MINOR 1
+#define SQLITE_VEC_VERSION_PATCH 9
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+SQLITE_VEC_API int sqlite3_vec_init(sqlite3 *db, char **pzErrMsg,
+ const sqlite3_api_routines *pApi);
+
+#ifdef __cplusplus
+} /* end of the 'extern "C"' block */
+#endif
+
+#endif /* ifndef SQLITE_VEC_H */
diff --git a/databases/py-sqlite-vec/pkg-descr b/databases/py-sqlite-vec/pkg-descr
new file mode 100644
index 000000000000..c8ab31cd1476
--- /dev/null
+++ b/databases/py-sqlite-vec/pkg-descr
@@ -0,0 +1,5 @@
+sqlite-vec is a vector search SQLite extension with Python bindings.
+It provides fast nearest-neighbor vector search using SQLite virtual tables.
+
+The Python package bundles the compiled vec0.so SQLite loadable extension
+and provides helper functions for loading it and serializing vectors.