Skip to content

Commit

Permalink
Change to history
Browse files Browse the repository at this point in the history
  • Loading branch information
rashidi committed Oct 30, 2023
1 parent 96b07ac commit 5ffe465
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 80 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package zin.rashidi.boot.langchain4j.history;

import dev.langchain4j.service.SystemMessage;
import dev.langchain4j.service.UserMessage;
import dev.langchain4j.service.V;

/**
* @author Rashidi Zin
*/
interface Historian {

@SystemMessage("""
You are a historian who is an expert for {{country}}.
Given provided year is supported, you will provide historical events that occurred within the year.
You will also include detail about the event.
""")
@UserMessage("{{year}}")
History chat(@V("country") String country, @V("year") int year);

}
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package zin.rashidi.boot.langchain4j.translate;
package zin.rashidi.boot.langchain4j.history;

import dev.langchain4j.data.segment.TextSegment;
import dev.langchain4j.memory.ChatMemory;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.embedding.AllMiniLmL6V2EmbeddingModel;
import dev.langchain4j.retriever.EmbeddingStoreRetriever;
import dev.langchain4j.retriever.Retriever;
import dev.langchain4j.service.AiServices;
import dev.langchain4j.store.embedding.EmbeddingStore;
import dev.langchain4j.store.embedding.elasticsearch.ElasticsearchEmbeddingStore;
import org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
Expand All @@ -19,14 +20,15 @@
* @author Rashidi Zin
*/
@Configuration
class TranslationConfiguration {
class HistorianConfiguration {

@Bean
TranslationService translateService(ChatLanguageModel chatLanguageModel, Retriever<TextSegment> retriever) {
return AiServices.builder(TranslationService.class)
.chatLanguageModel(chatLanguageModel)
.chatMemory(withMaxMessages(20))
Historian historian(ChatLanguageModel model, Retriever<TextSegment> retriever, HistorianTool tool) {
return AiServices.builder(Historian.class)
.chatLanguageModel(model)
.chatMemory(withMaxMessages(10))
.retriever(retriever)
.tools(tool)
.build();
}

Expand All @@ -39,7 +41,7 @@ Retriever<TextSegment> retriever(EmbeddingStore<TextSegment> embeddingStore) {
EmbeddingStore<TextSegment> embeddingStore(Environment environment) {
return ElasticsearchEmbeddingStore.builder()
.serverUrl(environment.getProperty("app.elasticsearch.uri"))
.indexName("translation")
.indexName("history")
.build();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package zin.rashidi.boot.langchain4j.history;

import dev.langchain4j.agent.tool.P;
import dev.langchain4j.agent.tool.Tool;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.time.LocalDate;
import java.time.Year;

/**
* @author Rashidi Zin
*/
@Component
class HistorianTool {

@Tool("Validate year is supported")
public void assertYear(int year) {
Assert.isTrue(year < 2021, "Year must be less than 2021");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package zin.rashidi.boot.langchain4j.history;

/**
* @author Rashidi Zin
*/
class History {

private final String country;
private final int year;
private String person;
private String event;
private String error;

public History(String country, int year) {
this.country = country;
this.year = year;
}

public String country() {
return country;
}

public int year() {
return year;
}

public String person() {
return person;
}

public History person(String person) {
this.person = person;
return this;
}

public String event() {
return event;
}

public History event(String event) {
this.event = event;
return this;
}

public String error() {
return error;
}

public History error(String error) {
this.error = error;
return this;
}

}

This file was deleted.

This file was deleted.

10 changes: 5 additions & 5 deletions langchain4j/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
langchain4j.chat-model.provider=openai
langchain4j.chat-model.openai.api-key=demo
langchain4j.chat-model.openai.model-name=gpt-3.5-turbo
langchain4j.chat-model.openai.temperature=0.0
#langchain4j.chat-model.openai.top-p=1.0
langchain4j.chat-model.openai.model-name=gpt-4
langchain4j.chat-model.openai.temperature=0
langchain4j.chat-model.openai.top-p=1.0
#langchain4j.chat-model.openai.max-tokens=100
#langchain4j.chat-model.openai.presence-penalty=0.0
#langchain4j.chat-model.openai.frequency-penalty=0.0
Expand All @@ -11,5 +11,5 @@ langchain4j.chat-model.openai.timeout=PT60S
#langchain4j.chat-model.openai.log-requests=true
#langchain4j.chat-model.openai.log-responses=true

logging.level.dev.langchain4j=ERROR
logging.level.dev.ai4j.openai4j=ERROR
logging.level.dev.langchain4j=DEBUG
logging.level.dev.ai4j.openai4j=DEBUG
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
package zin.rashidi.boot.langchain4j.translate;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.groups.Tuple.tuple;

import java.io.IOException;
package zin.rashidi.boot.langchain4j.history;

import org.apache.http.HttpHost;
import org.elasticsearch.client.Request;
Expand All @@ -20,12 +15,18 @@
import org.testcontainers.junit.jupiter.Testcontainers;
import org.testcontainers.utility.DockerImageName;

import java.io.IOException;
import java.time.LocalDate;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

/**
* @author Rashidi Zin
*/
@Testcontainers
@SpringBootTest
class TranslationServiceTests {
class HistorianTests {

@Container
private static final ElasticsearchContainer elastic = new ElasticsearchContainer(
Expand All @@ -41,35 +42,39 @@ static void properties(DynamicPropertyRegistry registry) {
@BeforeAll
static void createIndex() throws IOException {
try (var client = RestClient.builder(HttpHost.create(elastic.getHttpHostAddress())).build()) {
client.performRequest(new Request("PUT", "/translation"));
client.performRequest(new Request("PUT", "/history"));
}
}

@Autowired
private TranslationService service;
private Historian historian;

@Test
@DisplayName("When I request to translate a phrase from Spanish to English Then the translation should consists of source and target language, translated text, and word breakdowns")
void translate() {
var result = service.translate("spanish", "english", "Yo soy un salsero");
@DisplayName("When I ask the Historian about the history of Malaysia in 1957, Then I should get information about Hari Merdeka")
void chat() {
var message = historian.chat("Malaysia", 1957);

assertThat(result).satisfies(translate -> {
assertThat(message)
.extracting("country", "year", "person")
.containsExactly("Malaysia", 1957, "Tunku Abdul Rahman");

assertThat(translate)
.extracting("language.source", "language.target", "text.source", "text.target")
.containsExactly("es", "en", "Yo soy un salsero", "I am a salsa dancer");
assertThat(message)
.extracting("event").asString()
.contains("Hari Merdeka");
}

@Test
@DisplayName("When I ask the Historian about event after 2021, Then an error message should be returned")
void unsupportedYear() {
var message = historian.chat("Malaysia", 2022);

assertThat(translate)
.extracting("text.breakdowns").asList()
.extracting("source", "target")
.containsExactly(
tuple("Yo", "I"),
tuple("soy", "am"),
tuple("un", "a"),
tuple("salsero", "salsa dancer")
);
});
assertThat(message)
.extracting("country", "year", "error")
.containsExactly("Malaysia", 2022, "Year must be less than 2021");

assertThat(message)
.extracting("person", "event").asString()
.containsWhitespaces();
}

}
}

0 comments on commit 5ffe465

Please sign in to comment.