Skip to content

Commit fce7416

Browse files
Merge pull request #5 from bernardladenthin/claude/add-getmetadata-method-xVaJm
Claude/add getmetadata method x va jm
2 parents 737ff83 + 3faa6d3 commit fce7416

7 files changed

Lines changed: 92 additions & 17 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
.idea
22
target
33
build
4+
build-android
45
cmake-build-*
56
.DS_Store
67
.directory

CMakeLists.txt

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,27 +33,35 @@ FetchContent_MakeAvailable(llama.cpp)
3333

3434
# find which OS we build for if not set (make sure to run mvn compile first)
3535
if(NOT DEFINED OS_NAME)
36-
find_package(Java REQUIRED)
37-
find_program(JAVA_EXECUTABLE NAMES java)
38-
execute_process(
39-
COMMAND ${JAVA_EXECUTABLE} -cp ${CMAKE_SOURCE_DIR}/target/classes de.kherud.llama.OSInfo --os
40-
OUTPUT_VARIABLE OS_NAME
41-
OUTPUT_STRIP_TRAILING_WHITESPACE
42-
)
36+
if(ANDROID_ABI)
37+
set(OS_NAME "Android")
38+
else()
39+
find_package(Java REQUIRED)
40+
find_program(JAVA_EXECUTABLE NAMES java)
41+
execute_process(
42+
COMMAND ${JAVA_EXECUTABLE} -cp ${CMAKE_SOURCE_DIR}/target/classes de.kherud.llama.OSInfo --os
43+
OUTPUT_VARIABLE OS_NAME
44+
OUTPUT_STRIP_TRAILING_WHITESPACE
45+
)
46+
endif()
4347
endif()
4448
if(NOT OS_NAME)
4549
message(FATAL_ERROR "Could not determine OS name")
4650
endif()
4751

4852
# find which architecture we build for if not set (make sure to run mvn compile first)
4953
if(NOT DEFINED OS_ARCH)
50-
find_package(Java REQUIRED)
51-
find_program(JAVA_EXECUTABLE NAMES java)
52-
execute_process(
53-
COMMAND ${JAVA_EXECUTABLE} -cp ${CMAKE_SOURCE_DIR}/target/classes de.kherud.llama.OSInfo --arch
54-
OUTPUT_VARIABLE OS_ARCH
55-
OUTPUT_STRIP_TRAILING_WHITESPACE
56-
)
54+
if(ANDROID_ABI)
55+
set(OS_ARCH ${ANDROID_ABI})
56+
else()
57+
find_package(Java REQUIRED)
58+
find_program(JAVA_EXECUTABLE NAMES java)
59+
execute_process(
60+
COMMAND ${JAVA_EXECUTABLE} -cp ${CMAKE_SOURCE_DIR}/target/classes de.kherud.llama.OSInfo --arch
61+
OUTPUT_VARIABLE OS_ARCH
62+
OUTPUT_STRIP_TRAILING_WHITESPACE
63+
)
64+
endif()
5765
endif()
5866
if(NOT OS_ARCH)
5967
message(FATAL_ERROR "Could not determine CPU architecture")
@@ -89,7 +97,12 @@ if(NOT DEFINED JNI_INCLUDE_DIRS)
8997
endif()
9098
endif()
9199
if(NOT JNI_INCLUDE_DIRS)
92-
message(FATAL_ERROR "Could not determine JNI include directories")
100+
if(ANDROID_ABI)
101+
find_package(JNI REQUIRED)
102+
set(JNI_INCLUDE_DIRS ${JNI_INCLUDE_DIRS})
103+
else()
104+
message(FATAL_ERROR "Could not determine JNI include directories")
105+
endif()
93106
endif()
94107

95108
add_library(jllama SHARED src/main/cpp/jllama.cpp src/main/cpp/server.hpp src/main/cpp/utils.hpp)

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ Inference of Meta's LLaMA model (and others) in pure C/C++.
1919
> [!NOTE]
2020
> Now with support for Gemma 3
2121
22+
## Download
23+
24+
[![](https://img.shields.io/badge/download-class.jar-blue)](dist/llama-4.2.0.jar)
25+
2226
## Quick Start
2327

2428
Access this library via Maven:

src/main/java/de/kherud/llama/InferenceParameters.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public final class InferenceParameters extends JsonParameters {
4848
private static final String PARAM_SAMPLERS = "samplers";
4949
private static final String PARAM_STREAM = "stream";
5050
private static final String PARAM_USE_CHAT_TEMPLATE = "use_chat_template";
51+
private static final String PARAM_CHAT_TEMPLATE = "chat_template";
5152
private static final String PARAM_USE_JINJA = "use_jinja";
5253
private static final String PARAM_MESSAGES = "messages";
5354

@@ -490,7 +491,12 @@ public InferenceParameters setUseChatTemplate(boolean useChatTemplate) {
490491
parameters.put(PARAM_USE_JINJA, String.valueOf(useChatTemplate));
491492
return this;
492493
}
493-
494+
495+
public InferenceParameters setChatTemplate(String chatTemplate) {
496+
parameters.put(PARAM_CHAT_TEMPLATE, toJsonString(chatTemplate));
497+
return this;
498+
}
499+
494500
/**
495501
* Set the messages for chat-based inference.
496502
* - Allows **only one** system message.

src/main/java/de/kherud/llama/ModelParameters.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -959,6 +959,10 @@ public ModelParameters enableJinja() {
959959
return this;
960960
}
961961

962+
public boolean isDefault(String key) {
963+
return !parameters.containsKey("--" + key);
964+
}
965+
962966
}
963967

964968

src/main/java/de/kherud/llama/OSInfo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ static String resolveArmArchType() {
175175
if (isAndroid()) {
176176
if (armType.startsWith("aarch64")) {
177177
// Use arm64
178-
return "aarch64";
178+
return "arm64-v8a";
179179
}
180180
else {
181181
return "arm";
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package examples;
2+
3+
import de.kherud.llama.InferenceParameters;
4+
import de.kherud.llama.LlamaModel;
5+
import de.kherud.llama.LlamaOutput;
6+
import de.kherud.llama.ModelParameters;
7+
import de.kherud.llama.Pair;
8+
9+
import java.io.BufferedReader;
10+
import java.io.InputStreamReader;
11+
import java.nio.charset.StandardCharsets;
12+
import java.util.ArrayList;
13+
import java.util.List;
14+
15+
import org.junit.Ignore;
16+
17+
// Model file (models/codellama-7b.Q2_K.gguf) is not available in the models directory
18+
@Ignore
19+
public class ChatExample {
20+
21+
public static void main(String... args) throws Exception {
22+
ModelParameters modelParams = new ModelParameters()
23+
.setModel("models/codellama-7b.Q2_K.gguf")
24+
.setGpuLayers(43);
25+
try (LlamaModel model = new LlamaModel(modelParams)) {
26+
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in, StandardCharsets.UTF_8));
27+
List<Pair<String, String>> messages = new ArrayList<>();
28+
String system = "You are a helpful assistant.";
29+
while (true) {
30+
System.out.print("User: ");
31+
String input = reader.readLine();
32+
messages.add(new Pair<>("user", input));
33+
StringBuilder response = new StringBuilder();
34+
InferenceParameters inferParams = new InferenceParameters("")
35+
.setMessages(system, messages)
36+
.setUseChatTemplate(true);
37+
System.out.print("Assistant: ");
38+
for (LlamaOutput output : model.generate(inferParams)) {
39+
System.out.print(output);
40+
response.append(output);
41+
}
42+
System.out.println();
43+
messages.add(new Pair<>("assistant", response.toString()));
44+
}
45+
}
46+
}
47+
}

0 commit comments

Comments
 (0)