|
| 1 | +# llm4j |
| 2 | + |
| 3 | +[](https://github.com/llmjava/llm4j/actions/workflows/main.yml) [](https://jitpack.io/#llmjava/llm4j) [](https://llmjava.github.io/llm4j/javadoc/) [](https://opensource.org/licenses/Apache-2.0) |
| 4 | + |
| 5 | +</b> |
| 6 | + |
| 7 | +**llm4j** is Java library that you can use to interact with mulitple LLM backends: Google PaLM, Cohere, OpenAI, Hugging Face. It can be used in Android or any Java and Kotlin Project. |
| 8 | + |
| 9 | +## Add Dependency |
| 10 | + |
| 11 | +### Gradle |
| 12 | + |
| 13 | +To use library in your gradle project follow the steps below: |
| 14 | + |
| 15 | +1. Add this in your root `build.gradle` at the end of repositories: |
| 16 | + ```groovy |
| 17 | + allprojects { |
| 18 | + repositories { |
| 19 | + ... |
| 20 | + maven { url 'https://jitpack.io' } |
| 21 | + } |
| 22 | + } |
| 23 | + ``` |
| 24 | +2. Add the dependency |
| 25 | + ```groovy |
| 26 | + dependencies { |
| 27 | + def LLM4J_VERSION = "..." |
| 28 | + implementation "com.github.llmjava:llm4j:$LLM4J_VERSION" |
| 29 | + } |
| 30 | + ``` |
| 31 | + |
| 32 | +### Maven |
| 33 | + |
| 34 | +To use the library in your Maven project, follow the steps below: |
| 35 | + |
| 36 | +1. Add the JitPack repository to your build file: |
| 37 | + ```xml |
| 38 | + <repositories> |
| 39 | + <repository> |
| 40 | + <id>jitpack.io</id> |
| 41 | + <url>https://jitpack.io</url> |
| 42 | + </repository> |
| 43 | + </repositories> |
| 44 | + ``` |
| 45 | +2. Add the dependency |
| 46 | + ```xml |
| 47 | + <dependency> |
| 48 | + <groupId>com.github.llmjava</groupId> |
| 49 | + <artifactId>llm4j</artifactId> |
| 50 | + <version>${LLM4J_VERSION}</version> |
| 51 | + </dependency> |
| 52 | + ``` |
| 53 | + |
| 54 | +3. Add the dependency of the LLM backend API, for example Google PaLM |
| 55 | + ```xml |
| 56 | + <dependency> |
| 57 | + <groupId>com.github.llmjava</groupId> |
| 58 | + <artifactId>llm4j-palm</artifactId> |
| 59 | + <version>${LLM4J_VERSION}</version> |
| 60 | + </dependency> |
| 61 | + ``` |
| 62 | + |
| 63 | + |
| 64 | +## Usage |
| 65 | + |
| 66 | +Example code to use against **Google PALM**: |
| 67 | + |
| 68 | +Create a configuration file |
| 69 | +```properties |
| 70 | +# Set API key using env variable or put actual value |
| 71 | +palm.apiKey=${env:PALM_API_KEY} |
| 72 | +``` |
| 73 | + |
| 74 | +Create an instance of `LanguageModel` and submit text generation requests |
| 75 | + |
| 76 | +```java |
| 77 | +import org.apache.commons.configuration2.Configuration; |
| 78 | +import org.apache.commons.configuration2.builder.fluent.Configurations; |
| 79 | +import org.apache.commons.configuration2.ex.ConfigurationException; |
| 80 | +import org.llm4j.api.LLM4J; |
| 81 | +import org.llm4j.api.LanguageModel; |
| 82 | + |
| 83 | +public class Main { |
| 84 | + public static void main(String[] args) { |
| 85 | + Configuration config = new Configurations().properties("llm4j.properties"); |
| 86 | + LanguageModel llm = LLM4J.getLanguageModel(config); |
| 87 | + |
| 88 | + String answer = llm.process("In what country is El Oued?"); |
| 89 | + System.out.println(answer); |
| 90 | + } |
| 91 | +} |
| 92 | +``` |
| 93 | + |
| 94 | +The library uses a Java [Service Loader](https://docs.oracle.com/javase/8/docs/api/java/util/ServiceLoader.html) under the hood to load the appropriate `LanguageModel` implementation. But we also manually create an instance of `LanguageModel` using a specific implementation, like for PaLM: |
| 95 | + |
| 96 | +```java |
| 97 | +import org.apache.commons.configuration2.Configuration; |
| 98 | +import org.apache.commons.configuration2.builder.fluent.Configurations; |
| 99 | +import org.apache.commons.configuration2.ex.ConfigurationException; |
| 100 | +import org.llm4j.api.LLM4J; |
| 101 | +import org.llm4j.palm.PaLMLanguageModel; |
| 102 | + |
| 103 | +public class PaLMApp { |
| 104 | + |
| 105 | + public static void main(String[] args) throws ConfigurationException { |
| 106 | + Configuration config = new Configurations().properties("palm.properties"); |
| 107 | + |
| 108 | + PaLMLanguageModel.Builder factory = new PaLMLanguageModel.Builder(); |
| 109 | + PaLMLanguageModel llm = (PaLMLanguageModel) LLM4J.getLanguageModel(config, factory); |
| 110 | + |
| 111 | + String answer = llm.process("In what country is El Oued?"); |
| 112 | + System.out.println(answer); |
| 113 | + } |
| 114 | +} |
| 115 | +``` |
| 116 | + |
| 117 | +## Build Project |
| 118 | + |
| 119 | +Clone the repository and import as Maven project in IntelliJ IDEA or Eclipse |
| 120 | + |
| 121 | +Before building the project, make sure you have the following things installed. |
| 122 | + |
| 123 | +- Maven |
| 124 | +- Java 8 |
| 125 | + |
| 126 | +To install the library to your local Maven repository, simply execute: |
| 127 | + |
| 128 | +```shell |
| 129 | +mvn install |
| 130 | +``` |
| 131 | + |
| 132 | +To build the library using Gradle, execute the following command |
| 133 | + |
| 134 | +```shell |
| 135 | +./gradlew build |
| 136 | +``` |
| 137 | + |
| 138 | +Refer to the [official documentation](https://maven.apache.org/plugins/maven-deploy-plugin/usage.html) for more information. |
0 commit comments