├── .idea └── .gitignore ├── README.md ├── LICENSE ├── pom.xml └── src └── main └── java └── com └── hexadevlabs └── gpt4allsample └── Main.java /.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /shelf/ 3 | /workspace.xml 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # gpt4all-java-bindings-sample 2 | A sample project that uses GPT4ALL Java bindings. 3 | 4 | This project depends on the latest released version the bindings package. 5 | 6 | Update Main.java to set baseModelPath to location of your model files. 7 | 8 | You may pass **llama**, **mpt**, **replit** as and argument but make you have the corresponding model file downloaded. 9 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Felix Zaslavskiy 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.hexadevlabs 8 | gpt4all-java-bindings-sample 9 | 1.0 10 | jar 11 | 12 | 13 | 11 14 | 11 15 | 16 | 17 | 18 | 19 | com.hexadevlabs 20 | gpt4all-java-binding 21 | 1.1.5 22 | 23 | 24 | org.slf4j 25 | slf4j-simple 26 | 1.7.36 27 | 28 | 29 | 30 | 31 | 32 | org.apache.maven.plugins 33 | maven-jar-plugin 34 | 3.3.0 35 | 36 | 37 | 38 | true 39 | com.hexadevlabs.gpt4allsample.Main 40 | 41 | 42 | 43 | 44 | 45 | org.apache.maven.plugins 46 | maven-assembly-plugin 47 | 3.6.0 48 | 49 | 50 | jar-with-dependencies 51 | 52 | 53 | 54 | com.hexadevlabs.gpt4allsample.Main 55 | 56 | 57 | 58 | 59 | 60 | make-assembly 61 | package 62 | 63 | single 64 | 65 | 66 | 67 | 68 | 69 | 70 | -------------------------------------------------------------------------------- /src/main/java/com/hexadevlabs/gpt4allsample/Main.java: -------------------------------------------------------------------------------- 1 | package com.hexadevlabs.gpt4allsample; 2 | 3 | import com.hexadevlabs.gpt4all.LLModel; 4 | 5 | import java.nio.file.Path; 6 | import java.util.List; 7 | import java.util.Map; 8 | 9 | public class Main { 10 | 11 | public static void main(String[] args){ 12 | 13 | // DIRECTION: Update the baseModelPath to the location of model files on your system. 14 | 15 | String modelFilePath = "ggml-gpt4all-j-v1.3-groovy.bin"; 16 | 17 | String osName = System.getProperty("os.name").toLowerCase(); 18 | boolean isWindows = osName.startsWith("windows"); 19 | boolean isMac = osName.startsWith("mac os x"); 20 | boolean isLinux = osName.startsWith("linux"); 21 | 22 | String baseModelPath = null; 23 | if(isWindows){ 24 | baseModelPath ="C:\\Users\\felix\\AppData\\Local\\nomic.ai\\GPT4All\\"; 25 | } else if(isLinux){ 26 | baseModelPath = "/mnt/c/Users/felix/AppData/Local/nomic.ai/GPT4All/"; 27 | } else if(isMac) { 28 | baseModelPath = "/Users/fzaslavs/Library/Application Support/nomic.ai/GPT4All/"; 29 | } 30 | 31 | 32 | // Optionally in case override to location of shared libraries is necessary 33 | //LLModel.LIBRARY_SEARCH_PATH = "C:\\Users\\felix\\gpt4all\\lib\\"; 34 | 35 | if(args.length>0){ 36 | switch (args[0]) { 37 | case "mpt": 38 | modelFilePath = "ggml-mpt-7b-instruct.bin"; 39 | break; 40 | case "llama": 41 | modelFilePath = "ggml-vicuna-7b-1.1-q4_2.bin"; 42 | break; 43 | case "replit": 44 | modelFilePath = "ggml-replit-code-v1-3b.bin"; 45 | break; 46 | case "falcon": 47 | modelFilePath = "ggml-model-gpt4all-falcon-q4_0.bin"; 48 | break; 49 | case "orca": 50 | modelFilePath= "orca-mini-3b.ggmlv3.q4_0.bin"; 51 | break; 52 | } 53 | } 54 | 55 | // Debut output format. In case you need it. 56 | // LLModel.OUTPUT_DEBUG=true; 57 | 58 | Path modelPath = Path.of(baseModelPath + modelFilePath); 59 | 60 | try ( LLModel model = new LLModel(modelPath) ){ 61 | 62 | LLModel.GenerationConfig config = LLModel.config() 63 | .withNPredict(4096).build(); 64 | 65 | System.out.println("Using ChatCompletion api:\n"); 66 | // Using Chat Completion Template 67 | model.chatCompletion( 68 | List.of(Map.of("role", "system", "content", "You are a helpful assistant"), 69 | Map.of("role", "user", "content", "Add 2+2")), config, true, true); 70 | 71 | System.out.println(); 72 | System.out.println(); 73 | System.out.println("Using Generate method:\n"); 74 | 75 | // Using the Generate method. 76 | // Note that you will have to provide the full prompt here yourself. 77 | // Print to stdout. 78 | String prompt = "You are a helpful assistant. \n" + 79 | "Human: What is result of adding 2 and 2?\n" + 80 | "Assistant: "; 81 | System.out.println(prompt); 82 | model.generate(prompt, config, true); 83 | 84 | } catch (Exception e) { 85 | throw new RuntimeException(e); 86 | } 87 | 88 | } 89 | 90 | } 91 | --------------------------------------------------------------------------------