]> git.argeo.org Git - gpl/argeo-tp.git/commitdiff
Introduce de.kherud.llama
authorMathieu Baudier <mbaudier@argeo.org>
Thu, 15 Aug 2024 16:11:32 +0000 (18:11 +0200)
committerMathieu Baudier <mbaudier@argeo.org>
Thu, 15 Aug 2024 16:11:32 +0000 (18:11 +0200)
rebuild/org.argeo.tp.ml/Makefile [new file with mode: 0644]
rebuild/org.argeo.tp.ml/de.kherud.llama/.classpath [new file with mode: 0644]
rebuild/org.argeo.tp.ml/de.kherud.llama/.gitignore [new file with mode: 0644]
rebuild/org.argeo.tp.ml/de.kherud.llama/.project [new file with mode: 0644]
rebuild/org.argeo.tp.ml/de.kherud.llama/.settings/org.eclipse.jdt.core.prefs [new file with mode: 0644]
rebuild/org.argeo.tp.ml/de.kherud.llama/.settings/org.eclipse.pde.core.prefs [new file with mode: 0644]
rebuild/org.argeo.tp.ml/de.kherud.llama/META-INF/.gitignore [new file with mode: 0644]
rebuild/org.argeo.tp.ml/de.kherud.llama/bnd.bnd [new file with mode: 0644]
rebuild/org.argeo.tp.ml/de.kherud.llama/build.properties [new file with mode: 0644]
rebuild/org.argeo.tp.ml/remove-jetbrain-annotations.patch [new file with mode: 0644]
rebuild/org.argeo.tp.ml/src/.gitignore [new file with mode: 0644]

diff --git a/rebuild/org.argeo.tp.ml/Makefile b/rebuild/org.argeo.tp.ml/Makefile
new file mode 100644 (file)
index 0000000..d1268db
--- /dev/null
@@ -0,0 +1,39 @@
+include ../../sdk.mk
+.PHONY: clean all osgi
+
+export NO_SDK_LEGAL := true
+
+A2_CATEGORY = org.argeo.tp.ml
+
+## FIXME - DON'T FORGET TO UPDATE THE VERSION IN THE RELATED bnd.bnd FILE!
+JLLAMA_BRANCH=3.3
+JLLAMA_VERSION=$(JLLAMA_BRANCH).0
+
+all: retrieve-jllama osgi
+       mkdir -p $(SDK_BUILD_BASE)/a2/$(TARGET_ARCH_CATEGORY_PREFIX)/$(A2_CATEGORY)
+       mv $(SDK_BUILD_BASE)/a2/$(A2_CATEGORY)/de.kherud.llama.$(JLLAMA_BRANCH).jar $(SDK_BUILD_BASE)/a2/$(TARGET_ARCH_CATEGORY_PREFIX)/$(A2_CATEGORY)
+
+install:
+       mkdir -p $(A2_NATIVE_INSTALL_TARGET)/$(A2_CATEGORY)
+       $(INSTALL) $(A2_NATIVE_INSTALL_TARGET)/$(A2_CATEGORY) $(SDK_BUILD_BASE)/a2/$(TARGET_ARCH_CATEGORY_PREFIX)/$(A2_CATEGORY)/de.kherud.llama.$(JLLAMA_BRANCH).jar
+       
+uninstall: osgi-uninstall
+       @if [ -d $(A2_NATIVE_INSTALL_TARGET) ]; then find $(A2_NATIVE_INSTALL_TARGET) -empty -type d -delete; fi
+
+retrieve-jllama: SRC_DIR=src/jllama
+retrieve-jllama:
+       rm -rf $(SRC_DIR)
+       mkdir -p $(SRC_DIR)
+       git clone --branch v$(JLLAMA_VERSION) https://github.com/kherud/java-llama.cpp.git $(SRC_DIR)
+       rm -rf $(SRC_DIR)/.git
+       rsync -a --delete --exclude module-info.java $(SRC_DIR)/src/main/java/ de.kherud.llama/src
+       # diff -crB src/jllama/src/main/java de.kherud.llama/src > remove-jetbrain-annotations.patch
+       patch -p0 < remove-jetbrain-annotations.patch 
+
+BUNDLES = \
+de.kherud.llama \
+
+clean:
+       rm -rf $(BUILD_BASE)
+
+include $(SDK_SRC_BASE)/sdk/argeo-build/osgi.mk
diff --git a/rebuild/org.argeo.tp.ml/de.kherud.llama/.classpath b/rebuild/org.argeo.tp.ml/de.kherud.llama/.classpath
new file mode 100644 (file)
index 0000000..81fe078
--- /dev/null
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+       <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-17"/>
+       <classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
+       <classpathentry kind="src" path="src"/>
+       <classpathentry kind="output" path="bin"/>
+</classpath>
diff --git a/rebuild/org.argeo.tp.ml/de.kherud.llama/.gitignore b/rebuild/org.argeo.tp.ml/de.kherud.llama/.gitignore
new file mode 100644 (file)
index 0000000..651109a
--- /dev/null
@@ -0,0 +1,2 @@
+/bin/
+/src/
diff --git a/rebuild/org.argeo.tp.ml/de.kherud.llama/.project b/rebuild/org.argeo.tp.ml/de.kherud.llama/.project
new file mode 100644 (file)
index 0000000..1bc8a5a
--- /dev/null
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+       <name>de.kherud.llama</name>
+       <comment></comment>
+       <projects>
+       </projects>
+       <buildSpec>
+               <buildCommand>
+                       <name>org.eclipse.jdt.core.javabuilder</name>
+                       <arguments>
+                       </arguments>
+               </buildCommand>
+               <buildCommand>
+                       <name>org.eclipse.pde.ManifestBuilder</name>
+                       <arguments>
+                       </arguments>
+               </buildCommand>
+               <buildCommand>
+                       <name>org.eclipse.pde.SchemaBuilder</name>
+                       <arguments>
+                       </arguments>
+               </buildCommand>
+       </buildSpec>
+       <natures>
+               <nature>org.eclipse.pde.PluginNature</nature>
+               <nature>org.eclipse.jdt.core.javanature</nature>
+       </natures>
+</projectDescription>
diff --git a/rebuild/org.argeo.tp.ml/de.kherud.llama/.settings/org.eclipse.jdt.core.prefs b/rebuild/org.argeo.tp.ml/de.kherud.llama/.settings/org.eclipse.jdt.core.prefs
new file mode 100644 (file)
index 0000000..62ef348
--- /dev/null
@@ -0,0 +1,9 @@
+eclipse.preferences.version=1
+org.eclipse.jdt.core.compiler.codegen.targetPlatform=17
+org.eclipse.jdt.core.compiler.compliance=17
+org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
+org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
+org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
+org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
+org.eclipse.jdt.core.compiler.release=enabled
+org.eclipse.jdt.core.compiler.source=17
diff --git a/rebuild/org.argeo.tp.ml/de.kherud.llama/.settings/org.eclipse.pde.core.prefs b/rebuild/org.argeo.tp.ml/de.kherud.llama/.settings/org.eclipse.pde.core.prefs
new file mode 100644 (file)
index 0000000..f29e940
--- /dev/null
@@ -0,0 +1,3 @@
+eclipse.preferences.version=1
+pluginProject.extensions=false
+resolve.requirebundle=false
diff --git a/rebuild/org.argeo.tp.ml/de.kherud.llama/META-INF/.gitignore b/rebuild/org.argeo.tp.ml/de.kherud.llama/META-INF/.gitignore
new file mode 100644 (file)
index 0000000..4854a41
--- /dev/null
@@ -0,0 +1 @@
+/MANIFEST.MF
diff --git a/rebuild/org.argeo.tp.ml/de.kherud.llama/bnd.bnd b/rebuild/org.argeo.tp.ml/de.kherud.llama/bnd.bnd
new file mode 100644 (file)
index 0000000..c9b5922
--- /dev/null
@@ -0,0 +1,4 @@
+
+major: 3
+minor: 3
+micro: 0
diff --git a/rebuild/org.argeo.tp.ml/de.kherud.llama/build.properties b/rebuild/org.argeo.tp.ml/de.kherud.llama/build.properties
new file mode 100644 (file)
index 0000000..34d2e4d
--- /dev/null
@@ -0,0 +1,4 @@
+source.. = src/
+output.. = bin/
+bin.includes = META-INF/,\
+               .
diff --git a/rebuild/org.argeo.tp.ml/remove-jetbrain-annotations.patch b/rebuild/org.argeo.tp.ml/remove-jetbrain-annotations.patch
new file mode 100644 (file)
index 0000000..7a9d81b
--- /dev/null
@@ -0,0 +1,124 @@
+diff -crB src/jllama/src/main/java/de/kherud/llama/LlamaIterable.java de.kherud.llama/src/de/kherud/llama/LlamaIterable.java
+*** src/jllama/src/main/java/de/kherud/llama/LlamaIterable.java        2024-08-15 17:35:49.059197840 +0200
+--- de.kherud.llama/src/de/kherud/llama/LlamaIterable.java     2024-08-15 17:45:43.971905905 +0200
+***************
+*** 1,14 ****
+  package de.kherud.llama;
+  
+- import org.jetbrains.annotations.NotNull;
+- 
+  /**
+   * An iterable used by {@link LlamaModel#generate(InferenceParameters)} that specifically returns a {@link LlamaIterator}.
+   */
+  @FunctionalInterface
+  public interface LlamaIterable extends Iterable<LlamaOutput> {
+  
+-     @NotNull
+      @Override
+      LlamaIterator iterator();
+  
+--- 1,11 ----
+diff -crB src/jllama/src/main/java/de/kherud/llama/LlamaLoader.java de.kherud.llama/src/de/kherud/llama/LlamaLoader.java
+*** src/jllama/src/main/java/de/kherud/llama/LlamaLoader.java  2024-08-15 17:35:49.059197840 +0200
+--- de.kherud.llama/src/de/kherud/llama/LlamaLoader.java       2024-08-15 17:46:01.588281158 +0200
+***************
+*** 28,35 ****
+  import java.util.List;
+  import java.util.stream.Stream;
+  
+- import org.jetbrains.annotations.Nullable;
+- 
+  /**
+   * Set the system properties, de.kherud.llama.lib.path, de.kherud.llama.lib.name, appropriately so that the
+   * library can find *.dll, *.dylib and *.so files, according to the current OS (win, linux, mac).
+--- 28,33 ----
+***************
+*** 182,188 ****
+               }
+       }
+  
+-      @Nullable
+       private static Path extractFile(String sourceDirectory, String fileName, String targetDirectory, boolean addUuid) {
+               String nativeLibraryFilePath = sourceDirectory + "/" + fileName;
+  
+--- 180,185 ----
+diff -crB src/jllama/src/main/java/de/kherud/llama/LlamaModel.java de.kherud.llama/src/de/kherud/llama/LlamaModel.java
+*** src/jllama/src/main/java/de/kherud/llama/LlamaModel.java   2024-08-15 17:35:49.059197840 +0200
+--- de.kherud.llama/src/de/kherud/llama/LlamaModel.java        2024-08-15 17:46:21.072696202 +0200
+***************
+*** 1,7 ****
+  package de.kherud.llama;
+  
+  import de.kherud.llama.args.LogFormat;
+- import org.jetbrains.annotations.Nullable;
+  
+  import java.lang.annotation.Native;
+  import java.nio.charset.StandardCharsets;
+--- 1,6 ----
+***************
+*** 108,114 ****
+        * @param format the log format to use
+        * @param callback a method to call for log messages
+        */
+!      public static native void setLogger(LogFormat format, @Nullable BiConsumer<LogLevel, String> callback);
+  
+       @Override
+       public void close() {
+--- 107,113 ----
+        * @param format the log format to use
+        * @param callback a method to call for log messages
+        */
+!      public static native void setLogger(LogFormat format, BiConsumer<LogLevel, String> callback);
+  
+       @Override
+       public void close() {
+diff -crB src/jllama/src/main/java/de/kherud/llama/LlamaOutput.java de.kherud.llama/src/de/kherud/llama/LlamaOutput.java
+*** src/jllama/src/main/java/de/kherud/llama/LlamaOutput.java  2024-08-15 17:35:49.059197840 +0200
+--- de.kherud.llama/src/de/kherud/llama/LlamaOutput.java       2024-08-15 17:46:42.069143454 +0200
+***************
+*** 1,7 ****
+  package de.kherud.llama;
+  
+- import org.jetbrains.annotations.NotNull;
+- 
+  import java.nio.charset.StandardCharsets;
+  import java.util.Map;
+  
+--- 1,5 ----
+***************
+*** 15,32 ****
+       * The last bit of generated text that is representable as text (i.e., cannot be individual utf-8 multibyte code
+       * points).
+       */
+-     @NotNull
+      public final String text;
+  
+      /**
+       * Note, that you have to configure {@link InferenceParameters#setNProbs(int)} in order for probabilities to be returned.
+       */
+-     @NotNull
+      public final Map<String, Float> probabilities;
+  
+      final boolean stop;
+  
+!     LlamaOutput(byte[] generated, @NotNull Map<String, Float> probabilities, boolean stop) {
+          this.text = new String(generated, StandardCharsets.UTF_8);
+          this.probabilities = probabilities;
+          this.stop = stop;
+--- 13,28 ----
+       * The last bit of generated text that is representable as text (i.e., cannot be individual utf-8 multibyte code
+       * points).
+       */
+      public final String text;
+  
+      /**
+       * Note, that you have to configure {@link InferenceParameters#setNProbs(int)} in order for probabilities to be returned.
+       */
+      public final Map<String, Float> probabilities;
+  
+      final boolean stop;
+  
+!     LlamaOutput(byte[] generated, Map<String, Float> probabilities, boolean stop) {
+          this.text = new String(generated, StandardCharsets.UTF_8);
+          this.probabilities = probabilities;
+          this.stop = stop;
diff --git a/rebuild/org.argeo.tp.ml/src/.gitignore b/rebuild/org.argeo.tp.ml/src/.gitignore
new file mode 100644 (file)
index 0000000..2149a7e
--- /dev/null
@@ -0,0 +1 @@
+/jllama/