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,76 @@
|
|
|
1
|
+
///
|
|
2
|
+
/// JFunc_void_std__string_bool.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 <functional>
|
|
12
|
+
|
|
13
|
+
#include <string>
|
|
14
|
+
#include <functional>
|
|
15
|
+
#include <NitroModules/JNICallable.hpp>
|
|
16
|
+
|
|
17
|
+
namespace margelo::nitro::litertlm {
|
|
18
|
+
|
|
19
|
+
using namespace facebook;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Represents the Java/Kotlin callback `(token: String, done: Boolean) -> Unit`.
|
|
23
|
+
* This can be passed around between C++ and Java/Kotlin.
|
|
24
|
+
*/
|
|
25
|
+
struct JFunc_void_std__string_bool: public jni::JavaClass<JFunc_void_std__string_bool> {
|
|
26
|
+
public:
|
|
27
|
+
static auto constexpr kJavaDescriptor = "Lcom/margelo/nitro/dev/litert/litertlm/Func_void_std__string_bool;";
|
|
28
|
+
|
|
29
|
+
public:
|
|
30
|
+
/**
|
|
31
|
+
* Invokes the function this `JFunc_void_std__string_bool` instance holds through JNI.
|
|
32
|
+
*/
|
|
33
|
+
void invoke(const std::string& token, bool done) const {
|
|
34
|
+
static const auto method = javaClassStatic()->getMethod<void(jni::alias_ref<jni::JString> /* token */, jboolean /* done */)>("invoke");
|
|
35
|
+
method(self(), jni::make_jstring(token), done);
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* An implementation of Func_void_std__string_bool that is backed by a C++ implementation (using `std::function<...>`)
|
|
41
|
+
*/
|
|
42
|
+
class JFunc_void_std__string_bool_cxx final: public jni::HybridClass<JFunc_void_std__string_bool_cxx, JFunc_void_std__string_bool> {
|
|
43
|
+
public:
|
|
44
|
+
static jni::local_ref<JFunc_void_std__string_bool::javaobject> fromCpp(const std::function<void(const std::string& /* token */, bool /* done */)>& func) {
|
|
45
|
+
return JFunc_void_std__string_bool_cxx::newObjectCxxArgs(func);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
public:
|
|
49
|
+
/**
|
|
50
|
+
* Invokes the C++ `std::function<...>` this `JFunc_void_std__string_bool_cxx` instance holds.
|
|
51
|
+
*/
|
|
52
|
+
void invoke_cxx(jni::alias_ref<jni::JString> token, jboolean done) {
|
|
53
|
+
_func(token->toStdString(), static_cast<bool>(done));
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
public:
|
|
57
|
+
[[nodiscard]]
|
|
58
|
+
inline const std::function<void(const std::string& /* token */, bool /* done */)>& getFunction() const {
|
|
59
|
+
return _func;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
public:
|
|
63
|
+
static auto constexpr kJavaDescriptor = "Lcom/margelo/nitro/dev/litert/litertlm/Func_void_std__string_bool_cxx;";
|
|
64
|
+
static void registerNatives() {
|
|
65
|
+
registerHybrid({makeNativeMethod("invoke_cxx", JFunc_void_std__string_bool_cxx::invoke_cxx)});
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
private:
|
|
69
|
+
explicit JFunc_void_std__string_bool_cxx(const std::function<void(const std::string& /* token */, bool /* done */)>& func): _func(func) { }
|
|
70
|
+
|
|
71
|
+
private:
|
|
72
|
+
friend HybridBase;
|
|
73
|
+
std::function<void(const std::string& /* token */, bool /* done */)> _func;
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
} // namespace margelo::nitro::litertlm
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
///
|
|
2
|
+
/// JGenerationStats.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 "GenerationStats.hpp"
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
namespace margelo::nitro::litertlm {
|
|
16
|
+
|
|
17
|
+
using namespace facebook;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* The C++ JNI bridge between the C++ struct "GenerationStats" and the the Kotlin data class "GenerationStats".
|
|
21
|
+
*/
|
|
22
|
+
struct JGenerationStats final: public jni::JavaClass<JGenerationStats> {
|
|
23
|
+
public:
|
|
24
|
+
static auto constexpr kJavaDescriptor = "Lcom/margelo/nitro/dev/litert/litertlm/GenerationStats;";
|
|
25
|
+
|
|
26
|
+
public:
|
|
27
|
+
/**
|
|
28
|
+
* Convert this Java/Kotlin-based struct to the C++ struct GenerationStats by copying all values to C++.
|
|
29
|
+
*/
|
|
30
|
+
[[maybe_unused]]
|
|
31
|
+
[[nodiscard]]
|
|
32
|
+
GenerationStats toCpp() const {
|
|
33
|
+
static const auto clazz = javaClassStatic();
|
|
34
|
+
static const auto fieldPromptTokens = clazz->getField<double>("promptTokens");
|
|
35
|
+
double promptTokens = this->getFieldValue(fieldPromptTokens);
|
|
36
|
+
static const auto fieldCompletionTokens = clazz->getField<double>("completionTokens");
|
|
37
|
+
double completionTokens = this->getFieldValue(fieldCompletionTokens);
|
|
38
|
+
static const auto fieldTotalTokens = clazz->getField<double>("totalTokens");
|
|
39
|
+
double totalTokens = this->getFieldValue(fieldTotalTokens);
|
|
40
|
+
static const auto fieldTimeToFirstToken = clazz->getField<double>("timeToFirstToken");
|
|
41
|
+
double timeToFirstToken = this->getFieldValue(fieldTimeToFirstToken);
|
|
42
|
+
static const auto fieldTotalTime = clazz->getField<double>("totalTime");
|
|
43
|
+
double totalTime = this->getFieldValue(fieldTotalTime);
|
|
44
|
+
static const auto fieldTokensPerSecond = clazz->getField<double>("tokensPerSecond");
|
|
45
|
+
double tokensPerSecond = this->getFieldValue(fieldTokensPerSecond);
|
|
46
|
+
return GenerationStats(
|
|
47
|
+
promptTokens,
|
|
48
|
+
completionTokens,
|
|
49
|
+
totalTokens,
|
|
50
|
+
timeToFirstToken,
|
|
51
|
+
totalTime,
|
|
52
|
+
tokensPerSecond
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
public:
|
|
57
|
+
/**
|
|
58
|
+
* Create a Java/Kotlin-based struct by copying all values from the given C++ struct to Java.
|
|
59
|
+
*/
|
|
60
|
+
[[maybe_unused]]
|
|
61
|
+
static jni::local_ref<JGenerationStats::javaobject> fromCpp(const GenerationStats& value) {
|
|
62
|
+
using JSignature = JGenerationStats(double, double, double, double, double, double);
|
|
63
|
+
static const auto clazz = javaClassStatic();
|
|
64
|
+
static const auto create = clazz->getStaticMethod<JSignature>("fromCpp");
|
|
65
|
+
return create(
|
|
66
|
+
clazz,
|
|
67
|
+
value.promptTokens,
|
|
68
|
+
value.completionTokens,
|
|
69
|
+
value.totalTokens,
|
|
70
|
+
value.timeToFirstToken,
|
|
71
|
+
value.totalTime,
|
|
72
|
+
value.tokensPerSecond
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
} // namespace margelo::nitro::litertlm
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
///
|
|
2
|
+
/// JHybridLiteRTLMSpec.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
|
+
#include "JHybridLiteRTLMSpec.hpp"
|
|
9
|
+
|
|
10
|
+
// Forward declaration of `Message` to properly resolve imports.
|
|
11
|
+
namespace margelo::nitro::litertlm { struct Message; }
|
|
12
|
+
// Forward declaration of `Role` to properly resolve imports.
|
|
13
|
+
namespace margelo::nitro::litertlm { enum class Role; }
|
|
14
|
+
// Forward declaration of `GenerationStats` to properly resolve imports.
|
|
15
|
+
namespace margelo::nitro::litertlm { struct GenerationStats; }
|
|
16
|
+
// Forward declaration of `LLMConfig` to properly resolve imports.
|
|
17
|
+
namespace margelo::nitro::litertlm { struct LLMConfig; }
|
|
18
|
+
// Forward declaration of `Backend` to properly resolve imports.
|
|
19
|
+
namespace margelo::nitro::litertlm { enum class Backend; }
|
|
20
|
+
|
|
21
|
+
#include <string>
|
|
22
|
+
#include "Message.hpp"
|
|
23
|
+
#include <vector>
|
|
24
|
+
#include "JMessage.hpp"
|
|
25
|
+
#include "Role.hpp"
|
|
26
|
+
#include "JRole.hpp"
|
|
27
|
+
#include "GenerationStats.hpp"
|
|
28
|
+
#include "JGenerationStats.hpp"
|
|
29
|
+
#include "LLMConfig.hpp"
|
|
30
|
+
#include <optional>
|
|
31
|
+
#include "JLLMConfig.hpp"
|
|
32
|
+
#include "Backend.hpp"
|
|
33
|
+
#include "JBackend.hpp"
|
|
34
|
+
#include <functional>
|
|
35
|
+
#include "JFunc_void_std__string_bool.hpp"
|
|
36
|
+
#include <NitroModules/JNICallable.hpp>
|
|
37
|
+
|
|
38
|
+
namespace margelo::nitro::litertlm {
|
|
39
|
+
|
|
40
|
+
jni::local_ref<JHybridLiteRTLMSpec::jhybriddata> JHybridLiteRTLMSpec::initHybrid(jni::alias_ref<jhybridobject> jThis) {
|
|
41
|
+
return makeCxxInstance(jThis);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
void JHybridLiteRTLMSpec::registerNatives() {
|
|
45
|
+
registerHybrid({
|
|
46
|
+
makeNativeMethod("initHybrid", JHybridLiteRTLMSpec::initHybrid),
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
size_t JHybridLiteRTLMSpec::getExternalMemorySize() noexcept {
|
|
51
|
+
static const auto method = javaClassStatic()->getMethod<jlong()>("getMemorySize");
|
|
52
|
+
return method(_javaPart);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
bool JHybridLiteRTLMSpec::equals(const std::shared_ptr<HybridObject>& other) {
|
|
56
|
+
if (auto otherCast = std::dynamic_pointer_cast<JHybridLiteRTLMSpec>(other)) {
|
|
57
|
+
return _javaPart == otherCast->_javaPart;
|
|
58
|
+
}
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
void JHybridLiteRTLMSpec::dispose() noexcept {
|
|
63
|
+
static const auto method = javaClassStatic()->getMethod<void()>("dispose");
|
|
64
|
+
method(_javaPart);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
std::string JHybridLiteRTLMSpec::toString() {
|
|
68
|
+
static const auto method = javaClassStatic()->getMethod<jni::JString()>("toString");
|
|
69
|
+
auto javaString = method(_javaPart);
|
|
70
|
+
return javaString->toStdString();
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// Properties
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
// Methods
|
|
77
|
+
void JHybridLiteRTLMSpec::loadModel(const std::string& modelPath, const std::optional<LLMConfig>& config) {
|
|
78
|
+
static const auto method = javaClassStatic()->getMethod<void(jni::alias_ref<jni::JString> /* modelPath */, jni::alias_ref<JLLMConfig> /* config */)>("loadModel");
|
|
79
|
+
method(_javaPart, jni::make_jstring(modelPath), config.has_value() ? JLLMConfig::fromCpp(config.value()) : nullptr);
|
|
80
|
+
}
|
|
81
|
+
std::string JHybridLiteRTLMSpec::sendMessage(const std::string& message) {
|
|
82
|
+
static const auto method = javaClassStatic()->getMethod<jni::local_ref<jni::JString>(jni::alias_ref<jni::JString> /* message */)>("sendMessage");
|
|
83
|
+
auto __result = method(_javaPart, jni::make_jstring(message));
|
|
84
|
+
return __result->toStdString();
|
|
85
|
+
}
|
|
86
|
+
std::string JHybridLiteRTLMSpec::sendMessageWithImage(const std::string& message, const std::string& imagePath) {
|
|
87
|
+
static const auto method = javaClassStatic()->getMethod<jni::local_ref<jni::JString>(jni::alias_ref<jni::JString> /* message */, jni::alias_ref<jni::JString> /* imagePath */)>("sendMessageWithImage");
|
|
88
|
+
auto __result = method(_javaPart, jni::make_jstring(message), jni::make_jstring(imagePath));
|
|
89
|
+
return __result->toStdString();
|
|
90
|
+
}
|
|
91
|
+
std::string JHybridLiteRTLMSpec::sendMessageWithAudio(const std::string& message, const std::string& audioPath) {
|
|
92
|
+
static const auto method = javaClassStatic()->getMethod<jni::local_ref<jni::JString>(jni::alias_ref<jni::JString> /* message */, jni::alias_ref<jni::JString> /* audioPath */)>("sendMessageWithAudio");
|
|
93
|
+
auto __result = method(_javaPart, jni::make_jstring(message), jni::make_jstring(audioPath));
|
|
94
|
+
return __result->toStdString();
|
|
95
|
+
}
|
|
96
|
+
void JHybridLiteRTLMSpec::sendMessageAsync(const std::string& message, const std::function<void(const std::string& /* token */, bool /* done */)>& onToken) {
|
|
97
|
+
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");
|
|
98
|
+
method(_javaPart, jni::make_jstring(message), JFunc_void_std__string_bool_cxx::fromCpp(onToken));
|
|
99
|
+
}
|
|
100
|
+
std::vector<Message> JHybridLiteRTLMSpec::getHistory() {
|
|
101
|
+
static const auto method = javaClassStatic()->getMethod<jni::local_ref<jni::JArrayClass<JMessage>>()>("getHistory");
|
|
102
|
+
auto __result = method(_javaPart);
|
|
103
|
+
return [&]() {
|
|
104
|
+
size_t __size = __result->size();
|
|
105
|
+
std::vector<Message> __vector;
|
|
106
|
+
__vector.reserve(__size);
|
|
107
|
+
for (size_t __i = 0; __i < __size; __i++) {
|
|
108
|
+
auto __element = __result->getElement(__i);
|
|
109
|
+
__vector.push_back(__element->toCpp());
|
|
110
|
+
}
|
|
111
|
+
return __vector;
|
|
112
|
+
}();
|
|
113
|
+
}
|
|
114
|
+
void JHybridLiteRTLMSpec::resetConversation() {
|
|
115
|
+
static const auto method = javaClassStatic()->getMethod<void()>("resetConversation");
|
|
116
|
+
method(_javaPart);
|
|
117
|
+
}
|
|
118
|
+
bool JHybridLiteRTLMSpec::isReady() {
|
|
119
|
+
static const auto method = javaClassStatic()->getMethod<jboolean()>("isReady");
|
|
120
|
+
auto __result = method(_javaPart);
|
|
121
|
+
return static_cast<bool>(__result);
|
|
122
|
+
}
|
|
123
|
+
GenerationStats JHybridLiteRTLMSpec::getStats() {
|
|
124
|
+
static const auto method = javaClassStatic()->getMethod<jni::local_ref<JGenerationStats>()>("getStats");
|
|
125
|
+
auto __result = method(_javaPart);
|
|
126
|
+
return __result->toCpp();
|
|
127
|
+
}
|
|
128
|
+
void JHybridLiteRTLMSpec::close() {
|
|
129
|
+
static const auto method = javaClassStatic()->getMethod<void()>("close");
|
|
130
|
+
method(_javaPart);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
} // namespace margelo::nitro::litertlm
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
///
|
|
2
|
+
/// HybridLiteRTLMSpec.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 <NitroModules/JHybridObject.hpp>
|
|
11
|
+
#include <fbjni/fbjni.h>
|
|
12
|
+
#include "HybridLiteRTLMSpec.hpp"
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
namespace margelo::nitro::litertlm {
|
|
18
|
+
|
|
19
|
+
using namespace facebook;
|
|
20
|
+
|
|
21
|
+
class JHybridLiteRTLMSpec: public jni::HybridClass<JHybridLiteRTLMSpec, JHybridObject>,
|
|
22
|
+
public virtual HybridLiteRTLMSpec {
|
|
23
|
+
public:
|
|
24
|
+
static auto constexpr kJavaDescriptor = "Lcom/margelo/nitro/dev/litert/litertlm/HybridLiteRTLMSpec;";
|
|
25
|
+
static jni::local_ref<jhybriddata> initHybrid(jni::alias_ref<jhybridobject> jThis);
|
|
26
|
+
static void registerNatives();
|
|
27
|
+
|
|
28
|
+
protected:
|
|
29
|
+
// C++ constructor (called from Java via `initHybrid()`)
|
|
30
|
+
explicit JHybridLiteRTLMSpec(jni::alias_ref<jhybridobject> jThis) :
|
|
31
|
+
HybridObject(HybridLiteRTLMSpec::TAG),
|
|
32
|
+
HybridBase(jThis),
|
|
33
|
+
_javaPart(jni::make_global(jThis)) {}
|
|
34
|
+
|
|
35
|
+
public:
|
|
36
|
+
~JHybridLiteRTLMSpec() override {
|
|
37
|
+
// Hermes GC can destroy JS objects on a non-JNI Thread.
|
|
38
|
+
jni::ThreadScope::WithClassLoader([&] { _javaPart.reset(); });
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
public:
|
|
42
|
+
size_t getExternalMemorySize() noexcept override;
|
|
43
|
+
bool equals(const std::shared_ptr<HybridObject>& other) override;
|
|
44
|
+
void dispose() noexcept override;
|
|
45
|
+
std::string toString() override;
|
|
46
|
+
|
|
47
|
+
public:
|
|
48
|
+
inline const jni::global_ref<JHybridLiteRTLMSpec::javaobject>& getJavaPart() const noexcept {
|
|
49
|
+
return _javaPart;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
public:
|
|
53
|
+
// Properties
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
public:
|
|
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;
|
|
62
|
+
void sendMessageAsync(const std::string& message, const std::function<void(const std::string& /* token */, bool /* done */)>& onToken) override;
|
|
63
|
+
std::vector<Message> getHistory() override;
|
|
64
|
+
void resetConversation() override;
|
|
65
|
+
bool isReady() override;
|
|
66
|
+
GenerationStats getStats() override;
|
|
67
|
+
void close() override;
|
|
68
|
+
|
|
69
|
+
private:
|
|
70
|
+
friend HybridBase;
|
|
71
|
+
using HybridBase::HybridBase;
|
|
72
|
+
jni::global_ref<JHybridLiteRTLMSpec::javaobject> _javaPart;
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
} // namespace margelo::nitro::litertlm
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
///
|
|
2
|
+
/// JLLMConfig.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 "LLMConfig.hpp"
|
|
12
|
+
|
|
13
|
+
#include "Backend.hpp"
|
|
14
|
+
#include "JBackend.hpp"
|
|
15
|
+
#include <optional>
|
|
16
|
+
|
|
17
|
+
namespace margelo::nitro::litertlm {
|
|
18
|
+
|
|
19
|
+
using namespace facebook;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* The C++ JNI bridge between the C++ struct "LLMConfig" and the the Kotlin data class "LLMConfig".
|
|
23
|
+
*/
|
|
24
|
+
struct JLLMConfig final: public jni::JavaClass<JLLMConfig> {
|
|
25
|
+
public:
|
|
26
|
+
static auto constexpr kJavaDescriptor = "Lcom/margelo/nitro/dev/litert/litertlm/LLMConfig;";
|
|
27
|
+
|
|
28
|
+
public:
|
|
29
|
+
/**
|
|
30
|
+
* Convert this Java/Kotlin-based struct to the C++ struct LLMConfig by copying all values to C++.
|
|
31
|
+
*/
|
|
32
|
+
[[maybe_unused]]
|
|
33
|
+
[[nodiscard]]
|
|
34
|
+
LLMConfig toCpp() const {
|
|
35
|
+
static const auto clazz = javaClassStatic();
|
|
36
|
+
static const auto fieldBackend = clazz->getField<JBackend>("backend");
|
|
37
|
+
jni::local_ref<JBackend> backend = this->getFieldValue(fieldBackend);
|
|
38
|
+
static const auto fieldMaxTokens = clazz->getField<jni::JDouble>("maxTokens");
|
|
39
|
+
jni::local_ref<jni::JDouble> maxTokens = this->getFieldValue(fieldMaxTokens);
|
|
40
|
+
static const auto fieldTemperature = clazz->getField<jni::JDouble>("temperature");
|
|
41
|
+
jni::local_ref<jni::JDouble> temperature = this->getFieldValue(fieldTemperature);
|
|
42
|
+
static const auto fieldTopK = clazz->getField<jni::JDouble>("topK");
|
|
43
|
+
jni::local_ref<jni::JDouble> topK = this->getFieldValue(fieldTopK);
|
|
44
|
+
static const auto fieldTopP = clazz->getField<jni::JDouble>("topP");
|
|
45
|
+
jni::local_ref<jni::JDouble> topP = this->getFieldValue(fieldTopP);
|
|
46
|
+
return LLMConfig(
|
|
47
|
+
backend != nullptr ? std::make_optional(backend->toCpp()) : std::nullopt,
|
|
48
|
+
maxTokens != nullptr ? std::make_optional(maxTokens->value()) : std::nullopt,
|
|
49
|
+
temperature != nullptr ? std::make_optional(temperature->value()) : std::nullopt,
|
|
50
|
+
topK != nullptr ? std::make_optional(topK->value()) : std::nullopt,
|
|
51
|
+
topP != nullptr ? std::make_optional(topP->value()) : std::nullopt
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
public:
|
|
56
|
+
/**
|
|
57
|
+
* Create a Java/Kotlin-based struct by copying all values from the given C++ struct to Java.
|
|
58
|
+
*/
|
|
59
|
+
[[maybe_unused]]
|
|
60
|
+
static jni::local_ref<JLLMConfig::javaobject> fromCpp(const LLMConfig& value) {
|
|
61
|
+
using JSignature = JLLMConfig(jni::alias_ref<JBackend>, jni::alias_ref<jni::JDouble>, jni::alias_ref<jni::JDouble>, jni::alias_ref<jni::JDouble>, jni::alias_ref<jni::JDouble>);
|
|
62
|
+
static const auto clazz = javaClassStatic();
|
|
63
|
+
static const auto create = clazz->getStaticMethod<JSignature>("fromCpp");
|
|
64
|
+
return create(
|
|
65
|
+
clazz,
|
|
66
|
+
value.backend.has_value() ? JBackend::fromCpp(value.backend.value()) : nullptr,
|
|
67
|
+
value.maxTokens.has_value() ? jni::JDouble::valueOf(value.maxTokens.value()) : nullptr,
|
|
68
|
+
value.temperature.has_value() ? jni::JDouble::valueOf(value.temperature.value()) : nullptr,
|
|
69
|
+
value.topK.has_value() ? jni::JDouble::valueOf(value.topK.value()) : nullptr,
|
|
70
|
+
value.topP.has_value() ? jni::JDouble::valueOf(value.topP.value()) : nullptr
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
} // namespace margelo::nitro::litertlm
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
///
|
|
2
|
+
/// JMessage.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 "Message.hpp"
|
|
12
|
+
|
|
13
|
+
#include "JRole.hpp"
|
|
14
|
+
#include "Role.hpp"
|
|
15
|
+
#include <string>
|
|
16
|
+
|
|
17
|
+
namespace margelo::nitro::litertlm {
|
|
18
|
+
|
|
19
|
+
using namespace facebook;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* The C++ JNI bridge between the C++ struct "Message" and the the Kotlin data class "Message".
|
|
23
|
+
*/
|
|
24
|
+
struct JMessage final: public jni::JavaClass<JMessage> {
|
|
25
|
+
public:
|
|
26
|
+
static auto constexpr kJavaDescriptor = "Lcom/margelo/nitro/dev/litert/litertlm/Message;";
|
|
27
|
+
|
|
28
|
+
public:
|
|
29
|
+
/**
|
|
30
|
+
* Convert this Java/Kotlin-based struct to the C++ struct Message by copying all values to C++.
|
|
31
|
+
*/
|
|
32
|
+
[[maybe_unused]]
|
|
33
|
+
[[nodiscard]]
|
|
34
|
+
Message toCpp() const {
|
|
35
|
+
static const auto clazz = javaClassStatic();
|
|
36
|
+
static const auto fieldRole = clazz->getField<JRole>("role");
|
|
37
|
+
jni::local_ref<JRole> role = this->getFieldValue(fieldRole);
|
|
38
|
+
static const auto fieldContent = clazz->getField<jni::JString>("content");
|
|
39
|
+
jni::local_ref<jni::JString> content = this->getFieldValue(fieldContent);
|
|
40
|
+
return Message(
|
|
41
|
+
role->toCpp(),
|
|
42
|
+
content->toStdString()
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
public:
|
|
47
|
+
/**
|
|
48
|
+
* Create a Java/Kotlin-based struct by copying all values from the given C++ struct to Java.
|
|
49
|
+
*/
|
|
50
|
+
[[maybe_unused]]
|
|
51
|
+
static jni::local_ref<JMessage::javaobject> fromCpp(const Message& value) {
|
|
52
|
+
using JSignature = JMessage(jni::alias_ref<JRole>, jni::alias_ref<jni::JString>);
|
|
53
|
+
static const auto clazz = javaClassStatic();
|
|
54
|
+
static const auto create = clazz->getStaticMethod<JSignature>("fromCpp");
|
|
55
|
+
return create(
|
|
56
|
+
clazz,
|
|
57
|
+
JRole::fromCpp(value.role),
|
|
58
|
+
jni::make_jstring(value.content)
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
} // namespace margelo::nitro::litertlm
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
///
|
|
2
|
+
/// JRole.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 "Role.hpp"
|
|
12
|
+
|
|
13
|
+
namespace margelo::nitro::litertlm {
|
|
14
|
+
|
|
15
|
+
using namespace facebook;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* The C++ JNI bridge between the C++ enum "Role" and the the Kotlin enum "Role".
|
|
19
|
+
*/
|
|
20
|
+
struct JRole final: public jni::JavaClass<JRole> {
|
|
21
|
+
public:
|
|
22
|
+
static auto constexpr kJavaDescriptor = "Lcom/margelo/nitro/dev/litert/litertlm/Role;";
|
|
23
|
+
|
|
24
|
+
public:
|
|
25
|
+
/**
|
|
26
|
+
* Convert this Java/Kotlin-based enum to the C++ enum Role.
|
|
27
|
+
*/
|
|
28
|
+
[[maybe_unused]]
|
|
29
|
+
[[nodiscard]]
|
|
30
|
+
Role 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<Role>(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<JRole> fromCpp(Role value) {
|
|
43
|
+
static const auto clazz = javaClassStatic();
|
|
44
|
+
switch (value) {
|
|
45
|
+
case Role::USER:
|
|
46
|
+
static const auto fieldUSER = clazz->getStaticField<JRole>("USER");
|
|
47
|
+
return clazz->getStaticFieldValue(fieldUSER);
|
|
48
|
+
case Role::MODEL:
|
|
49
|
+
static const auto fieldMODEL = clazz->getStaticField<JRole>("MODEL");
|
|
50
|
+
return clazz->getStaticFieldValue(fieldMODEL);
|
|
51
|
+
case Role::SYSTEM:
|
|
52
|
+
static const auto fieldSYSTEM = clazz->getStaticField<JRole>("SYSTEM");
|
|
53
|
+
return clazz->getStaticFieldValue(fieldSYSTEM);
|
|
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
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
///
|
|
2
|
+
/// Backend.kt
|
|
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
|
+
package com.margelo.nitro.dev.litert.litertlm
|
|
9
|
+
|
|
10
|
+
import androidx.annotation.Keep
|
|
11
|
+
import com.facebook.proguard.annotations.DoNotStrip
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Represents the JavaScript enum/union "Backend".
|
|
15
|
+
*/
|
|
16
|
+
@DoNotStrip
|
|
17
|
+
@Keep
|
|
18
|
+
enum class Backend(@DoNotStrip @Keep val value: Int) {
|
|
19
|
+
CPU(0),
|
|
20
|
+
GPU(1),
|
|
21
|
+
NPU(2);
|
|
22
|
+
|
|
23
|
+
companion object
|
|
24
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
///
|
|
2
|
+
/// Func_void_std__string_bool.kt
|
|
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
|
+
package com.margelo.nitro.dev.litert.litertlm
|
|
9
|
+
|
|
10
|
+
import androidx.annotation.Keep
|
|
11
|
+
import com.facebook.jni.HybridData
|
|
12
|
+
import com.facebook.proguard.annotations.DoNotStrip
|
|
13
|
+
import dalvik.annotation.optimization.FastNative
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Represents the JavaScript callback `(token: string, done: boolean) => void`.
|
|
18
|
+
* This can be either implemented in C++ (in which case it might be a callback coming from JS),
|
|
19
|
+
* or in Kotlin/Java (in which case it is a native callback).
|
|
20
|
+
*/
|
|
21
|
+
@DoNotStrip
|
|
22
|
+
@Keep
|
|
23
|
+
@Suppress("ClassName", "RedundantUnitReturnType")
|
|
24
|
+
fun interface Func_void_std__string_bool: (String, Boolean) -> Unit {
|
|
25
|
+
/**
|
|
26
|
+
* Call the given JS callback.
|
|
27
|
+
* @throws Throwable if the JS function itself throws an error, or if the JS function/runtime has already been deleted.
|
|
28
|
+
*/
|
|
29
|
+
@DoNotStrip
|
|
30
|
+
@Keep
|
|
31
|
+
override fun invoke(token: String, done: Boolean): Unit
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Represents the JavaScript callback `(token: string, done: boolean) => void`.
|
|
36
|
+
* This is implemented in C++, via a `std::function<...>`.
|
|
37
|
+
* The callback might be coming from JS.
|
|
38
|
+
*/
|
|
39
|
+
@DoNotStrip
|
|
40
|
+
@Keep
|
|
41
|
+
@Suppress(
|
|
42
|
+
"KotlinJniMissingFunction", "unused",
|
|
43
|
+
"RedundantSuppression", "RedundantUnitReturnType", "FunctionName",
|
|
44
|
+
"ConvertSecondaryConstructorToPrimary", "ClassName", "LocalVariableName",
|
|
45
|
+
)
|
|
46
|
+
class Func_void_std__string_bool_cxx: Func_void_std__string_bool {
|
|
47
|
+
@DoNotStrip
|
|
48
|
+
@Keep
|
|
49
|
+
private val mHybridData: HybridData
|
|
50
|
+
|
|
51
|
+
@DoNotStrip
|
|
52
|
+
@Keep
|
|
53
|
+
private constructor(hybridData: HybridData) {
|
|
54
|
+
mHybridData = hybridData
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
@DoNotStrip
|
|
58
|
+
@Keep
|
|
59
|
+
override fun invoke(token: String, done: Boolean): Unit
|
|
60
|
+
= invoke_cxx(token,done)
|
|
61
|
+
|
|
62
|
+
@FastNative
|
|
63
|
+
private external fun invoke_cxx(token: String, done: Boolean): Unit
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Represents the JavaScript callback `(token: string, done: boolean) => void`.
|
|
68
|
+
* This is implemented in Java/Kotlin, via a `(String, Boolean) -> Unit`.
|
|
69
|
+
* The callback is always coming from native.
|
|
70
|
+
*/
|
|
71
|
+
@DoNotStrip
|
|
72
|
+
@Keep
|
|
73
|
+
@Suppress("ClassName", "RedundantUnitReturnType", "unused")
|
|
74
|
+
class Func_void_std__string_bool_java(private val function: (String, Boolean) -> Unit): Func_void_std__string_bool {
|
|
75
|
+
@DoNotStrip
|
|
76
|
+
@Keep
|
|
77
|
+
override fun invoke(token: String, done: Boolean): Unit {
|
|
78
|
+
return this.function(token, done)
|
|
79
|
+
}
|
|
80
|
+
}
|