react-native-nitro-mlx 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/MLXReactNative.podspec +42 -0
- package/ios/Bridge.h +8 -0
- package/ios/Sources/MLXReactNative.h +16 -0
- package/lib/module/index.js +6 -0
- package/lib/module/index.js.map +1 -0
- package/lib/module/llm.js +105 -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 +69 -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 +61 -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 +312 -0
- package/nitrogen/generated/ios/MLXReactNative-Swift-Cxx-Umbrella.hpp +55 -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 +132 -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 +64 -0
- package/nitrogen/generated/ios/swift/HybridLLMSpec_cxx.swift +250 -0
- package/nitrogen/generated/ios/swift/HybridModelManagerSpec.swift +60 -0
- package/nitrogen/generated/ios/swift/HybridModelManagerSpec_cxx.swift +234 -0
- package/nitrogen/generated/shared/c++/GenerationStats.hpp +87 -0
- package/nitrogen/generated/shared/c++/HybridLLMSpec.cpp +32 -0
- package/nitrogen/generated/shared/c++/HybridLLMSpec.hpp +76 -0
- package/nitrogen/generated/shared/c++/HybridModelManagerSpec.cpp +27 -0
- package/nitrogen/generated/shared/c++/HybridModelManagerSpec.hpp +70 -0
- package/package.json +96 -0
- package/src/index.ts +6 -0
- package/src/llm.ts +116 -0
- package/src/modelManager.ts +88 -0
- package/src/models.ts +45 -0
- package/src/specs/LLM.nitro.ts +66 -0
- package/src/specs/ModelManager.nitro.ts +44 -0
|
@@ -0,0 +1,76 @@
|
|
|
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 `GenerationStats` to properly resolve imports.
|
|
17
|
+
namespace margelo::nitro::mlxreactnative { struct GenerationStats; }
|
|
18
|
+
|
|
19
|
+
#include <string>
|
|
20
|
+
#include <NitroModules/Promise.hpp>
|
|
21
|
+
#include <functional>
|
|
22
|
+
#include "GenerationStats.hpp"
|
|
23
|
+
|
|
24
|
+
namespace margelo::nitro::mlxreactnative {
|
|
25
|
+
|
|
26
|
+
using namespace margelo::nitro;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* An abstract base class for `LLM`
|
|
30
|
+
* Inherit this class to create instances of `HybridLLMSpec` in C++.
|
|
31
|
+
* You must explicitly call `HybridObject`'s constructor yourself, because it is virtual.
|
|
32
|
+
* @example
|
|
33
|
+
* ```cpp
|
|
34
|
+
* class HybridLLM: public HybridLLMSpec {
|
|
35
|
+
* public:
|
|
36
|
+
* HybridLLM(...): HybridObject(TAG) { ... }
|
|
37
|
+
* // ...
|
|
38
|
+
* };
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
class HybridLLMSpec: public virtual HybridObject {
|
|
42
|
+
public:
|
|
43
|
+
// Constructor
|
|
44
|
+
explicit HybridLLMSpec(): HybridObject(TAG) { }
|
|
45
|
+
|
|
46
|
+
// Destructor
|
|
47
|
+
~HybridLLMSpec() override = default;
|
|
48
|
+
|
|
49
|
+
public:
|
|
50
|
+
// Properties
|
|
51
|
+
virtual bool getIsLoaded() = 0;
|
|
52
|
+
virtual bool getIsGenerating() = 0;
|
|
53
|
+
virtual std::string getModelId() = 0;
|
|
54
|
+
virtual bool getDebug() = 0;
|
|
55
|
+
virtual void setDebug(bool debug) = 0;
|
|
56
|
+
virtual std::string getSystemPrompt() = 0;
|
|
57
|
+
virtual void setSystemPrompt(const std::string& systemPrompt) = 0;
|
|
58
|
+
|
|
59
|
+
public:
|
|
60
|
+
// Methods
|
|
61
|
+
virtual std::shared_ptr<Promise<void>> load(const std::string& modelId, const std::function<void(double /* progress */)>& onProgress) = 0;
|
|
62
|
+
virtual std::shared_ptr<Promise<std::string>> generate(const std::string& prompt) = 0;
|
|
63
|
+
virtual std::shared_ptr<Promise<std::string>> stream(const std::string& prompt, const std::function<void(const std::string& /* token */)>& onToken) = 0;
|
|
64
|
+
virtual void stop() = 0;
|
|
65
|
+
virtual GenerationStats getLastGenerationStats() = 0;
|
|
66
|
+
|
|
67
|
+
protected:
|
|
68
|
+
// Hybrid Setup
|
|
69
|
+
void loadHybridMethods() override;
|
|
70
|
+
|
|
71
|
+
protected:
|
|
72
|
+
// Tag for logging
|
|
73
|
+
static constexpr auto TAG = "LLM";
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
} // 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
|
package/package.json
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "react-native-nitro-mlx",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Nitro module package",
|
|
5
|
+
"main": "./lib/module/index.js",
|
|
6
|
+
"module": "./lib/module/index.js",
|
|
7
|
+
"types": "./lib/typescript/src/index.d.ts",
|
|
8
|
+
"react-native": "src/index",
|
|
9
|
+
"source": "src/index",
|
|
10
|
+
"scripts": {
|
|
11
|
+
"build": "rm -rf lib && bun typecheck && bob build",
|
|
12
|
+
"typecheck": "tsc --noEmit",
|
|
13
|
+
"clean": "rm -rf android/build node_modules/**/android/build lib android/.cxx node_modules/**/android/.cxx",
|
|
14
|
+
"release": "release-it",
|
|
15
|
+
"specs": "bun typecheck && nitrogen --logLevel=\\\"debug\\\" && bun run build",
|
|
16
|
+
"specs:pod": "bun specs && bun --cwd ../example pod"
|
|
17
|
+
},
|
|
18
|
+
"keywords": [
|
|
19
|
+
"react-native",
|
|
20
|
+
"nitro",
|
|
21
|
+
"nitro-modile",
|
|
22
|
+
"expo",
|
|
23
|
+
"llm",
|
|
24
|
+
"mlx",
|
|
25
|
+
"mlx-swift",
|
|
26
|
+
"apple",
|
|
27
|
+
"react-native-nitro-mlx",
|
|
28
|
+
"ai"
|
|
29
|
+
],
|
|
30
|
+
"files": [
|
|
31
|
+
"src",
|
|
32
|
+
"react-native.config.js",
|
|
33
|
+
"lib",
|
|
34
|
+
"nitrogen",
|
|
35
|
+
"cpp",
|
|
36
|
+
"android/build.gradle",
|
|
37
|
+
"android/gradle.properties",
|
|
38
|
+
"android/CMakeLists.txt",
|
|
39
|
+
"android/src",
|
|
40
|
+
"ios/**/*.h",
|
|
41
|
+
"ios/**/*.m",
|
|
42
|
+
"ios/**/*.mm",
|
|
43
|
+
"ios/**/*.cpp",
|
|
44
|
+
"ios/specs/**/*.swift",
|
|
45
|
+
"app.plugin.js",
|
|
46
|
+
"*.podspec",
|
|
47
|
+
"README.md"
|
|
48
|
+
],
|
|
49
|
+
"repository": {
|
|
50
|
+
"type": "git",
|
|
51
|
+
"url": "git+https://github.com/henrypaulino/react-native-nitro-mlx.git"
|
|
52
|
+
},
|
|
53
|
+
"author": "Henry Paulino",
|
|
54
|
+
"license": "MIT",
|
|
55
|
+
"bugs": "https://github.com/henrypaulino/react-native-nitro-mlx/issues",
|
|
56
|
+
"homepage": "https://github.com/henrypaulino/react-native-nitro-mlx#readme",
|
|
57
|
+
"publishConfig": {
|
|
58
|
+
"registry": "https://registry.npmjs.org/"
|
|
59
|
+
},
|
|
60
|
+
"devDependencies": {
|
|
61
|
+
"@expo/config-plugins": "^9.0.10",
|
|
62
|
+
"nitrogen": "^0.31.10",
|
|
63
|
+
"react-native-builder-bob": "^0.40.13"
|
|
64
|
+
},
|
|
65
|
+
"peerDependencies": {
|
|
66
|
+
"react": "*",
|
|
67
|
+
"react-native": "*",
|
|
68
|
+
"react-native-nitro-modules": "*"
|
|
69
|
+
},
|
|
70
|
+
"release-it": {
|
|
71
|
+
"npm": {
|
|
72
|
+
"publish": true
|
|
73
|
+
},
|
|
74
|
+
"git": false,
|
|
75
|
+
"github": {
|
|
76
|
+
"release": false
|
|
77
|
+
},
|
|
78
|
+
"hooks": {
|
|
79
|
+
"before:init": "bun typecheck",
|
|
80
|
+
"after:bump": "bun build"
|
|
81
|
+
}
|
|
82
|
+
},
|
|
83
|
+
"react-native-builder-bob": {
|
|
84
|
+
"source": "src",
|
|
85
|
+
"output": "lib",
|
|
86
|
+
"targets": [
|
|
87
|
+
[
|
|
88
|
+
"module",
|
|
89
|
+
{
|
|
90
|
+
"esm": true
|
|
91
|
+
}
|
|
92
|
+
],
|
|
93
|
+
"typescript"
|
|
94
|
+
]
|
|
95
|
+
}
|
|
96
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { LLM } from './llm'
|
|
2
|
+
export { ModelManager } from './modelManager'
|
|
3
|
+
export { MLXModel } from './models'
|
|
4
|
+
|
|
5
|
+
export type { GenerationStats, LLM as LLMSpec } from './specs/LLM.nitro'
|
|
6
|
+
export type { ModelManager as ModelManagerSpec } from './specs/ModelManager.nitro'
|
package/src/llm.ts
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { NitroModules } from 'react-native-nitro-modules'
|
|
2
|
+
import type { GenerationStats, LLM as LLMSpec } from './specs/LLM.nitro'
|
|
3
|
+
|
|
4
|
+
let instance: LLMSpec | null = null
|
|
5
|
+
|
|
6
|
+
function getInstance(): LLMSpec {
|
|
7
|
+
if (!instance) {
|
|
8
|
+
instance = NitroModules.createHybridObject<LLMSpec>('LLM')
|
|
9
|
+
}
|
|
10
|
+
return instance
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* LLM text generation using MLX on Apple Silicon.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts
|
|
18
|
+
* import { LLM } from 'react-native-nitro-mlx'
|
|
19
|
+
*
|
|
20
|
+
* // Load a model
|
|
21
|
+
* await LLM.load('mlx-community/Qwen3-0.6B-4bit', progress => {
|
|
22
|
+
* console.log(`Loading: ${(progress * 100).toFixed(0)}%`)
|
|
23
|
+
* })
|
|
24
|
+
*
|
|
25
|
+
* // Stream a response
|
|
26
|
+
* await LLM.stream('Hello!', token => {
|
|
27
|
+
* process.stdout.write(token)
|
|
28
|
+
* })
|
|
29
|
+
*
|
|
30
|
+
* // Get generation stats
|
|
31
|
+
* const stats = LLM.getLastGenerationStats()
|
|
32
|
+
* console.log(`${stats.tokensPerSecond} tokens/sec`)
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
export const LLM = {
|
|
36
|
+
/**
|
|
37
|
+
* Load a model into memory. Downloads the model from HuggingFace if not already cached.
|
|
38
|
+
* @param modelId - HuggingFace model ID (e.g., 'mlx-community/Qwen3-0.6B-4bit')
|
|
39
|
+
* @param onProgress - Callback invoked with loading progress (0-1)
|
|
40
|
+
*/
|
|
41
|
+
load(modelId: string, onProgress: (progress: number) => void): Promise<void> {
|
|
42
|
+
return getInstance().load(modelId, onProgress)
|
|
43
|
+
},
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Generate a complete response for a prompt. Blocks until generation is complete.
|
|
47
|
+
* For streaming responses, use `stream()` instead.
|
|
48
|
+
* @param prompt - The input text to generate a response for
|
|
49
|
+
* @returns The complete generated text
|
|
50
|
+
*/
|
|
51
|
+
generate(prompt: string): Promise<string> {
|
|
52
|
+
return getInstance().generate(prompt)
|
|
53
|
+
},
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Stream a response token by token.
|
|
57
|
+
* @param prompt - The input text to generate a response for
|
|
58
|
+
* @param onToken - Callback invoked for each generated token
|
|
59
|
+
* @returns The complete generated text
|
|
60
|
+
*/
|
|
61
|
+
stream(prompt: string, onToken: (token: string) => void): Promise<string> {
|
|
62
|
+
return getInstance().stream(prompt, onToken)
|
|
63
|
+
},
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Stop the current generation. Safe to call even if not generating.
|
|
67
|
+
*/
|
|
68
|
+
stop(): void {
|
|
69
|
+
getInstance().stop()
|
|
70
|
+
},
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Get statistics from the last generation.
|
|
74
|
+
* @returns Statistics including token count, tokens/sec, TTFT, and total time
|
|
75
|
+
*/
|
|
76
|
+
getLastGenerationStats(): GenerationStats {
|
|
77
|
+
return getInstance().getLastGenerationStats()
|
|
78
|
+
},
|
|
79
|
+
|
|
80
|
+
/** Whether a model is currently loaded and ready for generation */
|
|
81
|
+
get isLoaded(): boolean {
|
|
82
|
+
return getInstance().isLoaded
|
|
83
|
+
},
|
|
84
|
+
|
|
85
|
+
/** Whether text is currently being generated */
|
|
86
|
+
get isGenerating(): boolean {
|
|
87
|
+
return getInstance().isGenerating
|
|
88
|
+
},
|
|
89
|
+
|
|
90
|
+
/** The ID of the currently loaded model, or empty string if none */
|
|
91
|
+
get modelId(): string {
|
|
92
|
+
return getInstance().modelId
|
|
93
|
+
},
|
|
94
|
+
|
|
95
|
+
/** Enable debug logging to console */
|
|
96
|
+
get debug(): boolean {
|
|
97
|
+
return getInstance().debug
|
|
98
|
+
},
|
|
99
|
+
|
|
100
|
+
set debug(value: boolean) {
|
|
101
|
+
getInstance().debug = value
|
|
102
|
+
},
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* System prompt used when loading the model.
|
|
106
|
+
* Set this before calling `load()`. Changes require reloading the model.
|
|
107
|
+
* @default "You are a helpful assistant."
|
|
108
|
+
*/
|
|
109
|
+
get systemPrompt(): string {
|
|
110
|
+
return getInstance().systemPrompt
|
|
111
|
+
},
|
|
112
|
+
|
|
113
|
+
set systemPrompt(value: string) {
|
|
114
|
+
getInstance().systemPrompt = value
|
|
115
|
+
},
|
|
116
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { NitroModules } from 'react-native-nitro-modules'
|
|
2
|
+
import type { ModelManager as ModelManagerSpec } from './specs/ModelManager.nitro'
|
|
3
|
+
|
|
4
|
+
let instance: ModelManagerSpec | null = null
|
|
5
|
+
|
|
6
|
+
function getInstance(): ModelManagerSpec {
|
|
7
|
+
if (!instance) {
|
|
8
|
+
instance = NitroModules.createHybridObject<ModelManagerSpec>('ModelManager')
|
|
9
|
+
}
|
|
10
|
+
return instance
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Manage MLX model downloads from HuggingFace.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts
|
|
18
|
+
* import { ModelManager } from 'react-native-nitro-mlx'
|
|
19
|
+
*
|
|
20
|
+
* // Download a model
|
|
21
|
+
* await ModelManager.download('mlx-community/Qwen3-0.6B-4bit', progress => {
|
|
22
|
+
* console.log(`Downloading: ${(progress * 100).toFixed(0)}%`)
|
|
23
|
+
* })
|
|
24
|
+
*
|
|
25
|
+
* // Check if downloaded
|
|
26
|
+
* const isReady = await ModelManager.isDownloaded('mlx-community/Qwen3-0.6B-4bit')
|
|
27
|
+
*
|
|
28
|
+
* // List all downloaded models
|
|
29
|
+
* const models = await ModelManager.getDownloadedModels()
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
export const ModelManager = {
|
|
33
|
+
/**
|
|
34
|
+
* Download a model from HuggingFace.
|
|
35
|
+
* @param modelId - HuggingFace model ID (e.g., 'mlx-community/Qwen3-0.6B-4bit')
|
|
36
|
+
* @param progressCallback - Callback invoked with download progress (0-1)
|
|
37
|
+
* @returns Path to the downloaded model directory
|
|
38
|
+
*/
|
|
39
|
+
download(
|
|
40
|
+
modelId: string,
|
|
41
|
+
progressCallback: (progress: number) => void,
|
|
42
|
+
): Promise<string> {
|
|
43
|
+
return getInstance().download(modelId, progressCallback)
|
|
44
|
+
},
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Check if a model is already downloaded.
|
|
48
|
+
* @param modelId - HuggingFace model ID
|
|
49
|
+
* @returns True if the model is fully downloaded
|
|
50
|
+
*/
|
|
51
|
+
isDownloaded(modelId: string): Promise<boolean> {
|
|
52
|
+
return getInstance().isDownloaded(modelId)
|
|
53
|
+
},
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Get a list of all downloaded model IDs.
|
|
57
|
+
* @returns Array of model IDs that are available locally
|
|
58
|
+
*/
|
|
59
|
+
getDownloadedModels(): Promise<string[]> {
|
|
60
|
+
return getInstance().getDownloadedModels()
|
|
61
|
+
},
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Delete a downloaded model to free up disk space.
|
|
65
|
+
* @param modelId - HuggingFace model ID
|
|
66
|
+
*/
|
|
67
|
+
deleteModel(modelId: string): Promise<void> {
|
|
68
|
+
return getInstance().deleteModel(modelId)
|
|
69
|
+
},
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Get the local filesystem path for a downloaded model.
|
|
73
|
+
* @param modelId - HuggingFace model ID
|
|
74
|
+
* @returns Absolute path to the model directory
|
|
75
|
+
*/
|
|
76
|
+
getModelPath(modelId: string): Promise<string> {
|
|
77
|
+
return getInstance().getModelPath(modelId)
|
|
78
|
+
},
|
|
79
|
+
|
|
80
|
+
/** Enable debug logging to console */
|
|
81
|
+
get debug(): boolean {
|
|
82
|
+
return getInstance().debug
|
|
83
|
+
},
|
|
84
|
+
|
|
85
|
+
set debug(value: boolean) {
|
|
86
|
+
getInstance().debug = value
|
|
87
|
+
},
|
|
88
|
+
}
|
package/src/models.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
export enum MLXModel {
|
|
2
|
+
// Llama 3.2 (Meta) - 1B and 3B variants
|
|
3
|
+
Llama_3_2_1B_Instruct_4bit = 'mlx-community/Llama-3.2-1B-Instruct-4bit',
|
|
4
|
+
Llama_3_2_1B_Instruct_8bit = 'mlx-community/Llama-3.2-1B-Instruct-8bit',
|
|
5
|
+
Llama_3_2_3B_Instruct_4bit = 'mlx-community/Llama-3.2-3B-Instruct-4bit',
|
|
6
|
+
Llama_3_2_3B_Instruct_8bit = 'mlx-community/Llama-3.2-3B-Instruct-8bit',
|
|
7
|
+
|
|
8
|
+
// Qwen 2.5 (Alibaba) - 0.5B, 1.5B, 3B variants
|
|
9
|
+
Qwen2_5_0_5B_Instruct_4bit = 'mlx-community/Qwen2.5-0.5B-Instruct-4bit',
|
|
10
|
+
Qwen2_5_0_5B_Instruct_8bit = 'mlx-community/Qwen2.5-0.5B-Instruct-8bit',
|
|
11
|
+
Qwen2_5_1_5B_Instruct_4bit = 'mlx-community/Qwen2.5-1.5B-Instruct-4bit',
|
|
12
|
+
Qwen2_5_1_5B_Instruct_8bit = 'mlx-community/Qwen2.5-1.5B-Instruct-8bit',
|
|
13
|
+
Qwen2_5_3B_Instruct_4bit = 'mlx-community/Qwen2.5-3B-Instruct-4bit',
|
|
14
|
+
Qwen2_5_3B_Instruct_8bit = 'mlx-community/Qwen2.5-3B-Instruct-8bit',
|
|
15
|
+
|
|
16
|
+
// Qwen 3 - 1.7B variant
|
|
17
|
+
Qwen3_1_7B_4bit = 'mlx-community/Qwen3-1.7B-4bit',
|
|
18
|
+
Qwen3_1_7B_8bit = 'mlx-community/Qwen3-1.7B-8bit',
|
|
19
|
+
|
|
20
|
+
// Gemma 3 (Google) - 1B variant
|
|
21
|
+
Gemma_3_1B_IT_4bit = 'mlx-community/gemma-3-1b-it-4bit',
|
|
22
|
+
Gemma_3_1B_IT_8bit = 'mlx-community/gemma-3-1b-it-8bit',
|
|
23
|
+
|
|
24
|
+
// Phi 3.5 Mini (Microsoft) - ~3.8B but runs well on mobile
|
|
25
|
+
Phi_3_5_Mini_Instruct_4bit = 'mlx-community/Phi-3.5-mini-instruct-4bit',
|
|
26
|
+
Phi_3_5_Mini_Instruct_8bit = 'mlx-community/Phi-3.5-mini-instruct-8bit',
|
|
27
|
+
|
|
28
|
+
// Phi 4 Mini (Microsoft)
|
|
29
|
+
Phi_4_Mini_Instruct_4bit = 'mlx-community/Phi-4-mini-instruct-4bit',
|
|
30
|
+
Phi_4_Mini_Instruct_8bit = 'mlx-community/Phi-4-mini-instruct-8bit',
|
|
31
|
+
|
|
32
|
+
// SmolLM (HuggingFace) - 1.7B
|
|
33
|
+
SmolLM_1_7B_Instruct_4bit = 'mlx-community/SmolLM-1.7B-Instruct-4bit',
|
|
34
|
+
SmolLM_1_7B_Instruct_8bit = 'mlx-community/SmolLM-1.7B-Instruct-8bit',
|
|
35
|
+
|
|
36
|
+
// SmolLM2 (HuggingFace) - 1.7B
|
|
37
|
+
SmolLM2_1_7B_Instruct_4bit = 'mlx-community/SmolLM2-1.7B-Instruct-4bit',
|
|
38
|
+
SmolLM2_1_7B_Instruct_8bit = 'mlx-community/SmolLM2-1.7B-Instruct-8bit',
|
|
39
|
+
|
|
40
|
+
// OpenELM (Apple) - 1.1B and 3B
|
|
41
|
+
OpenELM_1_1B_4bit = 'mlx-community/OpenELM-1_1B-4bit',
|
|
42
|
+
OpenELM_1_1B_8bit = 'mlx-community/OpenELM-1_1B-8bit',
|
|
43
|
+
OpenELM_3B_4bit = 'mlx-community/OpenELM-3B-4bit',
|
|
44
|
+
OpenELM_3B_8bit = 'mlx-community/OpenELM-3B-8bit',
|
|
45
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import type { HybridObject } from 'react-native-nitro-modules'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Statistics from the last text generation.
|
|
5
|
+
*/
|
|
6
|
+
export interface GenerationStats {
|
|
7
|
+
/** Total number of tokens generated */
|
|
8
|
+
tokenCount: number
|
|
9
|
+
/** Generation speed in tokens per second */
|
|
10
|
+
tokensPerSecond: number
|
|
11
|
+
/** Time in milliseconds until the first token was generated */
|
|
12
|
+
timeToFirstToken: number
|
|
13
|
+
/** Total generation time in milliseconds */
|
|
14
|
+
totalTime: number
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Low-level LLM interface for text generation using MLX.
|
|
19
|
+
* @internal Use the `LLM` export from `react-native-nitro-mlx` instead.
|
|
20
|
+
*/
|
|
21
|
+
export interface LLM extends HybridObject<{ ios: 'swift' }> {
|
|
22
|
+
/**
|
|
23
|
+
* Load a model into memory. Downloads from HuggingFace if not already cached.
|
|
24
|
+
* @param modelId - HuggingFace model ID (e.g., 'mlx-community/Qwen3-0.6B-4bit')
|
|
25
|
+
* @param onProgress - Callback invoked with loading progress (0-1)
|
|
26
|
+
*/
|
|
27
|
+
load(modelId: string, onProgress: (progress: number) => void): Promise<void>
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Generate a complete response for a prompt.
|
|
31
|
+
* @param prompt - The input text to generate a response for
|
|
32
|
+
* @returns The generated text
|
|
33
|
+
*/
|
|
34
|
+
generate(prompt: string): Promise<string>
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Stream a response token by token.
|
|
38
|
+
* @param prompt - The input text to generate a response for
|
|
39
|
+
* @param onToken - Callback invoked for each generated token
|
|
40
|
+
* @returns The complete generated text
|
|
41
|
+
*/
|
|
42
|
+
stream(prompt: string, onToken: (token: string) => void): Promise<string>
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Stop the current generation.
|
|
46
|
+
*/
|
|
47
|
+
stop(): void
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Get statistics from the last generation.
|
|
51
|
+
* @returns Statistics including token count, speed, and timing
|
|
52
|
+
*/
|
|
53
|
+
getLastGenerationStats(): GenerationStats
|
|
54
|
+
|
|
55
|
+
/** Whether a model is currently loaded */
|
|
56
|
+
readonly isLoaded: boolean
|
|
57
|
+
/** Whether text is currently being generated */
|
|
58
|
+
readonly isGenerating: boolean
|
|
59
|
+
/** The ID of the currently loaded model */
|
|
60
|
+
readonly modelId: string
|
|
61
|
+
|
|
62
|
+
/** Enable debug logging */
|
|
63
|
+
debug: boolean
|
|
64
|
+
/** System prompt used when loading the model */
|
|
65
|
+
systemPrompt: string
|
|
66
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { HybridObject } from 'react-native-nitro-modules'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Low-level interface for managing MLX model downloads.
|
|
5
|
+
* @internal Use the `ModelManager` export from `react-native-nitro-mlx` instead.
|
|
6
|
+
*/
|
|
7
|
+
export interface ModelManager extends HybridObject<{ ios: 'swift' }> {
|
|
8
|
+
/**
|
|
9
|
+
* Download a model from HuggingFace.
|
|
10
|
+
* @param modelId - HuggingFace model ID (e.g., 'mlx-community/Qwen3-0.6B-4bit')
|
|
11
|
+
* @param progressCallback - Callback invoked with download progress (0-1)
|
|
12
|
+
* @returns Path to the downloaded model directory
|
|
13
|
+
*/
|
|
14
|
+
download(modelId: string, progressCallback: (progress: number) => void): Promise<string>
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Check if a model is already downloaded.
|
|
18
|
+
* @param modelId - HuggingFace model ID
|
|
19
|
+
* @returns True if the model is downloaded
|
|
20
|
+
*/
|
|
21
|
+
isDownloaded(modelId: string): Promise<boolean>
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Get a list of all downloaded model IDs.
|
|
25
|
+
* @returns Array of downloaded model IDs
|
|
26
|
+
*/
|
|
27
|
+
getDownloadedModels(): Promise<string[]>
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Delete a downloaded model.
|
|
31
|
+
* @param modelId - HuggingFace model ID
|
|
32
|
+
*/
|
|
33
|
+
deleteModel(modelId: string): Promise<void>
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Get the local filesystem path for a downloaded model.
|
|
37
|
+
* @param modelId - HuggingFace model ID
|
|
38
|
+
* @returns Path to the model directory
|
|
39
|
+
*/
|
|
40
|
+
getModelPath(modelId: string): Promise<string>
|
|
41
|
+
|
|
42
|
+
/** Enable debug logging */
|
|
43
|
+
debug: boolean
|
|
44
|
+
}
|