Skip to content

Commit 64b4a88

Browse files
authored
Merge pull request #1 from llm-java/feature/palm
Add PaLM from makersuite
2 parents 074d61b + b40a3fc commit 64b4a88

21 files changed

+425
-34
lines changed

llm4j-api/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
<parent>
88
<groupId>org.llm4j</groupId>
99
<artifactId>llm4j-parent</artifactId>
10-
<version>1.0-SNAPSHOT</version>
10+
<version>0.0-SNAPSHOT</version>
1111
<relativePath>../parent-pom.xml</relativePath>
1212
</parent>
1313

1414
<artifactId>llm4j-api</artifactId>
1515

1616
<packaging>jar</packaging>
17-
<name>LLM4J API Module</name>
17+
<name>LLM4J API</name>
1818
<description>The LLM4J package</description>
1919

2020
<url>https://github.com/dzlab</url>

llm4j-examples/pom.xml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
<parent>
88
<groupId>org.llm4j</groupId>
99
<artifactId>llm4j-parent</artifactId>
10-
<version>1.0-SNAPSHOT</version>
10+
<version>0.0-SNAPSHOT</version>
1111
<relativePath>../parent-pom.xml</relativePath>
1212
</parent>
1313

1414
<artifactId>llm4j-examples</artifactId>
1515

1616
<packaging>jar</packaging>
17-
<name>LLM4J Examples Module</name>
17+
<name>LLM4J Examples</name>
1818
<description>Examples showcasing how to LLM4J to query LLMs</description>
1919

2020
<url>http://github.com/dzlab</url>
@@ -28,10 +28,15 @@
2828
<groupId>org.llm4j</groupId>
2929
<artifactId>llm4j-api</artifactId>
3030
</dependency>
31+
3132
<dependency>
3233
<groupId>org.llm4j</groupId>
3334
<artifactId>llm4j-huggingface</artifactId>
3435
</dependency>
36+
<dependency>
37+
<groupId>org.llm4j</groupId>
38+
<artifactId>llm4j-palm</artifactId>
39+
</dependency>
3540

3641
</dependencies>
3742

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.llm4j.examples;
2+
3+
import org.apache.commons.configuration2.Configuration;
4+
import org.apache.commons.configuration2.builder.fluent.Configurations;
5+
import org.apache.commons.configuration2.ex.ConfigurationException;
6+
import org.llm4j.api.LLM4J;
7+
import org.llm4j.palm.PaLMLanguageModel;
8+
9+
public class PaLMApp {
10+
11+
public static void main(String[] args) throws ConfigurationException {
12+
Configuration config = new Configurations().properties("palm.properties");
13+
14+
PaLMLanguageModel.Builder factory = new PaLMLanguageModel.Builder();
15+
PaLMLanguageModel llm = (PaLMLanguageModel) LLM4J.getLanguageModel(config, factory);
16+
17+
String answer = llm.process("In what country is Andalossia?");
18+
System.out.println(answer);
19+
}
20+
}

llm4j-examples/src/main/resources/META-INF/services/org.llm4j.spi.LLM4JServiceProvider

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Set API key using env variable or use actual value
2+
palm.apiKey=${env:PALM_API_KEY}
3+
4+
modelId=models/text-bison-001
5+
6+
# timeout in milliseconds
7+
timeout=10000
8+
9+
topK=3
10+
topP=0.4
11+
temperature=0.7
12+
maxNewTokens=256
13+
maxOutputTokens=1024
14+
candidateCount=1

llm4j-huggingface/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
<parent>
88
<groupId>org.llm4j</groupId>
99
<artifactId>llm4j-parent</artifactId>
10-
<version>1.0-SNAPSHOT</version>
10+
<version>0.0-SNAPSHOT</version>
1111
<relativePath>../parent-pom.xml</relativePath>
1212
</parent>
1313

1414
<artifactId>llm4j-huggingface</artifactId>
1515

1616
<packaging>jar</packaging>
17-
<name>LLM4J HuggingFace Module</name>
17+
<name>LLM4J HuggingFace</name>
1818
<description>The LLM4J API implementation for HuggingFace</description>
1919

2020
<url>http://github.com/dzlab</url>

llm4j-huggingface/src/main/java/org/llm4j/huggingface/HFApiClient.java

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,7 @@ class HFApiClient {
2020
private final String modelId;
2121

2222
HFApiClient(Builder builder) {
23-
24-
OkHttpClient okHttpClient = new OkHttpClient.Builder()
25-
.addInterceptor(new HFApiAuthorizationInterceptor(builder.apiKey))
26-
.callTimeout(builder.timeout)
27-
.connectTimeout(builder.timeout)
28-
.readTimeout(builder.timeout)
29-
.writeTimeout(builder.timeout)
30-
.build();
31-
32-
Gson gson = new GsonBuilder()
33-
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
34-
.create();
35-
36-
Retrofit retrofit = new Retrofit.Builder()
37-
.baseUrl("https://api-inference.huggingface.co")
38-
.client(okHttpClient)
39-
.addConverterFactory(GsonConverterFactory.create(gson))
40-
.build();
41-
42-
this.api = retrofit.create(HFApi.class);
23+
this.api = builder.api;
4324
this.modelId = builder.modelId;
4425
}
4526

@@ -97,16 +78,39 @@ private static RuntimeException newException(retrofit2.Response<?> response) thr
9778
}
9879

9980
public static class Builder {
100-
private String apiKey;
81+
private HFApi api;
10182
private String modelId;
102-
private Duration timeout;
10383

10484
public Builder withConfig(Configuration config) {
105-
this.apiKey = config.getString("hf.apiKey");
10685
this.modelId = config.getString("modelId");
107-
this.timeout = Duration.ofMillis(config.getLong("timeout", 15 * 1000L));
86+
String apiKey = config.getString("hf.apiKey");
87+
Duration timeout = Duration.ofMillis(config.getLong("timeout", 15 * 1000L));
88+
this.api = buildApi(apiKey, timeout);
10889
return this;
10990
}
91+
92+
HFApi buildApi(String apiKey, Duration timeout) {
93+
OkHttpClient okHttpClient = new OkHttpClient.Builder()
94+
.addInterceptor(new HFApiAuthorizationInterceptor(apiKey))
95+
.callTimeout(timeout)
96+
.connectTimeout(timeout)
97+
.readTimeout(timeout)
98+
.writeTimeout(timeout)
99+
.build();
100+
101+
Gson gson = new GsonBuilder()
102+
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
103+
.create();
104+
105+
Retrofit retrofit = new Retrofit.Builder()
106+
.baseUrl("https://api-inference.huggingface.co")
107+
.client(okHttpClient)
108+
.addConverterFactory(GsonConverterFactory.create(gson))
109+
.build();
110+
111+
return retrofit.create(HFApi.class);
112+
}
113+
110114
public HFApiClient build() {
111115
return new HFApiClient(this);
112116
}
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)