From ec505d77b59e816f17eb595e42b2ac869b1c362b Mon Sep 17 00:00:00 2001 From: Andrea Donetti Date: Fri, 7 Nov 2025 17:36:15 -0600 Subject: [PATCH 1/3] fix: add ggml-blas support for macOS build Fix the following error that occurred when loading the extension: Error: dlopen(./dist/ai.dylib, 0x000A): symbol not found in flat namespace '_ggml_backend_blas_reg' Updated the Makefile to link against libggml-blas.a and include -lggml-blas in LDFLAGS for macOS builds. This enables BLAS support, potentially improving performance on macOS platforms. --- Makefile | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index c5a3fb6..16b1104 100644 --- a/Makefile +++ b/Makefile @@ -72,6 +72,7 @@ ifeq ($(PLATFORM),windows) else ifeq ($(PLATFORM),macos) TARGET := $(DIST_DIR)/ai.dylib LLAMA_LIBS += $(BUILD_GGML)/lib/libggml-metal.a + LLAMA_LIBS += $(BUILD_GGML)/lib/libggml-blas.a ifndef ARCH LDFLAGS += -arch x86_64 -arch arm64 CFLAGS += -arch x86_64 -arch arm64 @@ -85,7 +86,7 @@ else ifeq ($(PLATFORM),macos) WHISPER_OPTIONS += -DGGML_OPENMP=OFF -DCMAKE_OSX_ARCHITECTURES="$(ARCH)" -DCMAKE_OSX_DEPLOYMENT_TARGET=11.0 MINIAUDIO_OPTIONS += -DCMAKE_OSX_ARCHITECTURES="$(ARCH)" -DCMAKE_OSX_DEPLOYMENT_TARGET=11.0 endif - LDFLAGS += -L./$(BUILD_GGML)/lib -lggml-metal -L./$(BUILD_GGML)/lib -framework Metal -framework Foundation -framework CoreFoundation -framework QuartzCore -dynamiclib -undefined dynamic_lookup -headerpad_max_install_names + LDFLAGS += -L./$(BUILD_GGML)/lib -lggml-metal -L./$(BUILD_GGML)/lib -lggml-blas -framework Metal -framework Foundation -framework CoreFoundation -framework QuartzCore -dynamiclib -undefined dynamic_lookup -headerpad_max_install_names STRIP = strip -x -S $@ else ifeq ($(PLATFORM),android) ifndef ARCH # Set ARCH to find Android NDK's Clang compiler, the user should set the ARCH @@ -348,4 +349,4 @@ help: @echo " xcframework - Build the Apple XCFramework" @echo " aar - Build the Android AAR package" -.PHONY: all clean test extension help version xcframework aar \ No newline at end of file +.PHONY: all clean test extension help version xcframework aar From 2fb8761776784314a973fa5ade60adcaee7da7d2 Mon Sep 17 00:00:00 2001 From: Andrea Donetti Date: Fri, 7 Nov 2025 17:41:02 -0600 Subject: [PATCH 2/3] fix: add context validation to llm_chat_create function Added checks to ensure a valid context exists before proceeding in llm_chat_check_context and llm_chat_create. This prevents misuse by requiring llm_context_create to be called prior to llm_chat_create, improving error handling and robustness. --- src/sqlite-ai.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/sqlite-ai.c b/src/sqlite-ai.c index e19f667..57ec200 100644 --- a/src/sqlite-ai.c +++ b/src/sqlite-ai.c @@ -1476,6 +1476,11 @@ static void llm_text_generate (sqlite3_context *context, int argc, sqlite3_value // MARK: - Chat - static bool llm_chat_check_context (ai_context *ai) { + if (!ai || !ai->ctx) { + sqlite_common_set_error(ai ? ai->context : NULL, ai ? ai->vtab : NULL, SQLITE_MISUSE, "No context found. Please call llm_context_create() before llm_chat_create()."); + return false; + } + // check sampler if (!ai->sampler) { llm_sampler_check(ai); @@ -1862,6 +1867,8 @@ static void llm_chat_free (sqlite3_context *context, int argc, sqlite3_value **a } static void llm_chat_create (sqlite3_context *context, int argc, sqlite3_value **argv) { + if (llm_check_context(context) == false) return; + ai_context *ai = (ai_context *)sqlite3_user_data(context); // clean-up old chat (if any) From 6300eef2da7e614010d41006167f5243be3bdcc2 Mon Sep 17 00:00:00 2001 From: Andrea Donetti Date: Mon, 10 Nov 2025 17:37:41 -0600 Subject: [PATCH 3/3] revert commit ec505d7: instead, use the right options in LLAMA env var for both make and make test Eliminates the inclusion of libggml-blas.a and its linker flag from the macOS build configuration in the Makefile. This streamlines the build process and avoids unnecessary dependencies for macOS targets. Usage example: export the following env var to use them both for make and make test: LLAMA="-DGGML_NATIVE=OFF -DGGML_METAL=ON -DGGML_ACCELERATE=ON -DGGML_BLAS=ON -DGGML_BLAS_VENDOR=Apple" WHISPER="-DWHISPER_COREML=ON -DWHISPER_COREML_ALLOW_FALLBACK=ON" SQLITE3=/opt/homebrew/Cellar/sqlite/3.50.4/bin/sqlite3 --- Makefile | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 16b1104..8a4e2ef 100644 --- a/Makefile +++ b/Makefile @@ -72,7 +72,6 @@ ifeq ($(PLATFORM),windows) else ifeq ($(PLATFORM),macos) TARGET := $(DIST_DIR)/ai.dylib LLAMA_LIBS += $(BUILD_GGML)/lib/libggml-metal.a - LLAMA_LIBS += $(BUILD_GGML)/lib/libggml-blas.a ifndef ARCH LDFLAGS += -arch x86_64 -arch arm64 CFLAGS += -arch x86_64 -arch arm64 @@ -86,7 +85,7 @@ else ifeq ($(PLATFORM),macos) WHISPER_OPTIONS += -DGGML_OPENMP=OFF -DCMAKE_OSX_ARCHITECTURES="$(ARCH)" -DCMAKE_OSX_DEPLOYMENT_TARGET=11.0 MINIAUDIO_OPTIONS += -DCMAKE_OSX_ARCHITECTURES="$(ARCH)" -DCMAKE_OSX_DEPLOYMENT_TARGET=11.0 endif - LDFLAGS += -L./$(BUILD_GGML)/lib -lggml-metal -L./$(BUILD_GGML)/lib -lggml-blas -framework Metal -framework Foundation -framework CoreFoundation -framework QuartzCore -dynamiclib -undefined dynamic_lookup -headerpad_max_install_names + LDFLAGS += -L./$(BUILD_GGML)/lib -lggml-metal -L./$(BUILD_GGML)/lib -framework Metal -framework Foundation -framework CoreFoundation -framework QuartzCore -dynamiclib -undefined dynamic_lookup -headerpad_max_install_names STRIP = strip -x -S $@ else ifeq ($(PLATFORM),android) ifndef ARCH # Set ARCH to find Android NDK's Clang compiler, the user should set the ARCH