react-native-litert-lm 0.1.0 → 0.2.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/README.md +17 -15
- package/android/src/main/java/com/margelo/nitro/dev/litert/litertlm/HybridLiteRTLM.kt +165 -102
- package/android/src/main/java/com/margelo/nitro/dev/litert/litertlm/LiteRTLMRegistry.kt +32 -0
- package/android/src/main/java/dev/litert/litertlm/LiteRTLMInitProvider.kt +14 -0
- package/cpp/HybridLiteRTLM.cpp +60 -27
- package/cpp/include/stb_image.h +7988 -0
- package/lib/specs/LiteRTLM.nitro.d.ts +4 -5
- package/nitrogen/generated/android/c++/JHybridLiteRTLMSpec.cpp +59 -12
- package/nitrogen/generated/android/c++/JHybridLiteRTLMSpec.hpp +4 -4
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/dev/litert/litertlm/HybridLiteRTLMSpec.kt +5 -4
- package/nitrogen/generated/shared/c++/HybridLiteRTLMSpec.hpp +5 -4
- package/package.json +1 -1
- package/src/specs/LiteRTLM.nitro.ts +4 -5
|
@@ -108,31 +108,30 @@ export interface LiteRTLM extends HybridObject<{
|
|
|
108
108
|
}> {
|
|
109
109
|
/**
|
|
110
110
|
* Load a .litertlm model file.
|
|
111
|
-
* @param modelPath Absolute path to the .litertlm file.
|
|
112
111
|
* @param config Optional configuration for backend and sampling.
|
|
113
112
|
* @throws Error if the model cannot be loaded.
|
|
114
113
|
*/
|
|
115
|
-
loadModel(modelPath: string, config?: LLMConfig): void
|
|
114
|
+
loadModel(modelPath: string, config?: LLMConfig): Promise<void>;
|
|
116
115
|
/**
|
|
117
116
|
* Send a text message and get the complete response (blocking).
|
|
118
117
|
* @param message User message text.
|
|
119
118
|
* @returns The model's response text.
|
|
120
119
|
*/
|
|
121
|
-
sendMessage(message: string): string
|
|
120
|
+
sendMessage(message: string): Promise<string>;
|
|
122
121
|
/**
|
|
123
122
|
* Send a text message with an image (multimodal).
|
|
124
123
|
* @param message User message text.
|
|
125
124
|
* @param imagePath Absolute path to an image file.
|
|
126
125
|
* @returns The model's response text.
|
|
127
126
|
*/
|
|
128
|
-
sendMessageWithImage(message: string, imagePath: string): string
|
|
127
|
+
sendMessageWithImage(message: string, imagePath: string): Promise<string>;
|
|
129
128
|
/**
|
|
130
129
|
* Send a text message with audio (multimodal).
|
|
131
130
|
* @param message User message text.
|
|
132
131
|
* @param audioPath Absolute path to an audio file (WAV).
|
|
133
132
|
* @returns The model's response text.
|
|
134
133
|
*/
|
|
135
|
-
sendMessageWithAudio(message: string, audioPath: string): string
|
|
134
|
+
sendMessageWithAudio(message: string, audioPath: string): Promise<string>;
|
|
136
135
|
/**
|
|
137
136
|
* Send a message with streaming response.
|
|
138
137
|
* Tokens are delivered via callback as they are generated.
|
|
@@ -18,6 +18,9 @@ namespace margelo::nitro::litertlm { struct LLMConfig; }
|
|
|
18
18
|
// Forward declaration of `Backend` to properly resolve imports.
|
|
19
19
|
namespace margelo::nitro::litertlm { enum class Backend; }
|
|
20
20
|
|
|
21
|
+
#include <NitroModules/Promise.hpp>
|
|
22
|
+
#include <NitroModules/JPromise.hpp>
|
|
23
|
+
#include <NitroModules/JUnit.hpp>
|
|
21
24
|
#include <string>
|
|
22
25
|
#include "Message.hpp"
|
|
23
26
|
#include <vector>
|
|
@@ -74,24 +77,68 @@ namespace margelo::nitro::litertlm {
|
|
|
74
77
|
|
|
75
78
|
|
|
76
79
|
// Methods
|
|
77
|
-
void JHybridLiteRTLMSpec::loadModel(const std::string& modelPath, const std::optional<LLMConfig>& config) {
|
|
78
|
-
static const auto method = javaClassStatic()->getMethod<
|
|
79
|
-
method(_javaPart, jni::make_jstring(modelPath), config.has_value() ? JLLMConfig::fromCpp(config.value()) : nullptr);
|
|
80
|
+
std::shared_ptr<Promise<void>> JHybridLiteRTLMSpec::loadModel(const std::string& modelPath, const std::optional<LLMConfig>& config) {
|
|
81
|
+
static const auto method = javaClassStatic()->getMethod<jni::local_ref<JPromise::javaobject>(jni::alias_ref<jni::JString> /* modelPath */, jni::alias_ref<JLLMConfig> /* config */)>("loadModel");
|
|
82
|
+
auto __result = method(_javaPart, jni::make_jstring(modelPath), config.has_value() ? JLLMConfig::fromCpp(config.value()) : nullptr);
|
|
83
|
+
return [&]() {
|
|
84
|
+
auto __promise = Promise<void>::create();
|
|
85
|
+
__result->cthis()->addOnResolvedListener([=](const jni::alias_ref<jni::JObject>& /* unit */) {
|
|
86
|
+
__promise->resolve();
|
|
87
|
+
});
|
|
88
|
+
__result->cthis()->addOnRejectedListener([=](const jni::alias_ref<jni::JThrowable>& __throwable) {
|
|
89
|
+
jni::JniException __jniError(__throwable);
|
|
90
|
+
__promise->reject(std::make_exception_ptr(__jniError));
|
|
91
|
+
});
|
|
92
|
+
return __promise;
|
|
93
|
+
}();
|
|
80
94
|
}
|
|
81
|
-
std::string JHybridLiteRTLMSpec::sendMessage(const std::string& message) {
|
|
82
|
-
static const auto method = javaClassStatic()->getMethod<jni::local_ref<
|
|
95
|
+
std::shared_ptr<Promise<std::string>> JHybridLiteRTLMSpec::sendMessage(const std::string& message) {
|
|
96
|
+
static const auto method = javaClassStatic()->getMethod<jni::local_ref<JPromise::javaobject>(jni::alias_ref<jni::JString> /* message */)>("sendMessage");
|
|
83
97
|
auto __result = method(_javaPart, jni::make_jstring(message));
|
|
84
|
-
return
|
|
98
|
+
return [&]() {
|
|
99
|
+
auto __promise = Promise<std::string>::create();
|
|
100
|
+
__result->cthis()->addOnResolvedListener([=](const jni::alias_ref<jni::JObject>& __boxedResult) {
|
|
101
|
+
auto __result = jni::static_ref_cast<jni::JString>(__boxedResult);
|
|
102
|
+
__promise->resolve(__result->toStdString());
|
|
103
|
+
});
|
|
104
|
+
__result->cthis()->addOnRejectedListener([=](const jni::alias_ref<jni::JThrowable>& __throwable) {
|
|
105
|
+
jni::JniException __jniError(__throwable);
|
|
106
|
+
__promise->reject(std::make_exception_ptr(__jniError));
|
|
107
|
+
});
|
|
108
|
+
return __promise;
|
|
109
|
+
}();
|
|
85
110
|
}
|
|
86
|
-
std::string JHybridLiteRTLMSpec::sendMessageWithImage(const std::string& message, const std::string& imagePath) {
|
|
87
|
-
static const auto method = javaClassStatic()->getMethod<jni::local_ref<
|
|
111
|
+
std::shared_ptr<Promise<std::string>> JHybridLiteRTLMSpec::sendMessageWithImage(const std::string& message, const std::string& imagePath) {
|
|
112
|
+
static const auto method = javaClassStatic()->getMethod<jni::local_ref<JPromise::javaobject>(jni::alias_ref<jni::JString> /* message */, jni::alias_ref<jni::JString> /* imagePath */)>("sendMessageWithImage");
|
|
88
113
|
auto __result = method(_javaPart, jni::make_jstring(message), jni::make_jstring(imagePath));
|
|
89
|
-
return
|
|
114
|
+
return [&]() {
|
|
115
|
+
auto __promise = Promise<std::string>::create();
|
|
116
|
+
__result->cthis()->addOnResolvedListener([=](const jni::alias_ref<jni::JObject>& __boxedResult) {
|
|
117
|
+
auto __result = jni::static_ref_cast<jni::JString>(__boxedResult);
|
|
118
|
+
__promise->resolve(__result->toStdString());
|
|
119
|
+
});
|
|
120
|
+
__result->cthis()->addOnRejectedListener([=](const jni::alias_ref<jni::JThrowable>& __throwable) {
|
|
121
|
+
jni::JniException __jniError(__throwable);
|
|
122
|
+
__promise->reject(std::make_exception_ptr(__jniError));
|
|
123
|
+
});
|
|
124
|
+
return __promise;
|
|
125
|
+
}();
|
|
90
126
|
}
|
|
91
|
-
std::string JHybridLiteRTLMSpec::sendMessageWithAudio(const std::string& message, const std::string& audioPath) {
|
|
92
|
-
static const auto method = javaClassStatic()->getMethod<jni::local_ref<
|
|
127
|
+
std::shared_ptr<Promise<std::string>> JHybridLiteRTLMSpec::sendMessageWithAudio(const std::string& message, const std::string& audioPath) {
|
|
128
|
+
static const auto method = javaClassStatic()->getMethod<jni::local_ref<JPromise::javaobject>(jni::alias_ref<jni::JString> /* message */, jni::alias_ref<jni::JString> /* audioPath */)>("sendMessageWithAudio");
|
|
93
129
|
auto __result = method(_javaPart, jni::make_jstring(message), jni::make_jstring(audioPath));
|
|
94
|
-
return
|
|
130
|
+
return [&]() {
|
|
131
|
+
auto __promise = Promise<std::string>::create();
|
|
132
|
+
__result->cthis()->addOnResolvedListener([=](const jni::alias_ref<jni::JObject>& __boxedResult) {
|
|
133
|
+
auto __result = jni::static_ref_cast<jni::JString>(__boxedResult);
|
|
134
|
+
__promise->resolve(__result->toStdString());
|
|
135
|
+
});
|
|
136
|
+
__result->cthis()->addOnRejectedListener([=](const jni::alias_ref<jni::JThrowable>& __throwable) {
|
|
137
|
+
jni::JniException __jniError(__throwable);
|
|
138
|
+
__promise->reject(std::make_exception_ptr(__jniError));
|
|
139
|
+
});
|
|
140
|
+
return __promise;
|
|
141
|
+
}();
|
|
95
142
|
}
|
|
96
143
|
void JHybridLiteRTLMSpec::sendMessageAsync(const std::string& message, const std::function<void(const std::string& /* token */, bool /* done */)>& onToken) {
|
|
97
144
|
static const auto method = javaClassStatic()->getMethod<void(jni::alias_ref<jni::JString> /* message */, jni::alias_ref<JFunc_void_std__string_bool::javaobject> /* onToken */)>("sendMessageAsync_cxx");
|
|
@@ -55,10 +55,10 @@ namespace margelo::nitro::litertlm {
|
|
|
55
55
|
|
|
56
56
|
public:
|
|
57
57
|
// Methods
|
|
58
|
-
void loadModel(const std::string& modelPath, const std::optional<LLMConfig>& config) override;
|
|
59
|
-
std::string sendMessage(const std::string& message) override;
|
|
60
|
-
std::string sendMessageWithImage(const std::string& message, const std::string& imagePath) override;
|
|
61
|
-
std::string sendMessageWithAudio(const std::string& message, const std::string& audioPath) override;
|
|
58
|
+
std::shared_ptr<Promise<void>> loadModel(const std::string& modelPath, const std::optional<LLMConfig>& config) override;
|
|
59
|
+
std::shared_ptr<Promise<std::string>> sendMessage(const std::string& message) override;
|
|
60
|
+
std::shared_ptr<Promise<std::string>> sendMessageWithImage(const std::string& message, const std::string& imagePath) override;
|
|
61
|
+
std::shared_ptr<Promise<std::string>> sendMessageWithAudio(const std::string& message, const std::string& audioPath) override;
|
|
62
62
|
void sendMessageAsync(const std::string& message, const std::function<void(const std::string& /* token */, bool /* done */)>& onToken) override;
|
|
63
63
|
std::vector<Message> getHistory() override;
|
|
64
64
|
void resetConversation() override;
|
|
@@ -10,6 +10,7 @@ package com.margelo.nitro.dev.litert.litertlm
|
|
|
10
10
|
import androidx.annotation.Keep
|
|
11
11
|
import com.facebook.jni.HybridData
|
|
12
12
|
import com.facebook.proguard.annotations.DoNotStrip
|
|
13
|
+
import com.margelo.nitro.core.Promise
|
|
13
14
|
import com.margelo.nitro.core.HybridObject
|
|
14
15
|
|
|
15
16
|
/**
|
|
@@ -47,19 +48,19 @@ abstract class HybridLiteRTLMSpec: HybridObject() {
|
|
|
47
48
|
// Methods
|
|
48
49
|
@DoNotStrip
|
|
49
50
|
@Keep
|
|
50
|
-
abstract fun loadModel(modelPath: String, config: LLMConfig?): Unit
|
|
51
|
+
abstract fun loadModel(modelPath: String, config: LLMConfig?): Promise<Unit>
|
|
51
52
|
|
|
52
53
|
@DoNotStrip
|
|
53
54
|
@Keep
|
|
54
|
-
abstract fun sendMessage(message: String): String
|
|
55
|
+
abstract fun sendMessage(message: String): Promise<String>
|
|
55
56
|
|
|
56
57
|
@DoNotStrip
|
|
57
58
|
@Keep
|
|
58
|
-
abstract fun sendMessageWithImage(message: String, imagePath: String): String
|
|
59
|
+
abstract fun sendMessageWithImage(message: String, imagePath: String): Promise<String>
|
|
59
60
|
|
|
60
61
|
@DoNotStrip
|
|
61
62
|
@Keep
|
|
62
|
-
abstract fun sendMessageWithAudio(message: String, audioPath: String): String
|
|
63
|
+
abstract fun sendMessageWithAudio(message: String, audioPath: String): Promise<String>
|
|
63
64
|
|
|
64
65
|
abstract fun sendMessageAsync(message: String, onToken: (token: String, done: Boolean) -> Unit): Unit
|
|
65
66
|
|
|
@@ -20,6 +20,7 @@ namespace margelo::nitro::litertlm { struct Message; }
|
|
|
20
20
|
// Forward declaration of `GenerationStats` to properly resolve imports.
|
|
21
21
|
namespace margelo::nitro::litertlm { struct GenerationStats; }
|
|
22
22
|
|
|
23
|
+
#include <NitroModules/Promise.hpp>
|
|
23
24
|
#include <string>
|
|
24
25
|
#include "LLMConfig.hpp"
|
|
25
26
|
#include <optional>
|
|
@@ -59,10 +60,10 @@ namespace margelo::nitro::litertlm {
|
|
|
59
60
|
|
|
60
61
|
public:
|
|
61
62
|
// Methods
|
|
62
|
-
virtual void loadModel(const std::string& modelPath, const std::optional<LLMConfig>& config) = 0;
|
|
63
|
-
virtual std::string sendMessage(const std::string& message) = 0;
|
|
64
|
-
virtual std::string sendMessageWithImage(const std::string& message, const std::string& imagePath) = 0;
|
|
65
|
-
virtual std::string sendMessageWithAudio(const std::string& message, const std::string& audioPath) = 0;
|
|
63
|
+
virtual std::shared_ptr<Promise<void>> loadModel(const std::string& modelPath, const std::optional<LLMConfig>& config) = 0;
|
|
64
|
+
virtual std::shared_ptr<Promise<std::string>> sendMessage(const std::string& message) = 0;
|
|
65
|
+
virtual std::shared_ptr<Promise<std::string>> sendMessageWithImage(const std::string& message, const std::string& imagePath) = 0;
|
|
66
|
+
virtual std::shared_ptr<Promise<std::string>> sendMessageWithAudio(const std::string& message, const std::string& audioPath) = 0;
|
|
66
67
|
virtual void sendMessageAsync(const std::string& message, const std::function<void(const std::string& /* token */, bool /* done */)>& onToken) = 0;
|
|
67
68
|
virtual std::vector<Message> getHistory() = 0;
|
|
68
69
|
virtual void resetConversation() = 0;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-native-litert-lm",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "High-performance LLM inference for React Native using LiteRT-LM. Optimized for Gemma 3n and other on-device language models.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Hugh Chen (https://github.com/hung-yueh)",
|
|
@@ -118,18 +118,17 @@ export interface LiteRTLM extends HybridObject<{
|
|
|
118
118
|
}> {
|
|
119
119
|
/**
|
|
120
120
|
* Load a .litertlm model file.
|
|
121
|
-
* @param modelPath Absolute path to the .litertlm file.
|
|
122
121
|
* @param config Optional configuration for backend and sampling.
|
|
123
122
|
* @throws Error if the model cannot be loaded.
|
|
124
123
|
*/
|
|
125
|
-
loadModel(modelPath: string, config?: LLMConfig): void
|
|
124
|
+
loadModel(modelPath: string, config?: LLMConfig): Promise<void>;
|
|
126
125
|
|
|
127
126
|
/**
|
|
128
127
|
* Send a text message and get the complete response (blocking).
|
|
129
128
|
* @param message User message text.
|
|
130
129
|
* @returns The model's response text.
|
|
131
130
|
*/
|
|
132
|
-
sendMessage(message: string): string
|
|
131
|
+
sendMessage(message: string): Promise<string>;
|
|
133
132
|
|
|
134
133
|
/**
|
|
135
134
|
* Send a text message with an image (multimodal).
|
|
@@ -137,7 +136,7 @@ export interface LiteRTLM extends HybridObject<{
|
|
|
137
136
|
* @param imagePath Absolute path to an image file.
|
|
138
137
|
* @returns The model's response text.
|
|
139
138
|
*/
|
|
140
|
-
sendMessageWithImage(message: string, imagePath: string): string
|
|
139
|
+
sendMessageWithImage(message: string, imagePath: string): Promise<string>;
|
|
141
140
|
|
|
142
141
|
/**
|
|
143
142
|
* Send a text message with audio (multimodal).
|
|
@@ -145,7 +144,7 @@ export interface LiteRTLM extends HybridObject<{
|
|
|
145
144
|
* @param audioPath Absolute path to an audio file (WAV).
|
|
146
145
|
* @returns The model's response text.
|
|
147
146
|
*/
|
|
148
|
-
sendMessageWithAudio(message: string, audioPath: string): string
|
|
147
|
+
sendMessageWithAudio(message: string, audioPath: string): Promise<string>;
|
|
149
148
|
|
|
150
149
|
/**
|
|
151
150
|
* Send a message with streaming response.
|