cactus-react-native 0.1.4 → 0.2.1
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/README.md +550 -721
- package/android/src/main/java/com/cactus/Cactus.java +41 -0
- package/android/src/main/java/com/cactus/LlamaContext.java +19 -0
- package/android/src/newarch/java/com/cactus/CactusModule.java +5 -0
- package/android/src/oldarch/java/com/cactus/CactusModule.java +5 -0
- package/ios/Cactus.mm +14 -0
- package/ios/CactusContext.h +1 -0
- package/ios/CactusContext.mm +18 -0
- package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/cactus +0 -0
- package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/cactus +0 -0
- package/lib/commonjs/NativeCactus.js.map +1 -1
- package/lib/commonjs/index.js +92 -6
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/lm.js +54 -20
- package/lib/commonjs/lm.js.map +1 -1
- package/lib/commonjs/projectId.js +8 -0
- package/lib/commonjs/projectId.js.map +1 -0
- package/lib/commonjs/remote.js +153 -0
- package/lib/commonjs/remote.js.map +1 -0
- package/lib/commonjs/telemetry.js +11 -5
- package/lib/commonjs/telemetry.js.map +1 -1
- package/lib/commonjs/vlm.js +90 -23
- package/lib/commonjs/vlm.js.map +1 -1
- package/lib/module/NativeCactus.js.map +1 -1
- package/lib/module/index.js +48 -5
- package/lib/module/index.js.map +1 -1
- package/lib/module/lm.js +54 -20
- package/lib/module/lm.js.map +1 -1
- package/lib/module/projectId.js +4 -0
- package/lib/module/projectId.js.map +1 -0
- package/lib/module/remote.js +144 -0
- package/lib/module/remote.js.map +1 -0
- package/lib/module/telemetry.js +11 -5
- package/lib/module/telemetry.js.map +1 -1
- package/lib/module/vlm.js +90 -23
- package/lib/module/vlm.js.map +1 -1
- package/lib/typescript/NativeCactus.d.ts +7 -0
- package/lib/typescript/NativeCactus.d.ts.map +1 -1
- package/lib/typescript/index.d.ts +3 -1
- package/lib/typescript/index.d.ts.map +1 -1
- package/lib/typescript/lm.d.ts +4 -3
- package/lib/typescript/lm.d.ts.map +1 -1
- package/lib/typescript/projectId.d.ts +2 -0
- package/lib/typescript/projectId.d.ts.map +1 -0
- package/lib/typescript/remote.d.ts +7 -0
- package/lib/typescript/remote.d.ts.map +1 -0
- package/lib/typescript/telemetry.d.ts +7 -3
- package/lib/typescript/telemetry.d.ts.map +1 -1
- package/lib/typescript/vlm.d.ts +4 -2
- package/lib/typescript/vlm.d.ts.map +1 -1
- package/package.json +4 -4
- package/scripts/postInstall.js +33 -0
- package/src/NativeCactus.ts +7 -0
- package/src/index.ts +58 -5
- package/src/lm.ts +59 -24
- package/src/projectId.ts +1 -0
- package/src/remote.ts +175 -0
- package/src/telemetry.ts +27 -12
- package/src/vlm.ts +104 -25
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import type { NativeContextParams, NativeLlamaContext, NativeCompletionParams, NativeCompletionTokenProb, NativeCompletionResult, NativeTokenizeResult, NativeEmbeddingResult, NativeSessionLoadResult, NativeEmbeddingParams, NativeCompletionTokenProbItem, NativeCompletionResultTimings, JinjaFormattedChatResult, NativeTTSType, NativeAudioCompletionResult, NativeAudioTokensResult, NativeAudioDecodeResult } from './NativeCactus';
|
|
1
|
+
import type { NativeContextParams, NativeLlamaContext, NativeCompletionParams, NativeCompletionTokenProb, NativeCompletionResult, NativeTokenizeResult, NativeEmbeddingResult, NativeSessionLoadResult, NativeEmbeddingParams, NativeCompletionTokenProbItem, NativeCompletionResultTimings, JinjaFormattedChatResult, NativeTTSType, NativeAudioCompletionResult, NativeAudioTokensResult, NativeAudioDecodeResult, NativeDeviceInfo } from './NativeCactus';
|
|
2
2
|
import type { SchemaGrammarConverterPropOrder, SchemaGrammarConverterBuiltinRule } from './grammar';
|
|
3
3
|
import { SchemaGrammarConverter, convertJsonSchemaToGrammar } from './grammar';
|
|
4
4
|
import type { CactusMessagePart, CactusOAICompatibleMessage } from './chat';
|
|
5
5
|
import { Tools } from './tools';
|
|
6
6
|
export type { NativeContextParams, NativeLlamaContext, NativeCompletionParams, NativeCompletionTokenProb, NativeCompletionResult, NativeTokenizeResult, NativeEmbeddingResult, NativeSessionLoadResult, NativeEmbeddingParams, NativeCompletionTokenProbItem, NativeCompletionResultTimings, CactusMessagePart, CactusOAICompatibleMessage, JinjaFormattedChatResult, NativeAudioDecodeResult, SchemaGrammarConverterPropOrder, SchemaGrammarConverterBuiltinRule, };
|
|
7
7
|
export { SchemaGrammarConverter, convertJsonSchemaToGrammar, Tools };
|
|
8
|
+
export * from './remote';
|
|
8
9
|
export type TokenData = {
|
|
9
10
|
token: string;
|
|
10
11
|
completion_probabilities?: Array<NativeCompletionTokenProb>;
|
|
@@ -113,6 +114,7 @@ export declare const getAudioCompletionGuideTokens: (contextId: number, textToSp
|
|
|
113
114
|
export declare const decodeAudioTokens: (contextId: number, tokens: number[]) => Promise<NativeAudioDecodeResult>;
|
|
114
115
|
export declare const releaseVocoder: (contextId: number) => Promise<void>;
|
|
115
116
|
export declare const tokenize: (contextId: number, text: string, mediaPaths?: string[]) => Promise<NativeTokenizeResult>;
|
|
117
|
+
export declare const getDeviceInfo: (contextId: number) => Promise<NativeDeviceInfo>;
|
|
116
118
|
export { CactusLM } from './lm';
|
|
117
119
|
export { CactusVLM } from './vlm';
|
|
118
120
|
export { CactusTTS } from './tts';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EACV,mBAAmB,EACnB,kBAAkB,EAClB,sBAAsB,EACtB,yBAAyB,EACzB,sBAAsB,EACtB,oBAAoB,EACpB,qBAAqB,EACrB,uBAAuB,EACvB,qBAAqB,EACrB,6BAA6B,EAC7B,6BAA6B,EAC7B,wBAAwB,EACxB,aAAa,EACb,2BAA2B,EAC3B,uBAAuB,EACvB,uBAAuB,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index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EF,eAAO,MAAM,6BAA6B,GAAU,WAAW,MAAM,EAAE,aAAa,MAAM,KAAG,OAAO,CAAC,uBAAuB,CAE3H,CAAC;AAEF,eAAO,MAAM,iBAAiB,GAAU,WAAW,MAAM,EAAE,QAAQ,MAAM,EAAE,KAAG,OAAO,CAAC,uBAAuB,CAE5G,CAAC;AAEF,eAAO,MAAM,cAAc,GAAU,WAAW,MAAM,kBAErD,CAAC;AAEF,eAAO,MAAM,QAAQ,GAAU,WAAW,MAAM,EAAE,MAAM,MAAM,EAAE,aAAa,MAAM,EAAE,KAAG,OAAO,CAAC,oBAAoB,CAMnH,CAAC;AAEF,eAAO,MAAM,aAAa,GAAU,WAAW,MAAM,KAAG,OAAO,CAAC,gBAAgB,CAE/E,CAAC;AAEF,OAAO,EAAE,QAAQ,EAAE,MAAM,MAAM,CAAC;AAChC,OAAO,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;AAClC,OAAO,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC"}
|
package/lib/typescript/lm.d.ts
CHANGED
|
@@ -5,11 +5,12 @@ interface CactusLMReturn {
|
|
|
5
5
|
}
|
|
6
6
|
export declare class CactusLM {
|
|
7
7
|
private context;
|
|
8
|
-
private initParams;
|
|
9
8
|
private constructor();
|
|
10
|
-
static init(params: ContextParams, onProgress?: (progress: number) => void): Promise<CactusLMReturn>;
|
|
9
|
+
static init(params: ContextParams, onProgress?: (progress: number) => void, cactusToken?: string): Promise<CactusLMReturn>;
|
|
11
10
|
completion(messages: CactusOAICompatibleMessage[], params?: CompletionParams, callback?: (data: any) => void): Promise<NativeCompletionResult>;
|
|
12
|
-
embedding(text: string, params?: EmbeddingParams): Promise<NativeEmbeddingResult>;
|
|
11
|
+
embedding(text: string, params?: EmbeddingParams, mode?: string): Promise<NativeEmbeddingResult>;
|
|
12
|
+
private _handleLocalEmbedding;
|
|
13
|
+
private _handleRemoteEmbedding;
|
|
13
14
|
rewind(): Promise<void>;
|
|
14
15
|
release(): Promise<void>;
|
|
15
16
|
}
|
|
@@ -1 +1 @@
|
|
|
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;
|
|
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,GACnB,OAAO,CAAC,cAAc,CAAC;IA4BpB,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"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"projectId.d.ts","sourceRoot":"","sources":["../../src/projectId.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,UAAU,YAAY,CAAC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export declare function setCactusToken(token: string | null): void;
|
|
2
|
+
export declare function getVertexAIEmbedding(text: string): Promise<number[]>;
|
|
3
|
+
export declare function getVertexAICompletion(textPrompt: string, imageData?: string, imagePath?: string, mimeType?: string): Promise<string>;
|
|
4
|
+
export declare function getTextCompletion(prompt: string): Promise<string>;
|
|
5
|
+
export declare function getVisionCompletion(prompt: string, imagePath: string): Promise<string>;
|
|
6
|
+
export declare function getVisionCompletionFromData(prompt: string, imageData: string, mimeType?: string): Promise<string>;
|
|
7
|
+
//# sourceMappingURL=remote.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"remote.d.ts","sourceRoot":"","sources":["../../src/remote.ts"],"names":[],"mappings":"AAEA,wBAAgB,cAAc,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,GAAG,IAAI,CAEzD;AAED,wBAAsB,oBAAoB,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,CAiD1E;AAED,wBAAsB,qBAAqB,CACzC,UAAU,EAAE,MAAM,EAClB,SAAS,CAAC,EAAE,MAAM,EAClB,SAAS,CAAC,EAAE,MAAM,EAClB,QAAQ,CAAC,EAAE,MAAM,GAChB,OAAO,CAAC,MAAM,CAAC,CAmFjB;AAED,wBAAsB,iBAAiB,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAEvE;AAED,wBAAsB,mBAAmB,CAAC,MAAM,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAE5F;AAED,wBAAsB,2BAA2B,CAAC,MAAM,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAEvH"}
|
|
@@ -1,4 +1,8 @@
|
|
|
1
|
-
|
|
1
|
+
export interface TelemetryParams {
|
|
2
|
+
n_gpu_layers: number | null;
|
|
3
|
+
n_ctx: number | null;
|
|
4
|
+
model: string | null;
|
|
5
|
+
}
|
|
2
6
|
interface TelemetryConfig {
|
|
3
7
|
supabaseUrl: string;
|
|
4
8
|
supabaseKey: string;
|
|
@@ -11,8 +15,8 @@ export declare class Telemetry {
|
|
|
11
15
|
private static getFilename;
|
|
12
16
|
static autoInit(): void;
|
|
13
17
|
static init(config: TelemetryConfig): void;
|
|
14
|
-
static track(payload: Record<string, any>, options:
|
|
15
|
-
static error(error: Error, options:
|
|
18
|
+
static track(payload: Record<string, any>, options: TelemetryParams, deviceMetadata?: Record<string, any>): void;
|
|
19
|
+
static error(error: Error, options: TelemetryParams): void;
|
|
16
20
|
private trackInternal;
|
|
17
21
|
private errorInternal;
|
|
18
22
|
private sendRecord;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"telemetry.d.ts","sourceRoot":"","sources":["../../src/telemetry.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"telemetry.d.ts","sourceRoot":"","sources":["../../src/telemetry.ts"],"names":[],"mappings":"AAKA,MAAM,WAAW,eAAe;IAC9B,YAAY,EAAE,MAAM,GAAG,IAAI,CAAA;IAC3B,KAAK,EAAE,MAAM,GAAG,IAAI,CAAA;IACpB,KAAK,EAAE,MAAM,GAAG,IAAI,CAAA;CACrB;AAmBD,UAAU,eAAe;IACvB,WAAW,EAAE,MAAM,CAAC;IACpB,WAAW,EAAE,MAAM,CAAC;IACpB,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAED,qBAAa,SAAS;IACpB,OAAO,CAAC,MAAM,CAAC,QAAQ,CAA0B;IACjD,OAAO,CAAC,MAAM,CAA4B;IAE1C,OAAO;IAOP,OAAO,CAAC,MAAM,CAAC,WAAW;IAQ1B,MAAM,CAAC,QAAQ,IAAI,IAAI;IASvB,MAAM,CAAC,IAAI,CAAC,MAAM,EAAE,eAAe,GAAG,IAAI;IAM1C,MAAM,CAAC,KAAK,CAAC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE,OAAO,EAAE,eAAe,EAAE,cAAc,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,IAAI;IAOhH,MAAM,CAAC,KAAK,CAAC,KAAK,EAAE,KAAK,EAAE,OAAO,EAAE,eAAe,GAAG,IAAI;IAO1D,OAAO,CAAC,aAAa;IAoBrB,OAAO,CAAC,aAAa;YAuBP,UAAU;CAYzB"}
|
package/lib/typescript/vlm.d.ts
CHANGED
|
@@ -8,13 +8,15 @@ export type VLMContextParams = ContextParams & {
|
|
|
8
8
|
};
|
|
9
9
|
export type VLMCompletionParams = Omit<CompletionParams, 'prompt'> & {
|
|
10
10
|
images?: string[];
|
|
11
|
+
mode?: string;
|
|
11
12
|
};
|
|
12
13
|
export declare class CactusVLM {
|
|
13
14
|
private context;
|
|
14
|
-
private initParams;
|
|
15
15
|
private constructor();
|
|
16
|
-
static init(params: VLMContextParams, onProgress?: (progress: number) => void): Promise<CactusVLMReturn>;
|
|
16
|
+
static init(params: VLMContextParams, onProgress?: (progress: number) => void, cactusToken?: string): Promise<CactusVLMReturn>;
|
|
17
17
|
completion(messages: CactusOAICompatibleMessage[], params?: VLMCompletionParams, callback?: (data: any) => void): Promise<NativeCompletionResult>;
|
|
18
|
+
private _handleLocalCompletion;
|
|
19
|
+
private _handleRemoteCompletion;
|
|
18
20
|
rewind(): Promise<void>;
|
|
19
21
|
release(): Promise<void>;
|
|
20
22
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"vlm.d.ts","sourceRoot":"","sources":["../../src/vlm.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EACV,aAAa,EACb,gBAAgB,EAChB,0BAA0B,EAC1B,sBAAsB,EACvB,MAAM,SAAS,CAAA;
|
|
1
|
+
{"version":3,"file":"vlm.d.ts","sourceRoot":"","sources":["../../src/vlm.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EACV,aAAa,EACb,gBAAgB,EAChB,0BAA0B,EAC1B,sBAAsB,EACvB,MAAM,SAAS,CAAA;AAIhB,UAAU,eAAe;IACvB,GAAG,EAAE,SAAS,GAAG,IAAI,CAAA;IACrB,KAAK,EAAE,KAAK,GAAG,IAAI,CAAA;CACpB;AAED,MAAM,MAAM,gBAAgB,GAAG,aAAa,GAAG;IAC7C,MAAM,EAAE,MAAM,CAAA;CACf,CAAA;AAED,MAAM,MAAM,mBAAmB,GAAG,IAAI,CAAC,gBAAgB,EAAE,QAAQ,CAAC,GAAG;IACnE,MAAM,CAAC,EAAE,MAAM,EAAE,CAAA;IACjB,IAAI,CAAC,EAAE,MAAM,CAAA;CACd,CAAA;AAED,qBAAa,SAAS;IACpB,OAAO,CAAC,OAAO,CAAc;IAE7B,OAAO;WAIM,IAAI,CACf,MAAM,EAAE,gBAAgB,EACxB,UAAU,CAAC,EAAE,CAAC,QAAQ,EAAE,MAAM,KAAK,IAAI,EACvC,WAAW,CAAC,EAAE,MAAM,GACnB,OAAO,CAAC,eAAe,CAAC;IA8BrB,UAAU,CACd,QAAQ,EAAE,0BAA0B,EAAE,EACtC,MAAM,GAAE,mBAAwB,EAChC,QAAQ,CAAC,EAAE,CAAC,IAAI,EAAE,GAAG,KAAK,IAAI,GAC7B,OAAO,CAAC,sBAAsB,CAAC;YAuCpB,sBAAsB;YAsBtB,uBAAuB;IA+C/B,MAAM,IAAI,OAAO,CAAC,IAAI,CAAC;IAKvB,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;CAG/B"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cactus-react-native",
|
|
3
|
-
"version": "0.1
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"description": "Run AI models locally on mobile devices",
|
|
5
5
|
"main": "lib/commonjs/index",
|
|
6
6
|
"module": "lib/module/index",
|
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
"files": [
|
|
11
11
|
"src",
|
|
12
12
|
"lib",
|
|
13
|
+
"scripts",
|
|
13
14
|
"ios",
|
|
14
15
|
"ios/CMakeLists.txt",
|
|
15
16
|
"android",
|
|
@@ -31,7 +32,8 @@
|
|
|
31
32
|
"docgen": "typedoc src/index.ts --plugin typedoc-plugin-markdown --readme none --out docs/API",
|
|
32
33
|
"typecheck": "tsc --noEmit",
|
|
33
34
|
"lint": "eslint \"**/*.{js,ts,tsx}\"",
|
|
34
|
-
"release": "release-it --ci"
|
|
35
|
+
"release": "release-it --ci",
|
|
36
|
+
"postinstall": "node ./scripts/postInstall.js"
|
|
35
37
|
},
|
|
36
38
|
"keywords": [
|
|
37
39
|
"react-native",
|
|
@@ -62,7 +64,6 @@
|
|
|
62
64
|
"@types/jest": "^28.1.2",
|
|
63
65
|
"@types/react": "~17.0.21",
|
|
64
66
|
"@types/react-native": "0.70.0",
|
|
65
|
-
|
|
66
67
|
"commitlint": "^17.0.2",
|
|
67
68
|
"eslint": "^8.4.1",
|
|
68
69
|
"jest": "^28.1.1",
|
|
@@ -71,7 +72,6 @@
|
|
|
71
72
|
"react": "18.2.0",
|
|
72
73
|
"react-native": "0.72.3",
|
|
73
74
|
"react-native-builder-bob": "^0.37.0",
|
|
74
|
-
|
|
75
75
|
"release-it": "^15.0.0",
|
|
76
76
|
"typedoc": "^0.24.7",
|
|
77
77
|
"typedoc-plugin-markdown": "^3.15.3",
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
const fs = require('fs');
|
|
2
|
+
const path = require('path');
|
|
3
|
+
const crypto = require('crypto');
|
|
4
|
+
|
|
5
|
+
const projectRoot = process.env.INIT_CWD;
|
|
6
|
+
|
|
7
|
+
if (!projectRoot || projectRoot === process.cwd()) {
|
|
8
|
+
return;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const rootIdPath = path.join(projectRoot, '.projectId.json');
|
|
12
|
+
const internalIdPath = path.join(__dirname, '..', 'src', 'projectId.ts');
|
|
13
|
+
|
|
14
|
+
let projectId;
|
|
15
|
+
try {
|
|
16
|
+
if (fs.existsSync(rootIdPath)) {
|
|
17
|
+
projectId = JSON.parse(fs.readFileSync(rootIdPath, 'utf8'));
|
|
18
|
+
} else {
|
|
19
|
+
projectId = crypto.randomUUID();
|
|
20
|
+
fs.writeFileSync(rootIdPath, JSON.stringify(projectId), 'utf8');
|
|
21
|
+
}
|
|
22
|
+
} catch (error) {
|
|
23
|
+
console.error('Failed to read or create project ID.', error);
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const fileContent = `// DO NOT EDIT. This file is automatically generated by a post-install script.\nexport const PROJECT_ID = '${projectId}';`;
|
|
28
|
+
|
|
29
|
+
try {
|
|
30
|
+
fs.writeFileSync(internalIdPath, fileContent, 'utf8');
|
|
31
|
+
} catch (error) {
|
|
32
|
+
console.error('Failed to sync project ID into the framework.', error);
|
|
33
|
+
}
|
package/src/NativeCactus.ts
CHANGED
|
@@ -306,6 +306,12 @@ export type NativeAudioDecodeResult = {
|
|
|
306
306
|
audio_data: Array<number> // Float array of audio samples
|
|
307
307
|
}
|
|
308
308
|
|
|
309
|
+
export type NativeDeviceInfo = {
|
|
310
|
+
deviceId: string
|
|
311
|
+
model: string
|
|
312
|
+
make: string
|
|
313
|
+
os: string
|
|
314
|
+
}
|
|
309
315
|
export type NativeLlamaContext = {
|
|
310
316
|
contextId: number
|
|
311
317
|
model: {
|
|
@@ -469,6 +475,7 @@ export interface Spec extends TurboModule {
|
|
|
469
475
|
contextId: number,
|
|
470
476
|
tokens: number[],
|
|
471
477
|
): Promise<NativeAudioDecodeResult>
|
|
478
|
+
getDeviceInfo(contextId: number): Promise<NativeDeviceInfo>
|
|
472
479
|
releaseVocoder(contextId: number): Promise<void>
|
|
473
480
|
|
|
474
481
|
releaseContext(contextId: number): Promise<void>
|
package/src/index.ts
CHANGED
|
@@ -18,6 +18,7 @@ import type {
|
|
|
18
18
|
NativeAudioCompletionResult,
|
|
19
19
|
NativeAudioTokensResult,
|
|
20
20
|
NativeAudioDecodeResult,
|
|
21
|
+
NativeDeviceInfo,
|
|
21
22
|
} from './NativeCactus'
|
|
22
23
|
import type {
|
|
23
24
|
SchemaGrammarConverterPropOrder,
|
|
@@ -27,6 +28,7 @@ import { SchemaGrammarConverter, convertJsonSchemaToGrammar } from './grammar'
|
|
|
27
28
|
import type { CactusMessagePart, CactusOAICompatibleMessage } from './chat'
|
|
28
29
|
import { formatChat } from './chat'
|
|
29
30
|
import { Tools, parseAndExecuteTool } from './tools'
|
|
31
|
+
import { Telemetry, type TelemetryParams } from './telemetry'
|
|
30
32
|
export type {
|
|
31
33
|
NativeContextParams,
|
|
32
34
|
NativeLlamaContext,
|
|
@@ -50,6 +52,7 @@ export type {
|
|
|
50
52
|
}
|
|
51
53
|
|
|
52
54
|
export { SchemaGrammarConverter, convertJsonSchemaToGrammar, Tools }
|
|
55
|
+
export * from './remote'
|
|
53
56
|
|
|
54
57
|
const EVENT_ON_INIT_CONTEXT_PROGRESS = '@Cactus_onInitContextProgress'
|
|
55
58
|
const EVENT_ON_TOKEN = '@Cactus_onToken'
|
|
@@ -161,6 +164,12 @@ const getJsonSchema = (responseFormat?: CompletionResponseFormat) => {
|
|
|
161
164
|
return null
|
|
162
165
|
}
|
|
163
166
|
|
|
167
|
+
const telemetryParams: TelemetryParams = {
|
|
168
|
+
n_gpu_layers: null,
|
|
169
|
+
n_ctx: null,
|
|
170
|
+
model: null
|
|
171
|
+
}
|
|
172
|
+
|
|
164
173
|
export class LlamaContext {
|
|
165
174
|
id: number
|
|
166
175
|
|
|
@@ -347,12 +356,21 @@ export class LlamaContext {
|
|
|
347
356
|
if (jsonSchema) nativeParams.json_schema = JSON.stringify(jsonSchema)
|
|
348
357
|
}
|
|
349
358
|
|
|
359
|
+
const startTime = Date.now();
|
|
360
|
+
let firstTokenTime: number | null = null;
|
|
361
|
+
const deviceInfo = await getDeviceInfo(this.id);
|
|
362
|
+
|
|
363
|
+
const wrappedCallback = callback ? (data: any) => {
|
|
364
|
+
if (firstTokenTime === null) firstTokenTime = Date.now();
|
|
365
|
+
callback(data);
|
|
366
|
+
} : undefined;
|
|
367
|
+
|
|
350
368
|
let tokenListener: any =
|
|
351
|
-
|
|
369
|
+
wrappedCallback &&
|
|
352
370
|
EventEmitter.addListener(EVENT_ON_TOKEN, (evt: TokenNativeEvent) => {
|
|
353
371
|
const { contextId, tokenResult } = evt
|
|
354
372
|
if (contextId !== this.id) return
|
|
355
|
-
|
|
373
|
+
wrappedCallback(tokenResult)
|
|
356
374
|
})
|
|
357
375
|
|
|
358
376
|
if (!nativeParams.prompt) throw new Error('Prompt is required')
|
|
@@ -360,6 +378,12 @@ export class LlamaContext {
|
|
|
360
378
|
const promise = Cactus.completion(this.id, nativeParams)
|
|
361
379
|
return promise
|
|
362
380
|
.then((completionResult) => {
|
|
381
|
+
Telemetry.track({
|
|
382
|
+
event: 'completion',
|
|
383
|
+
tok_per_sec: (completionResult as any).timings?.predicted_per_second,
|
|
384
|
+
toks_generated: (completionResult as any).timings?.predicted_n,
|
|
385
|
+
ttft: firstTokenTime ? firstTokenTime - startTime : null,
|
|
386
|
+
}, telemetryParams, deviceInfo);
|
|
363
387
|
tokenListener?.remove()
|
|
364
388
|
tokenListener = null
|
|
365
389
|
return completionResult
|
|
@@ -383,11 +407,21 @@ export class LlamaContext {
|
|
|
383
407
|
return Cactus.detokenize(this.id, tokens)
|
|
384
408
|
}
|
|
385
409
|
|
|
386
|
-
embedding(
|
|
410
|
+
async embedding(
|
|
387
411
|
text: string,
|
|
388
412
|
params?: EmbeddingParams,
|
|
389
413
|
): Promise<NativeEmbeddingResult> {
|
|
390
|
-
|
|
414
|
+
const startTime = Date.now();
|
|
415
|
+
const embeddingResult = await Cactus.embedding(this.id, text, params || {})
|
|
416
|
+
const totalTime = Date.now() - startTime;
|
|
417
|
+
const deviceInfo = await getDeviceInfo(this.id);
|
|
418
|
+
Telemetry.track({
|
|
419
|
+
event: 'embedding',
|
|
420
|
+
mode: 'local',
|
|
421
|
+
embedding_time: totalTime,
|
|
422
|
+
}, telemetryParams, deviceInfo);
|
|
423
|
+
return embeddingResult;
|
|
424
|
+
|
|
391
425
|
}
|
|
392
426
|
|
|
393
427
|
async bench(
|
|
@@ -511,6 +545,10 @@ export async function initLlama(
|
|
|
511
545
|
scaled: l.scaled,
|
|
512
546
|
}))
|
|
513
547
|
|
|
548
|
+
telemetryParams.n_gpu_layers = rest.n_gpu_layers || null;
|
|
549
|
+
telemetryParams.n_ctx = rest.n_ctx || null;
|
|
550
|
+
telemetryParams.model = model;
|
|
551
|
+
|
|
514
552
|
const contextId = contextIdCounter + contextIdRandom()
|
|
515
553
|
contextIdCounter += 1
|
|
516
554
|
|
|
@@ -582,7 +620,18 @@ export const releaseMultimodal = async (contextId: number) => {
|
|
|
582
620
|
};
|
|
583
621
|
|
|
584
622
|
export const multimodalCompletion = async (contextId: number, prompt: string, mediaPaths: string[], params: NativeCompletionParams): Promise<NativeCompletionResult> => {
|
|
585
|
-
|
|
623
|
+
const result = await Cactus.multimodalCompletion(contextId, prompt, mediaPaths, params);
|
|
624
|
+
|
|
625
|
+
const deviceInfo = await getDeviceInfo(contextId);
|
|
626
|
+
|
|
627
|
+
Telemetry.track({
|
|
628
|
+
event: 'completion',
|
|
629
|
+
tok_per_sec: (result as any).timings?.predicted_per_second,
|
|
630
|
+
toks_generated: (result as any).timings?.predicted_n,
|
|
631
|
+
num_images: mediaPaths?.length,
|
|
632
|
+
}, telemetryParams, deviceInfo);
|
|
633
|
+
|
|
634
|
+
return result;
|
|
586
635
|
};
|
|
587
636
|
|
|
588
637
|
export const initVocoder = async (contextId: number, vocoderModelPath: string) => {
|
|
@@ -621,6 +670,10 @@ export const tokenize = async (contextId: number, text: string, mediaPaths?: str
|
|
|
621
670
|
}
|
|
622
671
|
};
|
|
623
672
|
|
|
673
|
+
export const getDeviceInfo = async (contextId: number): Promise<NativeDeviceInfo> => {
|
|
674
|
+
return await Cactus.getDeviceInfo(contextId);
|
|
675
|
+
};
|
|
676
|
+
|
|
624
677
|
export { CactusLM } from './lm';
|
|
625
678
|
export { CactusVLM } from './vlm';
|
|
626
679
|
export { CactusTTS } from './tts';
|
package/src/lm.ts
CHANGED
|
@@ -8,6 +8,7 @@ import type {
|
|
|
8
8
|
NativeEmbeddingResult,
|
|
9
9
|
} from './index'
|
|
10
10
|
import { Telemetry } from './telemetry'
|
|
11
|
+
import { setCactusToken, getVertexAIEmbedding } from './remote'
|
|
11
12
|
|
|
12
13
|
interface CactusLMReturn {
|
|
13
14
|
lm: CactusLM | null
|
|
@@ -16,17 +17,20 @@ interface CactusLMReturn {
|
|
|
16
17
|
|
|
17
18
|
export class CactusLM {
|
|
18
19
|
private context: LlamaContext
|
|
19
|
-
private initParams: ContextParams
|
|
20
20
|
|
|
21
|
-
private constructor(context: LlamaContext
|
|
21
|
+
private constructor(context: LlamaContext) {
|
|
22
22
|
this.context = context
|
|
23
|
-
this.initParams = initParams
|
|
24
23
|
}
|
|
25
24
|
|
|
26
25
|
static async init(
|
|
27
26
|
params: ContextParams,
|
|
28
27
|
onProgress?: (progress: number) => void,
|
|
28
|
+
cactusToken?: string,
|
|
29
29
|
): Promise<CactusLMReturn> {
|
|
30
|
+
if (cactusToken) {
|
|
31
|
+
setCactusToken(cactusToken);
|
|
32
|
+
}
|
|
33
|
+
|
|
30
34
|
const configs = [
|
|
31
35
|
params,
|
|
32
36
|
{ ...params, n_gpu_layers: 0 }
|
|
@@ -35,9 +39,13 @@ export class CactusLM {
|
|
|
35
39
|
for (const config of configs) {
|
|
36
40
|
try {
|
|
37
41
|
const context = await initLlama(config, onProgress);
|
|
38
|
-
return { lm: new CactusLM(context
|
|
42
|
+
return { lm: new CactusLM(context), error: null };
|
|
39
43
|
} catch (e) {
|
|
40
|
-
Telemetry.error(e as Error,
|
|
44
|
+
Telemetry.error(e as Error, {
|
|
45
|
+
n_gpu_layers: config.n_gpu_layers ?? null,
|
|
46
|
+
n_ctx: config.n_ctx ?? null,
|
|
47
|
+
model: config.model ?? null,
|
|
48
|
+
});
|
|
41
49
|
if (configs.indexOf(config) === configs.length - 1) {
|
|
42
50
|
return { lm: null, error: e as Error };
|
|
43
51
|
}
|
|
@@ -51,31 +59,58 @@ export class CactusLM {
|
|
|
51
59
|
params: CompletionParams = {},
|
|
52
60
|
callback?: (data: any) => void,
|
|
53
61
|
): Promise<NativeCompletionResult> {
|
|
54
|
-
|
|
55
|
-
let firstTokenTime: number | null = null;
|
|
56
|
-
|
|
57
|
-
const wrappedCallback = callback ? (data: any) => {
|
|
58
|
-
if (firstTokenTime === null) firstTokenTime = Date.now();
|
|
59
|
-
callback(data);
|
|
60
|
-
} : undefined;
|
|
61
|
-
|
|
62
|
-
const result = await this.context.completion({ messages, ...params }, wrappedCallback);
|
|
63
|
-
|
|
64
|
-
Telemetry.track({
|
|
65
|
-
event: 'completion',
|
|
66
|
-
tok_per_sec: (result as any).timings?.predicted_per_second,
|
|
67
|
-
toks_generated: (result as any).timings?.predicted_n,
|
|
68
|
-
ttft: firstTokenTime ? firstTokenTime - startTime : null,
|
|
69
|
-
}, this.initParams);
|
|
70
|
-
|
|
71
|
-
return result;
|
|
62
|
+
return await this.context.completion({ messages, ...params }, callback);
|
|
72
63
|
}
|
|
73
64
|
|
|
74
65
|
async embedding(
|
|
75
66
|
text: string,
|
|
76
67
|
params?: EmbeddingParams,
|
|
68
|
+
mode: string = 'local',
|
|
77
69
|
): Promise<NativeEmbeddingResult> {
|
|
78
|
-
|
|
70
|
+
let result: NativeEmbeddingResult;
|
|
71
|
+
let lastError: Error | null = null;
|
|
72
|
+
|
|
73
|
+
if (mode === 'remote') {
|
|
74
|
+
result = await this._handleRemoteEmbedding(text);
|
|
75
|
+
} else if (mode === 'local') {
|
|
76
|
+
result = await this._handleLocalEmbedding(text, params);
|
|
77
|
+
} else if (mode === 'localfirst') {
|
|
78
|
+
try {
|
|
79
|
+
result = await this._handleLocalEmbedding(text, params);
|
|
80
|
+
} catch (e) {
|
|
81
|
+
lastError = e as Error;
|
|
82
|
+
try {
|
|
83
|
+
result = await this._handleRemoteEmbedding(text);
|
|
84
|
+
} catch (remoteError) {
|
|
85
|
+
throw lastError;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
} else if (mode === 'remotefirst') {
|
|
89
|
+
try {
|
|
90
|
+
result = await this._handleRemoteEmbedding(text);
|
|
91
|
+
} catch (e) {
|
|
92
|
+
lastError = e as Error;
|
|
93
|
+
try {
|
|
94
|
+
result = await this._handleLocalEmbedding(text, params);
|
|
95
|
+
} catch (localError) {
|
|
96
|
+
throw lastError;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
} else {
|
|
100
|
+
throw new Error('Invalid mode: ' + mode + '. Must be "local", "remote", "localfirst", or "remotefirst"');
|
|
101
|
+
}
|
|
102
|
+
return result;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
private async _handleLocalEmbedding(text: string, params?: EmbeddingParams): Promise<NativeEmbeddingResult> {
|
|
106
|
+
return this.context.embedding(text, params);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
private async _handleRemoteEmbedding(text: string): Promise<NativeEmbeddingResult> {
|
|
110
|
+
const embeddingValues = await getVertexAIEmbedding(text);
|
|
111
|
+
return {
|
|
112
|
+
embedding: embeddingValues,
|
|
113
|
+
};
|
|
79
114
|
}
|
|
80
115
|
|
|
81
116
|
async rewind(): Promise<void> {
|
package/src/projectId.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const PROJECT_ID = 'not-set';
|