@inferrlm/react-native-mlx 0.2.0-inferrlm.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/MLXReactNative.podspec +42 -0
- package/ios/Bridge.h +8 -0
- package/ios/Sources/HybridLLM.swift +245 -0
- package/ios/Sources/HybridModelManager.swift +77 -0
- package/ios/Sources/LLMError.swift +6 -0
- package/ios/Sources/MLXReactNative.h +16 -0
- package/ios/Sources/ModelDownloader.swift +103 -0
- package/lib/module/index.js +6 -0
- package/lib/module/index.js.map +1 -0
- package/lib/module/llm.js +125 -0
- package/lib/module/llm.js.map +1 -0
- package/lib/module/modelManager.js +79 -0
- package/lib/module/modelManager.js.map +1 -0
- package/lib/module/models.js +41 -0
- package/lib/module/models.js.map +1 -0
- package/lib/module/package.json +1 -0
- package/lib/module/specs/LLM.nitro.js +4 -0
- package/lib/module/specs/LLM.nitro.js.map +1 -0
- package/lib/module/specs/ModelManager.nitro.js +4 -0
- package/lib/module/specs/ModelManager.nitro.js.map +1 -0
- package/lib/typescript/package.json +1 -0
- package/lib/typescript/src/index.d.ts +6 -0
- package/lib/typescript/src/index.d.ts.map +1 -0
- package/lib/typescript/src/llm.d.ts +87 -0
- package/lib/typescript/src/llm.d.ts.map +1 -0
- package/lib/typescript/src/modelManager.d.ts +53 -0
- package/lib/typescript/src/modelManager.d.ts.map +1 -0
- package/lib/typescript/src/models.d.ts +29 -0
- package/lib/typescript/src/models.d.ts.map +1 -0
- package/lib/typescript/src/specs/LLM.nitro.d.ts +88 -0
- package/lib/typescript/src/specs/LLM.nitro.d.ts.map +1 -0
- package/lib/typescript/src/specs/ModelManager.nitro.d.ts +41 -0
- package/lib/typescript/src/specs/ModelManager.nitro.d.ts.map +1 -0
- package/nitrogen/generated/.gitattributes +1 -0
- package/nitrogen/generated/ios/MLXReactNative+autolinking.rb +60 -0
- package/nitrogen/generated/ios/MLXReactNative-Swift-Cxx-Bridge.cpp +98 -0
- package/nitrogen/generated/ios/MLXReactNative-Swift-Cxx-Bridge.hpp +399 -0
- package/nitrogen/generated/ios/MLXReactNative-Swift-Cxx-Umbrella.hpp +62 -0
- package/nitrogen/generated/ios/MLXReactNativeAutolinking.mm +41 -0
- package/nitrogen/generated/ios/MLXReactNativeAutolinking.swift +40 -0
- package/nitrogen/generated/ios/c++/HybridLLMSpecSwift.cpp +11 -0
- package/nitrogen/generated/ios/c++/HybridLLMSpecSwift.hpp +160 -0
- package/nitrogen/generated/ios/c++/HybridModelManagerSpecSwift.cpp +11 -0
- package/nitrogen/generated/ios/c++/HybridModelManagerSpecSwift.hpp +116 -0
- package/nitrogen/generated/ios/swift/Func_void.swift +47 -0
- package/nitrogen/generated/ios/swift/Func_void_bool.swift +47 -0
- package/nitrogen/generated/ios/swift/Func_void_double.swift +47 -0
- package/nitrogen/generated/ios/swift/Func_void_std__exception_ptr.swift +47 -0
- package/nitrogen/generated/ios/swift/Func_void_std__string.swift +47 -0
- package/nitrogen/generated/ios/swift/Func_void_std__vector_std__string_.swift +47 -0
- package/nitrogen/generated/ios/swift/GenerationStats.swift +69 -0
- package/nitrogen/generated/ios/swift/HybridLLMSpec.swift +67 -0
- package/nitrogen/generated/ios/swift/HybridLLMSpec_cxx.swift +285 -0
- package/nitrogen/generated/ios/swift/HybridModelManagerSpec.swift +60 -0
- package/nitrogen/generated/ios/swift/HybridModelManagerSpec_cxx.swift +234 -0
- package/nitrogen/generated/ios/swift/LLMLoadOptions.swift +138 -0
- package/nitrogen/generated/ios/swift/LLMMessage.swift +47 -0
- package/nitrogen/generated/shared/c++/GenerationStats.hpp +87 -0
- package/nitrogen/generated/shared/c++/HybridLLMSpec.cpp +35 -0
- package/nitrogen/generated/shared/c++/HybridLLMSpec.hpp +87 -0
- package/nitrogen/generated/shared/c++/HybridModelManagerSpec.cpp +27 -0
- package/nitrogen/generated/shared/c++/HybridModelManagerSpec.hpp +70 -0
- package/nitrogen/generated/shared/c++/LLMLoadOptions.hpp +87 -0
- package/nitrogen/generated/shared/c++/LLMMessage.hpp +79 -0
- package/package.json +142 -0
- package/src/index.ts +6 -0
- package/src/llm.ts +144 -0
- package/src/modelManager.ts +88 -0
- package/src/models.ts +45 -0
- package/src/specs/LLM.nitro.ts +98 -0
- package/src/specs/ModelManager.nitro.ts +44 -0
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
///
|
|
2
|
+
/// LLMLoadOptions.swift
|
|
3
|
+
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
|
|
4
|
+
/// https://github.com/mrousavy/nitro
|
|
5
|
+
/// Copyright © 2025 Marc Rousavy @ Margelo
|
|
6
|
+
///
|
|
7
|
+
|
|
8
|
+
import Foundation
|
|
9
|
+
import NitroModules
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Represents an instance of `LLMLoadOptions`, backed by a C++ struct.
|
|
13
|
+
*/
|
|
14
|
+
public typealias LLMLoadOptions = margelo.nitro.mlxreactnative.LLMLoadOptions
|
|
15
|
+
|
|
16
|
+
public extension LLMLoadOptions {
|
|
17
|
+
private typealias bridge = margelo.nitro.mlxreactnative.bridge.swift
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Create a new instance of `LLMLoadOptions`.
|
|
21
|
+
*/
|
|
22
|
+
init(onProgress: ((_ progress: Double) -> Void)?, additionalContext: [LLMMessage]?, manageHistory: Bool?) {
|
|
23
|
+
self.init({ () -> bridge.std__optional_std__function_void_double____progress______ in
|
|
24
|
+
if let __unwrappedValue = onProgress {
|
|
25
|
+
return bridge.create_std__optional_std__function_void_double____progress______({ () -> bridge.Func_void_double in
|
|
26
|
+
let __closureWrapper = Func_void_double(__unwrappedValue)
|
|
27
|
+
return bridge.create_Func_void_double(__closureWrapper.toUnsafe())
|
|
28
|
+
}())
|
|
29
|
+
} else {
|
|
30
|
+
return .init()
|
|
31
|
+
}
|
|
32
|
+
}(), { () -> bridge.std__optional_std__vector_LLMMessage__ in
|
|
33
|
+
if let __unwrappedValue = additionalContext {
|
|
34
|
+
return bridge.create_std__optional_std__vector_LLMMessage__({ () -> bridge.std__vector_LLMMessage_ in
|
|
35
|
+
var __vector = bridge.create_std__vector_LLMMessage_(__unwrappedValue.count)
|
|
36
|
+
for __item in __unwrappedValue {
|
|
37
|
+
__vector.push_back(__item)
|
|
38
|
+
}
|
|
39
|
+
return __vector
|
|
40
|
+
}())
|
|
41
|
+
} else {
|
|
42
|
+
return .init()
|
|
43
|
+
}
|
|
44
|
+
}(), { () -> bridge.std__optional_bool_ in
|
|
45
|
+
if let __unwrappedValue = manageHistory {
|
|
46
|
+
return bridge.create_std__optional_bool_(__unwrappedValue)
|
|
47
|
+
} else {
|
|
48
|
+
return .init()
|
|
49
|
+
}
|
|
50
|
+
}())
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
var onProgress: ((_ progress: Double) -> Void)? {
|
|
54
|
+
@inline(__always)
|
|
55
|
+
get {
|
|
56
|
+
return { () -> ((_ progress: Double) -> Void)? in
|
|
57
|
+
if bridge.has_value_std__optional_std__function_void_double____progress______(self.__onProgress) {
|
|
58
|
+
let __unwrapped = bridge.get_std__optional_std__function_void_double____progress______(self.__onProgress)
|
|
59
|
+
return { () -> (Double) -> Void in
|
|
60
|
+
let __wrappedFunction = bridge.wrap_Func_void_double(__unwrapped)
|
|
61
|
+
return { (__progress: Double) -> Void in
|
|
62
|
+
__wrappedFunction.call(__progress)
|
|
63
|
+
}
|
|
64
|
+
}()
|
|
65
|
+
} else {
|
|
66
|
+
return nil
|
|
67
|
+
}
|
|
68
|
+
}()
|
|
69
|
+
}
|
|
70
|
+
@inline(__always)
|
|
71
|
+
set {
|
|
72
|
+
self.__onProgress = { () -> bridge.std__optional_std__function_void_double____progress______ in
|
|
73
|
+
if let __unwrappedValue = newValue {
|
|
74
|
+
return bridge.create_std__optional_std__function_void_double____progress______({ () -> bridge.Func_void_double in
|
|
75
|
+
let __closureWrapper = Func_void_double(__unwrappedValue)
|
|
76
|
+
return bridge.create_Func_void_double(__closureWrapper.toUnsafe())
|
|
77
|
+
}())
|
|
78
|
+
} else {
|
|
79
|
+
return .init()
|
|
80
|
+
}
|
|
81
|
+
}()
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
var additionalContext: [LLMMessage]? {
|
|
86
|
+
@inline(__always)
|
|
87
|
+
get {
|
|
88
|
+
return { () -> [LLMMessage]? in
|
|
89
|
+
if bridge.has_value_std__optional_std__vector_LLMMessage__(self.__additionalContext) {
|
|
90
|
+
let __unwrapped = bridge.get_std__optional_std__vector_LLMMessage__(self.__additionalContext)
|
|
91
|
+
return __unwrapped.map({ __item in __item })
|
|
92
|
+
} else {
|
|
93
|
+
return nil
|
|
94
|
+
}
|
|
95
|
+
}()
|
|
96
|
+
}
|
|
97
|
+
@inline(__always)
|
|
98
|
+
set {
|
|
99
|
+
self.__additionalContext = { () -> bridge.std__optional_std__vector_LLMMessage__ in
|
|
100
|
+
if let __unwrappedValue = newValue {
|
|
101
|
+
return bridge.create_std__optional_std__vector_LLMMessage__({ () -> bridge.std__vector_LLMMessage_ in
|
|
102
|
+
var __vector = bridge.create_std__vector_LLMMessage_(__unwrappedValue.count)
|
|
103
|
+
for __item in __unwrappedValue {
|
|
104
|
+
__vector.push_back(__item)
|
|
105
|
+
}
|
|
106
|
+
return __vector
|
|
107
|
+
}())
|
|
108
|
+
} else {
|
|
109
|
+
return .init()
|
|
110
|
+
}
|
|
111
|
+
}()
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
var manageHistory: Bool? {
|
|
116
|
+
@inline(__always)
|
|
117
|
+
get {
|
|
118
|
+
return { () -> Bool? in
|
|
119
|
+
if bridge.has_value_std__optional_bool_(self.__manageHistory) {
|
|
120
|
+
let __unwrapped = bridge.get_std__optional_bool_(self.__manageHistory)
|
|
121
|
+
return __unwrapped
|
|
122
|
+
} else {
|
|
123
|
+
return nil
|
|
124
|
+
}
|
|
125
|
+
}()
|
|
126
|
+
}
|
|
127
|
+
@inline(__always)
|
|
128
|
+
set {
|
|
129
|
+
self.__manageHistory = { () -> bridge.std__optional_bool_ in
|
|
130
|
+
if let __unwrappedValue = newValue {
|
|
131
|
+
return bridge.create_std__optional_bool_(__unwrappedValue)
|
|
132
|
+
} else {
|
|
133
|
+
return .init()
|
|
134
|
+
}
|
|
135
|
+
}()
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
///
|
|
2
|
+
/// LLMMessage.swift
|
|
3
|
+
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
|
|
4
|
+
/// https://github.com/mrousavy/nitro
|
|
5
|
+
/// Copyright © 2025 Marc Rousavy @ Margelo
|
|
6
|
+
///
|
|
7
|
+
|
|
8
|
+
import Foundation
|
|
9
|
+
import NitroModules
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Represents an instance of `LLMMessage`, backed by a C++ struct.
|
|
13
|
+
*/
|
|
14
|
+
public typealias LLMMessage = margelo.nitro.mlxreactnative.LLMMessage
|
|
15
|
+
|
|
16
|
+
public extension LLMMessage {
|
|
17
|
+
private typealias bridge = margelo.nitro.mlxreactnative.bridge.swift
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Create a new instance of `LLMMessage`.
|
|
21
|
+
*/
|
|
22
|
+
init(role: String, content: String) {
|
|
23
|
+
self.init(std.string(role), std.string(content))
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
var role: String {
|
|
27
|
+
@inline(__always)
|
|
28
|
+
get {
|
|
29
|
+
return String(self.__role)
|
|
30
|
+
}
|
|
31
|
+
@inline(__always)
|
|
32
|
+
set {
|
|
33
|
+
self.__role = std.string(newValue)
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
var content: String {
|
|
38
|
+
@inline(__always)
|
|
39
|
+
get {
|
|
40
|
+
return String(self.__content)
|
|
41
|
+
}
|
|
42
|
+
@inline(__always)
|
|
43
|
+
set {
|
|
44
|
+
self.__content = std.string(newValue)
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
///
|
|
2
|
+
/// GenerationStats.hpp
|
|
3
|
+
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
|
|
4
|
+
/// https://github.com/mrousavy/nitro
|
|
5
|
+
/// Copyright © 2025 Marc Rousavy @ Margelo
|
|
6
|
+
///
|
|
7
|
+
|
|
8
|
+
#pragma once
|
|
9
|
+
|
|
10
|
+
#if __has_include(<NitroModules/JSIConverter.hpp>)
|
|
11
|
+
#include <NitroModules/JSIConverter.hpp>
|
|
12
|
+
#else
|
|
13
|
+
#error NitroModules cannot be found! Are you sure you installed NitroModules properly?
|
|
14
|
+
#endif
|
|
15
|
+
#if __has_include(<NitroModules/NitroDefines.hpp>)
|
|
16
|
+
#include <NitroModules/NitroDefines.hpp>
|
|
17
|
+
#else
|
|
18
|
+
#error NitroModules cannot be found! Are you sure you installed NitroModules properly?
|
|
19
|
+
#endif
|
|
20
|
+
#if __has_include(<NitroModules/JSIHelpers.hpp>)
|
|
21
|
+
#include <NitroModules/JSIHelpers.hpp>
|
|
22
|
+
#else
|
|
23
|
+
#error NitroModules cannot be found! Are you sure you installed NitroModules properly?
|
|
24
|
+
#endif
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
namespace margelo::nitro::mlxreactnative {
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* A struct which can be represented as a JavaScript object (GenerationStats).
|
|
34
|
+
*/
|
|
35
|
+
struct GenerationStats {
|
|
36
|
+
public:
|
|
37
|
+
double tokenCount SWIFT_PRIVATE;
|
|
38
|
+
double tokensPerSecond SWIFT_PRIVATE;
|
|
39
|
+
double timeToFirstToken SWIFT_PRIVATE;
|
|
40
|
+
double totalTime SWIFT_PRIVATE;
|
|
41
|
+
|
|
42
|
+
public:
|
|
43
|
+
GenerationStats() = default;
|
|
44
|
+
explicit GenerationStats(double tokenCount, double tokensPerSecond, double timeToFirstToken, double totalTime): tokenCount(tokenCount), tokensPerSecond(tokensPerSecond), timeToFirstToken(timeToFirstToken), totalTime(totalTime) {}
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
} // namespace margelo::nitro::mlxreactnative
|
|
48
|
+
|
|
49
|
+
namespace margelo::nitro {
|
|
50
|
+
|
|
51
|
+
// C++ GenerationStats <> JS GenerationStats (object)
|
|
52
|
+
template <>
|
|
53
|
+
struct JSIConverter<margelo::nitro::mlxreactnative::GenerationStats> final {
|
|
54
|
+
static inline margelo::nitro::mlxreactnative::GenerationStats fromJSI(jsi::Runtime& runtime, const jsi::Value& arg) {
|
|
55
|
+
jsi::Object obj = arg.asObject(runtime);
|
|
56
|
+
return margelo::nitro::mlxreactnative::GenerationStats(
|
|
57
|
+
JSIConverter<double>::fromJSI(runtime, obj.getProperty(runtime, "tokenCount")),
|
|
58
|
+
JSIConverter<double>::fromJSI(runtime, obj.getProperty(runtime, "tokensPerSecond")),
|
|
59
|
+
JSIConverter<double>::fromJSI(runtime, obj.getProperty(runtime, "timeToFirstToken")),
|
|
60
|
+
JSIConverter<double>::fromJSI(runtime, obj.getProperty(runtime, "totalTime"))
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
static inline jsi::Value toJSI(jsi::Runtime& runtime, const margelo::nitro::mlxreactnative::GenerationStats& arg) {
|
|
64
|
+
jsi::Object obj(runtime);
|
|
65
|
+
obj.setProperty(runtime, "tokenCount", JSIConverter<double>::toJSI(runtime, arg.tokenCount));
|
|
66
|
+
obj.setProperty(runtime, "tokensPerSecond", JSIConverter<double>::toJSI(runtime, arg.tokensPerSecond));
|
|
67
|
+
obj.setProperty(runtime, "timeToFirstToken", JSIConverter<double>::toJSI(runtime, arg.timeToFirstToken));
|
|
68
|
+
obj.setProperty(runtime, "totalTime", JSIConverter<double>::toJSI(runtime, arg.totalTime));
|
|
69
|
+
return obj;
|
|
70
|
+
}
|
|
71
|
+
static inline bool canConvert(jsi::Runtime& runtime, const jsi::Value& value) {
|
|
72
|
+
if (!value.isObject()) {
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
75
|
+
jsi::Object obj = value.getObject(runtime);
|
|
76
|
+
if (!nitro::isPlainObject(runtime, obj)) {
|
|
77
|
+
return false;
|
|
78
|
+
}
|
|
79
|
+
if (!JSIConverter<double>::canConvert(runtime, obj.getProperty(runtime, "tokenCount"))) return false;
|
|
80
|
+
if (!JSIConverter<double>::canConvert(runtime, obj.getProperty(runtime, "tokensPerSecond"))) return false;
|
|
81
|
+
if (!JSIConverter<double>::canConvert(runtime, obj.getProperty(runtime, "timeToFirstToken"))) return false;
|
|
82
|
+
if (!JSIConverter<double>::canConvert(runtime, obj.getProperty(runtime, "totalTime"))) return false;
|
|
83
|
+
return true;
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
} // namespace margelo::nitro
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
///
|
|
2
|
+
/// HybridLLMSpec.cpp
|
|
3
|
+
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
|
|
4
|
+
/// https://github.com/mrousavy/nitro
|
|
5
|
+
/// Copyright © 2025 Marc Rousavy @ Margelo
|
|
6
|
+
///
|
|
7
|
+
|
|
8
|
+
#include "HybridLLMSpec.hpp"
|
|
9
|
+
|
|
10
|
+
namespace margelo::nitro::mlxreactnative {
|
|
11
|
+
|
|
12
|
+
void HybridLLMSpec::loadHybridMethods() {
|
|
13
|
+
// load base methods/properties
|
|
14
|
+
HybridObject::loadHybridMethods();
|
|
15
|
+
// load custom methods/properties
|
|
16
|
+
registerHybrids(this, [](Prototype& prototype) {
|
|
17
|
+
prototype.registerHybridGetter("isLoaded", &HybridLLMSpec::getIsLoaded);
|
|
18
|
+
prototype.registerHybridGetter("isGenerating", &HybridLLMSpec::getIsGenerating);
|
|
19
|
+
prototype.registerHybridGetter("modelId", &HybridLLMSpec::getModelId);
|
|
20
|
+
prototype.registerHybridGetter("debug", &HybridLLMSpec::getDebug);
|
|
21
|
+
prototype.registerHybridSetter("debug", &HybridLLMSpec::setDebug);
|
|
22
|
+
prototype.registerHybridGetter("systemPrompt", &HybridLLMSpec::getSystemPrompt);
|
|
23
|
+
prototype.registerHybridSetter("systemPrompt", &HybridLLMSpec::setSystemPrompt);
|
|
24
|
+
prototype.registerHybridMethod("load", &HybridLLMSpec::load);
|
|
25
|
+
prototype.registerHybridMethod("generate", &HybridLLMSpec::generate);
|
|
26
|
+
prototype.registerHybridMethod("stream", &HybridLLMSpec::stream);
|
|
27
|
+
prototype.registerHybridMethod("stop", &HybridLLMSpec::stop);
|
|
28
|
+
prototype.registerHybridMethod("unload", &HybridLLMSpec::unload);
|
|
29
|
+
prototype.registerHybridMethod("getLastGenerationStats", &HybridLLMSpec::getLastGenerationStats);
|
|
30
|
+
prototype.registerHybridMethod("getHistory", &HybridLLMSpec::getHistory);
|
|
31
|
+
prototype.registerHybridMethod("clearHistory", &HybridLLMSpec::clearHistory);
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
} // namespace margelo::nitro::mlxreactnative
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
///
|
|
2
|
+
/// HybridLLMSpec.hpp
|
|
3
|
+
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
|
|
4
|
+
/// https://github.com/mrousavy/nitro
|
|
5
|
+
/// Copyright © 2025 Marc Rousavy @ Margelo
|
|
6
|
+
///
|
|
7
|
+
|
|
8
|
+
#pragma once
|
|
9
|
+
|
|
10
|
+
#if __has_include(<NitroModules/HybridObject.hpp>)
|
|
11
|
+
#include <NitroModules/HybridObject.hpp>
|
|
12
|
+
#else
|
|
13
|
+
#error NitroModules cannot be found! Are you sure you installed NitroModules properly?
|
|
14
|
+
#endif
|
|
15
|
+
|
|
16
|
+
// Forward declaration of `LLMLoadOptions` to properly resolve imports.
|
|
17
|
+
namespace margelo::nitro::mlxreactnative { struct LLMLoadOptions; }
|
|
18
|
+
// Forward declaration of `GenerationStats` to properly resolve imports.
|
|
19
|
+
namespace margelo::nitro::mlxreactnative { struct GenerationStats; }
|
|
20
|
+
// Forward declaration of `LLMMessage` to properly resolve imports.
|
|
21
|
+
namespace margelo::nitro::mlxreactnative { struct LLMMessage; }
|
|
22
|
+
|
|
23
|
+
#include <string>
|
|
24
|
+
#include <NitroModules/Promise.hpp>
|
|
25
|
+
#include "LLMLoadOptions.hpp"
|
|
26
|
+
#include <optional>
|
|
27
|
+
#include <functional>
|
|
28
|
+
#include "GenerationStats.hpp"
|
|
29
|
+
#include "LLMMessage.hpp"
|
|
30
|
+
#include <vector>
|
|
31
|
+
|
|
32
|
+
namespace margelo::nitro::mlxreactnative {
|
|
33
|
+
|
|
34
|
+
using namespace margelo::nitro;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* An abstract base class for `LLM`
|
|
38
|
+
* Inherit this class to create instances of `HybridLLMSpec` in C++.
|
|
39
|
+
* You must explicitly call `HybridObject`'s constructor yourself, because it is virtual.
|
|
40
|
+
* @example
|
|
41
|
+
* ```cpp
|
|
42
|
+
* class HybridLLM: public HybridLLMSpec {
|
|
43
|
+
* public:
|
|
44
|
+
* HybridLLM(...): HybridObject(TAG) { ... }
|
|
45
|
+
* // ...
|
|
46
|
+
* };
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
class HybridLLMSpec: public virtual HybridObject {
|
|
50
|
+
public:
|
|
51
|
+
// Constructor
|
|
52
|
+
explicit HybridLLMSpec(): HybridObject(TAG) { }
|
|
53
|
+
|
|
54
|
+
// Destructor
|
|
55
|
+
~HybridLLMSpec() override = default;
|
|
56
|
+
|
|
57
|
+
public:
|
|
58
|
+
// Properties
|
|
59
|
+
virtual bool getIsLoaded() = 0;
|
|
60
|
+
virtual bool getIsGenerating() = 0;
|
|
61
|
+
virtual std::string getModelId() = 0;
|
|
62
|
+
virtual bool getDebug() = 0;
|
|
63
|
+
virtual void setDebug(bool debug) = 0;
|
|
64
|
+
virtual std::string getSystemPrompt() = 0;
|
|
65
|
+
virtual void setSystemPrompt(const std::string& systemPrompt) = 0;
|
|
66
|
+
|
|
67
|
+
public:
|
|
68
|
+
// Methods
|
|
69
|
+
virtual std::shared_ptr<Promise<void>> load(const std::string& modelId, const std::optional<LLMLoadOptions>& options) = 0;
|
|
70
|
+
virtual std::shared_ptr<Promise<std::string>> generate(const std::string& prompt) = 0;
|
|
71
|
+
virtual std::shared_ptr<Promise<std::string>> stream(const std::string& prompt, const std::function<void(const std::string& /* token */)>& onToken) = 0;
|
|
72
|
+
virtual void stop() = 0;
|
|
73
|
+
virtual void unload() = 0;
|
|
74
|
+
virtual GenerationStats getLastGenerationStats() = 0;
|
|
75
|
+
virtual std::vector<LLMMessage> getHistory() = 0;
|
|
76
|
+
virtual void clearHistory() = 0;
|
|
77
|
+
|
|
78
|
+
protected:
|
|
79
|
+
// Hybrid Setup
|
|
80
|
+
void loadHybridMethods() override;
|
|
81
|
+
|
|
82
|
+
protected:
|
|
83
|
+
// Tag for logging
|
|
84
|
+
static constexpr auto TAG = "LLM";
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
} // namespace margelo::nitro::mlxreactnative
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
///
|
|
2
|
+
/// HybridModelManagerSpec.cpp
|
|
3
|
+
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
|
|
4
|
+
/// https://github.com/mrousavy/nitro
|
|
5
|
+
/// Copyright © 2025 Marc Rousavy @ Margelo
|
|
6
|
+
///
|
|
7
|
+
|
|
8
|
+
#include "HybridModelManagerSpec.hpp"
|
|
9
|
+
|
|
10
|
+
namespace margelo::nitro::mlxreactnative {
|
|
11
|
+
|
|
12
|
+
void HybridModelManagerSpec::loadHybridMethods() {
|
|
13
|
+
// load base methods/properties
|
|
14
|
+
HybridObject::loadHybridMethods();
|
|
15
|
+
// load custom methods/properties
|
|
16
|
+
registerHybrids(this, [](Prototype& prototype) {
|
|
17
|
+
prototype.registerHybridGetter("debug", &HybridModelManagerSpec::getDebug);
|
|
18
|
+
prototype.registerHybridSetter("debug", &HybridModelManagerSpec::setDebug);
|
|
19
|
+
prototype.registerHybridMethod("download", &HybridModelManagerSpec::download);
|
|
20
|
+
prototype.registerHybridMethod("isDownloaded", &HybridModelManagerSpec::isDownloaded);
|
|
21
|
+
prototype.registerHybridMethod("getDownloadedModels", &HybridModelManagerSpec::getDownloadedModels);
|
|
22
|
+
prototype.registerHybridMethod("deleteModel", &HybridModelManagerSpec::deleteModel);
|
|
23
|
+
prototype.registerHybridMethod("getModelPath", &HybridModelManagerSpec::getModelPath);
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
} // namespace margelo::nitro::mlxreactnative
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
///
|
|
2
|
+
/// HybridModelManagerSpec.hpp
|
|
3
|
+
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
|
|
4
|
+
/// https://github.com/mrousavy/nitro
|
|
5
|
+
/// Copyright © 2025 Marc Rousavy @ Margelo
|
|
6
|
+
///
|
|
7
|
+
|
|
8
|
+
#pragma once
|
|
9
|
+
|
|
10
|
+
#if __has_include(<NitroModules/HybridObject.hpp>)
|
|
11
|
+
#include <NitroModules/HybridObject.hpp>
|
|
12
|
+
#else
|
|
13
|
+
#error NitroModules cannot be found! Are you sure you installed NitroModules properly?
|
|
14
|
+
#endif
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
#include <string>
|
|
19
|
+
#include <NitroModules/Promise.hpp>
|
|
20
|
+
#include <functional>
|
|
21
|
+
#include <vector>
|
|
22
|
+
|
|
23
|
+
namespace margelo::nitro::mlxreactnative {
|
|
24
|
+
|
|
25
|
+
using namespace margelo::nitro;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* An abstract base class for `ModelManager`
|
|
29
|
+
* Inherit this class to create instances of `HybridModelManagerSpec` in C++.
|
|
30
|
+
* You must explicitly call `HybridObject`'s constructor yourself, because it is virtual.
|
|
31
|
+
* @example
|
|
32
|
+
* ```cpp
|
|
33
|
+
* class HybridModelManager: public HybridModelManagerSpec {
|
|
34
|
+
* public:
|
|
35
|
+
* HybridModelManager(...): HybridObject(TAG) { ... }
|
|
36
|
+
* // ...
|
|
37
|
+
* };
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
class HybridModelManagerSpec: public virtual HybridObject {
|
|
41
|
+
public:
|
|
42
|
+
// Constructor
|
|
43
|
+
explicit HybridModelManagerSpec(): HybridObject(TAG) { }
|
|
44
|
+
|
|
45
|
+
// Destructor
|
|
46
|
+
~HybridModelManagerSpec() override = default;
|
|
47
|
+
|
|
48
|
+
public:
|
|
49
|
+
// Properties
|
|
50
|
+
virtual bool getDebug() = 0;
|
|
51
|
+
virtual void setDebug(bool debug) = 0;
|
|
52
|
+
|
|
53
|
+
public:
|
|
54
|
+
// Methods
|
|
55
|
+
virtual std::shared_ptr<Promise<std::string>> download(const std::string& modelId, const std::function<void(double /* progress */)>& progressCallback) = 0;
|
|
56
|
+
virtual std::shared_ptr<Promise<bool>> isDownloaded(const std::string& modelId) = 0;
|
|
57
|
+
virtual std::shared_ptr<Promise<std::vector<std::string>>> getDownloadedModels() = 0;
|
|
58
|
+
virtual std::shared_ptr<Promise<void>> deleteModel(const std::string& modelId) = 0;
|
|
59
|
+
virtual std::shared_ptr<Promise<std::string>> getModelPath(const std::string& modelId) = 0;
|
|
60
|
+
|
|
61
|
+
protected:
|
|
62
|
+
// Hybrid Setup
|
|
63
|
+
void loadHybridMethods() override;
|
|
64
|
+
|
|
65
|
+
protected:
|
|
66
|
+
// Tag for logging
|
|
67
|
+
static constexpr auto TAG = "ModelManager";
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
} // namespace margelo::nitro::mlxreactnative
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
///
|
|
2
|
+
/// LLMLoadOptions.hpp
|
|
3
|
+
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
|
|
4
|
+
/// https://github.com/mrousavy/nitro
|
|
5
|
+
/// Copyright © 2025 Marc Rousavy @ Margelo
|
|
6
|
+
///
|
|
7
|
+
|
|
8
|
+
#pragma once
|
|
9
|
+
|
|
10
|
+
#if __has_include(<NitroModules/JSIConverter.hpp>)
|
|
11
|
+
#include <NitroModules/JSIConverter.hpp>
|
|
12
|
+
#else
|
|
13
|
+
#error NitroModules cannot be found! Are you sure you installed NitroModules properly?
|
|
14
|
+
#endif
|
|
15
|
+
#if __has_include(<NitroModules/NitroDefines.hpp>)
|
|
16
|
+
#include <NitroModules/NitroDefines.hpp>
|
|
17
|
+
#else
|
|
18
|
+
#error NitroModules cannot be found! Are you sure you installed NitroModules properly?
|
|
19
|
+
#endif
|
|
20
|
+
#if __has_include(<NitroModules/JSIHelpers.hpp>)
|
|
21
|
+
#include <NitroModules/JSIHelpers.hpp>
|
|
22
|
+
#else
|
|
23
|
+
#error NitroModules cannot be found! Are you sure you installed NitroModules properly?
|
|
24
|
+
#endif
|
|
25
|
+
|
|
26
|
+
// Forward declaration of `LLMMessage` to properly resolve imports.
|
|
27
|
+
namespace margelo::nitro::mlxreactnative { struct LLMMessage; }
|
|
28
|
+
|
|
29
|
+
#include <functional>
|
|
30
|
+
#include <optional>
|
|
31
|
+
#include "LLMMessage.hpp"
|
|
32
|
+
#include <vector>
|
|
33
|
+
|
|
34
|
+
namespace margelo::nitro::mlxreactnative {
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* A struct which can be represented as a JavaScript object (LLMLoadOptions).
|
|
38
|
+
*/
|
|
39
|
+
struct LLMLoadOptions {
|
|
40
|
+
public:
|
|
41
|
+
std::optional<std::function<void(double /* progress */)>> onProgress SWIFT_PRIVATE;
|
|
42
|
+
std::optional<std::vector<LLMMessage>> additionalContext SWIFT_PRIVATE;
|
|
43
|
+
std::optional<bool> manageHistory SWIFT_PRIVATE;
|
|
44
|
+
|
|
45
|
+
public:
|
|
46
|
+
LLMLoadOptions() = default;
|
|
47
|
+
explicit LLMLoadOptions(std::optional<std::function<void(double /* progress */)>> onProgress, std::optional<std::vector<LLMMessage>> additionalContext, std::optional<bool> manageHistory): onProgress(onProgress), additionalContext(additionalContext), manageHistory(manageHistory) {}
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
} // namespace margelo::nitro::mlxreactnative
|
|
51
|
+
|
|
52
|
+
namespace margelo::nitro {
|
|
53
|
+
|
|
54
|
+
// C++ LLMLoadOptions <> JS LLMLoadOptions (object)
|
|
55
|
+
template <>
|
|
56
|
+
struct JSIConverter<margelo::nitro::mlxreactnative::LLMLoadOptions> final {
|
|
57
|
+
static inline margelo::nitro::mlxreactnative::LLMLoadOptions fromJSI(jsi::Runtime& runtime, const jsi::Value& arg) {
|
|
58
|
+
jsi::Object obj = arg.asObject(runtime);
|
|
59
|
+
return margelo::nitro::mlxreactnative::LLMLoadOptions(
|
|
60
|
+
JSIConverter<std::optional<std::function<void(double)>>>::fromJSI(runtime, obj.getProperty(runtime, "onProgress")),
|
|
61
|
+
JSIConverter<std::optional<std::vector<margelo::nitro::mlxreactnative::LLMMessage>>>::fromJSI(runtime, obj.getProperty(runtime, "additionalContext")),
|
|
62
|
+
JSIConverter<std::optional<bool>>::fromJSI(runtime, obj.getProperty(runtime, "manageHistory"))
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
static inline jsi::Value toJSI(jsi::Runtime& runtime, const margelo::nitro::mlxreactnative::LLMLoadOptions& arg) {
|
|
66
|
+
jsi::Object obj(runtime);
|
|
67
|
+
obj.setProperty(runtime, "onProgress", JSIConverter<std::optional<std::function<void(double)>>>::toJSI(runtime, arg.onProgress));
|
|
68
|
+
obj.setProperty(runtime, "additionalContext", JSIConverter<std::optional<std::vector<margelo::nitro::mlxreactnative::LLMMessage>>>::toJSI(runtime, arg.additionalContext));
|
|
69
|
+
obj.setProperty(runtime, "manageHistory", JSIConverter<std::optional<bool>>::toJSI(runtime, arg.manageHistory));
|
|
70
|
+
return obj;
|
|
71
|
+
}
|
|
72
|
+
static inline bool canConvert(jsi::Runtime& runtime, const jsi::Value& value) {
|
|
73
|
+
if (!value.isObject()) {
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
jsi::Object obj = value.getObject(runtime);
|
|
77
|
+
if (!nitro::isPlainObject(runtime, obj)) {
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
if (!JSIConverter<std::optional<std::function<void(double)>>>::canConvert(runtime, obj.getProperty(runtime, "onProgress"))) return false;
|
|
81
|
+
if (!JSIConverter<std::optional<std::vector<margelo::nitro::mlxreactnative::LLMMessage>>>::canConvert(runtime, obj.getProperty(runtime, "additionalContext"))) return false;
|
|
82
|
+
if (!JSIConverter<std::optional<bool>>::canConvert(runtime, obj.getProperty(runtime, "manageHistory"))) return false;
|
|
83
|
+
return true;
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
} // namespace margelo::nitro
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
///
|
|
2
|
+
/// LLMMessage.hpp
|
|
3
|
+
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
|
|
4
|
+
/// https://github.com/mrousavy/nitro
|
|
5
|
+
/// Copyright © 2025 Marc Rousavy @ Margelo
|
|
6
|
+
///
|
|
7
|
+
|
|
8
|
+
#pragma once
|
|
9
|
+
|
|
10
|
+
#if __has_include(<NitroModules/JSIConverter.hpp>)
|
|
11
|
+
#include <NitroModules/JSIConverter.hpp>
|
|
12
|
+
#else
|
|
13
|
+
#error NitroModules cannot be found! Are you sure you installed NitroModules properly?
|
|
14
|
+
#endif
|
|
15
|
+
#if __has_include(<NitroModules/NitroDefines.hpp>)
|
|
16
|
+
#include <NitroModules/NitroDefines.hpp>
|
|
17
|
+
#else
|
|
18
|
+
#error NitroModules cannot be found! Are you sure you installed NitroModules properly?
|
|
19
|
+
#endif
|
|
20
|
+
#if __has_include(<NitroModules/JSIHelpers.hpp>)
|
|
21
|
+
#include <NitroModules/JSIHelpers.hpp>
|
|
22
|
+
#else
|
|
23
|
+
#error NitroModules cannot be found! Are you sure you installed NitroModules properly?
|
|
24
|
+
#endif
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
#include <string>
|
|
29
|
+
|
|
30
|
+
namespace margelo::nitro::mlxreactnative {
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* A struct which can be represented as a JavaScript object (LLMMessage).
|
|
34
|
+
*/
|
|
35
|
+
struct LLMMessage {
|
|
36
|
+
public:
|
|
37
|
+
std::string role SWIFT_PRIVATE;
|
|
38
|
+
std::string content SWIFT_PRIVATE;
|
|
39
|
+
|
|
40
|
+
public:
|
|
41
|
+
LLMMessage() = default;
|
|
42
|
+
explicit LLMMessage(std::string role, std::string content): role(role), content(content) {}
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
} // namespace margelo::nitro::mlxreactnative
|
|
46
|
+
|
|
47
|
+
namespace margelo::nitro {
|
|
48
|
+
|
|
49
|
+
// C++ LLMMessage <> JS LLMMessage (object)
|
|
50
|
+
template <>
|
|
51
|
+
struct JSIConverter<margelo::nitro::mlxreactnative::LLMMessage> final {
|
|
52
|
+
static inline margelo::nitro::mlxreactnative::LLMMessage fromJSI(jsi::Runtime& runtime, const jsi::Value& arg) {
|
|
53
|
+
jsi::Object obj = arg.asObject(runtime);
|
|
54
|
+
return margelo::nitro::mlxreactnative::LLMMessage(
|
|
55
|
+
JSIConverter<std::string>::fromJSI(runtime, obj.getProperty(runtime, "role")),
|
|
56
|
+
JSIConverter<std::string>::fromJSI(runtime, obj.getProperty(runtime, "content"))
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
static inline jsi::Value toJSI(jsi::Runtime& runtime, const margelo::nitro::mlxreactnative::LLMMessage& arg) {
|
|
60
|
+
jsi::Object obj(runtime);
|
|
61
|
+
obj.setProperty(runtime, "role", JSIConverter<std::string>::toJSI(runtime, arg.role));
|
|
62
|
+
obj.setProperty(runtime, "content", JSIConverter<std::string>::toJSI(runtime, arg.content));
|
|
63
|
+
return obj;
|
|
64
|
+
}
|
|
65
|
+
static inline bool canConvert(jsi::Runtime& runtime, const jsi::Value& value) {
|
|
66
|
+
if (!value.isObject()) {
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
jsi::Object obj = value.getObject(runtime);
|
|
70
|
+
if (!nitro::isPlainObject(runtime, obj)) {
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
73
|
+
if (!JSIConverter<std::string>::canConvert(runtime, obj.getProperty(runtime, "role"))) return false;
|
|
74
|
+
if (!JSIConverter<std::string>::canConvert(runtime, obj.getProperty(runtime, "content"))) return false;
|
|
75
|
+
return true;
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
} // namespace margelo::nitro
|