Skip to content

Commit 01b89ce

Browse files
authored
feat: use cmake instead of node-gyp (#37)
1 parent cf1f5f1 commit 01b89ce

File tree

18 files changed

+1670
-1471
lines changed

18 files changed

+1670
-1471
lines changed

.github/workflows/build.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,9 @@ jobs:
181181
182182
const majorNodeVersion = parseInt(nodeVersion.slice("v".length))
183183
184-
const binName = `${process.env.ARTIFACT_NAME}-${arch}-${majorNodeVersion}.node`;
184+
const binName = `${process.env.ARTIFACT_NAME}-${arch}-${majorNodeVersion}`;
185185
await $`node ./dist/cli/cli.js build --arch ${arch} --nodeTarget ${nodeVersion}`;
186-
await $`mv ./llama/build/Release/llama.node ${"./llamaBins/" + binName}`;
186+
await $`mv ./llama/build/Release ${"./llamaBins/" + binName}`;
187187
}
188188
}
189189

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,7 @@ node_modules
1515
/llama/build
1616
/llama/Release
1717
/llama/Debug
18+
/llama/xpack/cache
19+
/llama/xpack/store
20+
/llama/xpack/xpacks
1821
/llamaBins

README.md

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<img alt="node-llama-cpp Logo" src="https://media.githubusercontent.com/media/withcatai/node-llama-cpp/master/assets/logo.roundEdges.png" width="360px" />
33
<h1>Node Llama.cpp</h1>
44
<p>Node.js bindings for llama.cpp</p>
5-
<sub>Pre-built bindings are provided with a fallback to building from source with node-gyp</sub>
5+
<sub>Pre-built bindings are provided with a fallback to building from source with cmake</sub>
66
<p></p>
77
</div>
88

@@ -22,7 +22,7 @@ npm install --save node-llama-cpp
2222

2323
This package comes with pre-built binaries for macOS, Linux and Windows.
2424

25-
If binaries are not available for your platform, it'll fallback to download the latest version of `llama.cpp` and build it from source with `node-gyp`.
25+
If binaries are not available for your platform, it'll fallback to download the latest version of `llama.cpp` and build it from source with `cmake`.
2626
To disable this behavior set the environment variable `NODE_LLAMA_CPP_SKIP_DOWNLOAD` to `true`.
2727

2828
## Documentation
@@ -81,11 +81,13 @@ export class MyCustomChatPromptWrapper extends ChatPromptWrapper {
8181
}
8282

8383
const model = new LlamaModel({
84-
modelPath: path.join(__dirname, "models", "codellama-13b.Q3_K_M.gguf"),
85-
promptWrapper: new MyCustomChatPromptWrapper() // by default, LlamaChatPromptWrapper is used
84+
modelPath: path.join(__dirname, "models", "codellama-13b.Q3_K_M.gguf")
8685
})
8786
const context = new LlamaContext({model});
88-
const session = new LlamaChatSession({context});
87+
const session = new LlamaChatSession({
88+
context,
89+
promptWrapper: new MyCustomChatPromptWrapper() // by default, GeneralChatPromptWrapper is used
90+
});
8991

9092

9193
const q1 = "Hi there, how are you?";
@@ -170,7 +172,7 @@ console.log("AI: " + a1);
170172
console.log(JSON.parse(a1));
171173

172174

173-
const q2 = 'Add another field to the JSON with the key being "author" and the value being "LLama"';
175+
const q2 = 'Add another field to the JSON with the key being "author" and the value being "Llama"';
174176
console.log("User: " + q2);
175177

176178
const a2 = await session.prompt(q2, {maxTokens: context.getContextSize()});
@@ -179,21 +181,19 @@ console.log(JSON.parse(a2));
179181
```
180182

181183
### Metal and CUDA support
182-
To load a version of `llama.cpp` that was compiled to use Metal or CUDA,
183-
you have to build it from source with the `--metal` or `--cuda` flag before running your code that imports `node-llama-cpp`.
184+
**Metal:** `llama.cpp` is built with Metal support by default on macOS.
185+
186+
**CUDA:** To load a version of `llama.cpp` that was compiled to use CUDA,
187+
you have to build it from source with the `--cuda` flag before running your code that imports `node-llama-cpp`.
184188

185189
To do this, run this command inside of your project directory:
186190
```bash
187-
# For Metal support on macOS
188-
npx node-llama-cpp download --metal
189-
190-
# For CUDA support
191191
npx node-llama-cpp download --cuda
192192
```
193193

194-
> In order for `node-llama-cpp` to be able to build `llama.cpp` from source, make sure you have the required dependencies of `node-gyp` installed.
194+
> If `cmake` is not installed on your machine, `node-llama-cpp` will automatically download `cmake` to an internal directory and try to use it to build `llama.cpp` from source.
195195
>
196-
> More info is available [here](https://github.com/nodejs/node-gyp#on-unix) (you don't have to install `node-gyp` itself, just the dependencies).
196+
> If the build fails, make sure you have the required dependencies of `cmake` installed on your machine. More info is available [here](https://github.com/cmake-js/cmake-js#installation:~:text=projectRoot/build%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%5Bstring%5D-,Requirements%3A,-CMake) (you don't have to install `cmake` or `cmake-js`, just the dependencies).
197197
198198
### CLI
199199
```
@@ -202,8 +202,8 @@ Usage: node-llama-cpp <command> [options]
202202
Commands:
203203
node-llama-cpp download Download a release of llama.cpp and compile it
204204
node-llama-cpp build Compile the currently downloaded llama.cpp
205-
node-llama-cpp clear [type] Clear files created by node-llama-cpp
206-
node-llama-cpp chat Chat with a LLama model
205+
node-llama-cpp clear [type] Clear files created by node-llama-cpp [aliases: clean]
206+
node-llama-cpp chat Chat with a Llama model
207207
208208
Options:
209209
-h, --help Show help [boolean]
@@ -226,8 +226,9 @@ Options:
226226
ronment variable [string] [default: "latest"]
227227
-a, --arch The architecture to compile llama.cpp for [string]
228228
-t, --nodeTarget The Node.js version to compile llama.cpp for. Example: v18.0.0 [string]
229-
--metal Compile llama.cpp with Metal support. Can also be set via the NODE_LLAMA_CP
230-
P_METAL environment variable [boolean] [default: false]
229+
--metal Compile llama.cpp with Metal support. Enabled by default on macOS. Can be d
230+
isabled with "--no-metal". Can also be set via the NODE_LLAMA_CPP_METAL env
231+
ironment variable [boolean] [default: true]
231232
--cuda Compile llama.cpp with CUDA support. Can also be set via the NODE_LLAMA_CPP
232233
_CUDA environment variable [boolean] [default: false]
233234
--skipBuild, --sb Skip building llama.cpp after downloading it [boolean] [default: false]
@@ -244,13 +245,17 @@ Options:
244245
-h, --help Show help [boolean]
245246
-a, --arch The architecture to compile llama.cpp for [string]
246247
-t, --nodeTarget The Node.js version to compile llama.cpp for. Example: v18.0.0 [string]
247-
--metal Compile llama.cpp with Metal support. Can also be set via the NODE_LLAMA_CPP_MET
248-
AL environment variable [boolean] [default: false]
248+
--metal Compile llama.cpp with Metal support. Enabled by default on macOS. Can be disabl
249+
ed with "--no-metal". Can also be set via the NODE_LLAMA_CPP_METAL environment v
250+
ariable [boolean] [default: true]
249251
--cuda Compile llama.cpp with CUDA support. Can also be set via the NODE_LLAMA_CPP_CUDA
250252
environment variable [boolean] [default: false]
251253
-v, --version Show version number [boolean]
252254
```
253255

256+
> To set custom cmake options that are supported by `llama.cpp`'s cmake build,
257+
> set an environment variable of the option prefixed with `NODE_LLAMA_CPP_CMAKE_OPTION_`.
258+
254259
#### `clear` command
255260
```
256261
node-llama-cpp clear [type]
@@ -259,18 +264,19 @@ Clear files created by node-llama-cpp
259264
260265
Options:
261266
-h, --help Show help [boolean]
262-
--type Files to clear [string] [choices: "source", "build", "all"] [default: "all"]
267+
--type Files to clear
268+
[string] [choices: "source", "build", "cmake", "all"] [default: "all"]
263269
-v, --version Show version number [boolean]
264270
```
265271

266272
#### `chat` command
267273
```
268274
node-llama-cpp chat
269275
270-
Chat with a LLama model
276+
Chat with a Llama model
271277
272278
Required:
273-
-m, --model LLama model file to use for the chat [string] [required]
279+
-m, --model Llama model file to use for the chat [string] [required]
274280
275281
Optional:
276282
-i, --systemInfo Print llama.cpp system info [boolean] [default: false]

llama/CMakeLists.txt

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
cmake_minimum_required(VERSION 3.12)
2+
3+
project ("llama-addon")
4+
5+
if (MSVC)
6+
add_compile_options(/EHsc)
7+
else()
8+
add_compile_options(-fexceptions)
9+
endif()
10+
11+
add_definitions(-DNAPI_VERSION=7)
12+
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
13+
14+
execute_process(COMMAND node -p "require('node-addon-api').include.slice(1,-1)"
15+
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
16+
OUTPUT_VARIABLE NODE_ADDON_API_DIR
17+
OUTPUT_STRIP_TRAILING_WHITESPACE)
18+
19+
include_directories(${NODE_ADDON_API_DIR} ${CMAKE_JS_INC})
20+
21+
add_subdirectory("llama.cpp")
22+
include_directories("llama.cpp")
23+
include_directories("./llama.cpp/common")
24+
25+
file(GLOB SOURCE_FILES "addon.cpp")
26+
27+
add_library(${PROJECT_NAME} SHARED ${SOURCE_FILES} ${CMAKE_JS_SRC})
28+
set_target_properties(${PROJECT_NAME} PROPERTIES PREFIX "" SUFFIX ".node")
29+
target_link_libraries(${PROJECT_NAME} ${CMAKE_JS_LIB})
30+
target_link_libraries(${PROJECT_NAME} "llama")
31+
target_link_libraries(${PROJECT_NAME} "common")
32+
33+
if(MSVC AND CMAKE_JS_NODELIB_DEF AND CMAKE_JS_NODELIB_TARGET)
34+
# Generate node.lib
35+
execute_process(COMMAND ${CMAKE_AR} /def:${CMAKE_JS_NODELIB_DEF} /out:${CMAKE_JS_NODELIB_TARGET} ${CMAKE_STATIC_LINKER_FLAGS})
36+
endif()

llama/binding.gyp

Lines changed: 0 additions & 27 deletions
This file was deleted.

llama/xpack/package.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"xpack": {
3+
"minimumXpmRequired": "0.16.3",
4+
"dependencies": {},
5+
"devDependencies": {},
6+
"properties": {},
7+
"actions": {},
8+
"buildConfigurations": {}
9+
}
10+
}

0 commit comments

Comments
 (0)