Skip to content

Commit 934dc97

Browse files
authored
add readme (#13)
* add readme * fix api key
1 parent f9c293f commit 934dc97

File tree

5 files changed

+145
-7
lines changed

5 files changed

+145
-7
lines changed

README.md

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
# llm4j
2+
3+
[![build](https://github.com/llmjava/llm4j/actions/workflows/main.yml/badge.svg)](https://github.com/llmjava/llm4j/actions/workflows/main.yml) [![Jitpack](https://jitpack.io/v/llmjava/llm4j.svg)](https://jitpack.io/#llmjava/llm4j) [![Javadoc](https://img.shields.io/badge/JavaDoc-Online-green)](https://llmjava.github.io/llm4j/javadoc/) [![License](https://img.shields.io/badge/License-Apache_2.0-blue.svg)](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.

llm4j-cohere/src/test/java/org/llm4j/cohere/CohereLanguageModelTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public void should_process_text_generation_request() throws ConfigurationExcepti
3131
LanguageModel llm = new CohereLanguageModel.Builder()
3232
.getLanguageModel(config);
3333

34-
String answer = llm.process("In what country is El Outed located?");
34+
String answer = llm.process("In what country is El Oued located?");
3535

3636
assertWithMessage("Answer should contain right answer").
3737
that(answer.toLowerCase()).contains("algeria");
@@ -71,7 +71,7 @@ public void should_process_text_embed_request() throws ConfigurationException {
7171
LanguageModel llm = new CohereLanguageModel.Builder()
7272
.getLanguageModel(config);
7373

74-
List<Float> embeddings = llm.embed("In what country is El Outed located?");
74+
List<Float> embeddings = llm.embed("In what country is El Oued located?");
7575

7676
assertThat(embeddings).isNotEmpty();
7777
}

llm4j-huggingface/src/test/java/org/llm4j/huggingface/HFLanguageModelTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public void should_process_text_embed_request() throws ConfigurationException {
6161
LanguageModel llm = new HFLanguageModel.Builder()
6262
.getLanguageModel(config);
6363

64-
List<Float> embeddings = llm.embed("In what country is El Outed located?");
64+
List<Float> embeddings = llm.embed("In what country is El Oued located?");
6565

6666
assertThat(embeddings).isNotEmpty();
6767
}

llm4j-openai/src/test/java/org/llm4j/openai/OpenAILanguageModelTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public void should_process_text_generation_request() throws ConfigurationExcepti
2626
LanguageModel llm = new OpenAILanguageModel.Builder()
2727
.getLanguageModel(config);
2828

29-
String answer = llm.process("In what country is El Outed located?");
29+
String answer = llm.process("In what country is El Oued located?");
3030

3131
assertWithMessage("Answer should contain right answer").
3232
that(answer.toLowerCase()).contains("algeria");
@@ -64,7 +64,7 @@ public void should_process_text_embed_request() throws ConfigurationException {
6464
LanguageModel llm = new OpenAILanguageModel.Builder()
6565
.getLanguageModel(config);
6666

67-
List<Float> embeddings = llm.embed("In what country is El Outed located?");
67+
List<Float> embeddings = llm.embed("In what country is El Oued located?");
6868

6969
assertThat(embeddings).isNotEmpty();
7070
}

llm4j-palm/src/test/java/org/llm4j/palm/PaLMLanguageModelTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public void should_process_text_generation_request() throws ConfigurationExcepti
2525
LanguageModel llm = new PaLMLanguageModel.Builder()
2626
.getLanguageModel(config);
2727

28-
String answer = llm.process("In what country is El Outed located?");
28+
String answer = llm.process("In what country is El Oued located?");
2929

3030
assertWithMessage("Answer should contain right answer").
3131
that(answer.toLowerCase()).contains("algeria");
@@ -63,7 +63,7 @@ public void should_process_text_embed_request() throws ConfigurationException {
6363
LanguageModel llm = new PaLMLanguageModel.Builder()
6464
.getLanguageModel(config);
6565

66-
List<Float> embeddings = llm.embed("In what country is El Outed located?");
66+
List<Float> embeddings = llm.embed("In what country is El Oued located?");
6767

6868
assertThat(embeddings).isNotEmpty();
6969
}

0 commit comments

Comments
 (0)