Skip to content

Commit d1fb210

Browse files
authored
Merge pull request #4 from llm-java/feature/hf-chat
[hf] add support for chat
2 parents 9388161 + 82e2f72 commit d1fb210

File tree

4 files changed

+71
-5
lines changed

4 files changed

+71
-5
lines changed

.github/dependabot.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# To get started with Dependabot version updates, you'll need to specify which
2+
# package ecosystems to update and where the package manifests are located.
3+
# Please see the documentation for all configuration options:
4+
# https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates
5+
6+
version: 2
7+
updates:
8+
# Maintain dependencies for GitHub Actions
9+
- package-ecosystem: "github-actions"
10+
directory: "/"
11+
schedule:
12+
interval: "weekly"
13+
14+
# Maintain dependencies for maven
15+
- package-ecosystem: "maven"
16+
directory: "/"
17+
schedule:
18+
interval: "weekly"
19+
open-pull-requests-limit: 10
20+
target-branch: main
21+
commit-message:
22+
prefix: "[maven] "
23+
labels:
24+
- "maven dependencies"

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

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
import org.llm4j.huggingface.request.TextGenerationRequest;
88
import org.llm4j.huggingface.request.TextGenerationResponse;
99

10+
import java.util.ArrayList;
11+
import java.util.List;
12+
import java.util.Map;
13+
1014
public class HFLanguageModel implements LanguageModel {
1115

1216
private Configuration config;
@@ -19,7 +23,7 @@ public HFLanguageModel(Builder builder) {
1923
public String process(String text) {
2024
TextGenerationRequest request = new TextGenerationRequest.Builder()
2125
.withInputs(text)
22-
.withConfig(this.config)
26+
.withConfig(config)
2327
.build();
2428

2529
TextGenerationResponse response = client.generate(request);
@@ -29,7 +33,22 @@ public String process(String text) {
2933

3034
@Override
3135
public String process(ChatHistory history) {
32-
return null;
36+
List<String> lines = new ArrayList<>();
37+
// Add context
38+
if(history.getContext()!=null) lines.add(history.getContext());
39+
// Add examples
40+
for(Map.Entry<ChatHistory.Message, ChatHistory.Message> pair: history.getExampleList()) {
41+
lines.add(pair.getKey().toString());
42+
lines.add(pair.getValue().toString());
43+
}
44+
// Add conversations
45+
for(ChatHistory.Message message: history.getMessageList()) {
46+
lines.add(message.toString());
47+
}
48+
// submit
49+
String text = String.join("\n", lines);
50+
51+
return process(text);
3352
}
3453

3554
public static final class Builder implements LanguageModelFactory {

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

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
package org.llm4j.huggingface;
22

33
import org.apache.commons.configuration2.Configuration;
4-
import org.apache.commons.configuration2.EnvironmentConfiguration;
54
import org.apache.commons.configuration2.builder.fluent.Configurations;
65
import org.apache.commons.configuration2.ex.ConfigurationException;
76
import org.junit.jupiter.api.DisplayName;
87
import org.junit.jupiter.api.Test;
8+
import org.llm4j.api.ChatHistory;
99
import org.llm4j.api.LanguageModel;
1010

1111
import static com.google.common.truth.Truth.assertWithMessage;
1212

1313
public class HFLanguageModelTest {
1414

1515
@Test
16-
@DisplayName("Should check all LLM respond")
17-
public void should_send_messages_and_receive_response() throws ConfigurationException {
16+
@DisplayName("Should generate text")
17+
public void should_process_text_generation_request() throws ConfigurationException {
1818

1919
Configuration config = new Configurations().properties("llm4j.properties");
2020

@@ -26,4 +26,26 @@ public void should_send_messages_and_receive_response() throws ConfigurationExce
2626
assertWithMessage("Answer should contain right answer").
2727
that(answer.toLowerCase()).contains("middle east");
2828
}
29+
30+
@Test
31+
@DisplayName("Should process chat request")
32+
public void should_process_chat_request() throws ConfigurationException {
33+
34+
Configuration config = new Configurations().properties("llm4j.properties");
35+
36+
LanguageModel llm = new HFLanguageModel.Builder()
37+
.getLanguageModel(config);
38+
39+
ChatHistory history = new ChatHistory()
40+
.setContext("Respond to all questions with a rhyming poem.")
41+
.addExample(
42+
"What is the capital of Algeria?",
43+
"If the capital of Algeria is what you seek, Algiers is where you ought to peek.")
44+
.addMessage("How tall is Makam Echahid?");
45+
46+
String answer = llm.process(history);
47+
48+
assertWithMessage("Answer should contain right answer").
49+
that(answer.toLowerCase()).contains("algeria");
50+
}
2951
}

llm4j-palm/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
https://developers.generativeai.google/guide/palm_api_overview

0 commit comments

Comments
 (0)