base on Jlama is a modern LLM inference engine for Java # πŸ¦™ Jlama: A modern LLM inference engine for Java <p align="center"> <img src="docs/jlama.jpg" width="300" height="300" alt="Cute Jlama"> </p> [![Maven Central Version](https://img.shields.io/maven-central/v/com.github.tjake/jlama-parent?style=flat-square)](https://central.sonatype.com/artifact/com.github.tjake/jlama-core/overview) [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](LICENSE) [![Discord](https://img.shields.io/discord/1279855254812229642?style=flat-square&label=Discord&color=663399)](https://discord.gg/HsYXHrMu6J) [![DeepWiki](https://img.shields.io/badge/DeepWiki-tjake%2FJlama-blue.svg)](https://deepwiki.com/tjake/Jlama) ## πŸš€ Features Model Support: * Gemma & Gemma 2 Models * Llama & Llama2 & Llama3 Models * Mistral & Mixtral Models * Qwen2 Models * IBM Granite Models * GPT-2 Models * BERT Models * BPE Tokenizers * WordPiece Tokenizers Implements: * Paged Attention * Mixture of Experts * Tool Calling * Generate Embeddings * Classifier Support * Huggingface [SafeTensors](https://github.com/huggingface/safetensors) model and tokenizer format * Support for F32, F16, BF16 types * Support for Q8, Q4 model quantization * Fast GEMM operations * Distributed Inference! Jlama requires Java 20 or later and it built with the following backends: * [Panama Vector](https://openjdk.org/jeps/448) (default) * Native SIMD (optional) * [WebGPU](https://dawn.googlesource.com/dawn/) (optional, experimental) ## πŸ€” What is it used for? Add LLM Inference directly to your Java application. To learn more read the [DeepWiki](https://deepwiki.com/tjake/Jlama) docs. ## πŸ”¬ Quick Start ### πŸ•΅οΈβ€β™€οΈ How to use as a local client (with jbang!) Jlama includes a command line tool that makes it easy to use. The CLI can be run with [jbang](https://www.jbang.dev/download/). ```shell #Install jbang (or https://www.jbang.dev/download/) curl -Ls https://sh.jbang.dev | bash -s - app setup #Install Jlama CLI (will ask if you trust the source) jbang app install --force jlama@tjake ``` Now that you have jlama installed you can download a model from huggingface and chat with it. Note I have pre-quantized models available at https://hf.co/tjake ```shell # Run the openai chat api and UI on a model jlama restapi tjake/Llama-3.2-1B-Instruct-JQ4 --auto-download ``` open browser to http://localhost:8080/ <p align="center"> <img src="docs/demo.png" alt="Demo chat"> </p> ```shell Usage: jlama [COMMAND] Description: Jlama is a modern LLM inference engine for Java! Quantized models are maintained at https://hf.co/tjake Choose from the available commands: Inference: chat Interact with the specified model restapi Starts a openai compatible rest api for interacting with this model complete Completes a prompt using the specified model Distributed Inference: cluster-coordinator Starts a distributed rest api for a model using cluster workers cluster-worker Connects to a cluster coordinator to perform distributed inference Other: download Downloads a HuggingFace model - use owner/name format list Lists local models quantize Quantize the specified model rm Removes local model version Display JLama version information ``` ### πŸ‘¨β€πŸ’» How to use in your Java project The main purpose of Jlama is to provide a simple way to use large language models in Java. The simplest way to embed Jlama in your app is with the [Langchain4j Integration](https://github.com/langchain4j/langchain4j-examples/tree/main/jlama-examples). If you would like to embed Jlama without langchain4j, add the following [maven](https://central.sonatype.com/artifact/com.github.tjake/jlama-core/) dependencies to your project: ```xml <dependency> <groupId>com.github.tjake</groupId> <artifactId>jlama-core</artifactId> <version>${jlama.version}</version> </dependency> <dependency> <groupId>com.github.tjake</groupId> <artifactId>jlama-native</artifactId> <!-- supports linux-x86_64, macos-x86_64/aarch_64, windows-x86_64 Use https://github.com/trustin/os-maven-plugin to detect os and arch --> <classifier>${os.detected.name}-${os.detected.arch}</classifier> <version>${jlama.version}</version> </dependency> ``` jlama uses Java 21 preview features. You can enable the features globally with: ```shell export JDK_JAVA_OPTIONS="--add-modules jdk.incubator.vector --enable-preview" ``` or enable the preview features by configuring maven compiler and failsafe plugins. Then you can use the Model classes to run models: ```java public void sample() throws IOException { String model = "tjake/Llama-3.2-1B-Instruct-JQ4"; String workingDirectory = "./models"; String prompt = "What is the best season to plant avocados?"; // Downloads the model or just returns the local path if it's already downloaded File localModelPath = new Downloader(workingDirectory, model).huggingFaceModel(); // Loads the quantized model and specified use of quantized memory AbstractModel m = ModelSupport.loadModel(localModelPath, DType.F32, DType.I8); PromptContext ctx; // Checks if the model supports chat prompting and adds prompt in the expected format for this model if (m.promptSupport().isPresent()) { ctx = m.promptSupport() .get() .builder() .addSystemMessage("You are a helpful chatbot who writes short responses.") .addUserMessage(prompt) .build(); } else { ctx = PromptContext.of(prompt); } System.out.println("Prompt: " + ctx.getPrompt() + "\n"); // Generates a response to the prompt and prints it // The api allows for streaming or non-streaming responses // The response is generated with a temperature of 0.7 and a max token length of 256 Generator.Response r = m.generate(UUID.randomUUID(), ctx, 0.0f, 256, (s, f) -> {}); System.out.println(r.responseText); } ``` Or you can use a **Builder API**: ```java public void sample() throws IOException { String model = "tjake/Llama-3.2-1B-Instruct-JQ4"; String workingDirectory = "./models"; String prompt = "What is the best season to plant avocados?"; // Downloads the model or just returns the local path if it's already downloaded File localModelPath = new Downloader(workingDirectory, model).huggingFaceModel(); // Loads the quantized model and specified use of quantized memory AbstractModel m = ModelSupport.loadModel(localModelPath, DType.F32, DType.I8); PromptContext ctx; // Checks if the model supports chat prompting and adds prompt in the expected format for this model if (m.promptSupport().isPresent()) { ctx = m.promptSupport() .get() .builder() .addSystemMessage("You are a helpful chatbot who writes short responses.") .addUserMessage(prompt) .build(); } else { ctx = PromptContext.of(prompt); } System.out.println("Prompt: " + ctx.getPrompt() + "\n"); // Generates a response to the prompt and prints it // The api allows for streaming or non-streaming responses // The response is generated with a temperature of 0.7 and a max token length of 256 Generator.Response r = m.generateBuilder() .session(UUID.randomUUID()) //By default, UUID.randomUUID() .promptContext(ctx) // Required or use prompt(String text) .ntokens(256) //By default, 256 .temperature(0.0f) //By default, 0.0f .onTokenWithTimings((s, aFloat) -> {}) //By default, (s, aFloat) -> {}, nothing .generate(); System.out.println(r.responseText); } ``` You can simplify promptSupport using: ```java public void sample() throws IOException { String model = "tjake/Llama-3.2-1B-Instruct-JQ4"; String workingDirectory = "./models"; String prompt = "What is the best season to plant avocados?"; // Downloads the model or just returns the local path if it's already downloaded File localModelPath = new Downloader(workingDirectory, model).huggingFaceModel(); // Loads the quantized model and specified use of quantized memory AbstractModel m = ModelSupport.loadModel(localModelPath, DType.F32, DType.I8); var systemPrompt = "You are a helpful chatbot who writes short responses."; PromptContext ctx = m.prompt() .addUserMessage(prompt) .addSystemMessage(systemPrompt) .build(); //build method will create a PromptContext, if model don't support prompt, a simple PromptContext object will be created System.out.println("Prompt: " + ctx.getPrompt() + "\n"); // Generates a response to the prompt and prints it // The api allows for streaming or non-streaming responses // The response is generated with a temperature of 0.7 and a max token length of 256 Generator.Response r = m.generateBuilder() .session(UUID.randomUUID()) //By default, UUID.randomUUID() .promptContext(ctx) // Required or use prompt(String text) .ntokens(256) //By default, 256 .temperature(0.0f) //By default, 0.0f .onTokenWithTimings((s, aFloat) -> {}) //By default, (s, aFloat) -> {}, nothing .generate(); System.out.println(r.responseText); } ``` ## Devloping Jlama See [DEVELOPER_GUIDE.md](DEVELOPER_GUIDE.md) for more information. ## ⭐ Give us a Star! If you like or are using this project to build your own, please give us a star. It's a free way to show your support. ## πŸ—ΊοΈ Roadmap * Support more and more models * <s>Add pure java tokenizers</s> * <s>Support Quantization (e.g. k-quantization)</s> * Add LoRA support * GraalVM support * <s>Add distributed inference</s> ## 🏷️ License and Citation The code is available under [Apache License](./LICENSE). If you find this project helpful in your research, please cite this work at ``` @misc{jlama2024, title = {Jlama: A modern Java inference engine for large language models}, url = {https://github.com/tjake/jlama}, author = {T Jake Luciani}, month = {January}, year = {2024} } ``` ", Assign "at most 3 tags" to the expected json: {"id":"10674","tags":[]} "only from the tags list I provide: [{"id":77,"name":"3d"},{"id":89,"name":"agent"},{"id":17,"name":"ai"},{"id":54,"name":"algorithm"},{"id":24,"name":"api"},{"id":44,"name":"authentication"},{"id":3,"name":"aws"},{"id":27,"name":"backend"},{"id":60,"name":"benchmark"},{"id":72,"name":"best-practices"},{"id":39,"name":"bitcoin"},{"id":37,"name":"blockchain"},{"id":1,"name":"blog"},{"id":45,"name":"bundler"},{"id":58,"name":"cache"},{"id":21,"name":"chat"},{"id":49,"name":"cicd"},{"id":4,"name":"cli"},{"id":64,"name":"cloud-native"},{"id":48,"name":"cms"},{"id":61,"name":"compiler"},{"id":68,"name":"containerization"},{"id":92,"name":"crm"},{"id":34,"name":"data"},{"id":47,"name":"database"},{"id":8,"name":"declarative-gui "},{"id":9,"name":"deploy-tool"},{"id":53,"name":"desktop-app"},{"id":6,"name":"dev-exp-lib"},{"id":59,"name":"dev-tool"},{"id":13,"name":"ecommerce"},{"id":26,"name":"editor"},{"id":66,"name":"emulator"},{"id":62,"name":"filesystem"},{"id":80,"name":"finance"},{"id":15,"name":"firmware"},{"id":73,"name":"for-fun"},{"id":2,"name":"framework"},{"id":11,"name":"frontend"},{"id":22,"name":"game"},{"id":81,"name":"game-engine "},{"id":23,"name":"graphql"},{"id":84,"name":"gui"},{"id":91,"name":"http"},{"id":5,"name":"http-client"},{"id":51,"name":"iac"},{"id":30,"name":"ide"},{"id":78,"name":"iot"},{"id":40,"name":"json"},{"id":83,"name":"julian"},{"id":38,"name":"k8s"},{"id":31,"name":"language"},{"id":10,"name":"learning-resource"},{"id":33,"name":"lib"},{"id":41,"name":"linter"},{"id":28,"name":"lms"},{"id":16,"name":"logging"},{"id":76,"name":"low-code"},{"id":90,"name":"message-queue"},{"id":42,"name":"mobile-app"},{"id":18,"name":"monitoring"},{"id":36,"name":"networking"},{"id":7,"name":"node-version"},{"id":55,"name":"nosql"},{"id":57,"name":"observability"},{"id":46,"name":"orm"},{"id":52,"name":"os"},{"id":14,"name":"parser"},{"id":74,"name":"react"},{"id":82,"name":"real-time"},{"id":56,"name":"robot"},{"id":65,"name":"runtime"},{"id":32,"name":"sdk"},{"id":71,"name":"search"},{"id":63,"name":"secrets"},{"id":25,"name":"security"},{"id":85,"name":"server"},{"id":86,"name":"serverless"},{"id":70,"name":"storage"},{"id":75,"name":"system-design"},{"id":79,"name":"terminal"},{"id":29,"name":"testing"},{"id":12,"name":"ui"},{"id":50,"name":"ux"},{"id":88,"name":"video"},{"id":20,"name":"web-app"},{"id":35,"name":"web-server"},{"id":43,"name":"webassembly"},{"id":69,"name":"workflow"},{"id":87,"name":"yaml"}]" returns me the "expected json"