cactus-react-native 1.4.0 → 1.7.0
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 +1 -1
- package/README.md +465 -174
- package/android/CMakeLists.txt +24 -5
- package/android/src/main/jniLibs/arm64-v8a/libcactus.a +0 -0
- package/android/src/main/jniLibs/arm64-v8a/libcurl.a +0 -0
- package/android/src/main/jniLibs/arm64-v8a/libmbedcrypto.a +0 -0
- package/android/src/main/jniLibs/arm64-v8a/libmbedtls.a +0 -0
- package/android/src/main/jniLibs/arm64-v8a/libmbedx509.a +0 -0
- package/cpp/HybridCactus.cpp +157 -6
- package/cpp/HybridCactus.hpp +20 -3
- package/cpp/cactus_ffi.h +65 -30
- package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/cactus.h +0 -1
- package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/cactus_ffi.h +65 -30
- package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/cactus_utils.h +357 -122
- package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/engine.h +184 -63
- package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/gemma_tools.h +549 -0
- package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/graph.h +153 -27
- package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/kernel.h +90 -178
- package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/kernel_utils.h +276 -151
- package/ios/cactus.xcframework/ios-arm64/cactus.framework/cactus +0 -0
- package/ios/cactus.xcframework/ios-arm64-simulator/cactus.framework/Headers/cactus.h +0 -1
- package/ios/cactus.xcframework/ios-arm64-simulator/cactus.framework/Headers/cactus_ffi.h +65 -30
- package/ios/cactus.xcframework/ios-arm64-simulator/cactus.framework/Headers/cactus_utils.h +357 -122
- package/ios/cactus.xcframework/ios-arm64-simulator/cactus.framework/Headers/engine.h +184 -63
- package/ios/cactus.xcframework/ios-arm64-simulator/cactus.framework/Headers/gemma_tools.h +549 -0
- package/ios/cactus.xcframework/ios-arm64-simulator/cactus.framework/Headers/graph.h +153 -27
- package/ios/cactus.xcframework/ios-arm64-simulator/cactus.framework/Headers/kernel.h +90 -178
- package/ios/cactus.xcframework/ios-arm64-simulator/cactus.framework/Headers/kernel_utils.h +276 -151
- package/ios/cactus.xcframework/ios-arm64-simulator/cactus.framework/cactus +0 -0
- package/lib/module/classes/CactusLM.js +43 -58
- package/lib/module/classes/CactusLM.js.map +1 -1
- package/lib/module/classes/CactusSTT.js +64 -38
- package/lib/module/classes/CactusSTT.js.map +1 -1
- package/lib/module/classes/CactusVAD.js +95 -0
- package/lib/module/classes/CactusVAD.js.map +1 -0
- package/lib/module/hooks/useCactusLM.js +23 -15
- package/lib/module/hooks/useCactusLM.js.map +1 -1
- package/lib/module/hooks/useCactusSTT.js +85 -28
- package/lib/module/hooks/useCactusSTT.js.map +1 -1
- package/lib/module/hooks/useCactusVAD.js +171 -0
- package/lib/module/hooks/useCactusVAD.js.map +1 -0
- package/lib/module/index.js +2 -3
- package/lib/module/index.js.map +1 -1
- package/lib/module/modelRegistry.js +52 -0
- package/lib/module/modelRegistry.js.map +1 -0
- package/lib/module/native/Cactus.js +107 -8
- package/lib/module/native/Cactus.js.map +1 -1
- package/lib/module/native/CactusIndex.js.map +1 -1
- package/lib/module/native/index.js +0 -3
- package/lib/module/native/index.js.map +1 -1
- package/lib/module/types/CactusLM.js +2 -0
- package/lib/module/types/CactusSTT.js +2 -0
- package/lib/module/types/CactusVAD.js +4 -0
- package/lib/module/types/{CactusModel.js.map → CactusVAD.js.map} +1 -1
- package/lib/module/types/common.js +2 -0
- package/lib/module/types/{CactusSTTModel.js.map → common.js.map} +1 -1
- package/lib/typescript/src/classes/CactusLM.d.ts +8 -6
- package/lib/typescript/src/classes/CactusLM.d.ts.map +1 -1
- package/lib/typescript/src/classes/CactusSTT.d.ts +11 -6
- package/lib/typescript/src/classes/CactusSTT.d.ts.map +1 -1
- package/lib/typescript/src/classes/CactusVAD.d.ts +20 -0
- package/lib/typescript/src/classes/CactusVAD.d.ts.map +1 -0
- package/lib/typescript/src/hooks/useCactusLM.d.ts +3 -3
- package/lib/typescript/src/hooks/useCactusLM.d.ts.map +1 -1
- package/lib/typescript/src/hooks/useCactusSTT.d.ts +11 -5
- package/lib/typescript/src/hooks/useCactusSTT.d.ts.map +1 -1
- package/lib/typescript/src/hooks/useCactusVAD.d.ts +15 -0
- package/lib/typescript/src/hooks/useCactusVAD.d.ts.map +1 -0
- package/lib/typescript/src/index.d.ts +7 -6
- package/lib/typescript/src/index.d.ts.map +1 -1
- package/lib/typescript/src/modelRegistry.d.ts +5 -0
- package/lib/typescript/src/modelRegistry.d.ts.map +1 -0
- package/lib/typescript/src/native/Cactus.d.ts +12 -6
- package/lib/typescript/src/native/Cactus.d.ts.map +1 -1
- package/lib/typescript/src/native/CactusIndex.d.ts +2 -2
- package/lib/typescript/src/native/CactusIndex.d.ts.map +1 -1
- package/lib/typescript/src/native/index.d.ts +0 -3
- package/lib/typescript/src/native/index.d.ts.map +1 -1
- package/lib/typescript/src/specs/Cactus.nitro.d.ts +6 -1
- package/lib/typescript/src/specs/Cactus.nitro.d.ts.map +1 -1
- package/lib/typescript/src/types/CactusIndex.d.ts +2 -2
- package/lib/typescript/src/types/CactusIndex.d.ts.map +1 -1
- package/lib/typescript/src/types/CactusLM.d.ts +19 -9
- package/lib/typescript/src/types/CactusLM.d.ts.map +1 -1
- package/lib/typescript/src/types/CactusSTT.d.ts +45 -4
- package/lib/typescript/src/types/CactusSTT.d.ts.map +1 -1
- package/lib/typescript/src/types/CactusVAD.d.ts +34 -0
- package/lib/typescript/src/types/CactusVAD.d.ts.map +1 -0
- package/lib/typescript/src/types/common.d.ts +23 -0
- package/lib/typescript/src/types/common.d.ts.map +1 -0
- package/nitro.json +0 -11
- package/nitrogen/generated/android/cactus+autolinking.cmake +0 -5
- package/nitrogen/generated/android/cactusOnLoad.cpp +0 -30
- package/nitrogen/generated/ios/Cactus-Swift-Cxx-Bridge.cpp +0 -50
- package/nitrogen/generated/ios/Cactus-Swift-Cxx-Bridge.hpp +9 -147
- package/nitrogen/generated/ios/Cactus-Swift-Cxx-Umbrella.hpp +0 -13
- package/nitrogen/generated/ios/CactusAutolinking.mm +0 -26
- package/nitrogen/generated/ios/CactusAutolinking.swift +0 -30
- package/nitrogen/generated/shared/c++/HybridCactusSpec.cpp +5 -0
- package/nitrogen/generated/shared/c++/HybridCactusSpec.hpp +6 -1
- package/package.json +3 -3
- package/src/classes/CactusLM.ts +59 -74
- package/src/classes/CactusSTT.ts +92 -49
- package/src/classes/CactusVAD.ts +129 -0
- package/src/hooks/useCactusLM.ts +26 -9
- package/src/hooks/useCactusSTT.ts +105 -44
- package/src/hooks/useCactusVAD.ts +215 -0
- package/src/index.tsx +20 -10
- package/src/modelRegistry.ts +65 -0
- package/src/native/Cactus.ts +130 -14
- package/src/native/CactusIndex.ts +2 -2
- package/src/native/index.ts +0 -3
- package/src/specs/Cactus.nitro.ts +11 -2
- package/src/types/CactusIndex.ts +2 -2
- package/src/types/CactusLM.ts +20 -9
- package/src/types/CactusSTT.ts +50 -4
- package/src/types/CactusVAD.ts +39 -0
- package/src/types/common.ts +23 -0
- package/android/src/main/java/com/margelo/nitro/cactus/HybridCactusCrypto.kt +0 -46
- package/android/src/main/java/com/margelo/nitro/cactus/HybridCactusDeviceInfo.kt +0 -27
- package/android/src/main/jniLibs/arm64-v8a/libcactus_util.a +0 -0
- package/cpp/HybridCactusUtil.cpp +0 -47
- package/cpp/HybridCactusUtil.hpp +0 -27
- package/cpp/cactus_util.h +0 -25
- package/ios/HybridCactusCrypto.swift +0 -37
- package/ios/HybridCactusDeviceInfo.swift +0 -32
- package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/cactus_telemetry.h +0 -656
- package/ios/cactus.xcframework/ios-arm64-simulator/cactus.framework/Headers/cactus_telemetry.h +0 -656
- package/ios/cactus_util.xcframework/Info.plist +0 -39
- package/ios/cactus_util.xcframework/ios-arm64/cactus_util.framework/Headers/cactus_util.h +0 -25
- package/ios/cactus_util.xcframework/ios-arm64/cactus_util.framework/Headers/database.h +0 -27
- package/ios/cactus_util.xcframework/ios-arm64/cactus_util.framework/Headers/ios_utils.h +0 -10
- package/ios/cactus_util.xcframework/ios-arm64/cactus_util.framework/Headers/logging.h +0 -25
- 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 +0 -25
- package/ios/cactus_util.xcframework/ios-arm64-simulator/cactus_util.framework/Headers/database.h +0 -27
- package/ios/cactus_util.xcframework/ios-arm64-simulator/cactus_util.framework/Headers/ios_utils.h +0 -10
- package/ios/cactus_util.xcframework/ios-arm64-simulator/cactus_util.framework/Headers/logging.h +0 -25
- package/ios/cactus_util.xcframework/ios-arm64-simulator/cactus_util.framework/Info.plist +0 -0
- package/ios/cactus_util.xcframework/ios-arm64-simulator/cactus_util.framework/_CodeSignature/CodeResources +0 -135
- package/ios/cactus_util.xcframework/ios-arm64-simulator/cactus_util.framework/cactus_util +0 -0
- package/lib/module/api/Database.js +0 -137
- package/lib/module/api/Database.js.map +0 -1
- package/lib/module/api/RemoteLM.js +0 -201
- package/lib/module/api/RemoteLM.js.map +0 -1
- package/lib/module/config/CactusConfig.js +0 -12
- package/lib/module/config/CactusConfig.js.map +0 -1
- package/lib/module/native/CactusCrypto.js +0 -10
- package/lib/module/native/CactusCrypto.js.map +0 -1
- package/lib/module/native/CactusDeviceInfo.js +0 -13
- package/lib/module/native/CactusDeviceInfo.js.map +0 -1
- package/lib/module/native/CactusUtil.js +0 -36
- package/lib/module/native/CactusUtil.js.map +0 -1
- package/lib/module/specs/CactusCrypto.nitro.js +0 -4
- package/lib/module/specs/CactusCrypto.nitro.js.map +0 -1
- package/lib/module/specs/CactusDeviceInfo.nitro.js +0 -4
- package/lib/module/specs/CactusDeviceInfo.nitro.js.map +0 -1
- package/lib/module/specs/CactusUtil.nitro.js +0 -4
- package/lib/module/specs/CactusUtil.nitro.js.map +0 -1
- package/lib/module/telemetry/Telemetry.js +0 -154
- package/lib/module/telemetry/Telemetry.js.map +0 -1
- package/lib/module/types/CactusModel.js +0 -2
- package/lib/module/types/CactusSTTModel.js +0 -2
- package/lib/typescript/src/api/Database.d.ts +0 -18
- package/lib/typescript/src/api/Database.d.ts.map +0 -1
- package/lib/typescript/src/api/RemoteLM.d.ts +0 -14
- package/lib/typescript/src/api/RemoteLM.d.ts.map +0 -1
- package/lib/typescript/src/config/CactusConfig.d.ts +0 -7
- package/lib/typescript/src/config/CactusConfig.d.ts.map +0 -1
- package/lib/typescript/src/native/CactusCrypto.d.ts +0 -5
- package/lib/typescript/src/native/CactusCrypto.d.ts.map +0 -1
- package/lib/typescript/src/native/CactusDeviceInfo.d.ts +0 -7
- package/lib/typescript/src/native/CactusDeviceInfo.d.ts.map +0 -1
- package/lib/typescript/src/native/CactusUtil.d.ts +0 -6
- package/lib/typescript/src/native/CactusUtil.d.ts.map +0 -1
- package/lib/typescript/src/specs/CactusCrypto.nitro.d.ts +0 -8
- package/lib/typescript/src/specs/CactusCrypto.nitro.d.ts.map +0 -1
- package/lib/typescript/src/specs/CactusDeviceInfo.nitro.d.ts +0 -16
- package/lib/typescript/src/specs/CactusDeviceInfo.nitro.d.ts.map +0 -1
- package/lib/typescript/src/specs/CactusUtil.nitro.d.ts +0 -10
- package/lib/typescript/src/specs/CactusUtil.nitro.d.ts.map +0 -1
- package/lib/typescript/src/telemetry/Telemetry.d.ts +0 -34
- package/lib/typescript/src/telemetry/Telemetry.d.ts.map +0 -1
- package/lib/typescript/src/types/CactusModel.d.ts +0 -13
- package/lib/typescript/src/types/CactusModel.d.ts.map +0 -1
- package/lib/typescript/src/types/CactusSTTModel.d.ts +0 -8
- package/lib/typescript/src/types/CactusSTTModel.d.ts.map +0 -1
- package/nitrogen/generated/android/c++/JDeviceInfo.hpp +0 -74
- package/nitrogen/generated/android/c++/JHybridCactusCryptoSpec.cpp +0 -65
- package/nitrogen/generated/android/c++/JHybridCactusCryptoSpec.hpp +0 -65
- package/nitrogen/generated/android/c++/JHybridCactusDeviceInfoSpec.cpp +0 -85
- package/nitrogen/generated/android/c++/JHybridCactusDeviceInfoSpec.hpp +0 -66
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/cactus/DeviceInfo.kt +0 -50
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/cactus/HybridCactusCryptoSpec.kt +0 -58
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/cactus/HybridCactusDeviceInfoSpec.kt +0 -62
- package/nitrogen/generated/ios/c++/HybridCactusCryptoSpecSwift.cpp +0 -11
- package/nitrogen/generated/ios/c++/HybridCactusCryptoSpecSwift.hpp +0 -77
- package/nitrogen/generated/ios/c++/HybridCactusDeviceInfoSpecSwift.cpp +0 -11
- package/nitrogen/generated/ios/c++/HybridCactusDeviceInfoSpecSwift.hpp +0 -88
- package/nitrogen/generated/ios/swift/DeviceInfo.swift +0 -98
- package/nitrogen/generated/ios/swift/Func_void_DeviceInfo.swift +0 -47
- package/nitrogen/generated/ios/swift/Func_void_std__optional_std__string_.swift +0 -54
- package/nitrogen/generated/ios/swift/HybridCactusCryptoSpec.swift +0 -57
- package/nitrogen/generated/ios/swift/HybridCactusCryptoSpec_cxx.swift +0 -139
- package/nitrogen/generated/ios/swift/HybridCactusDeviceInfoSpec.swift +0 -58
- package/nitrogen/generated/ios/swift/HybridCactusDeviceInfoSpec_cxx.swift +0 -164
- package/nitrogen/generated/shared/c++/DeviceInfo.hpp +0 -92
- package/nitrogen/generated/shared/c++/HybridCactusCryptoSpec.cpp +0 -21
- package/nitrogen/generated/shared/c++/HybridCactusCryptoSpec.hpp +0 -63
- package/nitrogen/generated/shared/c++/HybridCactusDeviceInfoSpec.cpp +0 -22
- package/nitrogen/generated/shared/c++/HybridCactusDeviceInfoSpec.hpp +0 -67
- package/nitrogen/generated/shared/c++/HybridCactusUtilSpec.cpp +0 -23
- package/nitrogen/generated/shared/c++/HybridCactusUtilSpec.hpp +0 -66
- package/src/api/Database.ts +0 -188
- package/src/api/RemoteLM.ts +0 -273
- package/src/config/CactusConfig.ts +0 -11
- package/src/native/CactusCrypto.ts +0 -11
- package/src/native/CactusDeviceInfo.ts +0 -18
- package/src/native/CactusUtil.ts +0 -43
- package/src/specs/CactusCrypto.nitro.ts +0 -6
- package/src/specs/CactusDeviceInfo.nitro.ts +0 -15
- package/src/specs/CactusUtil.nitro.ts +0 -8
- package/src/telemetry/Telemetry.ts +0 -236
- package/src/types/CactusModel.ts +0 -15
- package/src/types/CactusSTTModel.ts +0 -10
|
@@ -1,32 +1,38 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
import { Cactus, CactusFileSystem } from "../native/index.js";
|
|
4
|
-
import {
|
|
5
|
-
import { CactusConfig } from "../config/CactusConfig.js";
|
|
6
|
-
import { Database } from "../api/Database.js";
|
|
7
|
-
import { getErrorMessage } from "../utils/error.js";
|
|
8
|
-
import { RemoteLM } from "../api/RemoteLM.js";
|
|
4
|
+
import { getRegistry } from "../modelRegistry.js";
|
|
9
5
|
export class CactusLM {
|
|
10
6
|
cactus = new Cactus();
|
|
11
7
|
isDownloading = false;
|
|
12
8
|
isInitialized = false;
|
|
13
9
|
isGenerating = false;
|
|
14
|
-
static defaultModel = 'qwen3-0.
|
|
15
|
-
static
|
|
10
|
+
static defaultModel = 'qwen3-0.6b';
|
|
11
|
+
static defaultOptions = {
|
|
12
|
+
quantization: 'int8',
|
|
13
|
+
pro: false
|
|
14
|
+
};
|
|
15
|
+
static quantizationExceptions = {
|
|
16
|
+
'gemma-3-270m-it': 'int8',
|
|
17
|
+
'functiongemma-270m-it': 'int8'
|
|
18
|
+
};
|
|
16
19
|
static defaultCompleteOptions = {
|
|
17
20
|
maxTokens: 512
|
|
18
21
|
};
|
|
19
|
-
static defaultCompleteMode = 'local';
|
|
20
22
|
static defaultEmbedBufferSize = 2048;
|
|
21
23
|
constructor({
|
|
22
24
|
model,
|
|
23
|
-
|
|
24
|
-
|
|
25
|
+
corpusDir,
|
|
26
|
+
cacheIndex,
|
|
27
|
+
options
|
|
25
28
|
} = {}) {
|
|
26
|
-
Telemetry.init(CactusConfig.telemetryToken);
|
|
27
29
|
this.model = model ?? CactusLM.defaultModel;
|
|
28
|
-
this.contextSize = contextSize ?? CactusLM.defaultContextSize;
|
|
29
30
|
this.corpusDir = corpusDir;
|
|
31
|
+
this.cacheIndex = cacheIndex ?? false;
|
|
32
|
+
this.options = {
|
|
33
|
+
quantization: options?.quantization ?? CactusLM.quantizationExceptions[this.model] ?? CactusLM.defaultOptions.quantization,
|
|
34
|
+
pro: options?.pro ?? CactusLM.defaultOptions.pro
|
|
35
|
+
};
|
|
30
36
|
}
|
|
31
37
|
async download({
|
|
32
38
|
onProgress
|
|
@@ -38,14 +44,20 @@ export class CactusLM {
|
|
|
38
44
|
if (this.isDownloading) {
|
|
39
45
|
throw new Error('CactusLM is already downloading');
|
|
40
46
|
}
|
|
41
|
-
if (await CactusFileSystem.modelExists(this.
|
|
47
|
+
if (await CactusFileSystem.modelExists(this.getModelName())) {
|
|
48
|
+
console.log('Model already exists', this.getModelName());
|
|
42
49
|
onProgress?.(1.0);
|
|
43
50
|
return;
|
|
44
51
|
}
|
|
45
52
|
this.isDownloading = true;
|
|
46
53
|
try {
|
|
47
|
-
const
|
|
48
|
-
|
|
54
|
+
const registry = await getRegistry();
|
|
55
|
+
const modelConfig = registry[this.model]?.quantization[this.options.quantization];
|
|
56
|
+
const url = this.options.pro ? modelConfig?.pro?.apple : modelConfig?.url;
|
|
57
|
+
if (!url) {
|
|
58
|
+
throw new Error(`Model ${this.model} with specified options not found`);
|
|
59
|
+
}
|
|
60
|
+
await CactusFileSystem.downloadModel(this.getModelName(), url, onProgress);
|
|
49
61
|
} finally {
|
|
50
62
|
this.isDownloading = false;
|
|
51
63
|
}
|
|
@@ -58,26 +70,22 @@ export class CactusLM {
|
|
|
58
70
|
if (this.isModelPath(this.model)) {
|
|
59
71
|
modelPath = this.model.replace('file://', '');
|
|
60
72
|
} else {
|
|
61
|
-
if (!(await CactusFileSystem.modelExists(this.
|
|
62
|
-
|
|
73
|
+
if (!(await CactusFileSystem.modelExists(this.getModelName()))) {
|
|
74
|
+
console.log('Model not found:', this.getModelName());
|
|
75
|
+
throw new Error(`Model "${this.model}" with options ${JSON.stringify(this.options)} is not downloaded`);
|
|
63
76
|
}
|
|
64
|
-
modelPath = await CactusFileSystem.getModelPath(this.
|
|
65
|
-
}
|
|
66
|
-
try {
|
|
67
|
-
await this.cactus.init(modelPath, this.contextSize, this.corpusDir);
|
|
68
|
-
Telemetry.logInit(this.model, true);
|
|
69
|
-
this.isInitialized = true;
|
|
70
|
-
} catch (error) {
|
|
71
|
-
Telemetry.logInit(this.model, false, getErrorMessage(error));
|
|
72
|
-
throw error;
|
|
77
|
+
modelPath = await CactusFileSystem.getModelPath(this.getModelName());
|
|
73
78
|
}
|
|
79
|
+
const cacheDir = await CactusFileSystem.getCactusDirectory();
|
|
80
|
+
await this.cactus.setTelemetryEnvironment(cacheDir);
|
|
81
|
+
await this.cactus.init(modelPath, this.corpusDir, this.cacheIndex);
|
|
82
|
+
this.isInitialized = true;
|
|
74
83
|
}
|
|
75
84
|
async complete({
|
|
76
85
|
messages,
|
|
77
86
|
options,
|
|
78
87
|
tools,
|
|
79
|
-
onToken
|
|
80
|
-
mode
|
|
88
|
+
onToken
|
|
81
89
|
}) {
|
|
82
90
|
if (this.isGenerating) {
|
|
83
91
|
throw new Error('CactusLM is already generating');
|
|
@@ -90,25 +98,11 @@ export class CactusLM {
|
|
|
90
98
|
type: 'function',
|
|
91
99
|
function: tool
|
|
92
100
|
}));
|
|
93
|
-
mode = mode ?? CactusLM.defaultCompleteMode;
|
|
94
101
|
const responseBufferSize = 8 * (options.maxTokens ?? CactusLM.defaultCompleteOptions.maxTokens) + 256;
|
|
102
|
+
await this.init();
|
|
103
|
+
this.isGenerating = true;
|
|
95
104
|
try {
|
|
96
|
-
await this.
|
|
97
|
-
this.isGenerating = true;
|
|
98
|
-
const result = await this.cactus.complete(messages, responseBufferSize, options, toolsInternal, onToken);
|
|
99
|
-
Telemetry.logCompletion(this.model, result.success, result.success ? undefined : result.response, result);
|
|
100
|
-
return result;
|
|
101
|
-
} catch (localError) {
|
|
102
|
-
if (mode === 'local') {
|
|
103
|
-
Telemetry.logCompletion(this.model, false, getErrorMessage(localError));
|
|
104
|
-
throw localError;
|
|
105
|
-
}
|
|
106
|
-
Telemetry.logCompletion(this.model, false, `Local completion error: ${getErrorMessage(localError)}. Falling back to remote completion.`);
|
|
107
|
-
try {
|
|
108
|
-
return RemoteLM.complete(messages, options, toolsInternal, onToken);
|
|
109
|
-
} catch (remoteError) {
|
|
110
|
-
throw new Error(`Remote completion error: ${getErrorMessage(remoteError)}`);
|
|
111
|
-
}
|
|
105
|
+
return await this.cactus.complete(messages, responseBufferSize, options, toolsInternal, onToken);
|
|
112
106
|
} finally {
|
|
113
107
|
this.isGenerating = false;
|
|
114
108
|
}
|
|
@@ -141,13 +135,9 @@ export class CactusLM {
|
|
|
141
135
|
this.isGenerating = true;
|
|
142
136
|
try {
|
|
143
137
|
const embedding = await this.cactus.embed(text, CactusLM.defaultEmbedBufferSize, normalize);
|
|
144
|
-
Telemetry.logEmbedding(this.model, true);
|
|
145
138
|
return {
|
|
146
139
|
embedding
|
|
147
140
|
};
|
|
148
|
-
} catch (error) {
|
|
149
|
-
Telemetry.logEmbedding(this.model, false, getErrorMessage(error));
|
|
150
|
-
throw error;
|
|
151
141
|
} finally {
|
|
152
142
|
this.isGenerating = false;
|
|
153
143
|
}
|
|
@@ -162,13 +152,9 @@ export class CactusLM {
|
|
|
162
152
|
this.isGenerating = true;
|
|
163
153
|
try {
|
|
164
154
|
const embedding = await this.cactus.imageEmbed(imagePath, CactusLM.defaultEmbedBufferSize);
|
|
165
|
-
Telemetry.logImageEmbedding(this.model, true);
|
|
166
155
|
return {
|
|
167
156
|
embedding
|
|
168
157
|
};
|
|
169
|
-
} catch (error) {
|
|
170
|
-
Telemetry.logImageEmbedding(this.model, false, getErrorMessage(error));
|
|
171
|
-
throw error;
|
|
172
158
|
} finally {
|
|
173
159
|
this.isGenerating = false;
|
|
174
160
|
}
|
|
@@ -189,14 +175,13 @@ export class CactusLM {
|
|
|
189
175
|
this.isInitialized = false;
|
|
190
176
|
}
|
|
191
177
|
async getModels() {
|
|
192
|
-
|
|
193
|
-
for (const model of models) {
|
|
194
|
-
model.isDownloaded = await CactusFileSystem.modelExists(model.slug);
|
|
195
|
-
}
|
|
196
|
-
return models;
|
|
178
|
+
return Object.values(await getRegistry());
|
|
197
179
|
}
|
|
198
180
|
isModelPath(model) {
|
|
199
181
|
return model.startsWith('file://') || model.startsWith('/');
|
|
200
182
|
}
|
|
183
|
+
getModelName() {
|
|
184
|
+
return `${this.model}-${this.options.quantization}${this.options.pro ? '-pro' : ''}`;
|
|
185
|
+
}
|
|
201
186
|
}
|
|
202
187
|
//# sourceMappingURL=CactusLM.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["Cactus","CactusFileSystem","
|
|
1
|
+
{"version":3,"names":["Cactus","CactusFileSystem","getRegistry","CactusLM","cactus","isDownloading","isInitialized","isGenerating","defaultModel","defaultOptions","quantization","pro","quantizationExceptions","defaultCompleteOptions","maxTokens","defaultEmbedBufferSize","constructor","model","corpusDir","cacheIndex","options","download","onProgress","isModelPath","Error","modelExists","getModelName","console","log","registry","modelConfig","url","apple","downloadModel","init","modelPath","replace","JSON","stringify","getModelPath","cacheDir","getCactusDirectory","setTelemetryEnvironment","complete","messages","tools","onToken","toolsInternal","map","tool","type","function","responseBufferSize","tokenize","text","tokens","scoreWindow","start","end","context","score","embed","normalize","embedding","imageEmbed","imagePath","stop","reset","destroy","getModels","Object","values","startsWith"],"sourceRoot":"../../../src","sources":["classes/CactusLM.ts"],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EAAE,EAAE;EACtF;AACF","ignoreList":[]}
|
|
@@ -1,29 +1,32 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
import { Cactus, CactusFileSystem } from "../native/index.js";
|
|
4
|
-
import {
|
|
5
|
-
import { CactusConfig } from "../config/CactusConfig.js";
|
|
6
|
-
import { Database } from "../api/Database.js";
|
|
7
|
-
import { getErrorMessage } from "../utils/error.js";
|
|
4
|
+
import { getRegistry } from "../modelRegistry.js";
|
|
8
5
|
export class CactusSTT {
|
|
9
6
|
cactus = new Cactus();
|
|
10
7
|
isDownloading = false;
|
|
11
8
|
isInitialized = false;
|
|
12
9
|
isGenerating = false;
|
|
10
|
+
isStreamTranscribing = false;
|
|
13
11
|
static defaultModel = 'whisper-small';
|
|
14
|
-
static
|
|
12
|
+
static defaultOptions = {
|
|
13
|
+
quantization: 'int8',
|
|
14
|
+
pro: false
|
|
15
|
+
};
|
|
15
16
|
static defaultPrompt = '<|startoftranscript|><|en|><|transcribe|><|notimestamps|>';
|
|
16
17
|
static defaultTranscribeOptions = {
|
|
17
|
-
maxTokens:
|
|
18
|
+
maxTokens: 384
|
|
18
19
|
};
|
|
19
20
|
static defaultEmbedBufferSize = 4096;
|
|
20
21
|
constructor({
|
|
21
22
|
model,
|
|
22
|
-
|
|
23
|
+
options
|
|
23
24
|
} = {}) {
|
|
24
|
-
Telemetry.init(CactusConfig.telemetryToken);
|
|
25
25
|
this.model = model ?? CactusSTT.defaultModel;
|
|
26
|
-
this.
|
|
26
|
+
this.options = {
|
|
27
|
+
quantization: options?.quantization ?? CactusSTT.defaultOptions.quantization,
|
|
28
|
+
pro: options?.pro ?? CactusSTT.defaultOptions.pro
|
|
29
|
+
};
|
|
27
30
|
}
|
|
28
31
|
async download({
|
|
29
32
|
onProgress
|
|
@@ -35,14 +38,20 @@ export class CactusSTT {
|
|
|
35
38
|
if (this.isDownloading) {
|
|
36
39
|
throw new Error('CactusSTT is already downloading');
|
|
37
40
|
}
|
|
38
|
-
if (await CactusFileSystem.modelExists(this.
|
|
41
|
+
if (await CactusFileSystem.modelExists(this.getModelName())) {
|
|
42
|
+
console.log('Model already exists', this.getModelName());
|
|
39
43
|
onProgress?.(1.0);
|
|
40
44
|
return;
|
|
41
45
|
}
|
|
42
46
|
this.isDownloading = true;
|
|
43
47
|
try {
|
|
44
|
-
const
|
|
45
|
-
|
|
48
|
+
const registry = await getRegistry();
|
|
49
|
+
const modelConfig = registry[this.model]?.quantization[this.options.quantization];
|
|
50
|
+
const url = this.options.pro ? modelConfig?.pro?.apple : modelConfig?.url;
|
|
51
|
+
if (!url) {
|
|
52
|
+
throw new Error(`Model ${this.model} with specified options not found`);
|
|
53
|
+
}
|
|
54
|
+
await CactusFileSystem.downloadModel(this.getModelName(), url, onProgress);
|
|
46
55
|
} finally {
|
|
47
56
|
this.isDownloading = false;
|
|
48
57
|
}
|
|
@@ -55,19 +64,16 @@ export class CactusSTT {
|
|
|
55
64
|
if (this.isModelPath(this.model)) {
|
|
56
65
|
modelPath = this.model.replace('file://', '');
|
|
57
66
|
} else {
|
|
58
|
-
if (!(await CactusFileSystem.modelExists(this.
|
|
59
|
-
|
|
67
|
+
if (!(await CactusFileSystem.modelExists(this.getModelName()))) {
|
|
68
|
+
console.log('Model does not exist', this.getModelName());
|
|
69
|
+
throw new Error(`Model "${this.model}" with options ${JSON.stringify(this.options)} is not downloaded`);
|
|
60
70
|
}
|
|
61
|
-
modelPath = await CactusFileSystem.getModelPath(this.
|
|
62
|
-
}
|
|
63
|
-
try {
|
|
64
|
-
await this.cactus.init(modelPath, this.contextSize);
|
|
65
|
-
Telemetry.logInit(this.model, true);
|
|
66
|
-
this.isInitialized = true;
|
|
67
|
-
} catch (error) {
|
|
68
|
-
Telemetry.logInit(this.model, false, getErrorMessage(error));
|
|
69
|
-
throw error;
|
|
71
|
+
modelPath = await CactusFileSystem.getModelPath(this.getModelName());
|
|
70
72
|
}
|
|
73
|
+
const cacheDir = await CactusFileSystem.getCactusDirectory();
|
|
74
|
+
await this.cactus.setTelemetryEnvironment(cacheDir);
|
|
75
|
+
await this.cactus.init(modelPath);
|
|
76
|
+
this.isInitialized = true;
|
|
71
77
|
}
|
|
72
78
|
async transcribe({
|
|
73
79
|
audio,
|
|
@@ -87,16 +93,37 @@ export class CactusSTT {
|
|
|
87
93
|
const responseBufferSize = 8 * (options.maxTokens ?? CactusSTT.defaultTranscribeOptions.maxTokens) + 256;
|
|
88
94
|
this.isGenerating = true;
|
|
89
95
|
try {
|
|
90
|
-
|
|
91
|
-
Telemetry.logTranscribe(this.model, result.success, result.success ? undefined : result.response, result);
|
|
92
|
-
return result;
|
|
93
|
-
} catch (error) {
|
|
94
|
-
Telemetry.logTranscribe(this.model, false, getErrorMessage(error));
|
|
95
|
-
throw error;
|
|
96
|
+
return await this.cactus.transcribe(audio, prompt, responseBufferSize, options, onToken);
|
|
96
97
|
} finally {
|
|
97
98
|
this.isGenerating = false;
|
|
98
99
|
}
|
|
99
100
|
}
|
|
101
|
+
async streamTranscribeStart(options) {
|
|
102
|
+
if (this.isStreamTranscribing) {
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
await this.init();
|
|
106
|
+
await this.cactus.streamTranscribeStart(options);
|
|
107
|
+
this.isStreamTranscribing = true;
|
|
108
|
+
}
|
|
109
|
+
async streamTranscribeProcess({
|
|
110
|
+
audio
|
|
111
|
+
}) {
|
|
112
|
+
if (!this.isStreamTranscribing) {
|
|
113
|
+
throw new Error('CactusSTT stream transcribe is not started');
|
|
114
|
+
}
|
|
115
|
+
return this.cactus.streamTranscribeProcess(audio);
|
|
116
|
+
}
|
|
117
|
+
async streamTranscribeStop() {
|
|
118
|
+
if (!this.isStreamTranscribing) {
|
|
119
|
+
throw new Error('CactusSTT stream transcribe is not started');
|
|
120
|
+
}
|
|
121
|
+
try {
|
|
122
|
+
return await this.cactus.streamTranscribeStop();
|
|
123
|
+
} finally {
|
|
124
|
+
this.isStreamTranscribing = false;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
100
127
|
async audioEmbed({
|
|
101
128
|
audioPath
|
|
102
129
|
}) {
|
|
@@ -107,13 +134,9 @@ export class CactusSTT {
|
|
|
107
134
|
this.isGenerating = true;
|
|
108
135
|
try {
|
|
109
136
|
const embedding = await this.cactus.audioEmbed(audioPath, CactusSTT.defaultEmbedBufferSize);
|
|
110
|
-
Telemetry.logAudioEmbedding(this.model, true);
|
|
111
137
|
return {
|
|
112
138
|
embedding
|
|
113
139
|
};
|
|
114
|
-
} catch (error) {
|
|
115
|
-
Telemetry.logAudioEmbedding(this.model, false, getErrorMessage(error));
|
|
116
|
-
throw error;
|
|
117
140
|
} finally {
|
|
118
141
|
this.isGenerating = false;
|
|
119
142
|
}
|
|
@@ -130,18 +153,21 @@ export class CactusSTT {
|
|
|
130
153
|
return;
|
|
131
154
|
}
|
|
132
155
|
await this.stop();
|
|
156
|
+
if (this.isStreamTranscribing) {
|
|
157
|
+
await this.cactus.streamTranscribeStop().catch(() => {});
|
|
158
|
+
this.isStreamTranscribing = false;
|
|
159
|
+
}
|
|
133
160
|
await this.cactus.destroy();
|
|
134
161
|
this.isInitialized = false;
|
|
135
162
|
}
|
|
136
163
|
async getModels() {
|
|
137
|
-
|
|
138
|
-
for (const model of models) {
|
|
139
|
-
model.isDownloaded = await CactusFileSystem.modelExists(model.slug);
|
|
140
|
-
}
|
|
141
|
-
return models;
|
|
164
|
+
return Object.values(await getRegistry());
|
|
142
165
|
}
|
|
143
166
|
isModelPath(model) {
|
|
144
167
|
return model.startsWith('file://') || model.startsWith('/');
|
|
145
168
|
}
|
|
169
|
+
getModelName() {
|
|
170
|
+
return `${this.model}-${this.options.quantization}${this.options.pro ? '-pro' : ''}`;
|
|
171
|
+
}
|
|
146
172
|
}
|
|
147
173
|
//# sourceMappingURL=CactusSTT.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["Cactus","CactusFileSystem","
|
|
1
|
+
{"version":3,"names":["Cactus","CactusFileSystem","getRegistry","CactusSTT","cactus","isDownloading","isInitialized","isGenerating","isStreamTranscribing","defaultModel","defaultOptions","quantization","pro","defaultPrompt","defaultTranscribeOptions","maxTokens","defaultEmbedBufferSize","constructor","model","options","download","onProgress","isModelPath","Error","modelExists","getModelName","console","log","registry","modelConfig","url","apple","downloadModel","init","modelPath","replace","JSON","stringify","getModelPath","cacheDir","getCactusDirectory","setTelemetryEnvironment","transcribe","audio","prompt","onToken","responseBufferSize","streamTranscribeStart","streamTranscribeProcess","streamTranscribeStop","audioEmbed","audioPath","embedding","stop","reset","destroy","catch","getModels","Object","values","startsWith"],"sourceRoot":"../../../src","sources":["classes/CactusSTT.ts"],"mappings":";;AAAA,SAASA,MAAM,EAAEC,gBAAgB,QAAQ,oBAAW;AAapD,SAASC,WAAW,QAAQ,qBAAkB;AAG9C,OAAO,MAAMC,SAAS,CAAC;EACJC,MAAM,GAAG,IAAIJ,MAAM,CAAC,CAAC;EAQ9BK,aAAa,GAAG,KAAK;EACrBC,aAAa,GAAG,KAAK;EACrBC,YAAY,GAAG,KAAK;EACpBC,oBAAoB,GAAG,KAAK;EAEpC,OAAwBC,YAAY,GAAG,eAAe;EACtD,OAAwBC,cAAc,GAAG;IACvCC,YAAY,EAAE,MAAe;IAC7BC,GAAG,EAAE;EACP,CAAC;EACD,OAAwBC,aAAa,GACnC,2DAA2D;EAC7D,OAAwBC,wBAAwB,GAAG;IACjDC,SAAS,EAAE;EACb,CAAC;EACD,OAAwBC,sBAAsB,GAAG,IAAI;EAErDC,WAAWA,CAAC;IAAEC,KAAK;IAAEC;EAAyB,CAAC,GAAG,CAAC,CAAC,EAAE;IACpD,IAAI,CAACD,KAAK,GAAGA,KAAK,IAAIf,SAAS,CAACM,YAAY;IAC5C,IAAI,CAACU,OAAO,GAAG;MACbR,YAAY,EACVQ,OAAO,EAAER,YAAY,IAAIR,SAAS,CAACO,cAAc,CAACC,YAAY;MAChEC,GAAG,EAAEO,OAAO,EAAEP,GAAG,IAAIT,SAAS,CAACO,cAAc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,CAAA,EAAW;IAC5B,OAAO,GAAG,IAAI,CAACP,KAAK,IAAI,IAAI,CAACC,OAAO,CAACR,YAAY,GAAG,IAAI,CAACQ,OAAO,CAACP,GAAG,GAAG,MAAM,GAAG,EAAE,EAAE;EACtF;AACF","ignoreList":[]}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
import { Cactus, CactusFileSystem } from "../native/index.js";
|
|
4
|
+
import { getRegistry } from "../modelRegistry.js";
|
|
5
|
+
export class CactusVAD {
|
|
6
|
+
cactus = new Cactus();
|
|
7
|
+
isDownloading = false;
|
|
8
|
+
isInitialized = false;
|
|
9
|
+
static defaultModel = 'silero-vad';
|
|
10
|
+
static defaultOptions = {
|
|
11
|
+
quantization: 'int8',
|
|
12
|
+
pro: false
|
|
13
|
+
};
|
|
14
|
+
constructor({
|
|
15
|
+
model,
|
|
16
|
+
options
|
|
17
|
+
} = {}) {
|
|
18
|
+
this.model = model ?? CactusVAD.defaultModel;
|
|
19
|
+
this.options = {
|
|
20
|
+
quantization: options?.quantization ?? CactusVAD.defaultOptions.quantization,
|
|
21
|
+
pro: options?.pro ?? CactusVAD.defaultOptions.pro
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
async download({
|
|
25
|
+
onProgress
|
|
26
|
+
} = {}) {
|
|
27
|
+
if (this.isModelPath(this.model)) {
|
|
28
|
+
onProgress?.(1.0);
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
if (this.isDownloading) {
|
|
32
|
+
throw new Error('CactusVAD is already downloading');
|
|
33
|
+
}
|
|
34
|
+
if (await CactusFileSystem.modelExists(this.getModelName())) {
|
|
35
|
+
console.log('Model already exists', this.getModelName());
|
|
36
|
+
onProgress?.(1.0);
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
this.isDownloading = true;
|
|
40
|
+
try {
|
|
41
|
+
const registry = await getRegistry();
|
|
42
|
+
const modelConfig = registry[this.model]?.quantization[this.options.quantization];
|
|
43
|
+
const url = this.options.pro ? modelConfig?.pro?.apple : modelConfig?.url;
|
|
44
|
+
if (!url) {
|
|
45
|
+
throw new Error(`Model ${this.model} with specified options not found`);
|
|
46
|
+
}
|
|
47
|
+
await CactusFileSystem.downloadModel(this.getModelName(), url, onProgress);
|
|
48
|
+
} finally {
|
|
49
|
+
this.isDownloading = false;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
async init() {
|
|
53
|
+
if (this.isInitialized) {
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
let modelPath;
|
|
57
|
+
if (this.isModelPath(this.model)) {
|
|
58
|
+
modelPath = this.model.replace('file://', '');
|
|
59
|
+
} else {
|
|
60
|
+
if (!(await CactusFileSystem.modelExists(this.getModelName()))) {
|
|
61
|
+
console.log('Model does not exist', this.getModelName());
|
|
62
|
+
throw new Error(`Model "${this.model}" with options ${JSON.stringify(this.options)} is not downloaded`);
|
|
63
|
+
}
|
|
64
|
+
modelPath = await CactusFileSystem.getModelPath(this.getModelName());
|
|
65
|
+
}
|
|
66
|
+
const cacheDir = await CactusFileSystem.getCactusDirectory();
|
|
67
|
+
await this.cactus.setTelemetryEnvironment(cacheDir);
|
|
68
|
+
await this.cactus.init(modelPath);
|
|
69
|
+
this.isInitialized = true;
|
|
70
|
+
}
|
|
71
|
+
async vad({
|
|
72
|
+
audio,
|
|
73
|
+
options
|
|
74
|
+
}) {
|
|
75
|
+
await this.init();
|
|
76
|
+
return this.cactus.vad(audio, options);
|
|
77
|
+
}
|
|
78
|
+
async destroy() {
|
|
79
|
+
if (!this.isInitialized) {
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
await this.cactus.destroy();
|
|
83
|
+
this.isInitialized = false;
|
|
84
|
+
}
|
|
85
|
+
async getModels() {
|
|
86
|
+
return Object.values(await getRegistry());
|
|
87
|
+
}
|
|
88
|
+
isModelPath(model) {
|
|
89
|
+
return model.startsWith('file://') || model.startsWith('/');
|
|
90
|
+
}
|
|
91
|
+
getModelName() {
|
|
92
|
+
return `${this.model}-${this.options.quantization}${this.options.pro ? '-pro' : ''}`;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
//# sourceMappingURL=CactusVAD.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["Cactus","CactusFileSystem","getRegistry","CactusVAD","cactus","isDownloading","isInitialized","defaultModel","defaultOptions","quantization","pro","constructor","model","options","download","onProgress","isModelPath","Error","modelExists","getModelName","console","log","registry","modelConfig","url","apple","downloadModel","init","modelPath","replace","JSON","stringify","getModelPath","cacheDir","getCactusDirectory","setTelemetryEnvironment","vad","audio","destroy","getModels","Object","values","startsWith"],"sourceRoot":"../../../src","sources":["classes/CactusVAD.ts"],"mappings":";;AAAA,SAASA,MAAM,EAAEC,gBAAgB,QAAQ,oBAAW;AAOpD,SAASC,WAAW,QAAQ,qBAAkB;AAG9C,OAAO,MAAMC,SAAS,CAAC;EACJC,MAAM,GAAG,IAAIJ,MAAM,CAAC,CAAC;EAQ9BK,aAAa,GAAG,KAAK;EACrBC,aAAa,GAAG,KAAK;EAE7B,OAAwBC,YAAY,GAAG,YAAY;EACnD,OAAwBC,cAAc,GAAG;IACvCC,YAAY,EAAE,MAAe;IAC7BC,GAAG,EAAE;EACP,CAAC;EAEDC,WAAWA,CAAC;IAAEC,KAAK;IAAEC;EAAyB,CAAC,GAAG,CAAC,CAAC,EAAE;IACpD,IAAI,CAACD,KAAK,GAAGA,KAAK,IAAIT,SAAS,CAACI,YAAY;IAC5C,IAAI,CAACM,OAAO,GAAG;MACbJ,YAAY,EACVI,OAAO,EAAEJ,YAAY,IAAIN,SAAS,CAACK,cAAc,CAACC,YAAY;MAChEC,GAAG,EAAEG,OAAO,EAAEH,GAAG,IAAIP,SAAS,CAACK,cAAc,CAACE;IAChD,CAAC;EACH;EAEA,MAAaI,QAAQA,CAAC;IACpBC;EACuB,CAAC,GAAG,CAAC,CAAC,EAAiB;IAC9C,IAAI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ACJ,KAAa,EAAW;IAC1C,OAAOA,KAAK,CAAC8B,UAAU,CAAC,SAAS,CAAC,IAAI9B,KAAK,CAAC8B,UAAU,CAAC,GAAG,CAAC;EAC7D;EAEOvB,YAAYA,CAAA,EAAW;IAC5B,OAAO,GAAG,IAAI,CAACP,KAAK,IAAI,IAAI,CAACC,OAAO,CAACJ,YAAY,GAAG,IAAI,CAACI,OAAO,CAACH,GAAG,GAAG,MAAM,GAAG,EAAE,EAAE;EACtF;AACF","ignoreList":[]}
|
|
@@ -5,14 +5,19 @@ import { CactusLM } from "../classes/CactusLM.js";
|
|
|
5
5
|
import { CactusFileSystem } from "../native/index.js";
|
|
6
6
|
import { getErrorMessage } from "../utils/error.js";
|
|
7
7
|
export const useCactusLM = ({
|
|
8
|
-
model = 'qwen3-0.
|
|
9
|
-
|
|
10
|
-
|
|
8
|
+
model = 'qwen3-0.6b',
|
|
9
|
+
corpusDir = undefined,
|
|
10
|
+
cacheIndex = false,
|
|
11
|
+
options: modelOptions = {
|
|
12
|
+
quantization: undefined,
|
|
13
|
+
pro: false
|
|
14
|
+
}
|
|
11
15
|
} = {}) => {
|
|
12
16
|
const [cactusLM, setCactusLM] = useState(() => new CactusLM({
|
|
13
17
|
model,
|
|
14
|
-
|
|
15
|
-
|
|
18
|
+
corpusDir,
|
|
19
|
+
cacheIndex,
|
|
20
|
+
options: modelOptions
|
|
16
21
|
}));
|
|
17
22
|
|
|
18
23
|
// State
|
|
@@ -29,11 +34,16 @@ export const useCactusLM = ({
|
|
|
29
34
|
currentModelRef.current = model;
|
|
30
35
|
}, [model]);
|
|
31
36
|
useEffect(() => {
|
|
32
|
-
|
|
37
|
+
const newInstance = new CactusLM({
|
|
33
38
|
model,
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
39
|
+
corpusDir,
|
|
40
|
+
cacheIndex,
|
|
41
|
+
options: {
|
|
42
|
+
quantization: modelOptions.quantization,
|
|
43
|
+
pro: modelOptions.pro
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
setCactusLM(newInstance);
|
|
37
47
|
setCompletion('');
|
|
38
48
|
setIsGenerating(false);
|
|
39
49
|
setIsInitializing(false);
|
|
@@ -42,7 +52,7 @@ export const useCactusLM = ({
|
|
|
42
52
|
setDownloadProgress(0);
|
|
43
53
|
setError(null);
|
|
44
54
|
let mounted = true;
|
|
45
|
-
CactusFileSystem.modelExists(
|
|
55
|
+
CactusFileSystem.modelExists(newInstance.getModelName()).then(exists => {
|
|
46
56
|
if (!mounted) {
|
|
47
57
|
return;
|
|
48
58
|
}
|
|
@@ -57,7 +67,7 @@ export const useCactusLM = ({
|
|
|
57
67
|
return () => {
|
|
58
68
|
mounted = false;
|
|
59
69
|
};
|
|
60
|
-
}, [model,
|
|
70
|
+
}, [model, corpusDir, cacheIndex, modelOptions.quantization, modelOptions.pro]);
|
|
61
71
|
useEffect(() => {
|
|
62
72
|
return () => {
|
|
63
73
|
cactusLM.destroy().catch(() => {});
|
|
@@ -128,8 +138,7 @@ export const useCactusLM = ({
|
|
|
128
138
|
messages,
|
|
129
139
|
options,
|
|
130
140
|
tools,
|
|
131
|
-
onToken
|
|
132
|
-
mode
|
|
141
|
+
onToken
|
|
133
142
|
}) => {
|
|
134
143
|
if (isGenerating) {
|
|
135
144
|
const message = 'CactusLM is already generating';
|
|
@@ -147,8 +156,7 @@ export const useCactusLM = ({
|
|
|
147
156
|
onToken: token => {
|
|
148
157
|
setCompletion(prev => prev + token);
|
|
149
158
|
onToken?.(token);
|
|
150
|
-
}
|
|
151
|
-
mode
|
|
159
|
+
}
|
|
152
160
|
});
|
|
153
161
|
} catch (e) {
|
|
154
162
|
setError(getErrorMessage(e));
|