cactus-react-native 0.2.2 → 0.2.3
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/android/src/main/java/com/cactus/LlamaContext.java +13 -1
- package/android/src/main/jni.cpp +14 -34
- package/android/src/main/jniLibs/arm64-v8a/libcactus.so +0 -0
- package/android/src/main/jniLibs/arm64-v8a/libcactus_v8.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/main/jniLibs/x86_64/libcactus.so +0 -0
- package/android/src/main/jniLibs/x86_64/libcactus_x86_64.so +0 -0
- package/lib/commonjs/lm.js +45 -27
- package/lib/commonjs/lm.js.map +1 -1
- package/lib/module/lm.js +45 -26
- package/lib/module/lm.js.map +1 -1
- package/lib/typescript/lm.d.ts +4 -1
- package/lib/typescript/lm.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/lm.ts +50 -19
|
@@ -64,6 +64,17 @@ public class LlamaContext {
|
|
|
64
64
|
if (!params.hasKey("model")) {
|
|
65
65
|
throw new IllegalArgumentException("Missing required parameter: model");
|
|
66
66
|
}
|
|
67
|
+
|
|
68
|
+
String modelPath = params.getString("model");
|
|
69
|
+
File modelFile = new File(modelPath);
|
|
70
|
+
if (!modelFile.exists()) {
|
|
71
|
+
Log.e(NAME, "Model file does not exist: " + modelPath);
|
|
72
|
+
throw new IllegalArgumentException("Model file does not exist: " + modelPath);
|
|
73
|
+
}
|
|
74
|
+
if (!modelFile.canRead()) {
|
|
75
|
+
Log.e(NAME, "Model file is not readable: " + modelPath);
|
|
76
|
+
throw new IllegalArgumentException("Model file is not readable: " + modelPath);
|
|
77
|
+
}
|
|
67
78
|
eventEmitter = reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class);
|
|
68
79
|
this.id = id;
|
|
69
80
|
this.context = initContext(
|
|
@@ -115,7 +126,8 @@ public class LlamaContext {
|
|
|
115
126
|
params.hasKey("use_progress_callback") ? new LoadProgressCallback(this) : null
|
|
116
127
|
);
|
|
117
128
|
if (this.context == -1) {
|
|
118
|
-
throw new IllegalStateException("Failed to initialize context"
|
|
129
|
+
throw new IllegalStateException("Failed to initialize context for model: " + modelPath +
|
|
130
|
+
". Please check if the model file exists and is accessible.");
|
|
119
131
|
}
|
|
120
132
|
this.modelDetails = loadModelDetails(this.context);
|
|
121
133
|
this.reactContext = reactContext;
|
package/android/src/main/jni.cpp
CHANGED
|
@@ -307,7 +307,6 @@ Java_com_cactus_LlamaContext_initContext(
|
|
|
307
307
|
llama->is_load_interrupted = false;
|
|
308
308
|
llama->loading_progress = 0;
|
|
309
309
|
|
|
310
|
-
callback_context *cb_ctx = nullptr;
|
|
311
310
|
if (load_progress_callback != nullptr) {
|
|
312
311
|
defaultParams.progress_callback = [](float progress, void * user_data) {
|
|
313
312
|
callback_context *cb_ctx = (callback_context *)user_data;
|
|
@@ -324,7 +323,7 @@ Java_com_cactus_LlamaContext_initContext(
|
|
|
324
323
|
return !llama->is_load_interrupted;
|
|
325
324
|
};
|
|
326
325
|
|
|
327
|
-
cb_ctx = new callback_context;
|
|
326
|
+
callback_context *cb_ctx = new callback_context;
|
|
328
327
|
cb_ctx->env = env;
|
|
329
328
|
cb_ctx->llama = llama;
|
|
330
329
|
cb_ctx->callback = env->NewGlobalRef(load_progress_callback);
|
|
@@ -340,30 +339,20 @@ Java_com_cactus_LlamaContext_initContext(
|
|
|
340
339
|
env->ReleaseStringUTFChars(cache_type_v, cache_type_v_chars);
|
|
341
340
|
|
|
342
341
|
LOGI("[CACTUS] is_model_loaded %s", (is_model_loaded ? "true" : "false"));
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
delete cb_ctx;
|
|
342
|
+
if (is_model_loaded) {
|
|
343
|
+
if (embedding && llama_model_has_encoder(llama->model) && llama_model_has_decoder(llama->model)) {
|
|
344
|
+
LOGI("[CACTUS] computing embeddings in encoder-decoder models is not supported");
|
|
345
|
+
llama_free(llama->ctx);
|
|
346
|
+
return -1;
|
|
349
347
|
}
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
if (embedding && llama_model_has_encoder(llama->model) && llama_model_has_decoder(llama->model)) {
|
|
355
|
-
LOGI("[CACTUS] computing embeddings in encoder-decoder models is not supported");
|
|
348
|
+
context_map[(long) llama->ctx] = llama;
|
|
349
|
+
} else {
|
|
350
|
+
LOGE("[CACTUS] Failed to load model from path: %s", model_path_chars);
|
|
356
351
|
llama_free(llama->ctx);
|
|
357
|
-
if (cb_ctx) {
|
|
358
|
-
env->DeleteGlobalRef(cb_ctx->callback);
|
|
359
|
-
delete cb_ctx;
|
|
360
|
-
}
|
|
361
352
|
delete llama;
|
|
362
353
|
return -1;
|
|
363
354
|
}
|
|
364
355
|
|
|
365
|
-
context_map[(long) llama->ctx] = llama;
|
|
366
|
-
|
|
367
356
|
std::vector<common_adapter_lora_info> lora;
|
|
368
357
|
const char *lora_chars = env->GetStringUTFChars(lora_str, nullptr);
|
|
369
358
|
if (lora_chars != nullptr && lora_chars[0] != '\0') {
|
|
@@ -392,20 +381,11 @@ Java_com_cactus_LlamaContext_initContext(
|
|
|
392
381
|
env->ReleaseStringUTFChars(lora_str, lora_chars);
|
|
393
382
|
int result = llama->applyLoraAdapters(lora);
|
|
394
383
|
if (result != 0) {
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
delete cb_ctx;
|
|
401
|
-
}
|
|
402
|
-
delete llama;
|
|
403
|
-
return -1;
|
|
404
|
-
}
|
|
405
|
-
|
|
406
|
-
if (cb_ctx) {
|
|
407
|
-
env->DeleteGlobalRef(cb_ctx->callback);
|
|
408
|
-
delete cb_ctx;
|
|
384
|
+
LOGE("[CACTUS] Failed to apply lora adapters");
|
|
385
|
+
llama_free(llama->ctx);
|
|
386
|
+
context_map.erase((long) llama->ctx);
|
|
387
|
+
delete llama;
|
|
388
|
+
return -1;
|
|
409
389
|
}
|
|
410
390
|
|
|
411
391
|
return reinterpret_cast<jlong>(llama->ctx);
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
package/lib/commonjs/lm.js
CHANGED
|
@@ -5,53 +5,71 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.CactusLM = void 0;
|
|
7
7
|
var _index = require("./index");
|
|
8
|
-
var _reactNative = require("react-native");
|
|
9
8
|
var _telemetry = require("./telemetry");
|
|
10
9
|
var _remote = require("./remote");
|
|
11
|
-
// @ts-ignore
|
|
12
|
-
|
|
13
10
|
class CactusLM {
|
|
14
11
|
constructor(context) {
|
|
15
12
|
this.context = context;
|
|
16
13
|
}
|
|
17
|
-
static async init(params, onProgress, cactusToken) {
|
|
14
|
+
static async init(params, onProgress, cactusToken, retryOptions) {
|
|
18
15
|
if (cactusToken) {
|
|
19
16
|
(0, _remote.setCactusToken)(cactusToken);
|
|
20
17
|
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
const
|
|
24
|
-
const configs = needGpuAttempt ? [params, {
|
|
25
|
-
...params,
|
|
26
|
-
n_gpu_layers: 0
|
|
27
|
-
}] : [{
|
|
18
|
+
const maxRetries = retryOptions?.maxRetries ?? 3;
|
|
19
|
+
const delayMs = retryOptions?.delayMs ?? 1000;
|
|
20
|
+
const configs = [params, {
|
|
28
21
|
...params,
|
|
29
22
|
n_gpu_layers: 0
|
|
30
23
|
}];
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
const
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
24
|
+
const sleep = ms => {
|
|
25
|
+
return new Promise(resolve => {
|
|
26
|
+
const start = Date.now();
|
|
27
|
+
const wait = () => {
|
|
28
|
+
if (Date.now() - start >= ms) {
|
|
29
|
+
resolve();
|
|
30
|
+
} else {
|
|
31
|
+
Promise.resolve().then(wait);
|
|
32
|
+
}
|
|
37
33
|
};
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
34
|
+
wait();
|
|
35
|
+
});
|
|
36
|
+
};
|
|
37
|
+
for (const config of configs) {
|
|
38
|
+
let lastError = null;
|
|
39
|
+
for (let attempt = 1; attempt <= maxRetries; attempt++) {
|
|
40
|
+
try {
|
|
41
|
+
const context = await (0, _index.initLlama)(config, onProgress);
|
|
45
42
|
return {
|
|
46
|
-
lm:
|
|
47
|
-
error:
|
|
43
|
+
lm: new CactusLM(context),
|
|
44
|
+
error: null
|
|
48
45
|
};
|
|
46
|
+
} catch (e) {
|
|
47
|
+
lastError = e;
|
|
48
|
+
const isLastConfig = configs.indexOf(config) === configs.length - 1;
|
|
49
|
+
const isLastAttempt = attempt === maxRetries;
|
|
50
|
+
_telemetry.Telemetry.error(e, {
|
|
51
|
+
n_gpu_layers: config.n_gpu_layers ?? null,
|
|
52
|
+
n_ctx: config.n_ctx ?? null,
|
|
53
|
+
model: config.model ?? null
|
|
54
|
+
});
|
|
55
|
+
if (!isLastAttempt) {
|
|
56
|
+
const delay = delayMs * Math.pow(2, attempt - 1);
|
|
57
|
+
await sleep(delay);
|
|
58
|
+
} else if (!isLastConfig) {
|
|
59
|
+
break;
|
|
60
|
+
}
|
|
49
61
|
}
|
|
50
62
|
}
|
|
63
|
+
if (configs.indexOf(config) === configs.length - 1 && lastError) {
|
|
64
|
+
return {
|
|
65
|
+
lm: null,
|
|
66
|
+
error: lastError
|
|
67
|
+
};
|
|
68
|
+
}
|
|
51
69
|
}
|
|
52
70
|
return {
|
|
53
71
|
lm: null,
|
|
54
|
-
error: new Error('Failed to initialize CactusLM')
|
|
72
|
+
error: new Error('Failed to initialize CactusLM after all retries')
|
|
55
73
|
};
|
|
56
74
|
}
|
|
57
75
|
async completion(messages, params = {}, callback) {
|
package/lib/commonjs/lm.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["_index","require","
|
|
1
|
+
{"version":3,"names":["_index","require","_telemetry","_remote","CactusLM","constructor","context","init","params","onProgress","cactusToken","retryOptions","setCactusToken","maxRetries","delayMs","configs","n_gpu_layers","sleep","ms","Promise","resolve","start","Date","now","wait","then","config","lastError","attempt","initLlama","lm","error","e","isLastConfig","indexOf","length","isLastAttempt","Telemetry","n_ctx","model","delay","Math","pow","Error","completion","messages","callback","embedding","text","mode","result","_handleRemoteEmbedding","_handleLocalEmbedding","remoteError","localError","embeddingValues","getVertexAIEmbedding","rewind","release","exports"],"sourceRoot":"../../src","sources":["lm.ts"],"mappings":";;;;;;AAAA,IAAAA,MAAA,GAAAC,OAAA;AASA,IAAAC,UAAA,GAAAD,OAAA;AACA,IAAAE,OAAA,GAAAF,OAAA;AAOO,MAAMG,QAAQ,CAAC;EAGZC,WAAWA,CAACC,OAAqB,EAAE;IACzC,IAAI,CAACA,OAAO,GAAGA,OAAO;EACxB;EAEA,aAAaC,IAAIA,CACfC,MAAqB,EACrBC,UAAuC,EACvCC,WAAoB,EACpBC,YAAwD,EAC/B;IACzB,IAAID,WAAW,EAAE;MACf,IAAAE,sBAAc,EAACF,WAAW,CAAC;IAC7B;IAEA,MAAMG,UAAU,GAAGF,YAAY,EAAEE,UAAU,IAAI,CAAC;IAChD,MAAMC,OAAO,GAAGH,YAAY,EAAEG,OAAO,IAAI,IAAI;IAE7C,MAAMC,OAAO,GAAG,CACdP,MAAM,EACN;MAAE,GAAGA,MAAM;MAAEQ,YAAY,EAAE;IAAE,CAAC,CAC/B;IAED,MAAMC,KAAK,GAAIC,EAAU,IAAoB;MAC3C,OAAO,IAAIC,OAAO,CAACC,OAAO,IAAI;QAC5B,MA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{ initLlama } from './index';
|
|
4
|
-
// @ts-ignore
|
|
5
|
-
import { Platform } from 'react-native';
|
|
6
4
|
import { Telemetry } from './telemetry';
|
|
7
5
|
import { setCactusToken, getVertexAIEmbedding } from './remote';
|
|
8
6
|
export class CactusLM {
|
|
9
7
|
constructor(context) {
|
|
10
8
|
this.context = context;
|
|
11
9
|
}
|
|
12
|
-
static async init(params, onProgress, cactusToken) {
|
|
10
|
+
static async init(params, onProgress, cactusToken, retryOptions) {
|
|
13
11
|
if (cactusToken) {
|
|
14
12
|
setCactusToken(cactusToken);
|
|
15
13
|
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
const
|
|
19
|
-
const configs = needGpuAttempt ? [params, {
|
|
20
|
-
...params,
|
|
21
|
-
n_gpu_layers: 0
|
|
22
|
-
}] : [{
|
|
14
|
+
const maxRetries = retryOptions?.maxRetries ?? 3;
|
|
15
|
+
const delayMs = retryOptions?.delayMs ?? 1000;
|
|
16
|
+
const configs = [params, {
|
|
23
17
|
...params,
|
|
24
18
|
n_gpu_layers: 0
|
|
25
19
|
}];
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
const
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
20
|
+
const sleep = ms => {
|
|
21
|
+
return new Promise(resolve => {
|
|
22
|
+
const start = Date.now();
|
|
23
|
+
const wait = () => {
|
|
24
|
+
if (Date.now() - start >= ms) {
|
|
25
|
+
resolve();
|
|
26
|
+
} else {
|
|
27
|
+
Promise.resolve().then(wait);
|
|
28
|
+
}
|
|
32
29
|
};
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
30
|
+
wait();
|
|
31
|
+
});
|
|
32
|
+
};
|
|
33
|
+
for (const config of configs) {
|
|
34
|
+
let lastError = null;
|
|
35
|
+
for (let attempt = 1; attempt <= maxRetries; attempt++) {
|
|
36
|
+
try {
|
|
37
|
+
const context = await initLlama(config, onProgress);
|
|
40
38
|
return {
|
|
41
|
-
lm:
|
|
42
|
-
error:
|
|
39
|
+
lm: new CactusLM(context),
|
|
40
|
+
error: null
|
|
43
41
|
};
|
|
42
|
+
} catch (e) {
|
|
43
|
+
lastError = e;
|
|
44
|
+
const isLastConfig = configs.indexOf(config) === configs.length - 1;
|
|
45
|
+
const isLastAttempt = attempt === maxRetries;
|
|
46
|
+
Telemetry.error(e, {
|
|
47
|
+
n_gpu_layers: config.n_gpu_layers ?? null,
|
|
48
|
+
n_ctx: config.n_ctx ?? null,
|
|
49
|
+
model: config.model ?? null
|
|
50
|
+
});
|
|
51
|
+
if (!isLastAttempt) {
|
|
52
|
+
const delay = delayMs * Math.pow(2, attempt - 1);
|
|
53
|
+
await sleep(delay);
|
|
54
|
+
} else if (!isLastConfig) {
|
|
55
|
+
break;
|
|
56
|
+
}
|
|
44
57
|
}
|
|
45
58
|
}
|
|
59
|
+
if (configs.indexOf(config) === configs.length - 1 && lastError) {
|
|
60
|
+
return {
|
|
61
|
+
lm: null,
|
|
62
|
+
error: lastError
|
|
63
|
+
};
|
|
64
|
+
}
|
|
46
65
|
}
|
|
47
66
|
return {
|
|
48
67
|
lm: null,
|
|
49
|
-
error: new Error('Failed to initialize CactusLM')
|
|
68
|
+
error: new Error('Failed to initialize CactusLM after all retries')
|
|
50
69
|
};
|
|
51
70
|
}
|
|
52
71
|
async completion(messages, params = {}, callback) {
|
package/lib/module/lm.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["initLlama","
|
|
1
|
+
{"version":3,"names":["initLlama","Telemetry","setCactusToken","getVertexAIEmbedding","CactusLM","constructor","context","init","params","onProgress","cactusToken","retryOptions","maxRetries","delayMs","configs","n_gpu_layers","sleep","ms","Promise","resolve","start","Date","now","wait","then","config","lastError","attempt","lm","error","e","isLastConfig","indexOf","length","isLastAttempt","n_ctx","model","delay","Math","pow","Error","completion","messages","callback","embedding","text","mode","result","_handleRemoteEmbedding","_handleLocalEmbedding","rem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AK,CAAC,gBAAgB,GAAGM,IAAI,GAAG,6DAA6D,CAAC;IAC1G;IACA,OAAOC,MAAM;EACf;EAEA,MAAcE,qBAAqBA,CAACJ,IAAY,EAAErC,MAAwB,EAAkC;IAC1G,OAAO,IAAI,CAACF,OAAO,CAACsC,SAAS,CAACC,IAAI,EAAErC,MAAM,CAAC;EAC7C;EAEA,MAAcwC,sBAAsBA,CAACH,IAAY,EAAkC;IACjF,MAAMO,eAAe,GAAG,MAAMjD,oBAAoB,CAAC0C,IAAI,CAAC;IACxD,OAAO;MACLD,SAAS,EAAEQ;IACb,CAAC;EACH;EAEA,MAAMC,MAAMA,CAAA,EAAkB;IAC5B;IACA,OAAO,IAAI,CAAC/C,OAAO,EAAE+C,MAAM,CAAC,CAAC;EAC/B;EAEA,MAAMC,OAAOA,CAAA,EAAkB;IAC7B,OAAO,IAAI,CAAChD,OAAO,CAACgD,OAAO,CAAC,CAAC;EAC/B;AACF","ignoreList":[]}
|
package/lib/typescript/lm.d.ts
CHANGED
|
@@ -6,7 +6,10 @@ interface CactusLMReturn {
|
|
|
6
6
|
export declare class CactusLM {
|
|
7
7
|
private context;
|
|
8
8
|
private constructor();
|
|
9
|
-
static init(params: ContextParams, onProgress?: (progress: number) => void, cactusToken?: string
|
|
9
|
+
static init(params: ContextParams, onProgress?: (progress: number) => void, cactusToken?: string, retryOptions?: {
|
|
10
|
+
maxRetries?: number;
|
|
11
|
+
delayMs?: number;
|
|
12
|
+
}): Promise<CactusLMReturn>;
|
|
10
13
|
completion(messages: CactusOAICompatibleMessage[], params?: CompletionParams, callback?: (data: any) => void): Promise<NativeCompletionResult>;
|
|
11
14
|
embedding(text: string, params?: EmbeddingParams, mode?: string): Promise<NativeEmbeddingResult>;
|
|
12
15
|
private _handleLocalEmbedding;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"lm.d.ts","sourceRoot":"","sources":["../../src/lm.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"lm.d.ts","sourceRoot":"","sources":["../../src/lm.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,aAAa,EACb,gBAAgB,EAChB,0BAA0B,EAC1B,sBAAsB,EACtB,eAAe,EACf,qBAAqB,EACtB,MAAM,SAAS,CAAA;AAIhB,UAAU,cAAc;IACtB,EAAE,EAAE,QAAQ,GAAG,IAAI,CAAA;IACnB,KAAK,EAAE,KAAK,GAAG,IAAI,CAAA;CACpB;AAED,qBAAa,QAAQ;IACnB,OAAO,CAAC,OAAO,CAAc;IAE7B,OAAO;WAIM,IAAI,CACf,MAAM,EAAE,aAAa,EACrB,UAAU,CAAC,EAAE,CAAC,QAAQ,EAAE,MAAM,KAAK,IAAI,EACvC,WAAW,CAAC,EAAE,MAAM,EACpB,YAAY,CAAC,EAAE;QAAE,UAAU,CAAC,EAAE,MAAM,CAAC;QAAC,OAAO,CAAC,EAAE,MAAM,CAAA;KAAE,GACvD,OAAO,CAAC,cAAc,CAAC;IA6DpB,UAAU,CACd,QAAQ,EAAE,0BAA0B,EAAE,EACtC,MAAM,GAAE,gBAAqB,EAC7B,QAAQ,CAAC,EAAE,CAAC,IAAI,EAAE,GAAG,KAAK,IAAI,GAC7B,OAAO,CAAC,sBAAsB,CAAC;IAI5B,SAAS,CACb,IAAI,EAAE,MAAM,EACZ,MAAM,CAAC,EAAE,eAAe,EACxB,IAAI,GAAE,MAAgB,GACrB,OAAO,CAAC,qBAAqB,CAAC;YAoCnB,qBAAqB;YAIrB,sBAAsB;IAO9B,MAAM,IAAI,OAAO,CAAC,IAAI,CAAC;IAKvB,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;CAG/B"}
|
package/package.json
CHANGED
package/src/lm.ts
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
1
|
import { initLlama, LlamaContext } from './index'
|
|
2
|
-
// @ts-ignore
|
|
3
|
-
import { Platform } from 'react-native'
|
|
4
2
|
import type {
|
|
5
3
|
ContextParams,
|
|
6
4
|
CompletionParams,
|
|
@@ -28,33 +26,66 @@ export class CactusLM {
|
|
|
28
26
|
params: ContextParams,
|
|
29
27
|
onProgress?: (progress: number) => void,
|
|
30
28
|
cactusToken?: string,
|
|
29
|
+
retryOptions?: { maxRetries?: number; delayMs?: number },
|
|
31
30
|
): Promise<CactusLMReturn> {
|
|
32
31
|
if (cactusToken) {
|
|
33
32
|
setCactusToken(cactusToken);
|
|
34
33
|
}
|
|
35
34
|
|
|
36
|
-
|
|
37
|
-
const
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
35
|
+
const maxRetries = retryOptions?.maxRetries ?? 3;
|
|
36
|
+
const delayMs = retryOptions?.delayMs ?? 1000;
|
|
37
|
+
|
|
38
|
+
const configs = [
|
|
39
|
+
params,
|
|
40
|
+
{ ...params, n_gpu_layers: 0 }
|
|
41
|
+
];
|
|
42
|
+
|
|
43
|
+
const sleep = (ms: number): Promise<void> => {
|
|
44
|
+
return new Promise(resolve => {
|
|
45
|
+
const start = Date.now();
|
|
46
|
+
const wait = () => {
|
|
47
|
+
if (Date.now() - start >= ms) {
|
|
48
|
+
resolve();
|
|
49
|
+
} else {
|
|
50
|
+
Promise.resolve().then(wait);
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
wait();
|
|
54
|
+
});
|
|
55
|
+
};
|
|
41
56
|
|
|
42
57
|
for (const config of configs) {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
58
|
+
let lastError: Error | null = null;
|
|
59
|
+
|
|
60
|
+
for (let attempt = 1; attempt <= maxRetries; attempt++) {
|
|
61
|
+
try {
|
|
62
|
+
const context = await initLlama(config, onProgress);
|
|
63
|
+
return { lm: new CactusLM(context), error: null };
|
|
64
|
+
} catch (e) {
|
|
65
|
+
lastError = e as Error;
|
|
66
|
+
const isLastConfig = configs.indexOf(config) === configs.length - 1;
|
|
67
|
+
const isLastAttempt = attempt === maxRetries;
|
|
68
|
+
|
|
69
|
+
Telemetry.error(e as Error, {
|
|
70
|
+
n_gpu_layers: config.n_gpu_layers ?? null,
|
|
71
|
+
n_ctx: config.n_ctx ?? null,
|
|
72
|
+
model: config.model ?? null,
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
if (!isLastAttempt) {
|
|
76
|
+
const delay = delayMs * Math.pow(2, attempt - 1);
|
|
77
|
+
await sleep(delay);
|
|
78
|
+
} else if (!isLastConfig) {
|
|
79
|
+
break;
|
|
80
|
+
}
|
|
54
81
|
}
|
|
55
82
|
}
|
|
83
|
+
|
|
84
|
+
if (configs.indexOf(config) === configs.length - 1 && lastError) {
|
|
85
|
+
return { lm: null, error: lastError };
|
|
86
|
+
}
|
|
56
87
|
}
|
|
57
|
-
return { lm: null, error: new Error('Failed to initialize CactusLM') };
|
|
88
|
+
return { lm: null, error: new Error('Failed to initialize CactusLM after all retries') };
|
|
58
89
|
}
|
|
59
90
|
|
|
60
91
|
async completion(
|