react-native-litert-lm 0.1.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/LICENSE +21 -0
- package/README.md +259 -0
- package/android/CMakeLists.txt +32 -0
- package/android/build.gradle +88 -0
- package/android/src/main/AndroidManifest.xml +11 -0
- package/android/src/main/java/com/margelo/nitro/dev/litert/litertlm/HybridLiteRTLM.kt +280 -0
- package/android/src/main/java/dev/litert/litertlm/LiteRTLMInitProvider.kt +43 -0
- package/android/src/main/java/dev/litert/litertlm/LiteRTLMPackage.kt +26 -0
- package/cpp/HybridLiteRTLM.cpp +483 -0
- package/cpp/HybridLiteRTLM.hpp +120 -0
- package/cpp/cpp-adapter.cpp +13 -0
- package/cpp/include/README.md +34 -0
- package/lib/index.d.ts +82 -0
- package/lib/index.js +106 -0
- package/lib/specs/LiteRTLM.nitro.d.ts +165 -0
- package/lib/specs/LiteRTLM.nitro.js +2 -0
- package/nitrogen/generated/.gitattributes +1 -0
- package/nitrogen/generated/android/LiteRTLM+autolinking.cmake +81 -0
- package/nitrogen/generated/android/LiteRTLM+autolinking.gradle +27 -0
- package/nitrogen/generated/android/LiteRTLMOnLoad.cpp +46 -0
- package/nitrogen/generated/android/LiteRTLMOnLoad.hpp +25 -0
- package/nitrogen/generated/android/c++/JBackend.hpp +61 -0
- package/nitrogen/generated/android/c++/JFunc_void_std__string_bool.hpp +76 -0
- package/nitrogen/generated/android/c++/JGenerationStats.hpp +77 -0
- package/nitrogen/generated/android/c++/JHybridLiteRTLMSpec.cpp +133 -0
- package/nitrogen/generated/android/c++/JHybridLiteRTLMSpec.hpp +75 -0
- package/nitrogen/generated/android/c++/JLLMConfig.hpp +75 -0
- package/nitrogen/generated/android/c++/JMessage.hpp +63 -0
- package/nitrogen/generated/android/c++/JRole.hpp +61 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/dev/litert/litertlm/Backend.kt +24 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/dev/litert/litertlm/Func_void_std__string_bool.kt +80 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/dev/litert/litertlm/GenerationStats.kt +53 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/dev/litert/litertlm/HybridLiteRTLMSpec.kt +98 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/dev/litert/litertlm/LLMConfig.kt +50 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/dev/litert/litertlm/LiteRTLMOnLoad.kt +35 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/dev/litert/litertlm/Message.kt +41 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/dev/litert/litertlm/Role.kt +24 -0
- package/nitrogen/generated/ios/LiteRTLM+autolinking.rb +60 -0
- package/nitrogen/generated/ios/LiteRTLM-Swift-Cxx-Bridge.cpp +17 -0
- package/nitrogen/generated/ios/LiteRTLM-Swift-Cxx-Bridge.hpp +27 -0
- package/nitrogen/generated/ios/LiteRTLM-Swift-Cxx-Umbrella.hpp +38 -0
- package/nitrogen/generated/shared/c++/Backend.hpp +80 -0
- package/nitrogen/generated/shared/c++/GenerationStats.hpp +103 -0
- package/nitrogen/generated/shared/c++/HybridLiteRTLMSpec.cpp +30 -0
- package/nitrogen/generated/shared/c++/HybridLiteRTLMSpec.hpp +82 -0
- package/nitrogen/generated/shared/c++/LLMConfig.hpp +101 -0
- package/nitrogen/generated/shared/c++/Message.hpp +89 -0
- package/nitrogen/generated/shared/c++/Role.hpp +80 -0
- package/package.json +87 -0
- package/react-native-litert-lm.podspec +51 -0
- package/react-native.config.js +16 -0
- package/src/index.ts +125 -0
- package/src/specs/LiteRTLM.nitro.ts +187 -0
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# LiteRT-LM Headers Fallback
|
|
2
|
+
|
|
3
|
+
This directory is a fallback location for LiteRT-LM C++ headers when Prefab doesn't expose them correctly.
|
|
4
|
+
|
|
5
|
+
## If Headers Are Missing
|
|
6
|
+
|
|
7
|
+
If you get compilation errors like `litert/lm/engine.h: No such file or directory`, you need to manually copy LiteRT-LM headers here:
|
|
8
|
+
|
|
9
|
+
1. Clone LiteRT-LM repository:
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
git clone https://github.com/google-ai-edge/LiteRT-LM.git /tmp/LiteRT-LM
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
2. Copy the headers:
|
|
16
|
+
```bash
|
|
17
|
+
cp -r /tmp/LiteRT-LM/runtime/include/litert ./
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
The expected directory structure after copying:
|
|
21
|
+
|
|
22
|
+
```
|
|
23
|
+
cpp/include/
|
|
24
|
+
└── litert/
|
|
25
|
+
└── lm/
|
|
26
|
+
├── engine.h
|
|
27
|
+
├── conversation.h
|
|
28
|
+
├── types.h
|
|
29
|
+
└── ...
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## Note
|
|
33
|
+
|
|
34
|
+
The ideal scenario is that the Maven package `litertlm-android:0.9.0-alpha01` exposes headers via Prefab, making this directory unnecessary. This is just a fallback.
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import type { LiteRTLM, Backend } from "./specs/LiteRTLM.nitro";
|
|
2
|
+
export type { LiteRTLM, LLMConfig, Message, Backend, Role, GenerationStats, } from "./specs/LiteRTLM.nitro";
|
|
3
|
+
/**
|
|
4
|
+
* Creates a new LiteRT-LM inference engine instance.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```typescript
|
|
8
|
+
* import { createLLM } from 'react-native-litert-lm';
|
|
9
|
+
*
|
|
10
|
+
* // Basic usage with Gemma 3n
|
|
11
|
+
* const llm = createLLM();
|
|
12
|
+
* llm.loadModel('/path/to/gemma-3n-e2b.litertlm', {
|
|
13
|
+
* backend: 'gpu',
|
|
14
|
+
* temperature: 0.7,
|
|
15
|
+
* maxTokens: 512
|
|
16
|
+
* });
|
|
17
|
+
*
|
|
18
|
+
* // Simple text generation
|
|
19
|
+
* const response = llm.sendMessage('Hello, how are you?');
|
|
20
|
+
* console.log(response);
|
|
21
|
+
*
|
|
22
|
+
* // Streaming generation
|
|
23
|
+
* llm.sendMessageAsync('Tell me about React Native', (token, done) => {
|
|
24
|
+
* process.stdout.write(token);
|
|
25
|
+
* if (done) console.log('\n--- Done ---');
|
|
26
|
+
* });
|
|
27
|
+
*
|
|
28
|
+
* // Check stats
|
|
29
|
+
* const stats = llm.getStats();
|
|
30
|
+
* console.log(`Generated at ${stats.tokensPerSecond} tokens/sec`);
|
|
31
|
+
*
|
|
32
|
+
* // Cleanup
|
|
33
|
+
* llm.close();
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
export declare function createLLM(): LiteRTLM;
|
|
37
|
+
/**
|
|
38
|
+
* Pre-defined model identifiers for common models.
|
|
39
|
+
* Use with model download utilities or as reference.
|
|
40
|
+
*/
|
|
41
|
+
export declare const Models: {
|
|
42
|
+
/** Gemma 3n E2B (2B parameters, efficient) */
|
|
43
|
+
readonly GEMMA_3N_E2B: "gemma-3n-E2B-it-litert-lm-preview";
|
|
44
|
+
/** Gemma 3n E4B (4B parameters, higher quality) */
|
|
45
|
+
readonly GEMMA_3N_E4B: "gemma-3n-E4B-it-litert-lm-preview";
|
|
46
|
+
/** Gemma 3 1B (smallest Gemma) */
|
|
47
|
+
readonly GEMMA_3_1B: "Gemma3-1B-IT_multi-prefill-seq_q4_ekv4096";
|
|
48
|
+
/** Phi-4 Mini Instruct */
|
|
49
|
+
readonly PHI_4_MINI: "Phi-4-mini-instruct_multi-prefill-seq_q8_ekv4096";
|
|
50
|
+
/** Qwen 2.5 1.5B Instruct */
|
|
51
|
+
readonly QWEN_2_5_1_5B: "Qwen2.5-1.5B-Instruct_multi-prefill-seq_q8_ekv4096";
|
|
52
|
+
};
|
|
53
|
+
export type ModelId = (typeof Models)[keyof typeof Models];
|
|
54
|
+
/**
|
|
55
|
+
* Get the recommended backend for the current platform.
|
|
56
|
+
* Returns 'gpu' for most devices as it provides the best balance of speed and compatibility.
|
|
57
|
+
*
|
|
58
|
+
* @returns The recommended backend ('gpu' for most cases)
|
|
59
|
+
*
|
|
60
|
+
* @example
|
|
61
|
+
* ```typescript
|
|
62
|
+
* const backend = getRecommendedBackend();
|
|
63
|
+
* llm.loadModel(path, { backend });
|
|
64
|
+
* ```
|
|
65
|
+
*/
|
|
66
|
+
export declare function getRecommendedBackend(): Backend;
|
|
67
|
+
/**
|
|
68
|
+
* Check if a backend configuration is supported on the current platform.
|
|
69
|
+
* Returns a warning message if the configuration may have issues.
|
|
70
|
+
*
|
|
71
|
+
* @param backend The backend to check
|
|
72
|
+
* @returns Warning message if there may be issues, undefined if OK
|
|
73
|
+
*
|
|
74
|
+
* @example
|
|
75
|
+
* ```typescript
|
|
76
|
+
* const warning = checkBackendSupport('npu');
|
|
77
|
+
* if (warning) {
|
|
78
|
+
* console.warn(warning);
|
|
79
|
+
* }
|
|
80
|
+
* ```
|
|
81
|
+
*/
|
|
82
|
+
export declare function checkBackendSupport(backend: Backend): string | undefined;
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Models = void 0;
|
|
4
|
+
exports.createLLM = createLLM;
|
|
5
|
+
exports.getRecommendedBackend = getRecommendedBackend;
|
|
6
|
+
exports.checkBackendSupport = checkBackendSupport;
|
|
7
|
+
const react_native_nitro_modules_1 = require("react-native-nitro-modules");
|
|
8
|
+
const react_native_1 = require("react-native");
|
|
9
|
+
/**
|
|
10
|
+
* Creates a new LiteRT-LM inference engine instance.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* import { createLLM } from 'react-native-litert-lm';
|
|
15
|
+
*
|
|
16
|
+
* // Basic usage with Gemma 3n
|
|
17
|
+
* const llm = createLLM();
|
|
18
|
+
* llm.loadModel('/path/to/gemma-3n-e2b.litertlm', {
|
|
19
|
+
* backend: 'gpu',
|
|
20
|
+
* temperature: 0.7,
|
|
21
|
+
* maxTokens: 512
|
|
22
|
+
* });
|
|
23
|
+
*
|
|
24
|
+
* // Simple text generation
|
|
25
|
+
* const response = llm.sendMessage('Hello, how are you?');
|
|
26
|
+
* console.log(response);
|
|
27
|
+
*
|
|
28
|
+
* // Streaming generation
|
|
29
|
+
* llm.sendMessageAsync('Tell me about React Native', (token, done) => {
|
|
30
|
+
* process.stdout.write(token);
|
|
31
|
+
* if (done) console.log('\n--- Done ---');
|
|
32
|
+
* });
|
|
33
|
+
*
|
|
34
|
+
* // Check stats
|
|
35
|
+
* const stats = llm.getStats();
|
|
36
|
+
* console.log(`Generated at ${stats.tokensPerSecond} tokens/sec`);
|
|
37
|
+
*
|
|
38
|
+
* // Cleanup
|
|
39
|
+
* llm.close();
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
function createLLM() {
|
|
43
|
+
return react_native_nitro_modules_1.NitroModules.createHybridObject("LiteRTLM");
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Pre-defined model identifiers for common models.
|
|
47
|
+
* Use with model download utilities or as reference.
|
|
48
|
+
*/
|
|
49
|
+
exports.Models = {
|
|
50
|
+
/** Gemma 3n E2B (2B parameters, efficient) */
|
|
51
|
+
GEMMA_3N_E2B: "gemma-3n-E2B-it-litert-lm-preview",
|
|
52
|
+
/** Gemma 3n E4B (4B parameters, higher quality) */
|
|
53
|
+
GEMMA_3N_E4B: "gemma-3n-E4B-it-litert-lm-preview",
|
|
54
|
+
/** Gemma 3 1B (smallest Gemma) */
|
|
55
|
+
GEMMA_3_1B: "Gemma3-1B-IT_multi-prefill-seq_q4_ekv4096",
|
|
56
|
+
/** Phi-4 Mini Instruct */
|
|
57
|
+
PHI_4_MINI: "Phi-4-mini-instruct_multi-prefill-seq_q8_ekv4096",
|
|
58
|
+
/** Qwen 2.5 1.5B Instruct */
|
|
59
|
+
QWEN_2_5_1_5B: "Qwen2.5-1.5B-Instruct_multi-prefill-seq_q8_ekv4096",
|
|
60
|
+
};
|
|
61
|
+
/**
|
|
62
|
+
* Get the recommended backend for the current platform.
|
|
63
|
+
* Returns 'gpu' for most devices as it provides the best balance of speed and compatibility.
|
|
64
|
+
*
|
|
65
|
+
* @returns The recommended backend ('gpu' for most cases)
|
|
66
|
+
*
|
|
67
|
+
* @example
|
|
68
|
+
* ```typescript
|
|
69
|
+
* const backend = getRecommendedBackend();
|
|
70
|
+
* llm.loadModel(path, { backend });
|
|
71
|
+
* ```
|
|
72
|
+
*/
|
|
73
|
+
function getRecommendedBackend() {
|
|
74
|
+
// GPU is the recommended default for all platforms
|
|
75
|
+
// It provides good performance and broad compatibility
|
|
76
|
+
return "gpu";
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Check if a backend configuration is supported on the current platform.
|
|
80
|
+
* Returns a warning message if the configuration may have issues.
|
|
81
|
+
*
|
|
82
|
+
* @param backend The backend to check
|
|
83
|
+
* @returns Warning message if there may be issues, undefined if OK
|
|
84
|
+
*
|
|
85
|
+
* @example
|
|
86
|
+
* ```typescript
|
|
87
|
+
* const warning = checkBackendSupport('npu');
|
|
88
|
+
* if (warning) {
|
|
89
|
+
* console.warn(warning);
|
|
90
|
+
* }
|
|
91
|
+
* ```
|
|
92
|
+
*/
|
|
93
|
+
function checkBackendSupport(backend) {
|
|
94
|
+
if (backend === "npu") {
|
|
95
|
+
if (react_native_1.Platform.OS === "android") {
|
|
96
|
+
return "NPU backend requires compatible hardware (Qualcomm Hexagon, MediaTek APU, etc.). Will fall back to GPU if unavailable.";
|
|
97
|
+
}
|
|
98
|
+
if (react_native_1.Platform.OS === "ios") {
|
|
99
|
+
return "NPU/CoreML is not yet supported on iOS. LiteRT-LM iOS support is pending.";
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
if (react_native_1.Platform.OS === "ios" && backend !== "cpu") {
|
|
103
|
+
return "LiteRT-LM iOS is not yet released. Only CPU backend may work via fallback.";
|
|
104
|
+
}
|
|
105
|
+
return undefined;
|
|
106
|
+
}
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import type { HybridObject } from "react-native-nitro-modules";
|
|
2
|
+
/**
|
|
3
|
+
* Backend types for LLM inference.
|
|
4
|
+
* - 'cpu': CPU inference (slowest, always available)
|
|
5
|
+
* - 'gpu': GPU acceleration (fast, recommended for most devices)
|
|
6
|
+
* - 'npu': NPU/Neural Engine (fastest on supported hardware)
|
|
7
|
+
*
|
|
8
|
+
* @remarks
|
|
9
|
+
* NPU acceleration requires compatible hardware (e.g., Qualcomm Hexagon, MediaTek APU).
|
|
10
|
+
* If NPU is unavailable, LiteRT-LM automatically falls back to GPU.
|
|
11
|
+
*/
|
|
12
|
+
export type Backend = "cpu" | "gpu" | "npu";
|
|
13
|
+
/**
|
|
14
|
+
* Message roles for conversation.
|
|
15
|
+
*/
|
|
16
|
+
export type Role = "user" | "model" | "system";
|
|
17
|
+
/**
|
|
18
|
+
* Configuration options for loading an LLM.
|
|
19
|
+
*/
|
|
20
|
+
export interface LLMConfig {
|
|
21
|
+
/**
|
|
22
|
+
* Primary compute backend for text generation.
|
|
23
|
+
* - 'cpu': CPU inference (slower but always available)
|
|
24
|
+
* - 'gpu': GPU acceleration (fast, recommended)
|
|
25
|
+
* - 'npu': NPU/Neural Engine (fastest on supported devices)
|
|
26
|
+
*
|
|
27
|
+
* If not specified, defaults to 'gpu'.
|
|
28
|
+
* If specified backend is unavailable, falls back automatically.
|
|
29
|
+
*
|
|
30
|
+
* @remarks
|
|
31
|
+
* Vision encoder is always set to GPU (required by Gemma 3n).
|
|
32
|
+
* Audio encoder is always set to CPU (optimal for audio processing).
|
|
33
|
+
*
|
|
34
|
+
* @default 'gpu'
|
|
35
|
+
*/
|
|
36
|
+
backend?: Backend;
|
|
37
|
+
/**
|
|
38
|
+
* Maximum number of tokens to generate.
|
|
39
|
+
* @default 1024
|
|
40
|
+
*/
|
|
41
|
+
maxTokens?: number;
|
|
42
|
+
/**
|
|
43
|
+
* Sampling temperature (0.0 = deterministic, 1.0 = creative).
|
|
44
|
+
* @default 0.7
|
|
45
|
+
*/
|
|
46
|
+
temperature?: number;
|
|
47
|
+
/**
|
|
48
|
+
* Top-K sampling (number of top tokens to consider).
|
|
49
|
+
* @default 40
|
|
50
|
+
*/
|
|
51
|
+
topK?: number;
|
|
52
|
+
/**
|
|
53
|
+
* Top-P (nucleus) sampling threshold.
|
|
54
|
+
* @default 0.95
|
|
55
|
+
*/
|
|
56
|
+
topP?: number;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* A simple message in the conversation.
|
|
60
|
+
* For multimodal, use sendMessageWithImage/sendMessageWithAudio instead.
|
|
61
|
+
*/
|
|
62
|
+
export interface Message {
|
|
63
|
+
/** Role of the message sender */
|
|
64
|
+
role: Role;
|
|
65
|
+
/** Text content of the message */
|
|
66
|
+
content: string;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Generation statistics returned after completion.
|
|
70
|
+
*/
|
|
71
|
+
export interface GenerationStats {
|
|
72
|
+
/** Number of tokens in the prompt */
|
|
73
|
+
promptTokens: number;
|
|
74
|
+
/** Number of tokens generated */
|
|
75
|
+
completionTokens: number;
|
|
76
|
+
/** Total tokens (prompt + completion) */
|
|
77
|
+
totalTokens: number;
|
|
78
|
+
/** Time to first token in milliseconds */
|
|
79
|
+
timeToFirstToken: number;
|
|
80
|
+
/** Total generation time in milliseconds */
|
|
81
|
+
totalTime: number;
|
|
82
|
+
/** Tokens per second */
|
|
83
|
+
tokensPerSecond: number;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* LiteRT-LM: High-performance LLM inference engine.
|
|
87
|
+
* Supports Gemma 3n, Phi-4, Qwen, and other .litertlm models.
|
|
88
|
+
*
|
|
89
|
+
* @example
|
|
90
|
+
* ```typescript
|
|
91
|
+
* const llm = createLLM();
|
|
92
|
+
* llm.loadModel('/path/to/gemma-3n.litertlm', { backend: 'gpu' });
|
|
93
|
+
*
|
|
94
|
+
* // Blocking generation
|
|
95
|
+
* const response = llm.sendMessage('What is the capital of France?');
|
|
96
|
+
*
|
|
97
|
+
* // Streaming generation
|
|
98
|
+
* llm.sendMessageAsync('Tell me a story', (token, done) => {
|
|
99
|
+
* process.stdout.write(token);
|
|
100
|
+
* });
|
|
101
|
+
*
|
|
102
|
+
* llm.close();
|
|
103
|
+
* ```
|
|
104
|
+
*/
|
|
105
|
+
export interface LiteRTLM extends HybridObject<{
|
|
106
|
+
ios: "c++";
|
|
107
|
+
android: "kotlin";
|
|
108
|
+
}> {
|
|
109
|
+
/**
|
|
110
|
+
* Load a .litertlm model file.
|
|
111
|
+
* @param modelPath Absolute path to the .litertlm file.
|
|
112
|
+
* @param config Optional configuration for backend and sampling.
|
|
113
|
+
* @throws Error if the model cannot be loaded.
|
|
114
|
+
*/
|
|
115
|
+
loadModel(modelPath: string, config?: LLMConfig): void;
|
|
116
|
+
/**
|
|
117
|
+
* Send a text message and get the complete response (blocking).
|
|
118
|
+
* @param message User message text.
|
|
119
|
+
* @returns The model's response text.
|
|
120
|
+
*/
|
|
121
|
+
sendMessage(message: string): string;
|
|
122
|
+
/**
|
|
123
|
+
* Send a text message with an image (multimodal).
|
|
124
|
+
* @param message User message text.
|
|
125
|
+
* @param imagePath Absolute path to an image file.
|
|
126
|
+
* @returns The model's response text.
|
|
127
|
+
*/
|
|
128
|
+
sendMessageWithImage(message: string, imagePath: string): string;
|
|
129
|
+
/**
|
|
130
|
+
* Send a text message with audio (multimodal).
|
|
131
|
+
* @param message User message text.
|
|
132
|
+
* @param audioPath Absolute path to an audio file (WAV).
|
|
133
|
+
* @returns The model's response text.
|
|
134
|
+
*/
|
|
135
|
+
sendMessageWithAudio(message: string, audioPath: string): string;
|
|
136
|
+
/**
|
|
137
|
+
* Send a message with streaming response.
|
|
138
|
+
* Tokens are delivered via callback as they are generated.
|
|
139
|
+
* @param message User message text.
|
|
140
|
+
* @param onToken Callback invoked for each token (token, isDone).
|
|
141
|
+
*/
|
|
142
|
+
sendMessageAsync(message: string, onToken: (token: string, done: boolean) => void): void;
|
|
143
|
+
/**
|
|
144
|
+
* Get the current conversation history.
|
|
145
|
+
* @returns Array of messages in the conversation.
|
|
146
|
+
*/
|
|
147
|
+
getHistory(): Message[];
|
|
148
|
+
/**
|
|
149
|
+
* Clear the conversation context and start fresh.
|
|
150
|
+
*/
|
|
151
|
+
resetConversation(): void;
|
|
152
|
+
/**
|
|
153
|
+
* Check if a model is loaded and ready for inference.
|
|
154
|
+
*/
|
|
155
|
+
isReady(): boolean;
|
|
156
|
+
/**
|
|
157
|
+
* Get the last generation statistics.
|
|
158
|
+
*/
|
|
159
|
+
getStats(): GenerationStats;
|
|
160
|
+
/**
|
|
161
|
+
* Release all native resources.
|
|
162
|
+
* Call this when done with the LLM instance.
|
|
163
|
+
*/
|
|
164
|
+
close(): void;
|
|
165
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
** linguist-generated=true
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
#
|
|
2
|
+
# LiteRTLM+autolinking.cmake
|
|
3
|
+
# This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
|
|
4
|
+
# https://github.com/mrousavy/nitro
|
|
5
|
+
# Copyright © Marc Rousavy @ Margelo
|
|
6
|
+
#
|
|
7
|
+
|
|
8
|
+
# This is a CMake file that adds all files generated by Nitrogen
|
|
9
|
+
# to the current CMake project.
|
|
10
|
+
#
|
|
11
|
+
# To use it, add this to your CMakeLists.txt:
|
|
12
|
+
# ```cmake
|
|
13
|
+
# include(${CMAKE_SOURCE_DIR}/../nitrogen/generated/android/LiteRTLM+autolinking.cmake)
|
|
14
|
+
# ```
|
|
15
|
+
|
|
16
|
+
# Define a flag to check if we are building properly
|
|
17
|
+
add_definitions(-DBUILDING_LITERTLM_WITH_GENERATED_CMAKE_PROJECT)
|
|
18
|
+
|
|
19
|
+
# Enable Raw Props parsing in react-native (for Nitro Views)
|
|
20
|
+
add_definitions(-DRN_SERIALIZABLE_STATE)
|
|
21
|
+
|
|
22
|
+
# Add all headers that were generated by Nitrogen
|
|
23
|
+
include_directories(
|
|
24
|
+
"../nitrogen/generated/shared/c++"
|
|
25
|
+
"../nitrogen/generated/android/c++"
|
|
26
|
+
"../nitrogen/generated/android/"
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
# Add all .cpp sources that were generated by Nitrogen
|
|
30
|
+
target_sources(
|
|
31
|
+
# CMake project name (Android C++ library name)
|
|
32
|
+
LiteRTLM PRIVATE
|
|
33
|
+
# Autolinking Setup
|
|
34
|
+
../nitrogen/generated/android/LiteRTLMOnLoad.cpp
|
|
35
|
+
# Shared Nitrogen C++ sources
|
|
36
|
+
../nitrogen/generated/shared/c++/HybridLiteRTLMSpec.cpp
|
|
37
|
+
# Android-specific Nitrogen C++ sources
|
|
38
|
+
../nitrogen/generated/android/c++/JHybridLiteRTLMSpec.cpp
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
# From node_modules/react-native/ReactAndroid/cmake-utils/folly-flags.cmake
|
|
42
|
+
# Used in node_modules/react-native/ReactAndroid/cmake-utils/ReactNative-application.cmake
|
|
43
|
+
target_compile_definitions(
|
|
44
|
+
LiteRTLM PRIVATE
|
|
45
|
+
-DFOLLY_NO_CONFIG=1
|
|
46
|
+
-DFOLLY_HAVE_CLOCK_GETTIME=1
|
|
47
|
+
-DFOLLY_USE_LIBCPP=1
|
|
48
|
+
-DFOLLY_CFG_NO_COROUTINES=1
|
|
49
|
+
-DFOLLY_MOBILE=1
|
|
50
|
+
-DFOLLY_HAVE_RECVMMSG=1
|
|
51
|
+
-DFOLLY_HAVE_PTHREAD=1
|
|
52
|
+
# Once we target android-23 above, we can comment
|
|
53
|
+
# the following line. NDK uses GNU style stderror_r() after API 23.
|
|
54
|
+
-DFOLLY_HAVE_XSI_STRERROR_R=1
|
|
55
|
+
)
|
|
56
|
+
|
|
57
|
+
# Add all libraries required by the generated specs
|
|
58
|
+
find_package(fbjni REQUIRED) # <-- Used for communication between Java <-> C++
|
|
59
|
+
find_package(ReactAndroid REQUIRED) # <-- Used to set up React Native bindings (e.g. CallInvoker/TurboModule)
|
|
60
|
+
find_package(react-native-nitro-modules REQUIRED) # <-- Used to create all HybridObjects and use the Nitro core library
|
|
61
|
+
|
|
62
|
+
# Link all libraries together
|
|
63
|
+
target_link_libraries(
|
|
64
|
+
LiteRTLM
|
|
65
|
+
fbjni::fbjni # <-- Facebook C++ JNI helpers
|
|
66
|
+
ReactAndroid::jsi # <-- RN: JSI
|
|
67
|
+
react-native-nitro-modules::NitroModules # <-- NitroModules Core :)
|
|
68
|
+
)
|
|
69
|
+
|
|
70
|
+
# Link react-native (different prefab between RN 0.75 and RN 0.76)
|
|
71
|
+
if(ReactAndroid_VERSION_MINOR GREATER_EQUAL 76)
|
|
72
|
+
target_link_libraries(
|
|
73
|
+
LiteRTLM
|
|
74
|
+
ReactAndroid::reactnative # <-- RN: Native Modules umbrella prefab
|
|
75
|
+
)
|
|
76
|
+
else()
|
|
77
|
+
target_link_libraries(
|
|
78
|
+
LiteRTLM
|
|
79
|
+
ReactAndroid::react_nativemodule_core # <-- RN: TurboModules Core
|
|
80
|
+
)
|
|
81
|
+
endif()
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
///
|
|
2
|
+
/// LiteRTLM+autolinking.gradle
|
|
3
|
+
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
|
|
4
|
+
/// https://github.com/mrousavy/nitro
|
|
5
|
+
/// Copyright © Marc Rousavy @ Margelo
|
|
6
|
+
///
|
|
7
|
+
|
|
8
|
+
/// This is a Gradle file that adds all files generated by Nitrogen
|
|
9
|
+
/// to the current Gradle project.
|
|
10
|
+
///
|
|
11
|
+
/// To use it, add this to your build.gradle:
|
|
12
|
+
/// ```gradle
|
|
13
|
+
/// apply from: '../nitrogen/generated/android/LiteRTLM+autolinking.gradle'
|
|
14
|
+
/// ```
|
|
15
|
+
|
|
16
|
+
logger.warn("[NitroModules] 🔥 LiteRTLM is boosted by nitro!")
|
|
17
|
+
|
|
18
|
+
android {
|
|
19
|
+
sourceSets {
|
|
20
|
+
main {
|
|
21
|
+
java.srcDirs += [
|
|
22
|
+
// Nitrogen files
|
|
23
|
+
"${project.projectDir}/../nitrogen/generated/android/kotlin"
|
|
24
|
+
]
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
///
|
|
2
|
+
/// LiteRTLMOnLoad.cpp
|
|
3
|
+
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
|
|
4
|
+
/// https://github.com/mrousavy/nitro
|
|
5
|
+
/// Copyright © Marc Rousavy @ Margelo
|
|
6
|
+
///
|
|
7
|
+
|
|
8
|
+
#ifndef BUILDING_LITERTLM_WITH_GENERATED_CMAKE_PROJECT
|
|
9
|
+
#error LiteRTLMOnLoad.cpp is not being built with the autogenerated CMakeLists.txt project. Is a different CMakeLists.txt building this?
|
|
10
|
+
#endif
|
|
11
|
+
|
|
12
|
+
#include "LiteRTLMOnLoad.hpp"
|
|
13
|
+
|
|
14
|
+
#include <jni.h>
|
|
15
|
+
#include <fbjni/fbjni.h>
|
|
16
|
+
#include <NitroModules/HybridObjectRegistry.hpp>
|
|
17
|
+
|
|
18
|
+
#include "JHybridLiteRTLMSpec.hpp"
|
|
19
|
+
#include "JFunc_void_std__string_bool.hpp"
|
|
20
|
+
#include <NitroModules/DefaultConstructableObject.hpp>
|
|
21
|
+
|
|
22
|
+
namespace margelo::nitro::litertlm {
|
|
23
|
+
|
|
24
|
+
int initialize(JavaVM* vm) {
|
|
25
|
+
using namespace margelo::nitro;
|
|
26
|
+
using namespace margelo::nitro::litertlm;
|
|
27
|
+
using namespace facebook;
|
|
28
|
+
|
|
29
|
+
return facebook::jni::initialize(vm, [] {
|
|
30
|
+
// Register native JNI methods
|
|
31
|
+
margelo::nitro::litertlm::JHybridLiteRTLMSpec::registerNatives();
|
|
32
|
+
margelo::nitro::litertlm::JFunc_void_std__string_bool_cxx::registerNatives();
|
|
33
|
+
|
|
34
|
+
// Register Nitro Hybrid Objects
|
|
35
|
+
HybridObjectRegistry::registerHybridObjectConstructor(
|
|
36
|
+
"LiteRTLM",
|
|
37
|
+
[]() -> std::shared_ptr<HybridObject> {
|
|
38
|
+
static DefaultConstructableObject<JHybridLiteRTLMSpec::javaobject> object("com/margelo/nitro/dev/litert/litertlm/HybridLiteRTLM");
|
|
39
|
+
auto instance = object.create();
|
|
40
|
+
return instance->cthis()->shared();
|
|
41
|
+
}
|
|
42
|
+
);
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
} // namespace margelo::nitro::litertlm
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
///
|
|
2
|
+
/// LiteRTLMOnLoad.hpp
|
|
3
|
+
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
|
|
4
|
+
/// https://github.com/mrousavy/nitro
|
|
5
|
+
/// Copyright © Marc Rousavy @ Margelo
|
|
6
|
+
///
|
|
7
|
+
|
|
8
|
+
#include <jni.h>
|
|
9
|
+
#include <NitroModules/NitroDefines.hpp>
|
|
10
|
+
|
|
11
|
+
namespace margelo::nitro::litertlm {
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Initializes the native (C++) part of LiteRTLM, and autolinks all Hybrid Objects.
|
|
15
|
+
* Call this in your `JNI_OnLoad` function (probably inside `cpp-adapter.cpp`).
|
|
16
|
+
* Example:
|
|
17
|
+
* ```cpp (cpp-adapter.cpp)
|
|
18
|
+
* JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void*) {
|
|
19
|
+
* return margelo::nitro::litertlm::initialize(vm);
|
|
20
|
+
* }
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
int initialize(JavaVM* vm);
|
|
24
|
+
|
|
25
|
+
} // namespace margelo::nitro::litertlm
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
///
|
|
2
|
+
/// JBackend.hpp
|
|
3
|
+
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
|
|
4
|
+
/// https://github.com/mrousavy/nitro
|
|
5
|
+
/// Copyright © Marc Rousavy @ Margelo
|
|
6
|
+
///
|
|
7
|
+
|
|
8
|
+
#pragma once
|
|
9
|
+
|
|
10
|
+
#include <fbjni/fbjni.h>
|
|
11
|
+
#include "Backend.hpp"
|
|
12
|
+
|
|
13
|
+
namespace margelo::nitro::litertlm {
|
|
14
|
+
|
|
15
|
+
using namespace facebook;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* The C++ JNI bridge between the C++ enum "Backend" and the the Kotlin enum "Backend".
|
|
19
|
+
*/
|
|
20
|
+
struct JBackend final: public jni::JavaClass<JBackend> {
|
|
21
|
+
public:
|
|
22
|
+
static auto constexpr kJavaDescriptor = "Lcom/margelo/nitro/dev/litert/litertlm/Backend;";
|
|
23
|
+
|
|
24
|
+
public:
|
|
25
|
+
/**
|
|
26
|
+
* Convert this Java/Kotlin-based enum to the C++ enum Backend.
|
|
27
|
+
*/
|
|
28
|
+
[[maybe_unused]]
|
|
29
|
+
[[nodiscard]]
|
|
30
|
+
Backend toCpp() const {
|
|
31
|
+
static const auto clazz = javaClassStatic();
|
|
32
|
+
static const auto fieldOrdinal = clazz->getField<int>("value");
|
|
33
|
+
int ordinal = this->getFieldValue(fieldOrdinal);
|
|
34
|
+
return static_cast<Backend>(ordinal);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
public:
|
|
38
|
+
/**
|
|
39
|
+
* Create a Java/Kotlin-based enum with the given C++ enum's value.
|
|
40
|
+
*/
|
|
41
|
+
[[maybe_unused]]
|
|
42
|
+
static jni::alias_ref<JBackend> fromCpp(Backend value) {
|
|
43
|
+
static const auto clazz = javaClassStatic();
|
|
44
|
+
switch (value) {
|
|
45
|
+
case Backend::CPU:
|
|
46
|
+
static const auto fieldCPU = clazz->getStaticField<JBackend>("CPU");
|
|
47
|
+
return clazz->getStaticFieldValue(fieldCPU);
|
|
48
|
+
case Backend::GPU:
|
|
49
|
+
static const auto fieldGPU = clazz->getStaticField<JBackend>("GPU");
|
|
50
|
+
return clazz->getStaticFieldValue(fieldGPU);
|
|
51
|
+
case Backend::NPU:
|
|
52
|
+
static const auto fieldNPU = clazz->getStaticField<JBackend>("NPU");
|
|
53
|
+
return clazz->getStaticFieldValue(fieldNPU);
|
|
54
|
+
default:
|
|
55
|
+
std::string stringValue = std::to_string(static_cast<int>(value));
|
|
56
|
+
throw std::invalid_argument("Invalid enum value (" + stringValue + "!");
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
} // namespace margelo::nitro::litertlm
|