cactus-react-native 0.2.11 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Cactus.podspec +34 -0
- package/LICENSE +38 -0
- package/README.md +550 -69
- package/android/CMakeLists.txt +40 -0
- package/android/build.gradle +68 -44
- package/android/gradle.properties +5 -5
- package/android/src/main/AndroidManifest.xml +1 -3
- package/android/src/main/cpp/cpp-adapter.cpp +6 -0
- package/android/src/main/java/com/margelo/nitro/cactus/CactusPackage.kt +22 -0
- package/android/src/main/java/com/margelo/nitro/cactus/HybridCactusCrypto.kt +38 -0
- package/android/src/main/java/com/margelo/nitro/cactus/HybridCactusDeviceInfo.kt +24 -0
- package/android/src/main/java/com/margelo/nitro/cactus/HybridCactusFileSystem.kt +227 -0
- package/android/src/main/jniLibs/arm64-v8a/{libcactus_v8.so → libcactus.a} +0 -0
- package/android/src/main/jniLibs/arm64-v8a/libcactus_util.so +0 -0
- package/cpp/HybridCactus.cpp +135 -0
- package/cpp/HybridCactus.hpp +42 -0
- package/cpp/HybridCactusUtil.cpp +47 -0
- package/cpp/HybridCactusUtil.hpp +26 -0
- package/cpp/cactus_ffi.h +55 -0
- package/cpp/cactus_util.h +25 -0
- package/ios/HybridCactusCrypto.swift +37 -0
- package/ios/HybridCactusDeviceInfo.swift +32 -0
- package/ios/HybridCactusFileSystem.swift +234 -0
- package/ios/cactus.xcframework/{info.plist → Info.plist} +2 -37
- package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/cactus.h +8 -229
- package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/cactus_ffi.h +28 -229
- package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/engine.h +545 -0
- package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/ffi_utils.h +400 -0
- package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/graph.h +338 -0
- package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/kernel.h +275 -0
- package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/kernel_utils.h +343 -0
- package/ios/cactus.xcframework/ios-arm64/cactus.framework/Info.plist +0 -0
- package/ios/cactus.xcframework/ios-arm64/cactus.framework/cactus +0 -0
- package/ios/cactus.xcframework/ios-arm64-simulator/cactus.framework/Headers/cactus.h +11 -0
- package/ios/cactus.xcframework/ios-arm64-simulator/cactus.framework/Headers/cactus_ffi.h +55 -0
- package/ios/cactus.xcframework/ios-arm64-simulator/cactus.framework/Headers/engine.h +545 -0
- package/ios/cactus.xcframework/ios-arm64-simulator/cactus.framework/Headers/ffi_utils.h +400 -0
- package/ios/cactus.xcframework/ios-arm64-simulator/cactus.framework/Headers/graph.h +338 -0
- package/ios/cactus.xcframework/ios-arm64-simulator/cactus.framework/Headers/kernel.h +275 -0
- package/ios/cactus.xcframework/ios-arm64-simulator/cactus.framework/Headers/kernel_utils.h +343 -0
- package/ios/cactus.xcframework/ios-arm64-simulator/cactus.framework/Info.plist +0 -0
- package/ios/cactus.xcframework/{tvos-arm64_x86_64-simulator → ios-arm64-simulator}/cactus.framework/_CodeSignature/CodeResources +1 -1
- package/ios/cactus.xcframework/ios-arm64-simulator/cactus.framework/cactus +0 -0
- package/ios/cactus_util.xcframework/Info.plist +39 -0
- package/ios/cactus_util.xcframework/ios-arm64/cactus_util.framework/Headers/cactus_util.h +25 -0
- package/ios/cactus_util.xcframework/ios-arm64/cactus_util.framework/Headers/ios_utils.h +10 -0
- package/ios/cactus_util.xcframework/ios-arm64/cactus_util.framework/Headers/logging.h +25 -0
- package/ios/cactus_util.xcframework/ios-arm64/cactus_util.framework/Info.plist +0 -0
- package/ios/cactus_util.xcframework/ios-arm64/cactus_util.framework/cactus_util +0 -0
- package/ios/cactus_util.xcframework/ios-arm64-simulator/cactus_util.framework/Headers/cactus_util.h +25 -0
- package/ios/cactus_util.xcframework/ios-arm64-simulator/cactus_util.framework/Headers/ios_utils.h +10 -0
- package/ios/cactus_util.xcframework/ios-arm64-simulator/cactus_util.framework/Headers/logging.h +25 -0
- package/ios/{cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework → cactus_util.xcframework/ios-arm64-simulator/cactus_util.framework}/Info.plist +0 -0
- package/ios/{cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework → cactus_util.xcframework/ios-arm64-simulator/cactus_util.framework}/_CodeSignature/CodeResources +36 -2
- package/ios/cactus_util.xcframework/ios-arm64-simulator/cactus_util.framework/cactus_util +0 -0
- package/lib/module/api/Database.js +60 -0
- package/lib/module/api/Database.js.map +1 -0
- package/lib/module/classes/CactusLM.js +151 -0
- package/lib/module/classes/CactusLM.js.map +1 -0
- package/lib/module/config/CactusConfig.js +6 -0
- package/lib/module/config/CactusConfig.js.map +1 -0
- package/lib/module/constants/packageVersion.js +4 -0
- package/lib/module/constants/packageVersion.js.map +1 -0
- package/lib/module/hooks/useCactusLM.js +238 -0
- package/lib/module/hooks/useCactusLM.js.map +1 -0
- package/lib/module/index.js +9 -409
- package/lib/module/index.js.map +1 -1
- package/lib/module/native/Cactus.js +50 -0
- package/lib/module/native/Cactus.js.map +1 -0
- package/lib/module/native/CactusCrypto.js +10 -0
- package/lib/module/native/CactusCrypto.js.map +1 -0
- package/lib/module/native/CactusDeviceInfo.js +13 -0
- package/lib/module/native/CactusDeviceInfo.js.map +1 -0
- package/lib/module/native/CactusFileSystem.js +35 -0
- package/lib/module/native/CactusFileSystem.js.map +1 -0
- package/lib/module/native/CactusUtil.js +23 -0
- package/lib/module/native/CactusUtil.js.map +1 -0
- package/lib/module/native/index.js +8 -0
- package/lib/module/native/index.js.map +1 -0
- package/lib/module/specs/Cactus.nitro.js +4 -0
- package/lib/module/specs/Cactus.nitro.js.map +1 -0
- package/lib/module/specs/CactusCrypto.nitro.js +4 -0
- package/lib/module/specs/CactusCrypto.nitro.js.map +1 -0
- package/lib/module/specs/CactusDeviceInfo.nitro.js +4 -0
- package/lib/module/specs/CactusDeviceInfo.nitro.js.map +1 -0
- package/lib/module/specs/CactusFileSystem.nitro.js +4 -0
- package/lib/module/specs/CactusFileSystem.nitro.js.map +1 -0
- package/lib/module/specs/CactusUtil.nitro.js +4 -0
- package/lib/module/specs/CactusUtil.nitro.js.map +1 -0
- package/lib/module/telemetry/Telemetry.js +100 -0
- package/lib/module/telemetry/Telemetry.js.map +1 -0
- package/lib/module/types/CactusLM.js +2 -0
- package/lib/module/types/CactusLM.js.map +1 -0
- package/lib/module/types/CactusModel.js +2 -0
- package/lib/module/types/CactusModel.js.map +1 -0
- package/lib/module/utils/error.js +4 -0
- package/lib/module/utils/error.js.map +1 -0
- package/lib/typescript/package.json +1 -0
- package/lib/typescript/src/api/Database.d.ts +11 -0
- package/lib/typescript/src/api/Database.d.ts.map +1 -0
- package/lib/typescript/src/classes/CactusLM.d.ts +26 -0
- package/lib/typescript/src/classes/CactusLM.d.ts.map +1 -0
- package/lib/typescript/src/config/CactusConfig.d.ts +5 -0
- package/lib/typescript/src/config/CactusConfig.d.ts.map +1 -0
- package/lib/typescript/src/constants/packageVersion.d.ts +2 -0
- package/lib/typescript/src/constants/packageVersion.d.ts.map +1 -0
- package/lib/typescript/src/hooks/useCactusLM.d.ts +20 -0
- package/lib/typescript/src/hooks/useCactusLM.d.ts.map +1 -0
- package/lib/typescript/src/index.d.ts +6 -0
- package/lib/typescript/src/index.d.ts.map +1 -0
- package/lib/typescript/src/native/Cactus.d.ts +11 -0
- package/lib/typescript/src/native/Cactus.d.ts.map +1 -0
- package/lib/typescript/src/native/CactusCrypto.d.ts +5 -0
- package/lib/typescript/src/native/CactusCrypto.d.ts.map +1 -0
- package/lib/typescript/src/native/CactusDeviceInfo.d.ts +7 -0
- package/lib/typescript/src/native/CactusDeviceInfo.d.ts.map +1 -0
- package/lib/typescript/src/native/CactusFileSystem.d.ts +13 -0
- package/lib/typescript/src/native/CactusFileSystem.d.ts.map +1 -0
- package/lib/typescript/src/native/CactusUtil.d.ts +6 -0
- package/lib/typescript/src/native/CactusUtil.d.ts.map +1 -0
- package/lib/typescript/src/native/index.d.ts +6 -0
- package/lib/typescript/src/native/index.d.ts.map +1 -0
- package/lib/typescript/src/specs/Cactus.nitro.d.ts +13 -0
- package/lib/typescript/src/specs/Cactus.nitro.d.ts.map +1 -0
- package/lib/typescript/src/specs/CactusCrypto.nitro.d.ts +8 -0
- package/lib/typescript/src/specs/CactusCrypto.nitro.d.ts.map +1 -0
- package/lib/typescript/src/specs/CactusDeviceInfo.nitro.d.ts +16 -0
- package/lib/typescript/src/specs/CactusDeviceInfo.nitro.d.ts.map +1 -0
- package/lib/typescript/src/specs/CactusFileSystem.nitro.d.ts +16 -0
- package/lib/typescript/src/specs/CactusFileSystem.nitro.d.ts.map +1 -0
- package/lib/typescript/src/specs/CactusUtil.nitro.d.ts +10 -0
- package/lib/typescript/src/specs/CactusUtil.nitro.d.ts.map +1 -0
- package/lib/typescript/src/telemetry/Telemetry.d.ts +30 -0
- package/lib/typescript/src/telemetry/Telemetry.d.ts.map +1 -0
- package/lib/typescript/src/types/CactusLM.d.ts +67 -0
- package/lib/typescript/src/types/CactusLM.d.ts.map +1 -0
- package/lib/typescript/src/types/CactusModel.d.ts +12 -0
- package/lib/typescript/src/types/CactusModel.d.ts.map +1 -0
- package/lib/typescript/src/utils/error.d.ts +2 -0
- package/lib/typescript/src/utils/error.d.ts.map +1 -0
- package/nitro.json +31 -0
- package/nitrogen/generated/android/c++/JDeviceInfo.hpp +74 -0
- package/nitrogen/generated/android/c++/JFunc_void_double.hpp +74 -0
- package/nitrogen/generated/android/c++/JHybridCactusCryptoSpec.cpp +65 -0
- package/nitrogen/generated/android/c++/JHybridCactusCryptoSpec.hpp +65 -0
- package/nitrogen/generated/android/c++/JHybridCactusDeviceInfoSpec.cpp +85 -0
- package/nitrogen/generated/android/c++/JHybridCactusDeviceInfoSpec.hpp +66 -0
- package/nitrogen/generated/android/c++/JHybridCactusFileSystemSpec.cpp +192 -0
- package/nitrogen/generated/android/c++/JHybridCactusFileSystemSpec.hpp +73 -0
- package/nitrogen/generated/android/cactus+autolinking.cmake +87 -0
- package/nitrogen/generated/android/cactus+autolinking.gradle +27 -0
- package/nitrogen/generated/android/cactusOnLoad.cpp +86 -0
- package/nitrogen/generated/android/cactusOnLoad.hpp +25 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/cactus/DeviceInfo.kt +50 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/cactus/Func_void_double.kt +80 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/cactus/HybridCactusCryptoSpec.kt +58 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/cactus/HybridCactusDeviceInfoSpec.kt +62 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/cactus/HybridCactusFileSystemSpec.kt +95 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/cactus/cactusOnLoad.kt +35 -0
- package/nitrogen/generated/ios/Cactus+autolinking.rb +60 -0
- package/nitrogen/generated/ios/Cactus-Swift-Cxx-Bridge.cpp +122 -0
- package/nitrogen/generated/ios/Cactus-Swift-Cxx-Bridge.hpp +373 -0
- package/nitrogen/generated/ios/Cactus-Swift-Cxx-Umbrella.hpp +60 -0
- package/nitrogen/generated/ios/CactusAutolinking.mm +69 -0
- package/nitrogen/generated/ios/CactusAutolinking.swift +55 -0
- package/nitrogen/generated/ios/c++/HybridCactusCryptoSpecSwift.cpp +11 -0
- package/nitrogen/generated/ios/c++/HybridCactusCryptoSpecSwift.hpp +77 -0
- package/nitrogen/generated/ios/c++/HybridCactusDeviceInfoSpecSwift.cpp +11 -0
- package/nitrogen/generated/ios/c++/HybridCactusDeviceInfoSpecSwift.hpp +88 -0
- package/nitrogen/generated/ios/c++/HybridCactusFileSystemSpecSwift.cpp +11 -0
- package/nitrogen/generated/ios/c++/HybridCactusFileSystemSpecSwift.hpp +143 -0
- package/nitrogen/generated/ios/swift/DeviceInfo.swift +98 -0
- package/nitrogen/generated/ios/swift/Func_void.swift +47 -0
- package/nitrogen/generated/ios/swift/Func_void_DeviceInfo.swift +47 -0
- package/nitrogen/generated/ios/swift/Func_void_bool.swift +47 -0
- package/nitrogen/generated/ios/swift/Func_void_double.swift +47 -0
- package/nitrogen/generated/ios/swift/Func_void_std__exception_ptr.swift +47 -0
- package/nitrogen/generated/ios/swift/Func_void_std__optional_std__string_.swift +54 -0
- package/nitrogen/generated/ios/swift/Func_void_std__string.swift +47 -0
- package/nitrogen/generated/ios/swift/HybridCactusCryptoSpec.swift +57 -0
- package/nitrogen/generated/ios/swift/HybridCactusCryptoSpec_cxx.swift +139 -0
- package/nitrogen/generated/ios/swift/HybridCactusDeviceInfoSpec.swift +58 -0
- package/nitrogen/generated/ios/swift/HybridCactusDeviceInfoSpec_cxx.swift +164 -0
- package/nitrogen/generated/ios/swift/HybridCactusFileSystemSpec.swift +65 -0
- package/nitrogen/generated/ios/swift/HybridCactusFileSystemSpec_cxx.swift +303 -0
- package/nitrogen/generated/shared/c++/DeviceInfo.hpp +92 -0
- package/nitrogen/generated/shared/c++/HybridCactusCryptoSpec.cpp +21 -0
- package/nitrogen/generated/shared/c++/HybridCactusCryptoSpec.hpp +63 -0
- package/nitrogen/generated/shared/c++/HybridCactusDeviceInfoSpec.cpp +22 -0
- package/nitrogen/generated/shared/c++/HybridCactusDeviceInfoSpec.hpp +67 -0
- package/nitrogen/generated/shared/c++/HybridCactusFileSystemSpec.cpp +29 -0
- package/nitrogen/generated/shared/c++/HybridCactusFileSystemSpec.hpp +73 -0
- package/nitrogen/generated/shared/c++/HybridCactusSpec.cpp +26 -0
- package/nitrogen/generated/shared/c++/HybridCactusSpec.hpp +71 -0
- package/nitrogen/generated/shared/c++/HybridCactusUtilSpec.cpp +23 -0
- package/nitrogen/generated/shared/c++/HybridCactusUtilSpec.hpp +66 -0
- package/package.json +84 -143
- package/src/api/Database.ts +83 -0
- package/src/classes/CactusLM.ts +205 -0
- package/src/config/CactusConfig.ts +4 -0
- package/src/constants/packageVersion.ts +1 -0
- package/src/hooks/useCactusLM.ts +285 -0
- package/src/index.tsx +23 -0
- package/src/native/Cactus.ts +83 -0
- package/src/native/CactusCrypto.ts +11 -0
- package/src/native/CactusDeviceInfo.ts +18 -0
- package/src/native/CactusFileSystem.ts +47 -0
- package/src/native/CactusUtil.ts +27 -0
- package/src/native/index.ts +5 -0
- package/src/specs/Cactus.nitro.ts +20 -0
- package/src/specs/CactusCrypto.nitro.ts +6 -0
- package/src/specs/CactusDeviceInfo.nitro.ts +15 -0
- package/src/specs/CactusFileSystem.nitro.ts +21 -0
- package/src/specs/CactusUtil.nitro.ts +8 -0
- package/src/telemetry/Telemetry.ts +159 -0
- package/src/types/CactusLM.ts +73 -0
- package/src/types/CactusModel.ts +14 -0
- package/src/utils/error.ts +2 -0
- package/LICENSE.txt +0 -20
- package/android/src/main/CMakeLists.txt +0 -140
- package/android/src/main/java/com/cactus/Cactus.java +0 -1190
- package/android/src/main/java/com/cactus/CactusPackage.java +0 -48
- package/android/src/main/java/com/cactus/LlamaContext.java +0 -748
- package/android/src/main/jni-utils.h +0 -100
- package/android/src/main/jni.cpp +0 -1605
- package/android/src/main/jniLibs/arm64-v8a/libcactus.so +0 -0
- package/android/src/main/jniLibs/arm64-v8a/libcactus_v8_2.so +0 -0
- package/android/src/main/jniLibs/arm64-v8a/libcactus_v8_2_dotprod.so +0 -0
- package/android/src/main/jniLibs/arm64-v8a/libcactus_v8_2_dotprod_i8mm.so +0 -0
- package/android/src/main/jniLibs/arm64-v8a/libcactus_v8_2_i8mm.so +0 -0
- package/android/src/newarch/java/com/cactus/CactusModule.java +0 -204
- package/android/src/oldarch/java/com/cactus/CactusModule.java +0 -205
- package/cactus-react-native.podspec +0 -42
- package/ios/CMakeLists.txt +0 -131
- package/ios/Cactus.h +0 -6
- package/ios/Cactus.mm +0 -681
- package/ios/CactusContext.h +0 -81
- package/ios/CactusContext.mm +0 -1032
- package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/chat.h +0 -145
- package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/common.h +0 -674
- package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/ggml-alloc.h +0 -76
- package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/ggml-backend-impl.h +0 -255
- package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/ggml-backend.h +0 -354
- package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/ggml-common.h +0 -1857
- package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/ggml-cpp.h +0 -39
- package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/ggml-cpu.h +0 -143
- package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/ggml-impl.h +0 -601
- package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/ggml-metal-impl.h +0 -622
- package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/ggml-metal.h +0 -66
- package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/ggml-opt.h +0 -237
- package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/ggml-quants.h +0 -100
- package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/ggml-threading.h +0 -14
- package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/ggml.h +0 -2202
- package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/gguf.h +0 -202
- package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/json-schema-to-grammar.h +0 -21
- package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/json.hpp +0 -24766
- package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/llama-adapter.h +0 -76
- package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/llama-arch.h +0 -437
- package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/llama-batch.h +0 -89
- package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/llama-chat.h +0 -58
- package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/llama-context.h +0 -276
- package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/llama-cparams.h +0 -39
- package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/llama-cpp.h +0 -30
- package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/llama-grammar.h +0 -173
- package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/llama-graph.h +0 -640
- package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/llama-hparams.h +0 -190
- package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/llama-impl.h +0 -61
- package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/llama-io.h +0 -35
- package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/llama-kv-cache.h +0 -515
- package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/llama-memory.h +0 -32
- package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/llama-mmap.h +0 -68
- package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/llama-model-loader.h +0 -169
- package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/llama-model-saver.h +0 -37
- package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/llama-model.h +0 -425
- package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/llama-sampling.h +0 -32
- package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/llama-vocab.h +0 -131
- package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/llama.h +0 -1376
- package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/log.h +0 -103
- package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/minja/chat-template.hpp +0 -542
- package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/minja/minja.hpp +0 -2974
- package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/sampling.h +0 -107
- package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/unicode-data.h +0 -20
- package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/unicode.h +0 -66
- package/ios/cactus.xcframework/ios-arm64/cactus.framework/ggml-llama.metallib +0 -0
- package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/Headers/cactus.h +0 -232
- package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/Headers/cactus_ffi.h +0 -256
- package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/Headers/chat.h +0 -145
- package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/Headers/common.h +0 -674
- package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/Headers/ggml-alloc.h +0 -76
- package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/Headers/ggml-backend-impl.h +0 -255
- package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/Headers/ggml-backend.h +0 -354
- package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/Headers/ggml-common.h +0 -1857
- package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/Headers/ggml-cpp.h +0 -39
- package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/Headers/ggml-cpu.h +0 -143
- package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/Headers/ggml-impl.h +0 -601
- package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/Headers/ggml-metal-impl.h +0 -622
- package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/Headers/ggml-metal.h +0 -66
- package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/Headers/ggml-opt.h +0 -237
- package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/Headers/ggml-quants.h +0 -100
- package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/Headers/ggml-threading.h +0 -14
- package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/Headers/ggml.h +0 -2202
- package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/Headers/gguf.h +0 -202
- package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/Headers/json-schema-to-grammar.h +0 -21
- package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/Headers/json.hpp +0 -24766
- package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/Headers/llama-adapter.h +0 -76
- package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/Headers/llama-arch.h +0 -437
- package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/Headers/llama-batch.h +0 -89
- package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/Headers/llama-chat.h +0 -58
- package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/Headers/llama-context.h +0 -276
- package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/Headers/llama-cparams.h +0 -39
- package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/Headers/llama-cpp.h +0 -30
- package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/Headers/llama-grammar.h +0 -173
- package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/Headers/llama-graph.h +0 -640
- package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/Headers/llama-hparams.h +0 -190
- package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/Headers/llama-impl.h +0 -61
- package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/Headers/llama-io.h +0 -35
- package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/Headers/llama-kv-cache.h +0 -515
- package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/Headers/llama-memory.h +0 -32
- package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/Headers/llama-mmap.h +0 -68
- package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/Headers/llama-model-loader.h +0 -169
- package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/Headers/llama-model-saver.h +0 -37
- package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/Headers/llama-model.h +0 -425
- package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/Headers/llama-sampling.h +0 -32
- package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/Headers/llama-vocab.h +0 -131
- package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/Headers/llama.h +0 -1376
- package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/Headers/log.h +0 -103
- package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/Headers/minja/chat-template.hpp +0 -542
- package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/Headers/minja/minja.hpp +0 -2974
- package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/Headers/sampling.h +0 -107
- package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/Headers/unicode-data.h +0 -20
- package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/Headers/unicode.h +0 -66
- package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/cactus +0 -0
- package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/ggml-llama-sim.metallib +0 -0
- package/ios/cactus.xcframework/tvos-arm64/cactus.framework/Headers/cactus.h +0 -232
- package/ios/cactus.xcframework/tvos-arm64/cactus.framework/Headers/cactus_ffi.h +0 -256
- package/ios/cactus.xcframework/tvos-arm64/cactus.framework/Headers/chat.h +0 -145
- package/ios/cactus.xcframework/tvos-arm64/cactus.framework/Headers/common.h +0 -674
- package/ios/cactus.xcframework/tvos-arm64/cactus.framework/Headers/ggml-alloc.h +0 -76
- package/ios/cactus.xcframework/tvos-arm64/cactus.framework/Headers/ggml-backend-impl.h +0 -255
- package/ios/cactus.xcframework/tvos-arm64/cactus.framework/Headers/ggml-backend.h +0 -354
- package/ios/cactus.xcframework/tvos-arm64/cactus.framework/Headers/ggml-common.h +0 -1857
- package/ios/cactus.xcframework/tvos-arm64/cactus.framework/Headers/ggml-cpp.h +0 -39
- package/ios/cactus.xcframework/tvos-arm64/cactus.framework/Headers/ggml-cpu.h +0 -143
- package/ios/cactus.xcframework/tvos-arm64/cactus.framework/Headers/ggml-impl.h +0 -601
- package/ios/cactus.xcframework/tvos-arm64/cactus.framework/Headers/ggml-metal-impl.h +0 -622
- package/ios/cactus.xcframework/tvos-arm64/cactus.framework/Headers/ggml-metal.h +0 -66
- package/ios/cactus.xcframework/tvos-arm64/cactus.framework/Headers/ggml-opt.h +0 -237
- package/ios/cactus.xcframework/tvos-arm64/cactus.framework/Headers/ggml-quants.h +0 -100
- package/ios/cactus.xcframework/tvos-arm64/cactus.framework/Headers/ggml-threading.h +0 -14
- package/ios/cactus.xcframework/tvos-arm64/cactus.framework/Headers/ggml.h +0 -2202
- package/ios/cactus.xcframework/tvos-arm64/cactus.framework/Headers/gguf.h +0 -202
- package/ios/cactus.xcframework/tvos-arm64/cactus.framework/Headers/json-schema-to-grammar.h +0 -21
- package/ios/cactus.xcframework/tvos-arm64/cactus.framework/Headers/json.hpp +0 -24766
- package/ios/cactus.xcframework/tvos-arm64/cactus.framework/Headers/llama-adapter.h +0 -76
- package/ios/cactus.xcframework/tvos-arm64/cactus.framework/Headers/llama-arch.h +0 -437
- package/ios/cactus.xcframework/tvos-arm64/cactus.framework/Headers/llama-batch.h +0 -89
- package/ios/cactus.xcframework/tvos-arm64/cactus.framework/Headers/llama-chat.h +0 -58
- package/ios/cactus.xcframework/tvos-arm64/cactus.framework/Headers/llama-context.h +0 -276
- package/ios/cactus.xcframework/tvos-arm64/cactus.framework/Headers/llama-cparams.h +0 -39
- package/ios/cactus.xcframework/tvos-arm64/cactus.framework/Headers/llama-cpp.h +0 -30
- package/ios/cactus.xcframework/tvos-arm64/cactus.framework/Headers/llama-grammar.h +0 -173
- package/ios/cactus.xcframework/tvos-arm64/cactus.framework/Headers/llama-graph.h +0 -640
- package/ios/cactus.xcframework/tvos-arm64/cactus.framework/Headers/llama-hparams.h +0 -190
- package/ios/cactus.xcframework/tvos-arm64/cactus.framework/Headers/llama-impl.h +0 -61
- package/ios/cactus.xcframework/tvos-arm64/cactus.framework/Headers/llama-io.h +0 -35
- package/ios/cactus.xcframework/tvos-arm64/cactus.framework/Headers/llama-kv-cache.h +0 -515
- package/ios/cactus.xcframework/tvos-arm64/cactus.framework/Headers/llama-memory.h +0 -32
- package/ios/cactus.xcframework/tvos-arm64/cactus.framework/Headers/llama-mmap.h +0 -68
- package/ios/cactus.xcframework/tvos-arm64/cactus.framework/Headers/llama-model-loader.h +0 -169
- package/ios/cactus.xcframework/tvos-arm64/cactus.framework/Headers/llama-model-saver.h +0 -37
- package/ios/cactus.xcframework/tvos-arm64/cactus.framework/Headers/llama-model.h +0 -425
- package/ios/cactus.xcframework/tvos-arm64/cactus.framework/Headers/llama-sampling.h +0 -32
- package/ios/cactus.xcframework/tvos-arm64/cactus.framework/Headers/llama-vocab.h +0 -131
- package/ios/cactus.xcframework/tvos-arm64/cactus.framework/Headers/llama.h +0 -1376
- package/ios/cactus.xcframework/tvos-arm64/cactus.framework/Headers/log.h +0 -103
- package/ios/cactus.xcframework/tvos-arm64/cactus.framework/Headers/minja/chat-template.hpp +0 -542
- package/ios/cactus.xcframework/tvos-arm64/cactus.framework/Headers/minja/minja.hpp +0 -2974
- package/ios/cactus.xcframework/tvos-arm64/cactus.framework/Headers/sampling.h +0 -107
- package/ios/cactus.xcframework/tvos-arm64/cactus.framework/Headers/unicode-data.h +0 -20
- package/ios/cactus.xcframework/tvos-arm64/cactus.framework/Headers/unicode.h +0 -66
- package/ios/cactus.xcframework/tvos-arm64/cactus.framework/Info.plist +0 -0
- package/ios/cactus.xcframework/tvos-arm64/cactus.framework/cactus +0 -0
- package/ios/cactus.xcframework/tvos-arm64/cactus.framework/ggml-llama.metallib +0 -0
- package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/Headers/cactus.h +0 -232
- package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/Headers/cactus_ffi.h +0 -256
- package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/Headers/chat.h +0 -145
- package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/Headers/common.h +0 -674
- package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/Headers/ggml-alloc.h +0 -76
- package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/Headers/ggml-backend-impl.h +0 -255
- package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/Headers/ggml-backend.h +0 -354
- package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/Headers/ggml-common.h +0 -1857
- package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/Headers/ggml-cpp.h +0 -39
- package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/Headers/ggml-cpu.h +0 -143
- package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/Headers/ggml-impl.h +0 -601
- package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/Headers/ggml-metal-impl.h +0 -622
- package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/Headers/ggml-metal.h +0 -66
- package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/Headers/ggml-opt.h +0 -237
- package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/Headers/ggml-quants.h +0 -100
- package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/Headers/ggml-threading.h +0 -14
- package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/Headers/ggml.h +0 -2202
- package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/Headers/gguf.h +0 -202
- package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/Headers/json-schema-to-grammar.h +0 -21
- package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/Headers/json.hpp +0 -24766
- package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/Headers/llama-adapter.h +0 -76
- package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/Headers/llama-arch.h +0 -437
- package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/Headers/llama-batch.h +0 -89
- package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/Headers/llama-chat.h +0 -58
- package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/Headers/llama-context.h +0 -276
- package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/Headers/llama-cparams.h +0 -39
- package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/Headers/llama-cpp.h +0 -30
- package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/Headers/llama-grammar.h +0 -173
- package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/Headers/llama-graph.h +0 -640
- package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/Headers/llama-hparams.h +0 -190
- package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/Headers/llama-impl.h +0 -61
- package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/Headers/llama-io.h +0 -35
- package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/Headers/llama-kv-cache.h +0 -515
- package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/Headers/llama-memory.h +0 -32
- package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/Headers/llama-mmap.h +0 -68
- package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/Headers/llama-model-loader.h +0 -169
- package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/Headers/llama-model-saver.h +0 -37
- package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/Headers/llama-model.h +0 -425
- package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/Headers/llama-sampling.h +0 -32
- package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/Headers/llama-vocab.h +0 -131
- package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/Headers/llama.h +0 -1376
- package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/Headers/log.h +0 -103
- package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/Headers/minja/chat-template.hpp +0 -542
- package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/Headers/minja/minja.hpp +0 -2974
- package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/Headers/sampling.h +0 -107
- package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/Headers/unicode-data.h +0 -20
- package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/Headers/unicode.h +0 -66
- package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/Info.plist +0 -0
- package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/cactus +0 -0
- package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/ggml-llama-sim.metallib +0 -0
- package/lib/commonjs/NativeCactus.js +0 -9
- package/lib/commonjs/NativeCactus.js.map +0 -1
- package/lib/commonjs/agent.js +0 -73
- package/lib/commonjs/agent.js.map +0 -1
- package/lib/commonjs/chat.js +0 -73
- package/lib/commonjs/chat.js.map +0 -1
- package/lib/commonjs/index.js +0 -523
- package/lib/commonjs/index.js.map +0 -1
- package/lib/commonjs/lm.js +0 -272
- package/lib/commonjs/lm.js.map +0 -1
- package/lib/commonjs/package.json +0 -1
- package/lib/commonjs/projectId.js +0 -9
- package/lib/commonjs/projectId.js.map +0 -1
- package/lib/commonjs/remote.js +0 -100
- package/lib/commonjs/remote.js.map +0 -1
- package/lib/commonjs/telemetry.js +0 -102
- package/lib/commonjs/telemetry.js.map +0 -1
- package/lib/commonjs/tools.js +0 -72
- package/lib/commonjs/tools.js.map +0 -1
- package/lib/commonjs/tts.js +0 -141
- package/lib/commonjs/tts.js.map +0 -1
- package/lib/commonjs/vlm.js +0 -221
- package/lib/commonjs/vlm.js.map +0 -1
- package/lib/module/NativeCactus.js +0 -5
- package/lib/module/NativeCactus.js.map +0 -1
- package/lib/module/agent.js +0 -68
- package/lib/module/agent.js.map +0 -1
- package/lib/module/chat.js +0 -67
- package/lib/module/chat.js.map +0 -1
- package/lib/module/lm.js +0 -267
- package/lib/module/lm.js.map +0 -1
- package/lib/module/projectId.js +0 -5
- package/lib/module/projectId.js.map +0 -1
- package/lib/module/remote.js +0 -91
- package/lib/module/remote.js.map +0 -1
- package/lib/module/telemetry.js +0 -97
- package/lib/module/telemetry.js.map +0 -1
- package/lib/module/tools.js +0 -66
- package/lib/module/tools.js.map +0 -1
- package/lib/module/tts.js +0 -135
- package/lib/module/tts.js.map +0 -1
- package/lib/module/vlm.js +0 -216
- package/lib/module/vlm.js.map +0 -1
- package/lib/typescript/NativeCactus.d.ts +0 -252
- package/lib/typescript/NativeCactus.d.ts.map +0 -1
- package/lib/typescript/agent.d.ts +0 -31
- package/lib/typescript/agent.d.ts.map +0 -1
- package/lib/typescript/chat.d.ts +0 -23
- package/lib/typescript/chat.d.ts.map +0 -1
- package/lib/typescript/index.d.ts +0 -114
- package/lib/typescript/index.d.ts.map +0 -1
- package/lib/typescript/lm.d.ts +0 -36
- package/lib/typescript/lm.d.ts.map +0 -1
- package/lib/typescript/projectId.d.ts +0 -2
- package/lib/typescript/projectId.d.ts.map +0 -1
- package/lib/typescript/remote.d.ts +0 -8
- package/lib/typescript/remote.d.ts.map +0 -1
- package/lib/typescript/telemetry.d.ts +0 -25
- package/lib/typescript/telemetry.d.ts.map +0 -1
- package/lib/typescript/tools.d.ts +0 -36
- package/lib/typescript/tools.d.ts.map +0 -1
- package/lib/typescript/tts.d.ts +0 -54
- package/lib/typescript/tts.d.ts.map +0 -1
- package/lib/typescript/vlm.d.ts +0 -33
- package/lib/typescript/vlm.d.ts.map +0 -1
- package/scripts/postInstall.js +0 -33
- package/src/NativeCactus.ts +0 -317
- package/src/agent.ts +0 -112
- package/src/chat.ts +0 -91
- package/src/index.ts +0 -663
- package/src/lm.ts +0 -324
- package/src/projectId.ts +0 -1
- package/src/remote.ts +0 -113
- package/src/telemetry.ts +0 -137
- package/src/tools.ts +0 -94
- package/src/tts.ts +0 -236
- package/src/vlm.ts +0 -276
package/ios/CactusContext.mm
DELETED
|
@@ -1,1032 +0,0 @@
|
|
|
1
|
-
#import "CactusContext.h"
|
|
2
|
-
#import <Metal/Metal.h>
|
|
3
|
-
#import <sys/utsname.h>
|
|
4
|
-
|
|
5
|
-
@implementation CactusContext
|
|
6
|
-
|
|
7
|
-
+ (void)toggleNativeLog:(BOOL)enabled onEmitLog:(void (^)(NSString *level, NSString *text))onEmitLog {
|
|
8
|
-
if (enabled) {
|
|
9
|
-
void (^copiedBlock)(NSString *, NSString *) = [onEmitLog copy];
|
|
10
|
-
llama_log_set([](lm_ggml_log_level level, const char * text, void * data) {
|
|
11
|
-
llama_log_callback_default(level, text, data);
|
|
12
|
-
NSString *levelStr = @"";
|
|
13
|
-
if (level == LM_GGML_LOG_LEVEL_ERROR) {
|
|
14
|
-
levelStr = @"error";
|
|
15
|
-
} else if (level == LM_GGML_LOG_LEVEL_INFO) {
|
|
16
|
-
levelStr = @"info";
|
|
17
|
-
} else if (level == LM_GGML_LOG_LEVEL_WARN) {
|
|
18
|
-
levelStr = @"warn";
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
NSString *textStr = [NSString stringWithUTF8String:text];
|
|
22
|
-
// NOTE: Convert to UTF-8 string may fail
|
|
23
|
-
if (!textStr) {
|
|
24
|
-
return;
|
|
25
|
-
}
|
|
26
|
-
void (^block)(NSString *, NSString *) = (__bridge void (^)(NSString *, NSString *))(data);
|
|
27
|
-
block(levelStr, textStr);
|
|
28
|
-
}, copiedBlock);
|
|
29
|
-
} else {
|
|
30
|
-
llama_log_set(llama_log_callback_default, nullptr);
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
+ (NSDictionary *)modelInfo:(NSString *)path skip:(NSArray *)skip {
|
|
35
|
-
struct lm_gguf_init_params params = {
|
|
36
|
-
/*.no_alloc = */ false,
|
|
37
|
-
/*.ctx = */ NULL,
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
struct lm_gguf_context * ctx = lm_gguf_init_from_file([path UTF8String], params);
|
|
41
|
-
|
|
42
|
-
if (!ctx) {
|
|
43
|
-
NSLog(@"%s: failed to load '%s'\n", __func__, [path UTF8String]);
|
|
44
|
-
return @{};
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
NSMutableDictionary *info = [[NSMutableDictionary alloc] init];
|
|
48
|
-
|
|
49
|
-
info[@"version"] = @(lm_gguf_get_version(ctx));
|
|
50
|
-
info[@"alignment"] = @(lm_gguf_get_alignment(ctx));
|
|
51
|
-
info[@"data_offset"] = @(lm_gguf_get_data_offset(ctx));
|
|
52
|
-
|
|
53
|
-
// kv
|
|
54
|
-
{
|
|
55
|
-
const int n_kv = lm_gguf_get_n_kv(ctx);
|
|
56
|
-
|
|
57
|
-
for (int i = 0; i < n_kv; ++i) {
|
|
58
|
-
const char * key = lm_gguf_get_key(ctx, i);
|
|
59
|
-
|
|
60
|
-
if (skip && [skip containsObject:[NSString stringWithUTF8String:key]]) {
|
|
61
|
-
continue;
|
|
62
|
-
}
|
|
63
|
-
const std::string value = lm_gguf_kv_to_str(ctx, i);
|
|
64
|
-
info[[NSString stringWithUTF8String:key]] = [NSString stringWithUTF8String:value.c_str()];
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
lm_gguf_free(ctx);
|
|
69
|
-
|
|
70
|
-
return info;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
+ (instancetype)initWithParams:(NSDictionary *)params onProgress:(void (^)(unsigned int progress))onProgress {
|
|
74
|
-
// llama_backend_init(false);
|
|
75
|
-
common_params defaultParams;
|
|
76
|
-
|
|
77
|
-
if (params[@"vocab_only"]) {
|
|
78
|
-
defaultParams.vocab_only = [params[@"vocab_only"] boolValue];
|
|
79
|
-
defaultParams.warmup = false;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
NSString *modelPath = params[@"model"];
|
|
83
|
-
BOOL isAsset = [params[@"is_model_asset"] boolValue];
|
|
84
|
-
NSString *path = modelPath;
|
|
85
|
-
if (isAsset) path = [[NSBundle mainBundle] pathForResource:modelPath ofType:nil];
|
|
86
|
-
defaultParams.model.path = [path UTF8String];
|
|
87
|
-
|
|
88
|
-
NSString *chatTemplate = params[@"chat_template"];
|
|
89
|
-
if (chatTemplate) {
|
|
90
|
-
defaultParams.chat_template = [chatTemplate UTF8String];
|
|
91
|
-
NSLog(@"chatTemplate: %@", chatTemplate);
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
NSString *reasoningFormat = params[@"reasoning_format"];
|
|
95
|
-
if (reasoningFormat && [reasoningFormat isEqualToString:@"deepseek"]) {
|
|
96
|
-
defaultParams.reasoning_format = COMMON_REASONING_FORMAT_DEEPSEEK;
|
|
97
|
-
} else {
|
|
98
|
-
defaultParams.reasoning_format = COMMON_REASONING_FORMAT_NONE;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
if (params[@"n_ctx"]) defaultParams.n_ctx = [params[@"n_ctx"] intValue];
|
|
102
|
-
if (params[@"use_mlock"]) defaultParams.use_mlock = [params[@"use_mlock"]boolValue];
|
|
103
|
-
|
|
104
|
-
BOOL skipGpuDevices = params[@"no_gpu_devices"] && [params[@"no_gpu_devices"] boolValue];
|
|
105
|
-
|
|
106
|
-
BOOL isMetalEnabled = false;
|
|
107
|
-
NSString *reasonNoMetal = @"";
|
|
108
|
-
defaultParams.n_gpu_layers = 0;
|
|
109
|
-
#ifdef LM_GGML_USE_METAL
|
|
110
|
-
// Check ggml-metal availability
|
|
111
|
-
NSError * error = nil;
|
|
112
|
-
id<MTLDevice> device = MTLCreateSystemDefaultDevice();
|
|
113
|
-
id<MTLLibrary> library = [device
|
|
114
|
-
newLibraryWithSource:@"#include <metal_stdlib>\n"
|
|
115
|
-
"using namespace metal;"
|
|
116
|
-
"typedef matrix<bfloat, 4, 4> bfloat4x4;"
|
|
117
|
-
"kernel void test() { simd_sum(0); }"
|
|
118
|
-
options:nil
|
|
119
|
-
error:&error
|
|
120
|
-
];
|
|
121
|
-
if (error) {
|
|
122
|
-
reasonNoMetal = [error localizedDescription];
|
|
123
|
-
skipGpuDevices = true;
|
|
124
|
-
} else {
|
|
125
|
-
id<MTLFunction> kernel = [library newFunctionWithName:@"test"];
|
|
126
|
-
id<MTLComputePipelineState> pipeline = [device newComputePipelineStateWithFunction:kernel error:&error];
|
|
127
|
-
if (pipeline == nil) {
|
|
128
|
-
reasonNoMetal = [error localizedDescription];
|
|
129
|
-
skipGpuDevices = true;
|
|
130
|
-
} else {
|
|
131
|
-
#if TARGET_OS_SIMULATOR
|
|
132
|
-
// Use the backend, but no layers because not supported fully on simulator
|
|
133
|
-
defaultParams.n_gpu_layers = 0;
|
|
134
|
-
isMetalEnabled = true;
|
|
135
|
-
#else
|
|
136
|
-
defaultParams.n_gpu_layers = [params[@"n_gpu_layers"] intValue];
|
|
137
|
-
isMetalEnabled = true;
|
|
138
|
-
#endif
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
device = nil;
|
|
142
|
-
#else
|
|
143
|
-
reasonNoMetal = @"Metal is not enabled in this build";
|
|
144
|
-
isMetalEnabled = false;
|
|
145
|
-
#endif
|
|
146
|
-
|
|
147
|
-
if (skipGpuDevices) {
|
|
148
|
-
std::vector<lm_ggml_backend_dev_t> cpu_devs;
|
|
149
|
-
for (size_t i = 0; i < lm_ggml_backend_dev_count(); ++i) {
|
|
150
|
-
lm_ggml_backend_dev_t dev = lm_ggml_backend_dev_get(i);
|
|
151
|
-
switch (lm_ggml_backend_dev_type(dev)) {
|
|
152
|
-
case LM_GGML_BACKEND_DEVICE_TYPE_CPU:
|
|
153
|
-
case LM_GGML_BACKEND_DEVICE_TYPE_ACCEL:
|
|
154
|
-
cpu_devs.push_back(dev);
|
|
155
|
-
break;
|
|
156
|
-
case LM_GGML_BACKEND_DEVICE_TYPE_GPU:
|
|
157
|
-
break;
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
if (cpu_devs.size() > 0) {
|
|
161
|
-
defaultParams.devices = cpu_devs;
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
if (params[@"n_batch"]) defaultParams.n_batch = [params[@"n_batch"] intValue];
|
|
166
|
-
if (params[@"n_ubatch"]) defaultParams.n_ubatch = [params[@"n_ubatch"] intValue];
|
|
167
|
-
if (params[@"use_mmap"]) defaultParams.use_mmap = [params[@"use_mmap"] boolValue];
|
|
168
|
-
|
|
169
|
-
if (params[@"pooling_type"] && [params[@"pooling_type"] isKindOfClass:[NSNumber class]]) {
|
|
170
|
-
defaultParams.pooling_type = static_cast<enum llama_pooling_type>([params[@"pooling_type"] intValue]);
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
if (params[@"embedding"] && [params[@"embedding"] boolValue]) {
|
|
174
|
-
defaultParams.embedding = true;
|
|
175
|
-
// For non-causal models, batch size must be equal to ubatch size
|
|
176
|
-
defaultParams.n_ubatch = defaultParams.n_batch;
|
|
177
|
-
|
|
178
|
-
if (params[@"embd_normalize"] && [params[@"embd_normalize"] isKindOfClass:[NSNumber class]]) {
|
|
179
|
-
defaultParams.embd_normalize = [params[@"embd_normalize"] intValue];
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
if (params[@"rope_freq_base"]) defaultParams.rope_freq_base = [params[@"rope_freq_base"] floatValue];
|
|
184
|
-
if (params[@"rope_freq_scale"]) defaultParams.rope_freq_scale = [params[@"rope_freq_scale"] floatValue];
|
|
185
|
-
|
|
186
|
-
if (params[@"flash_attn"] && [params[@"flash_attn"] boolValue]) defaultParams.flash_attn = true;
|
|
187
|
-
|
|
188
|
-
if (params[@"cache_type_k"]) defaultParams.cache_type_k = cactus::kv_cache_type_from_str([params[@"cache_type_k"] UTF8String]);
|
|
189
|
-
if (params[@"cache_type_v"]) defaultParams.cache_type_v = cactus::kv_cache_type_from_str([params[@"cache_type_v"] UTF8String]);
|
|
190
|
-
|
|
191
|
-
int nThreads = params[@"n_threads"] ? [params[@"n_threads"] intValue] : 0;
|
|
192
|
-
const int maxThreads = (int) [[NSProcessInfo processInfo] processorCount];
|
|
193
|
-
// Use 2 threads by default on 4-core devices, 4 threads on more cores
|
|
194
|
-
const int defaultNThreads = nThreads == 4 ? 2 : MIN(4, maxThreads);
|
|
195
|
-
defaultParams.cpuparams.n_threads = nThreads > 0 ? nThreads : defaultNThreads;
|
|
196
|
-
|
|
197
|
-
CactusContext *context = [[CactusContext alloc] init];
|
|
198
|
-
context->llama = new cactus::cactus_context();
|
|
199
|
-
context->llama->is_load_interrupted = false;
|
|
200
|
-
context->llama->loading_progress = 0;
|
|
201
|
-
context->onProgress = onProgress;
|
|
202
|
-
|
|
203
|
-
if (params[@"use_progress_callback"] && [params[@"use_progress_callback"] boolValue]) {
|
|
204
|
-
defaultParams.progress_callback = [](float progress, void * user_data) {
|
|
205
|
-
CactusContext *context = (__bridge CactusContext *)(user_data);
|
|
206
|
-
unsigned percentage = (unsigned) (100 * progress);
|
|
207
|
-
if (percentage > context->llama->loading_progress) {
|
|
208
|
-
context->llama->loading_progress = percentage;
|
|
209
|
-
context->onProgress(percentage);
|
|
210
|
-
}
|
|
211
|
-
return !context->llama->is_load_interrupted;
|
|
212
|
-
};
|
|
213
|
-
defaultParams.progress_callback_user_data = context;
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
context->is_model_loaded = context->llama->loadModel(defaultParams);
|
|
217
|
-
|
|
218
|
-
if (
|
|
219
|
-
params[@"embedding"] && [params[@"embedding"] boolValue] &&
|
|
220
|
-
llama_model_has_encoder(context->llama->model) && llama_model_has_decoder(context->llama->model)
|
|
221
|
-
) {
|
|
222
|
-
delete context->llama;
|
|
223
|
-
@throw [NSException exceptionWithName:@"LlamaException" reason:@"Embedding is not supported in encoder-decoder models" userInfo:nil];
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
std::vector<common_adapter_lora_info> lora;
|
|
227
|
-
if (params[@"lora"]) {
|
|
228
|
-
common_adapter_lora_info la;
|
|
229
|
-
la.path = [params[@"lora"] UTF8String];
|
|
230
|
-
la.scale = 1.0f;
|
|
231
|
-
if (params[@"lora_scaled"]) la.scale = [params[@"lora_scaled"] floatValue];
|
|
232
|
-
lora.push_back(la);
|
|
233
|
-
}
|
|
234
|
-
if (params[@"lora_list"] && [params[@"lora_list"] isKindOfClass:[NSArray class]]) {
|
|
235
|
-
NSArray *lora_list = params[@"lora_list"];
|
|
236
|
-
for (NSDictionary *lora_adapter in lora_list) {
|
|
237
|
-
NSString *path = lora_adapter[@"path"];
|
|
238
|
-
if (!path) continue;
|
|
239
|
-
float scale = [lora_adapter[@"scaled"] floatValue];
|
|
240
|
-
common_adapter_lora_info la;
|
|
241
|
-
la.path = [path UTF8String];
|
|
242
|
-
la.scale = scale;
|
|
243
|
-
lora.push_back(la);
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
if (lora.size() > 0) {
|
|
247
|
-
int result = context->llama->applyLoraAdapters(lora);
|
|
248
|
-
if (result != 0) {
|
|
249
|
-
delete context->llama;
|
|
250
|
-
@throw [NSException exceptionWithName:@"LlamaException" reason:@"Failed to apply lora adapters" userInfo:nil];
|
|
251
|
-
}
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
context->is_metal_enabled = isMetalEnabled;
|
|
255
|
-
context->reason_no_metal = reasonNoMetal;
|
|
256
|
-
|
|
257
|
-
return context;
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
- (void)interruptLoad {
|
|
261
|
-
llama->is_load_interrupted = true;
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
- (bool)isMetalEnabled {
|
|
265
|
-
return is_metal_enabled;
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
- (NSString *)reasonNoMetal {
|
|
269
|
-
return reason_no_metal;
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
- (NSDictionary *)modelInfo {
|
|
273
|
-
char desc[1024];
|
|
274
|
-
llama_model_desc(llama->model, desc, sizeof(desc));
|
|
275
|
-
|
|
276
|
-
int count = llama_model_meta_count(llama->model);
|
|
277
|
-
NSDictionary *meta = [[NSMutableDictionary alloc] init];
|
|
278
|
-
for (int i = 0; i < count; i++) {
|
|
279
|
-
char key[256];
|
|
280
|
-
llama_model_meta_key_by_index(llama->model, i, key, sizeof(key));
|
|
281
|
-
char val[4096];
|
|
282
|
-
llama_model_meta_val_str_by_index(llama->model, i, val, sizeof(val));
|
|
283
|
-
|
|
284
|
-
NSString *keyStr = [NSString stringWithUTF8String:key];
|
|
285
|
-
NSString *valStr = [NSString stringWithUTF8String:val];
|
|
286
|
-
[meta setValue:valStr forKey:keyStr];
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
auto template_tool_use = llama->templates.get()->template_tool_use.get();
|
|
290
|
-
NSDictionary *tool_use_caps_dir = nil;
|
|
291
|
-
if (template_tool_use) {
|
|
292
|
-
auto tool_use_caps = template_tool_use->original_caps();
|
|
293
|
-
tool_use_caps_dir = @{
|
|
294
|
-
@"tools": @(tool_use_caps.supports_tools),
|
|
295
|
-
@"toolCalls": @(tool_use_caps.supports_tool_calls),
|
|
296
|
-
@"toolResponses": @(tool_use_caps.supports_tool_responses),
|
|
297
|
-
@"systemRole": @(tool_use_caps.supports_system_role),
|
|
298
|
-
@"parallelToolCalls": @(tool_use_caps.supports_parallel_tool_calls),
|
|
299
|
-
@"toolCallId": @(tool_use_caps.supports_tool_call_id)
|
|
300
|
-
};
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
auto default_tmpl = llama->templates.get()->template_default.get();
|
|
304
|
-
auto default_tmpl_caps = default_tmpl->original_caps();
|
|
305
|
-
|
|
306
|
-
return @{
|
|
307
|
-
@"desc": [NSString stringWithUTF8String:desc],
|
|
308
|
-
@"size": @(llama_model_size(llama->model)),
|
|
309
|
-
@"nEmbd": @(llama_model_n_embd(llama->model)),
|
|
310
|
-
@"nParams": @(llama_model_n_params(llama->model)),
|
|
311
|
-
@"chatTemplates": @{
|
|
312
|
-
@"llamaChat": @(llama->validateModelChatTemplate(false, nullptr)),
|
|
313
|
-
@"minja": @{
|
|
314
|
-
@"default": @(llama->validateModelChatTemplate(true, nullptr)),
|
|
315
|
-
@"defaultCaps": @{
|
|
316
|
-
@"tools": @(default_tmpl_caps.supports_tools),
|
|
317
|
-
@"toolCalls": @(default_tmpl_caps.supports_tool_calls),
|
|
318
|
-
@"toolResponses": @(default_tmpl_caps.supports_tool_responses),
|
|
319
|
-
@"systemRole": @(default_tmpl_caps.supports_system_role),
|
|
320
|
-
@"parallelToolCalls": @(default_tmpl_caps.supports_parallel_tool_calls),
|
|
321
|
-
@"toolCallId": @(default_tmpl_caps.supports_tool_call_id)
|
|
322
|
-
},
|
|
323
|
-
@"toolUse": @(llama->validateModelChatTemplate(true, "tool_use")),
|
|
324
|
-
@"toolUseCaps": tool_use_caps_dir ?: @{}
|
|
325
|
-
}
|
|
326
|
-
},
|
|
327
|
-
@"metadata": meta,
|
|
328
|
-
|
|
329
|
-
// deprecated
|
|
330
|
-
@"isChatTemplateSupported": @(llama->validateModelChatTemplate(false, nullptr))
|
|
331
|
-
};
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
- (bool)isModelLoaded {
|
|
335
|
-
return is_model_loaded;
|
|
336
|
-
}
|
|
337
|
-
|
|
338
|
-
- (bool)isPredicting {
|
|
339
|
-
return llama->is_predicting;
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
- (NSDictionary *)getFormattedChatWithJinja:(NSString *)messages
|
|
343
|
-
withChatTemplate:(NSString *)chatTemplate
|
|
344
|
-
withJsonSchema:(NSString *)jsonSchema
|
|
345
|
-
withTools:(NSString *)tools
|
|
346
|
-
withParallelToolCalls:(BOOL)parallelToolCalls
|
|
347
|
-
withToolChoice:(NSString *)toolChoice
|
|
348
|
-
{
|
|
349
|
-
auto tmpl_str = chatTemplate == nil ? "" : [chatTemplate UTF8String];
|
|
350
|
-
|
|
351
|
-
NSMutableDictionary *result = [[NSMutableDictionary alloc] init];
|
|
352
|
-
auto chatParams = llama->getFormattedChatWithJinja(
|
|
353
|
-
[messages UTF8String],
|
|
354
|
-
tmpl_str,
|
|
355
|
-
jsonSchema == nil ? "" : [jsonSchema UTF8String],
|
|
356
|
-
tools == nil ? "" : [tools UTF8String],
|
|
357
|
-
parallelToolCalls,
|
|
358
|
-
toolChoice == nil ? "" : [toolChoice UTF8String]
|
|
359
|
-
);
|
|
360
|
-
result[@"prompt"] = [NSString stringWithUTF8String:chatParams.prompt.c_str()];
|
|
361
|
-
result[@"chat_format"] = @(static_cast<int>(chatParams.format));
|
|
362
|
-
result[@"grammar"] = [NSString stringWithUTF8String:chatParams.grammar.c_str()];
|
|
363
|
-
result[@"grammar_lazy"] = @(chatParams.grammar_lazy);
|
|
364
|
-
NSMutableArray *grammar_triggers = [[NSMutableArray alloc] init];
|
|
365
|
-
for (const auto & trigger : chatParams.grammar_triggers) {
|
|
366
|
-
[grammar_triggers addObject:@{
|
|
367
|
-
@"type": @(trigger.type),
|
|
368
|
-
@"value": [NSString stringWithUTF8String:trigger.value.c_str()],
|
|
369
|
-
@"token": @(trigger.token),
|
|
370
|
-
}];
|
|
371
|
-
}
|
|
372
|
-
result[@"grammar_triggers"] = grammar_triggers;
|
|
373
|
-
NSMutableArray *preserved_tokens = [[NSMutableArray alloc] init];
|
|
374
|
-
for (const auto & token : chatParams.preserved_tokens) {
|
|
375
|
-
[preserved_tokens addObject:[NSString stringWithUTF8String:token.c_str()]];
|
|
376
|
-
}
|
|
377
|
-
result[@"preserved_tokens"] = preserved_tokens;
|
|
378
|
-
NSMutableArray *additional_stops = [[NSMutableArray alloc] init];
|
|
379
|
-
for (const auto & stop : chatParams.additional_stops) {
|
|
380
|
-
[additional_stops addObject:[NSString stringWithUTF8String:stop.c_str()]];
|
|
381
|
-
}
|
|
382
|
-
result[@"additional_stops"] = additional_stops;
|
|
383
|
-
|
|
384
|
-
return result;
|
|
385
|
-
}
|
|
386
|
-
|
|
387
|
-
- (NSString *)getFormattedChat:(NSString *)messages withChatTemplate:(NSString *)chatTemplate {
|
|
388
|
-
auto tmpl_str = chatTemplate == nil ? "" : [chatTemplate UTF8String];
|
|
389
|
-
return [NSString stringWithUTF8String:llama->getFormattedChat(
|
|
390
|
-
[messages UTF8String],
|
|
391
|
-
tmpl_str
|
|
392
|
-
).c_str()];;
|
|
393
|
-
}
|
|
394
|
-
|
|
395
|
-
- (NSArray *)tokenProbsToDict:(std::vector<cactus::completion_token_output>)probs {
|
|
396
|
-
NSMutableArray *out = [[NSMutableArray alloc] init];
|
|
397
|
-
for (const auto &prob : probs)
|
|
398
|
-
{
|
|
399
|
-
NSMutableArray *probsForToken = [[NSMutableArray alloc] init];
|
|
400
|
-
for (const auto &p : prob.probs)
|
|
401
|
-
{
|
|
402
|
-
std::string tokStr = cactus::tokens_to_output_formatted_string(llama->ctx, p.tok);
|
|
403
|
-
[probsForToken addObject:@{
|
|
404
|
-
@"tok_str": [NSString stringWithUTF8String:tokStr.c_str()],
|
|
405
|
-
@"prob": [NSNumber numberWithDouble:p.prob]
|
|
406
|
-
}];
|
|
407
|
-
}
|
|
408
|
-
std::string tokStr = cactus::tokens_to_output_formatted_string(llama->ctx, prob.tok);
|
|
409
|
-
[out addObject:@{
|
|
410
|
-
@"content": [NSString stringWithUTF8String:tokStr.c_str()],
|
|
411
|
-
@"probs": probsForToken
|
|
412
|
-
}];
|
|
413
|
-
}
|
|
414
|
-
return out;
|
|
415
|
-
}
|
|
416
|
-
|
|
417
|
-
- (NSDictionary *)completion:(NSDictionary *)params
|
|
418
|
-
onToken:(void (^)(NSMutableDictionary * tokenResult))onToken
|
|
419
|
-
{
|
|
420
|
-
// llama->rewind();
|
|
421
|
-
|
|
422
|
-
//llama_reset_timings(llama->ctx);
|
|
423
|
-
|
|
424
|
-
NSString *prompt = [params objectForKey:@"prompt"];
|
|
425
|
-
|
|
426
|
-
llama->params.prompt = [prompt UTF8String];
|
|
427
|
-
llama->params.sampling.seed = params[@"seed"] ? [params[@"seed"] intValue] : -1;
|
|
428
|
-
|
|
429
|
-
if (params[@"n_threads"]) {
|
|
430
|
-
int nThreads = params[@"n_threads"] ? [params[@"n_threads"] intValue] : llama->params.cpuparams.n_threads;
|
|
431
|
-
const int maxThreads = (int) [[NSProcessInfo processInfo] processorCount];
|
|
432
|
-
// Use 2 threads by default on 4-core devices, 4 threads on more cores
|
|
433
|
-
const int defaultNThreads = nThreads == 4 ? 2 : MIN(4, maxThreads);
|
|
434
|
-
llama->params.cpuparams.n_threads = nThreads > 0 ? nThreads : defaultNThreads;
|
|
435
|
-
}
|
|
436
|
-
if (params[@"n_predict"]) llama->params.n_predict = [params[@"n_predict"] intValue];
|
|
437
|
-
if (params[@"ignore_eos"]) llama->params.sampling.ignore_eos = [params[@"ignore_eos"] boolValue];
|
|
438
|
-
|
|
439
|
-
auto & sparams = llama->params.sampling;
|
|
440
|
-
|
|
441
|
-
if (params[@"temperature"]) sparams.temp = [params[@"temperature"] doubleValue];
|
|
442
|
-
|
|
443
|
-
if (params[@"n_probs"]) sparams.n_probs = [params[@"n_probs"] intValue];
|
|
444
|
-
|
|
445
|
-
if (params[@"penalty_last_n"]) sparams.penalty_last_n = [params[@"penalty_last_n"] intValue];
|
|
446
|
-
if (params[@"penalty_repeat"]) sparams.penalty_repeat = [params[@"penalty_repeat"] doubleValue];
|
|
447
|
-
if (params[@"penalty_freq"]) sparams.penalty_freq = [params[@"penalty_freq"] doubleValue];
|
|
448
|
-
if (params[@"penalty_present"]) sparams.penalty_present = [params[@"penalty_present"] doubleValue];
|
|
449
|
-
|
|
450
|
-
if (params[@"mirostat"]) sparams.mirostat = [params[@"mirostat"] intValue];
|
|
451
|
-
if (params[@"mirostat_tau"]) sparams.mirostat_tau = [params[@"mirostat_tau"] doubleValue];
|
|
452
|
-
if (params[@"mirostat_eta"]) sparams.mirostat_eta = [params[@"mirostat_eta"] doubleValue];
|
|
453
|
-
|
|
454
|
-
if (params[@"top_k"]) sparams.top_k = [params[@"top_k"] intValue];
|
|
455
|
-
if (params[@"top_p"]) sparams.top_p = [params[@"top_p"] doubleValue];
|
|
456
|
-
if (params[@"min_p"]) sparams.min_p = [params[@"min_p"] doubleValue];
|
|
457
|
-
if (params[@"xtc_threshold"]) sparams.xtc_threshold = [params[@"xtc_threshold"] doubleValue];
|
|
458
|
-
if (params[@"xtc_probability"]) sparams.xtc_probability = [params[@"xtc_probability"] doubleValue];
|
|
459
|
-
if (params[@"typical_p"]) sparams.typ_p = [params[@"typical_p"] doubleValue];
|
|
460
|
-
|
|
461
|
-
if (params[@"dry_multiplier"]) sparams.dry_multiplier = [params[@"dry_multiplier"] doubleValue];
|
|
462
|
-
if (params[@"dry_base"]) sparams.dry_base = [params[@"dry_base"] doubleValue];
|
|
463
|
-
if (params[@"dry_allowed_length"]) sparams.dry_allowed_length = [params[@"dry_allowed_length"] intValue];
|
|
464
|
-
if (params[@"dry_penalty_last_n"]) sparams.dry_penalty_last_n = [params[@"dry_penalty_last_n"] intValue];
|
|
465
|
-
|
|
466
|
-
if (params[@"top_n_sigma"]) sparams.top_n_sigma = [params[@"top_n_sigma"] doubleValue];
|
|
467
|
-
|
|
468
|
-
// dry break seq
|
|
469
|
-
if (params[@"dry_sequence_breakers"] && [params[@"dry_sequence_breakers"] isKindOfClass:[NSArray class]]) {
|
|
470
|
-
NSArray *dry_sequence_breakers = params[@"dry_sequence_breakers"];
|
|
471
|
-
for (NSString *s in dry_sequence_breakers) {
|
|
472
|
-
sparams.dry_sequence_breakers.push_back([s UTF8String]);
|
|
473
|
-
}
|
|
474
|
-
}
|
|
475
|
-
|
|
476
|
-
if (params[@"grammar"]) {
|
|
477
|
-
sparams.grammar = [params[@"grammar"] UTF8String];
|
|
478
|
-
}
|
|
479
|
-
|
|
480
|
-
if (params[@"json_schema"] && !params[@"grammar"]) {
|
|
481
|
-
sparams.grammar = json_schema_to_grammar(json::parse([params[@"json_schema"] UTF8String]));
|
|
482
|
-
}
|
|
483
|
-
|
|
484
|
-
if (params[@"grammar_lazy"]) {
|
|
485
|
-
sparams.grammar_lazy = [params[@"grammar_lazy"] boolValue];
|
|
486
|
-
}
|
|
487
|
-
|
|
488
|
-
if (params[@"preserved_tokens"] && [params[@"preserved_tokens"] isKindOfClass:[NSArray class]]) {
|
|
489
|
-
NSArray *preserved_tokens = params[@"preserved_tokens"];
|
|
490
|
-
for (NSString *token in preserved_tokens) {
|
|
491
|
-
auto ids = common_tokenize(llama->ctx, [token UTF8String], /* add_special= */ false, /* parse_special= */ true);
|
|
492
|
-
if (ids.size() == 1) {
|
|
493
|
-
sparams.preserved_tokens.insert(ids[0]);
|
|
494
|
-
} else {
|
|
495
|
-
// LOG_WRN("Not preserved because more than 1 token (wrong chat template override?): %s\n", [token UTF8String]);
|
|
496
|
-
}
|
|
497
|
-
}
|
|
498
|
-
}
|
|
499
|
-
|
|
500
|
-
if (params[@"grammar_triggers"] && [params[@"grammar_triggers"] isKindOfClass:[NSArray class]]) {
|
|
501
|
-
NSArray *grammar_triggers = params[@"grammar_triggers"];
|
|
502
|
-
for (NSDictionary *grammar_trigger in grammar_triggers) {
|
|
503
|
-
const auto type = static_cast<common_grammar_trigger_type>([grammar_trigger[@"type"] intValue]);
|
|
504
|
-
const auto & word = [grammar_trigger[@"value"] UTF8String];
|
|
505
|
-
|
|
506
|
-
if (type == COMMON_GRAMMAR_TRIGGER_TYPE_WORD) {
|
|
507
|
-
auto ids = common_tokenize(llama->ctx, word, /* add_special= */ false, /* parse_special= */ true);
|
|
508
|
-
if (ids.size() == 1) {
|
|
509
|
-
auto token = ids[0];
|
|
510
|
-
if (std::find(sparams.preserved_tokens.begin(), sparams.preserved_tokens.end(), (llama_token) token) == sparams.preserved_tokens.end()) {
|
|
511
|
-
throw std::runtime_error("Grammar trigger word should be marked as preserved token");
|
|
512
|
-
}
|
|
513
|
-
common_grammar_trigger trigger;
|
|
514
|
-
trigger.type = COMMON_GRAMMAR_TRIGGER_TYPE_TOKEN;
|
|
515
|
-
trigger.value = word;
|
|
516
|
-
trigger.token = token;
|
|
517
|
-
sparams.grammar_triggers.push_back(std::move(trigger));
|
|
518
|
-
} else {
|
|
519
|
-
sparams.grammar_triggers.push_back({COMMON_GRAMMAR_TRIGGER_TYPE_WORD, word});
|
|
520
|
-
}
|
|
521
|
-
} else {
|
|
522
|
-
common_grammar_trigger trigger;
|
|
523
|
-
trigger.type = type;
|
|
524
|
-
trigger.value = word;
|
|
525
|
-
if (type == COMMON_GRAMMAR_TRIGGER_TYPE_TOKEN) {
|
|
526
|
-
const auto token = (llama_token) [grammar_trigger[@"token"] intValue];
|
|
527
|
-
trigger.token = token;
|
|
528
|
-
}
|
|
529
|
-
sparams.grammar_triggers.push_back(std::move(trigger));
|
|
530
|
-
}
|
|
531
|
-
}
|
|
532
|
-
}
|
|
533
|
-
|
|
534
|
-
llama->params.antiprompt.clear();
|
|
535
|
-
if (params[@"stop"]) {
|
|
536
|
-
NSArray *stop = params[@"stop"];
|
|
537
|
-
for (NSString *s in stop) {
|
|
538
|
-
llama->params.antiprompt.push_back([s UTF8String]);
|
|
539
|
-
}
|
|
540
|
-
}
|
|
541
|
-
|
|
542
|
-
const llama_model * model = llama_get_model(llama->ctx);
|
|
543
|
-
const llama_vocab * vocab = llama_model_get_vocab(model);
|
|
544
|
-
|
|
545
|
-
sparams.logit_bias.clear();
|
|
546
|
-
if (params[@"ignore_eos"] && [params[@"ignore_eos"] boolValue]) {
|
|
547
|
-
sparams.logit_bias[llama_vocab_eos(vocab)].bias = -INFINITY;
|
|
548
|
-
}
|
|
549
|
-
|
|
550
|
-
if (params[@"logit_bias"] && [params[@"logit_bias"] isKindOfClass:[NSArray class]]) {
|
|
551
|
-
const int n_vocab = llama_vocab_n_tokens(vocab);
|
|
552
|
-
NSArray *logit_bias = params[@"logit_bias"];
|
|
553
|
-
for (NSArray *el in logit_bias) {
|
|
554
|
-
if ([el isKindOfClass:[NSArray class]] && [el count] == 2) {
|
|
555
|
-
llama_token tok = [el[0] intValue];
|
|
556
|
-
if (tok >= 0 && tok < n_vocab) {
|
|
557
|
-
if ([el[1] isKindOfClass:[NSNumber class]]) {
|
|
558
|
-
sparams.logit_bias[tok].bias = [el[1] doubleValue];
|
|
559
|
-
} else if ([el[1] isKindOfClass:[NSNumber class]] && ![el[1] boolValue]) {
|
|
560
|
-
sparams.logit_bias[tok].bias = -INFINITY;
|
|
561
|
-
}
|
|
562
|
-
}
|
|
563
|
-
}
|
|
564
|
-
}
|
|
565
|
-
}
|
|
566
|
-
|
|
567
|
-
if (!llama->initSampling()) {
|
|
568
|
-
@throw [NSException exceptionWithName:@"LlamaException" reason:@"Failed to initialize sampling" userInfo:nil];
|
|
569
|
-
}
|
|
570
|
-
llama->beginCompletion();
|
|
571
|
-
llama->loadPrompt();
|
|
572
|
-
|
|
573
|
-
size_t sent_count = 0;
|
|
574
|
-
size_t sent_token_probs_index = 0;
|
|
575
|
-
|
|
576
|
-
while (llama->has_next_token && !llama->is_interrupted) {
|
|
577
|
-
const cactus::completion_token_output token_with_probs = llama->doCompletion();
|
|
578
|
-
if (token_with_probs.tok == -1 || llama->incomplete) {
|
|
579
|
-
continue;
|
|
580
|
-
}
|
|
581
|
-
const std::string token_text = common_token_to_piece(llama->ctx, token_with_probs.tok);
|
|
582
|
-
|
|
583
|
-
size_t pos = std::min(sent_count, llama->generated_text.size());
|
|
584
|
-
|
|
585
|
-
const std::string str_test = llama->generated_text.substr(pos);
|
|
586
|
-
bool is_stop_full = false;
|
|
587
|
-
size_t stop_pos =
|
|
588
|
-
llama->findStoppingStrings(str_test, token_text.size(), cactus::STOP_FULL);
|
|
589
|
-
if (stop_pos != std::string::npos) {
|
|
590
|
-
is_stop_full = true;
|
|
591
|
-
llama->generated_text.erase(
|
|
592
|
-
llama->generated_text.begin() + pos + stop_pos,
|
|
593
|
-
llama->generated_text.end());
|
|
594
|
-
pos = std::min(sent_count, llama->generated_text.size());
|
|
595
|
-
} else {
|
|
596
|
-
is_stop_full = false;
|
|
597
|
-
stop_pos = llama->findStoppingStrings(str_test, token_text.size(),
|
|
598
|
-
cactus::STOP_PARTIAL);
|
|
599
|
-
}
|
|
600
|
-
|
|
601
|
-
if (
|
|
602
|
-
stop_pos == std::string::npos ||
|
|
603
|
-
// Send rest of the text if we are at the end of the generation
|
|
604
|
-
(!llama->has_next_token && !is_stop_full && stop_pos > 0)
|
|
605
|
-
) {
|
|
606
|
-
const std::string to_send = llama->generated_text.substr(pos, std::string::npos);
|
|
607
|
-
|
|
608
|
-
sent_count += to_send.size();
|
|
609
|
-
|
|
610
|
-
std::vector<cactus::completion_token_output> probs_output = {};
|
|
611
|
-
|
|
612
|
-
NSMutableDictionary *tokenResult = [[NSMutableDictionary alloc] init];
|
|
613
|
-
tokenResult[@"token"] = [NSString stringWithUTF8String:to_send.c_str()];
|
|
614
|
-
|
|
615
|
-
if (llama->params.sampling.n_probs > 0) {
|
|
616
|
-
const std::vector<llama_token> to_send_toks = common_tokenize(llama->ctx, to_send, false);
|
|
617
|
-
size_t probs_pos = std::min(sent_token_probs_index, llama->generated_token_probs.size());
|
|
618
|
-
size_t probs_stop_pos = std::min(sent_token_probs_index + to_send_toks.size(), llama->generated_token_probs.size());
|
|
619
|
-
if (probs_pos < probs_stop_pos) {
|
|
620
|
-
probs_output = std::vector<cactus::completion_token_output>(llama->generated_token_probs.begin() + probs_pos, llama->generated_token_probs.begin() + probs_stop_pos);
|
|
621
|
-
}
|
|
622
|
-
sent_token_probs_index = probs_stop_pos;
|
|
623
|
-
|
|
624
|
-
tokenResult[@"completion_probabilities"] = [self tokenProbsToDict:probs_output];
|
|
625
|
-
}
|
|
626
|
-
|
|
627
|
-
onToken(tokenResult);
|
|
628
|
-
}
|
|
629
|
-
}
|
|
630
|
-
|
|
631
|
-
llama_perf_context_print(llama->ctx);
|
|
632
|
-
llama->is_predicting = false;
|
|
633
|
-
|
|
634
|
-
const auto timings = llama_perf_context(llama->ctx);
|
|
635
|
-
|
|
636
|
-
NSMutableArray *toolCalls = nil;
|
|
637
|
-
NSString *reasoningContent = nil;
|
|
638
|
-
NSString *content = nil;
|
|
639
|
-
if (!llama->is_interrupted) {
|
|
640
|
-
try {
|
|
641
|
-
auto chat_format = params[@"chat_format"] ? [params[@"chat_format"] intValue] : COMMON_CHAT_FORMAT_CONTENT_ONLY;
|
|
642
|
-
common_chat_msg message = common_chat_parse(llama->generated_text, static_cast<common_chat_format>(chat_format));
|
|
643
|
-
if (!message.reasoning_content.empty()) {
|
|
644
|
-
reasoningContent = [NSString stringWithUTF8String:message.reasoning_content.c_str()];
|
|
645
|
-
}
|
|
646
|
-
content = [NSString stringWithUTF8String:message.content.c_str()];
|
|
647
|
-
toolCalls = [[NSMutableArray alloc] init];
|
|
648
|
-
for (const auto &tc : message.tool_calls) {
|
|
649
|
-
[toolCalls addObject:@{
|
|
650
|
-
@"type": @"function",
|
|
651
|
-
@"function": @{
|
|
652
|
-
@"name": [NSString stringWithUTF8String:tc.name.c_str()],
|
|
653
|
-
@"arguments": [NSString stringWithUTF8String:tc.arguments.c_str()],
|
|
654
|
-
},
|
|
655
|
-
@"id": tc.id.empty() ? [NSNull null] : [NSString stringWithUTF8String:tc.id.c_str()],
|
|
656
|
-
}];
|
|
657
|
-
}
|
|
658
|
-
} catch (const std::exception &e) {
|
|
659
|
-
// NSLog(@"Error parsing tool calls: %s", e.what());
|
|
660
|
-
}
|
|
661
|
-
}
|
|
662
|
-
|
|
663
|
-
NSMutableDictionary *result = [[NSMutableDictionary alloc] init];
|
|
664
|
-
result[@"text"] = [NSString stringWithUTF8String:llama->generated_text.c_str()]; // Original text
|
|
665
|
-
if (content) result[@"content"] = content;
|
|
666
|
-
if (reasoningContent) result[@"reasoning_content"] = reasoningContent;
|
|
667
|
-
if (toolCalls && toolCalls.count > 0) result[@"tool_calls"] = toolCalls;
|
|
668
|
-
result[@"completion_probabilities"] = [self tokenProbsToDict:llama->generated_token_probs];
|
|
669
|
-
result[@"tokens_predicted"] = @(llama->num_tokens_predicted);
|
|
670
|
-
result[@"tokens_evaluated"] = @(llama->num_prompt_tokens);
|
|
671
|
-
result[@"truncated"] = @(llama->truncated);
|
|
672
|
-
result[@"stopped_eos"] = @(llama->stopped_eos);
|
|
673
|
-
result[@"stopped_word"] = @(llama->stopped_word);
|
|
674
|
-
result[@"stopped_limit"] = @(llama->stopped_limit);
|
|
675
|
-
result[@"stopping_word"] = [NSString stringWithUTF8String:llama->stopping_word.c_str()];
|
|
676
|
-
result[@"tokens_cached"] = @(llama->n_past);
|
|
677
|
-
result[@"timings"] = @{
|
|
678
|
-
@"prompt_n": @(timings.n_p_eval),
|
|
679
|
-
@"prompt_ms": @(timings.t_p_eval_ms),
|
|
680
|
-
@"prompt_per_token_ms": @(timings.t_p_eval_ms / timings.n_p_eval),
|
|
681
|
-
@"prompt_per_second": @(1e3 / timings.t_p_eval_ms * timings.n_p_eval),
|
|
682
|
-
@"predicted_n": @(timings.n_eval),
|
|
683
|
-
@"predicted_n": @(timings.n_eval),
|
|
684
|
-
@"predicted_ms": @(timings.t_eval_ms),
|
|
685
|
-
@"predicted_per_token_ms": @(timings.t_eval_ms / timings.n_eval),
|
|
686
|
-
@"predicted_per_second": @(1e3 / timings.t_eval_ms * timings.n_eval),
|
|
687
|
-
};
|
|
688
|
-
return result;
|
|
689
|
-
}
|
|
690
|
-
|
|
691
|
-
- (void)stopCompletion {
|
|
692
|
-
llama->is_interrupted = true;
|
|
693
|
-
}
|
|
694
|
-
|
|
695
|
-
- (NSArray *)tokenize:(NSString *)text {
|
|
696
|
-
const std::vector<llama_token> toks = common_tokenize(llama->ctx, [text UTF8String], false);
|
|
697
|
-
NSMutableArray *result = [[NSMutableArray alloc] init];
|
|
698
|
-
for (llama_token tok : toks) {
|
|
699
|
-
[result addObject:@(tok)];
|
|
700
|
-
}
|
|
701
|
-
return result;
|
|
702
|
-
}
|
|
703
|
-
|
|
704
|
-
- (NSArray *)tokenize:(NSString *)text withMediaPaths:(NSArray *)mediaPaths {
|
|
705
|
-
std::vector<std::string> media_paths_vector;
|
|
706
|
-
if (mediaPaths) {
|
|
707
|
-
for (NSString *mediaPath in mediaPaths) {
|
|
708
|
-
media_paths_vector.push_back([mediaPath UTF8String]);
|
|
709
|
-
}
|
|
710
|
-
}
|
|
711
|
-
|
|
712
|
-
cactus::cactus_tokenize_result tokenize_result = llama->tokenize([text UTF8String], media_paths_vector);
|
|
713
|
-
|
|
714
|
-
// Return just the tokens array to match the method signature
|
|
715
|
-
NSMutableArray *tokens = [[NSMutableArray alloc] init];
|
|
716
|
-
for (const auto &tok : tokenize_result.tokens) {
|
|
717
|
-
[tokens addObject:@(tok)];
|
|
718
|
-
}
|
|
719
|
-
|
|
720
|
-
return tokens;
|
|
721
|
-
}
|
|
722
|
-
|
|
723
|
-
- (NSString *)detokenize:(NSArray *)tokens {
|
|
724
|
-
std::vector<llama_token> toks;
|
|
725
|
-
for (NSNumber *tok in tokens) {
|
|
726
|
-
toks.push_back([tok intValue]);
|
|
727
|
-
}
|
|
728
|
-
const std::string text = cactus::tokens_to_str(llama->ctx, toks.cbegin(), toks.cend());
|
|
729
|
-
return [NSString stringWithUTF8String:text.c_str()];
|
|
730
|
-
}
|
|
731
|
-
|
|
732
|
-
- (NSDictionary *)embedding:(NSString *)text params:(NSDictionary *)params {
|
|
733
|
-
if (llama->params.embedding != true) {
|
|
734
|
-
@throw [NSException exceptionWithName:@"LlamaException" reason:@"Embedding is not enabled" userInfo:nil];
|
|
735
|
-
}
|
|
736
|
-
|
|
737
|
-
common_params embdParams;
|
|
738
|
-
embdParams.embedding = true;
|
|
739
|
-
embdParams.embd_normalize = llama->params.embd_normalize;
|
|
740
|
-
|
|
741
|
-
if (params[@"embd_normalize"] && [params[@"embd_normalize"] isKindOfClass:[NSNumber class]]) {
|
|
742
|
-
embdParams.embd_normalize = [params[@"embd_normalize"] intValue];
|
|
743
|
-
}
|
|
744
|
-
|
|
745
|
-
llama->rewind();
|
|
746
|
-
|
|
747
|
-
llama_perf_context_reset(llama->ctx);
|
|
748
|
-
|
|
749
|
-
llama->params.prompt = [text UTF8String];
|
|
750
|
-
|
|
751
|
-
llama->params.n_predict = 0;
|
|
752
|
-
|
|
753
|
-
if (!llama->initSampling()) {
|
|
754
|
-
@throw [NSException exceptionWithName:@"LlamaException" reason:@"Failed to initialize sampling" userInfo:nil];
|
|
755
|
-
}
|
|
756
|
-
llama->beginCompletion();
|
|
757
|
-
llama->loadPrompt();
|
|
758
|
-
llama->doCompletion();
|
|
759
|
-
|
|
760
|
-
std::vector<float> result = llama->getEmbedding(embdParams);
|
|
761
|
-
|
|
762
|
-
NSMutableDictionary *resultDict = [[NSMutableDictionary alloc] init];
|
|
763
|
-
NSMutableArray *embeddingResult = [[NSMutableArray alloc] init];
|
|
764
|
-
for (float f : result) {
|
|
765
|
-
[embeddingResult addObject:@(f)];
|
|
766
|
-
}
|
|
767
|
-
resultDict[@"embedding"] = embeddingResult;
|
|
768
|
-
NSMutableArray *promptTokens = [[NSMutableArray alloc] init];
|
|
769
|
-
for (llama_token tok : llama->embd) {
|
|
770
|
-
[promptTokens addObject:[NSString stringWithUTF8String:common_token_to_piece(llama->ctx, tok).c_str()]];
|
|
771
|
-
}
|
|
772
|
-
resultDict[@"prompt_tokens"] = promptTokens;
|
|
773
|
-
|
|
774
|
-
llama->is_predicting = false;
|
|
775
|
-
return resultDict;
|
|
776
|
-
}
|
|
777
|
-
|
|
778
|
-
- (NSDictionary *)loadSession:(NSString *)path {
|
|
779
|
-
if (!path || [path length] == 0) {
|
|
780
|
-
@throw [NSException exceptionWithName:@"LlamaException" reason:@"Session path is empty" userInfo:nil];
|
|
781
|
-
}
|
|
782
|
-
if (![[NSFileManager defaultManager] fileExistsAtPath:path]) {
|
|
783
|
-
@throw [NSException exceptionWithName:@"LlamaException" reason:@"Session file does not exist" userInfo:nil];
|
|
784
|
-
}
|
|
785
|
-
|
|
786
|
-
size_t n_token_count_out = 0;
|
|
787
|
-
llama->embd.resize(llama->params.n_ctx);
|
|
788
|
-
if (!llama_state_load_file(llama->ctx, [path UTF8String], llama->embd.data(), llama->embd.capacity(), &n_token_count_out)) {
|
|
789
|
-
@throw [NSException exceptionWithName:@"LlamaException" reason:@"Failed to load session" userInfo:nil];
|
|
790
|
-
}
|
|
791
|
-
llama->embd.resize(n_token_count_out);
|
|
792
|
-
const std::string text = cactus::tokens_to_str(llama->ctx, llama->embd.cbegin(), llama->embd.cend());
|
|
793
|
-
return @{
|
|
794
|
-
@"tokens_loaded": @(n_token_count_out),
|
|
795
|
-
@"prompt": [NSString stringWithUTF8String:text.c_str()]
|
|
796
|
-
};
|
|
797
|
-
}
|
|
798
|
-
|
|
799
|
-
- (int)saveSession:(NSString *)path size:(int)size {
|
|
800
|
-
if (!path || [path length] == 0) {
|
|
801
|
-
@throw [NSException exceptionWithName:@"LlamaException" reason:@"Session path is empty" userInfo:nil];
|
|
802
|
-
}
|
|
803
|
-
std::vector<llama_token> session_tokens = llama->embd;
|
|
804
|
-
int default_size = session_tokens.size();
|
|
805
|
-
int save_size = size > 0 && size <= default_size ? size : default_size;
|
|
806
|
-
if (!llama_state_save_file(llama->ctx, [path UTF8String], session_tokens.data(), save_size)) {
|
|
807
|
-
@throw [NSException exceptionWithName:@"LlamaException" reason:@"Failed to save session" userInfo:nil];
|
|
808
|
-
}
|
|
809
|
-
return session_tokens.size();
|
|
810
|
-
}
|
|
811
|
-
|
|
812
|
-
- (NSString *)bench:(int)pp tg:(int)tg pl:(int)pl nr:(int)nr {
|
|
813
|
-
return [NSString stringWithUTF8String:llama->bench(pp, tg, pl, nr).c_str()];
|
|
814
|
-
}
|
|
815
|
-
|
|
816
|
-
- (void)applyLoraAdapters:(NSArray *)loraAdapters {
|
|
817
|
-
std::vector<common_adapter_lora_info> lora_adapters;
|
|
818
|
-
for (NSDictionary *loraAdapter in loraAdapters) {
|
|
819
|
-
common_adapter_lora_info la;
|
|
820
|
-
la.path = [loraAdapter[@"path"] UTF8String];
|
|
821
|
-
la.scale = [loraAdapter[@"scaled"] doubleValue];
|
|
822
|
-
la.ptr = llama_adapter_lora_init(llama->model, la.path.c_str());
|
|
823
|
-
if (la.ptr == nullptr) {
|
|
824
|
-
@throw [NSException exceptionWithName:@"LlamaException" reason:@"Failed to apply lora adapter" userInfo:nil];
|
|
825
|
-
}
|
|
826
|
-
lora_adapters.push_back(la);
|
|
827
|
-
}
|
|
828
|
-
int result = llama->applyLoraAdapters(lora_adapters);
|
|
829
|
-
if (result != 0) {
|
|
830
|
-
@throw [NSException exceptionWithName:@"LlamaException" reason:@"Failed to apply lora adapters" userInfo:nil];
|
|
831
|
-
}
|
|
832
|
-
}
|
|
833
|
-
|
|
834
|
-
- (void)removeLoraAdapters {
|
|
835
|
-
llama->removeLoraAdapters();
|
|
836
|
-
}
|
|
837
|
-
|
|
838
|
-
- (NSArray *)getLoadedLoraAdapters {
|
|
839
|
-
std::vector<common_adapter_lora_info> loaded_lora_adapters = llama->getLoadedLoraAdapters();
|
|
840
|
-
NSMutableArray *result = [[NSMutableArray alloc] init];
|
|
841
|
-
for (common_adapter_lora_info &la : loaded_lora_adapters) {
|
|
842
|
-
[result addObject:@{
|
|
843
|
-
@"path": [NSString stringWithUTF8String:la.path.c_str()],
|
|
844
|
-
@"scale": @(la.scale)
|
|
845
|
-
}];
|
|
846
|
-
}
|
|
847
|
-
return result;
|
|
848
|
-
}
|
|
849
|
-
|
|
850
|
-
// New Multimodal Methods
|
|
851
|
-
- (BOOL)initMultimodal:(NSString *)mmprojPath useGpu:(BOOL)useGpu {
|
|
852
|
-
return llama->initMultimodal([mmprojPath UTF8String], useGpu);
|
|
853
|
-
}
|
|
854
|
-
|
|
855
|
-
- (BOOL)isMultimodalEnabled {
|
|
856
|
-
return llama->isMultimodalEnabled();
|
|
857
|
-
}
|
|
858
|
-
|
|
859
|
-
- (BOOL)isMultimodalSupportVision {
|
|
860
|
-
return llama->isMultimodalSupportVision();
|
|
861
|
-
}
|
|
862
|
-
|
|
863
|
-
- (BOOL)isMultimodalSupportAudio {
|
|
864
|
-
return llama->isMultimodalSupportAudio();
|
|
865
|
-
}
|
|
866
|
-
|
|
867
|
-
- (void)releaseMultimodal {
|
|
868
|
-
llama->releaseMultimodal();
|
|
869
|
-
}
|
|
870
|
-
|
|
871
|
-
- (NSDictionary *)multimodalCompletion:(NSString *)prompt
|
|
872
|
-
withMediaPaths:(NSArray *)mediaPaths
|
|
873
|
-
params:(NSDictionary *)params
|
|
874
|
-
onToken:(void (^)(NSMutableDictionary *tokenResult))onToken {
|
|
875
|
-
|
|
876
|
-
if (!llama->isMultimodalEnabled()) {
|
|
877
|
-
@throw [NSException exceptionWithName:@"LlamaException" reason:@"Multimodal is not enabled" userInfo:nil];
|
|
878
|
-
}
|
|
879
|
-
|
|
880
|
-
// llama->rewind();
|
|
881
|
-
|
|
882
|
-
llama->params.prompt = [prompt UTF8String];
|
|
883
|
-
llama->params.sampling.seed = params[@"seed"] ? [params[@"seed"] intValue] : -1;
|
|
884
|
-
|
|
885
|
-
// Set all completion parameters (similar to existing completion method)
|
|
886
|
-
if (params[@"n_threads"]) {
|
|
887
|
-
int nThreads = [params[@"n_threads"] intValue];
|
|
888
|
-
const int maxThreads = (int) [[NSProcessInfo processInfo] processorCount];
|
|
889
|
-
const int defaultNThreads = nThreads == 4 ? 2 : MIN(4, maxThreads);
|
|
890
|
-
llama->params.cpuparams.n_threads = nThreads > 0 ? nThreads : defaultNThreads;
|
|
891
|
-
}
|
|
892
|
-
if (params[@"n_predict"]) llama->params.n_predict = [params[@"n_predict"] intValue];
|
|
893
|
-
if (params[@"ignore_eos"]) llama->params.sampling.ignore_eos = [params[@"ignore_eos"] boolValue];
|
|
894
|
-
|
|
895
|
-
auto & sparams = llama->params.sampling;
|
|
896
|
-
if (params[@"temperature"]) sparams.temp = [params[@"temperature"] doubleValue];
|
|
897
|
-
if (params[@"top_k"]) sparams.top_k = [params[@"top_k"] intValue];
|
|
898
|
-
if (params[@"top_p"]) sparams.top_p = [params[@"top_p"] doubleValue];
|
|
899
|
-
|
|
900
|
-
// Convert media paths
|
|
901
|
-
std::vector<std::string> media_paths_vector;
|
|
902
|
-
if (mediaPaths) {
|
|
903
|
-
for (NSString *mediaPath in mediaPaths) {
|
|
904
|
-
media_paths_vector.push_back([mediaPath UTF8String]);
|
|
905
|
-
}
|
|
906
|
-
}
|
|
907
|
-
|
|
908
|
-
if (!llama->initSampling()) {
|
|
909
|
-
@throw [NSException exceptionWithName:@"LlamaException" reason:@"Failed to initialize sampling" userInfo:nil];
|
|
910
|
-
}
|
|
911
|
-
|
|
912
|
-
llama->beginCompletion();
|
|
913
|
-
|
|
914
|
-
@try {
|
|
915
|
-
llama->loadPrompt(media_paths_vector);
|
|
916
|
-
} @catch (NSException *exception) {
|
|
917
|
-
@throw [NSException exceptionWithName:@"LlamaException" reason:exception.reason userInfo:nil];
|
|
918
|
-
}
|
|
919
|
-
|
|
920
|
-
size_t sent_count = 0;
|
|
921
|
-
while (llama->has_next_token && !llama->is_interrupted) {
|
|
922
|
-
const cactus::completion_token_output token_with_probs = llama->doCompletion();
|
|
923
|
-
if (token_with_probs.tok == -1 || llama->incomplete) {
|
|
924
|
-
continue;
|
|
925
|
-
}
|
|
926
|
-
|
|
927
|
-
const std::string token_text = common_token_to_piece(llama->ctx, token_with_probs.tok);
|
|
928
|
-
size_t pos = std::min(sent_count, llama->generated_text.size());
|
|
929
|
-
const std::string str_test = llama->generated_text.substr(pos);
|
|
930
|
-
|
|
931
|
-
size_t stop_pos = llama->findStoppingStrings(str_test, token_text.size(), cactus::STOP_FULL);
|
|
932
|
-
|
|
933
|
-
if (stop_pos == std::string::npos || (!llama->has_next_token && stop_pos > 0)) {
|
|
934
|
-
const std::string to_send = llama->generated_text.substr(pos, std::string::npos);
|
|
935
|
-
sent_count += to_send.size();
|
|
936
|
-
|
|
937
|
-
if (onToken) {
|
|
938
|
-
NSMutableDictionary *tokenResult = [[NSMutableDictionary alloc] init];
|
|
939
|
-
tokenResult[@"token"] = [NSString stringWithUTF8String:to_send.c_str()];
|
|
940
|
-
onToken(tokenResult);
|
|
941
|
-
}
|
|
942
|
-
}
|
|
943
|
-
}
|
|
944
|
-
|
|
945
|
-
llama->is_predicting = false;
|
|
946
|
-
|
|
947
|
-
NSMutableDictionary *result = [[NSMutableDictionary alloc] init];
|
|
948
|
-
result[@"text"] = [NSString stringWithUTF8String:llama->generated_text.c_str()];
|
|
949
|
-
result[@"tokens_predicted"] = @(llama->num_tokens_predicted);
|
|
950
|
-
result[@"tokens_evaluated"] = @(llama->num_prompt_tokens);
|
|
951
|
-
result[@"truncated"] = @(llama->truncated);
|
|
952
|
-
result[@"stopped_eos"] = @(llama->stopped_eos);
|
|
953
|
-
result[@"stopped_word"] = @(llama->stopped_word);
|
|
954
|
-
result[@"stopped_limit"] = @(llama->stopped_limit);
|
|
955
|
-
result[@"stopping_word"] = [NSString stringWithUTF8String:llama->stopping_word.c_str()];
|
|
956
|
-
result[@"tokens_cached"] = @(llama->n_past);
|
|
957
|
-
|
|
958
|
-
return result;
|
|
959
|
-
}
|
|
960
|
-
|
|
961
|
-
- (void)releaseVocoder {
|
|
962
|
-
llama->releaseVocoder();
|
|
963
|
-
}
|
|
964
|
-
|
|
965
|
-
- (void)rewind {
|
|
966
|
-
llama->rewind();
|
|
967
|
-
}
|
|
968
|
-
|
|
969
|
-
- (void)invalidate {
|
|
970
|
-
delete llama;
|
|
971
|
-
// llama_backend_free();
|
|
972
|
-
}
|
|
973
|
-
|
|
974
|
-
// New TTS/Vocoder Methods
|
|
975
|
-
- (BOOL)initVocoder:(NSString *)vocoderModelPath {
|
|
976
|
-
return llama->initVocoder([vocoderModelPath UTF8String]);
|
|
977
|
-
}
|
|
978
|
-
|
|
979
|
-
- (BOOL)isVocoderEnabled {
|
|
980
|
-
return llama->isVocoderEnabled();
|
|
981
|
-
}
|
|
982
|
-
|
|
983
|
-
- (int)getTTSType {
|
|
984
|
-
return static_cast<int>(llama->getTTSType());
|
|
985
|
-
}
|
|
986
|
-
|
|
987
|
-
- (NSString *)getFormattedAudioCompletion:(NSString *)speakerJsonStr textToSpeak:(NSString *)textToSpeak {
|
|
988
|
-
std::string result = llama->getFormattedAudioCompletion([speakerJsonStr UTF8String], [textToSpeak UTF8String]);
|
|
989
|
-
return [NSString stringWithUTF8String:result.c_str()];
|
|
990
|
-
}
|
|
991
|
-
|
|
992
|
-
- (NSArray *)getAudioCompletionGuideTokens:(NSString *)textToSpeak {
|
|
993
|
-
std::vector<llama_token> tokens = llama->getAudioCompletionGuideTokens([textToSpeak UTF8String]);
|
|
994
|
-
NSMutableArray *result = [[NSMutableArray alloc] init];
|
|
995
|
-
for (const auto &token : tokens) {
|
|
996
|
-
[result addObject:@(token)];
|
|
997
|
-
}
|
|
998
|
-
return result;
|
|
999
|
-
}
|
|
1000
|
-
|
|
1001
|
-
- (NSArray *)decodeAudioTokens:(NSArray *)tokens {
|
|
1002
|
-
std::vector<llama_token> token_vector;
|
|
1003
|
-
for (NSNumber *token in tokens) {
|
|
1004
|
-
token_vector.push_back([token intValue]);
|
|
1005
|
-
}
|
|
1006
|
-
|
|
1007
|
-
std::vector<float> audio_data = llama->decodeAudioTokens(token_vector);
|
|
1008
|
-
NSMutableArray *result = [[NSMutableArray alloc] init];
|
|
1009
|
-
for (const auto &sample : audio_data) {
|
|
1010
|
-
[result addObject:@(sample)];
|
|
1011
|
-
}
|
|
1012
|
-
return result;
|
|
1013
|
-
}
|
|
1014
|
-
|
|
1015
|
-
- (NSDictionary *)getDeviceInfo {
|
|
1016
|
-
UIDevice *device = [UIDevice currentDevice];
|
|
1017
|
-
NSString *deviceId = [[device identifierForVendor] UUIDString];
|
|
1018
|
-
struct utsname systemInfo;
|
|
1019
|
-
uname(&systemInfo);
|
|
1020
|
-
NSString *model = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
|
|
1021
|
-
NSString *os = [device systemName];
|
|
1022
|
-
NSDictionary *deviceInfo = @{
|
|
1023
|
-
@"deviceId": deviceId,
|
|
1024
|
-
@"model": model,
|
|
1025
|
-
@"make": @"Apple",
|
|
1026
|
-
@"os": os
|
|
1027
|
-
};
|
|
1028
|
-
|
|
1029
|
-
return deviceInfo;
|
|
1030
|
-
}
|
|
1031
|
-
|
|
1032
|
-
@end
|