All Downloads are FREE. Search and download functionalities are using the official Maven repository.

anlavn.ai.YourGPT Maven / Gradle / Ivy

The newest version!
package anlavn.ai;
// Make By Bình An || AnLaVN || KatoVN

import anlavn.file.Log;
import anlavn.file.Raw;
import anlavn.file.Zip;
import anlavn.net.DocNet;
import anlavn.net.License;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**The YourGPT class supports to LLM (Large Language Model) inference with LLaMA.
This class provides methods to load the YourGPT module, start the server, interact with the model... * @author AnLaVN - https://github.com/AnLaVN */ public final class YourGPT implements AutoCloseable { private static Process YGPTProcess = null; private static String MODEL_FILE = null; private static final String LLAMA_PATH = "YourGPT_LLaMA", MODEL_PATH = "YourGPT_Model", MODEL_DEFAULT = "https://huggingface.co/TheBloke/Mistral-7B-OpenOrca-GGUF/resolve/main/mistral-7b-openorca.Q4_K_M.gguf"; /**Configurable LLaMA parameters for the YourGPT server.
These parameters must be adjusted before starting the server.
* Read more about available parameters at: https://github.com/ggerganov/llama.cpp/blob/master/examples/server/README.md */ public static Map params = new HashMap<>(); public static String API_KEY = "alk~XXX...XXX"; @Override public void close() throws Exception { destroy(); } private static void setFile(String filePath, String module) { String fileName = filePath.substring(filePath.lastIndexOf("/") + 1), fileLoca = module + "/" + fileName; File modulef = new File(module); if (!modulef.exists()) modulef.mkdir(); boolean isNet = filePath.startsWith("http"); MODEL_FILE = fileLoca; if (isNet && !new File(fileLoca).exists()) { try { Log.add("YourGPT - Download \"" + fileName + "\" to module " + module); new DocNet(filePath).saveAs(fileLoca); if (fileName.endsWith(".zip")) Zip.extract(isNet ? fileLoca : filePath, module); else new Raw(filePath).copyTo(fileLoca, 26214400); } catch (IOException e) { Log.add("!!! Error try to init YourGPT: Can not init module " + module + ". Please check your connecttion. !!!\n\t\tError code: " + e.toString()); throw new RuntimeException(e); } } } /**Use this method to load YourGPT module using the default LLaMA "Win_CUDA12" and default LLM.
Equivalent to calling `loadModule("Win_CUDA12")`. * @see YourGPT#loadModule(java.lang.String) * @see YourGPT#loadModule(java.lang.String, java.lang.String) */ public static void loadModule() { loadModule("Win_CUDA12"); } /**Use this method to load YourGPT module using the specified LLaMA and default LLM. * @param llama the name of the LLaMA file to load.
* Example: "Win_CUDA12"
* Explore more at https://anlavn-api.vercel.app/api/llama * @see YourGPT#loadModule() * @see YourGPT#loadModule(java.lang.String, java.lang.String) */ public static void loadModule(String llama) { loadModule(llama, MODEL_DEFAULT); } /**Use this method to load YourGPT module using the specified LLaMA and the specified LLM. * @param llama the name of the LLaMA file to load.
* Example: "Win_CUDA12"
* Explore more at https://anlavn-api.vercel.app/api/llama
* @param model the location of the specified LLM, it can be local path or link address.
* Example: *
"C:/Users/AnLaVN/mistral-7b-openorca.Q4_K_M.gguf" for file location or, *
"https://huggingface.co/TheBloke/Mistral-7B-OpenOrca-GGUF/resolve/main/mistral-7b-openorca.Q4_K_M.gguf"

* Explore more LLM at https://huggingface.co * @see YourGPT#loadModule() * @see YourGPT#loadModule(java.lang.String) */ public static void loadModule(String llama, String model) { try { License.check(API_KEY, "AnLaVN03"); String licenseKey = URLEncoder.encode(API_KEY, StandardCharsets.UTF_8.toString()); llama = new DocNet("https://anlavn-api.vercel.app/api/llama?name="+llama+"&key="+licenseKey).readLine(); setFile(llama, LLAMA_PATH); setFile(model, MODEL_PATH); } catch (IOException e) { Log.add("!!! Error try to load module YourGPT: Please check your connecttion or API key. !!!\n\t\tError code: " + e.toString()); throw new RuntimeException(e); } } /**Use this method to starts the YourGPT server with the specified parameters.
* If the module has not been loaded, it will attempt to load using the default settings. */ public static void start() { License.check(API_KEY, "AnLaVN03"); if (YGPTProcess != null) { Log.add("YourGPT - Process already start before. Try to destroy by default."); destroy(); } if (MODEL_FILE == null) { Log.add("YourGPT - Module has not been loaded. Try to load module by default."); loadModule(); } List commands = new ArrayList<>(); commands.add(LLAMA_PATH + "/llama-server"); params.put("-m", "../" + MODEL_FILE); for (Map.Entry entry : params.entrySet()) { commands.add(entry.getKey()); commands.add(entry.getValue()); } String[] command = commands.toArray(String[]::new); new Thread() { @Override public void run() { try { Log.add("YourGPT - Start process with command:\n\t\t" + String.join(" ", command)); ProcessBuilder pb = new ProcessBuilder(command); pb.directory(new File(LLAMA_PATH)); pb.redirectErrorStream(true); YGPTProcess = pb.start(); BufferedReader reader = new BufferedReader(new InputStreamReader(YGPTProcess.getInputStream())); int result = reader.read(); while (result != -1) { System.out.print((char) result); result = reader.read(); } } catch (IOException e) { Log.add("!!! Error try to start YourGPT: Error code: " + e.toString()); throw new RuntimeException(e); } }}.start(); } /**Use this method to retrieves a list of the latest LLaMA names available. * @return An array of the latest LLaMA names.
* Explore more at https://anlavn-api.vercel.app/api/llama * @throws java.io.IOException if there is no internet connection or server down. */ public static String[] getLLaMA() throws IOException { return new DocNet("https://anlavn-api.vercel.app/api/llama").readLine().split(","); } /**Use this method to cleans up the YourGPT.LLaMA module if it exists.*/ public static void cleanLLaMA() { Log.add("YourGPT - Clean YourGPT.LLaMA module if exists."); new File(LLAMA_PATH).deleteOnExit(); } /**Use this method to cleans up the YourGPT.Model module if it exists.*/ public static void cleanModel() { Log.add("YourGPT - Clean YourGPT.Model module if exists."); new File(MODEL_PATH).deleteOnExit(); } /**Use this method to forcibly destroy the YourGPT process.*/ public static void destroy() { if (YGPTProcess != null) { Log.add("YourGPT - Forcibly destroy process."); YGPTProcess.destroyForcibly(); YGPTProcess = null; } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy