Skip to content

Commit

Permalink
Remove Lombok in langchain4j module (langchain4j#2063)
Browse files Browse the repository at this point in the history
## Issue
Contributes to langchain4j#1636 

## Change

- Remove Lombok in langchain4j module: Introduces inline `equals`,
`hashCode`, and `toString` methods instead of Lombok-generated methods
for improved flexibility and customization.
- Reformat code to improve readability and consistency.
- Remove unused imports and replace direct class loading with specific
imports for better clarity.
- Added `@EnabledIfEnvironmentVariable` condition to integration tests,
relaying on the presence of the OPENAI_API_KEY environment variable


## General checklist
- [x] There are no breaking changes
- [ ] I have added unit and integration tests for my change
- [x] I have manually run all the unit and integration tests in the
module I have added/changed, and they are all green
- [ ] I have manually run all the unit and integration tests in the
[core](https://github.com/langchain4j/langchain4j/tree/main/langchain4j-core)
and
[main](https://github.com/langchain4j/langchain4j/tree/main/langchain4j)
modules, and they are all green
- [ ] I have added/updated the
[documentation](https://github.com/langchain4j/langchain4j/tree/main/docs/docs)
- [ ] I have added an example in the [examples
repo](https://github.com/langchain4j/langchain4j-examples) (only for
"big" features)
- [ ] I have added/updated [Spring Boot
starter(s)](https://github.com/langchain4j/langchain4j-spring) (if
applicable)
  • Loading branch information
kpavlov authored Nov 14, 2024
1 parent 9a63db4 commit 28c2c11
Show file tree
Hide file tree
Showing 26 changed files with 328 additions and 244 deletions.
10 changes: 3 additions & 7 deletions langchain4j/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@

<artifactId>langchain4j</artifactId>
<name>LangChain4j</name>
<description>Java implementation of LangChain: Integrate your Java application with countless AI tools and services smoothly</description>
<description>Java implementation of LangChain: Integrate your Java application with countless AI
tools and services smoothly
</description>

<dependencies>

Expand All @@ -28,12 +30,6 @@
<artifactId>opennlp-tools</artifactId>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.service.AiServices;
import lombok.Builder;

import static dev.langchain4j.data.message.UserMessage.userMessage;
import static dev.langchain4j.internal.ValidationUtils.ensureNotBlank;
Expand All @@ -22,12 +21,15 @@ public class ConversationalChain implements Chain<String, String> {
private final ChatLanguageModel chatLanguageModel;
private final ChatMemory chatMemory;

@Builder
private ConversationalChain(ChatLanguageModel chatLanguageModel, ChatMemory chatMemory) {
this.chatLanguageModel = ensureNotNull(chatLanguageModel, "chatLanguageModel");
this.chatMemory = chatMemory == null ? MessageWindowChatMemory.withMaxMessages(10) : chatMemory;
}

public static ConversationalChainBuilder builder() {
return new ConversationalChainBuilder();
}

@Override
public String execute(String userMessage) {

Expand All @@ -39,4 +41,30 @@ public String execute(String userMessage) {

return aiMessage.text();
}

public static class ConversationalChainBuilder {
private ChatLanguageModel chatLanguageModel;
private ChatMemory chatMemory;

ConversationalChainBuilder() {
}

public ConversationalChainBuilder chatLanguageModel(ChatLanguageModel chatLanguageModel) {
this.chatLanguageModel = chatLanguageModel;
return this;
}

public ConversationalChainBuilder chatMemory(ChatMemory chatMemory) {
this.chatMemory = chatMemory;
return this;
}

public ConversationalChain build() {
return new ConversationalChain(this.chatLanguageModel, this.chatMemory);
}

public String toString() {
return "ConversationalChain.ConversationalChainBuilder(chatLanguageModel=" + this.chatLanguageModel + ", chatMemory=" + this.chatMemory + ")";
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package dev.langchain4j.data.document.loader;

import dev.langchain4j.data.document.Document;
import dev.langchain4j.data.document.BlankDocumentException;
import dev.langchain4j.data.document.Document;
import dev.langchain4j.data.document.DocumentLoader;
import dev.langchain4j.data.document.DocumentParser;
import dev.langchain4j.data.document.parser.TextDocumentParser;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package dev.langchain4j.data.document.parser;

import dev.langchain4j.data.document.Document;
import dev.langchain4j.data.document.BlankDocumentException;
import dev.langchain4j.data.document.Document;
import dev.langchain4j.data.document.DocumentParser;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.nio.charset.Charset;

import static dev.langchain4j.internal.Utils.isNullOrBlank;
import static dev.langchain4j.internal.ValidationUtils.ensureNotNull;
import static java.nio.charset.StandardCharsets.UTF_8;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import dev.langchain4j.data.document.Metadata;
import dev.langchain4j.data.segment.TextSegment;
import dev.langchain4j.model.Tokenizer;
import lombok.Getter;

import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -14,7 +13,9 @@
import java.util.concurrent.atomic.AtomicInteger;

import static dev.langchain4j.internal.Utils.firstChars;
import static dev.langchain4j.internal.ValidationUtils.*;
import static dev.langchain4j.internal.ValidationUtils.ensureBetween;
import static dev.langchain4j.internal.ValidationUtils.ensureGreaterThanZero;
import static dev.langchain4j.internal.ValidationUtils.ensureNotNull;

/**
* Base class for hierarchical document splitters.
Expand All @@ -23,9 +24,14 @@
* when a single segment is too long.
*/
public abstract class HierarchicalDocumentSplitter implements DocumentSplitter {
@Getter(lazy = true)
private final HierarchicalDocumentSplitter overlapSentenceSplitter =
new DocumentBySentenceSplitter(1, 0, null, null);
private HierarchicalDocumentSplitter overlapSentenceSplitter;

private HierarchicalDocumentSplitter getOverlapSentenceSplitter() {
if (overlapSentenceSplitter == null) {
overlapSentenceSplitter = new DocumentBySentenceSplitter(1, 0, null, null);
}
return overlapSentenceSplitter;
}

private static final String INDEX = "index";

Expand Down
54 changes: 50 additions & 4 deletions langchain4j/src/main/java/dev/langchain4j/service/Result.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package dev.langchain4j.service;

import dev.langchain4j.service.tool.ToolExecution;
import dev.langchain4j.model.output.FinishReason;
import dev.langchain4j.model.output.TokenUsage;
import dev.langchain4j.rag.content.Content;
import lombok.Builder;
import dev.langchain4j.service.tool.ToolExecution;

import java.util.List;

Expand All @@ -17,7 +16,7 @@
* such as {@link TokenUsage} and sources ({@link Content}s retrieved during RAG).
*
* @param <T> The type of the content. Can be of any return type supported by AI Services,
* such as String, Enum, MyCustomPojo, etc.
* such as String, Enum, MyCustomPojo, etc.
*/
public class Result<T> {

Expand All @@ -27,7 +26,6 @@ public class Result<T> {
private final FinishReason finishReason;
private final List<ToolExecution> toolExecutions;

@Builder
public Result(T content, TokenUsage tokenUsage, List<Content> sources, FinishReason finishReason, List<ToolExecution> toolExecutions) {
this.content = ensureNotNull(content, "content");
this.tokenUsage = tokenUsage;
Expand All @@ -36,6 +34,10 @@ public Result(T content, TokenUsage tokenUsage, List<Content> sources, FinishRea
this.toolExecutions = copyIfNotNull(toolExecutions);
}

public static <T> ResultBuilder<T> builder() {
return new ResultBuilder<T>();
}

public T content() {
return content;
}
Expand All @@ -55,4 +57,48 @@ public FinishReason finishReason() {
public List<ToolExecution> toolExecutions() {
return toolExecutions;
}

public static class ResultBuilder<T> {
private T content;
private TokenUsage tokenUsage;
private List<Content> sources;
private FinishReason finishReason;
private List<ToolExecution> toolExecutions;

ResultBuilder() {
}

public ResultBuilder<T> content(T content) {
this.content = content;
return this;
}

public ResultBuilder<T> tokenUsage(TokenUsage tokenUsage) {
this.tokenUsage = tokenUsage;
return this;
}

public ResultBuilder<T> sources(List<Content> sources) {
this.sources = sources;
return this;
}

public ResultBuilder<T> finishReason(FinishReason finishReason) {
this.finishReason = finishReason;
return this;
}

public ResultBuilder<T> toolExecutions(List<ToolExecution> toolExecutions) {
this.toolExecutions = toolExecutions;
return this;
}

public Result<T> build() {
return new Result<T>(this.content, this.tokenUsage, this.sources, this.finishReason, this.toolExecutions);
}

public String toString() {
return "Result.ResultBuilder(content=" + this.content + ", tokenUsage=" + this.tokenUsage + ", sources=" + this.sources + ", finishReason=" + this.finishReason + ", toolExecutions=" + this.toolExecutions + ")";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.*;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

class DefaultOutputParserFactory implements OutputParserFactory {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,32 @@
import dev.langchain4j.data.embedding.Embedding;
import dev.langchain4j.data.segment.TextSegment;
import dev.langchain4j.spi.store.embedding.inmemory.InMemoryEmbeddingStoreJsonCodecFactory;
import dev.langchain4j.store.embedding.*;
import dev.langchain4j.store.embedding.CosineSimilarity;
import dev.langchain4j.store.embedding.EmbeddingMatch;
import dev.langchain4j.store.embedding.EmbeddingSearchRequest;
import dev.langchain4j.store.embedding.EmbeddingSearchResult;
import dev.langchain4j.store.embedding.EmbeddingStore;
import dev.langchain4j.store.embedding.RelevanceScore;
import dev.langchain4j.store.embedding.filter.Filter;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.PriorityQueue;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.stream.IntStream;

import static dev.langchain4j.internal.Utils.randomUUID;
import static dev.langchain4j.internal.ValidationUtils.*;
import static dev.langchain4j.internal.ValidationUtils.ensureNotBlank;
import static dev.langchain4j.internal.ValidationUtils.ensureNotEmpty;
import static dev.langchain4j.internal.ValidationUtils.ensureNotNull;
import static dev.langchain4j.spi.ServiceHelper.loadFactories;
import static java.nio.file.StandardOpenOption.CREATE;
import static java.nio.file.StandardOpenOption.TRUNCATE_EXISTING;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void test_reset() {
builder.reset();

assertThat(builder.isNotEmpty()).isFalse();
assertThat(builder.getSize()).isEqualTo(0);
assertThat(builder.getSize()).isZero();
assertThat(builder.toString()).isEqualTo("");
}

Expand All @@ -70,4 +70,4 @@ public void test_append_prepend() {
assertThat(builder.toString()).isEqualTo("Hello world");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import dev.langchain4j.model.openai.OpenAiChatModel;
import dev.langchain4j.rag.content.retriever.ContentRetriever;
import dev.langchain4j.rag.query.Query;
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
Expand All @@ -19,6 +20,7 @@
import static org.assertj.core.api.Assertions.assertThat;

@ExtendWith(MockitoExtension.class)
@EnabledIfEnvironmentVariable(named = "OPENAI_API_KEY", matches = ".+")
class LanguageModelQueryRouterIT {

@Mock
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import dev.langchain4j.model.openai.OpenAiChatModel;
import dev.langchain4j.rag.query.Metadata;
import dev.langchain4j.rag.query.Query;
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
Expand All @@ -18,6 +19,7 @@
import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;

@EnabledIfEnvironmentVariable(named = "OPENAI_API_KEY", matches = ".+")
class CompressingQueryTransformerIT {

@ParameterizedTest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.openai.OpenAiChatModel;
import dev.langchain4j.rag.query.Query;
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
Expand All @@ -12,6 +13,7 @@

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

@EnabledIfEnvironmentVariable(named = "OPENAI_API_KEY", matches = ".+")
class ExpandingQueryTransformerIT {

@ParameterizedTest
Expand Down Expand Up @@ -49,4 +51,4 @@ static Stream<Arguments> should_expand_query() {
// TODO add more models
);
}
}
}
Loading

0 comments on commit 28c2c11

Please sign in to comment.