Skip to content

Commit

Permalink
langchain4j#1636 Get rid of Lombok in langchain4j-core (langchain4j#1987
Browse files Browse the repository at this point in the history
)

## Issue
Contributes to langchain4j#1636 

## Change
Get rid of Lombok in langchain4j-core: Run Delombok refactoring in
IntelliJ IDEA to remove Lombok annotations and replace them with the
equivalent Java code.

This pull request focuses on removing the Lombok dependency and
replacing it with manually implemented builder patterns across several
classes. Additionally, it includes some minor code improvements.

## General checklist
- [x] There are no breaking changes
- [ ] I have added unit and integration tests for my change
- [ ] I have manually run all the unit tests in all modules, and they
are all green
- [ ] 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 Oct 29, 2024
1 parent 09faf9e commit 99dec54
Show file tree
Hide file tree
Showing 63 changed files with 1,232 additions and 466 deletions.
17 changes: 17 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
root = true

[*]
charset = utf-8
end_of_line = lf
indent_size = 4
indent_style = space
insert_final_newline = true
max_line_length = 100
tab_width = 4

[*.java]
ij_java_names_count_to_use_import_on_demand = 999
ij_java_class_count_to_use_import_on_demand = 999

[{*.yaml,*.yml}]
indent_size = 2
3 changes: 2 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ Thank you for investing your time and effort in contributing to our project, we
- If you want to contribute a bug fix or a new feature that isn't listed in the [issues](https://github.com/langchain4j/langchain4j/issues) yet, please open a new issue for it. We will prioritize is shortly.
- Follow [Google's Best Practices for Java Libraries](https://jlbp.dev/)
- Keep the code compatible with Java 17.
- Avoid adding new dependencies as much as possible (new dependencies with test scope are OK). If absolutely necessary, try to use the same libraries which are already used in the project.
- Avoid adding new dependencies as much as possible (new dependencies with test scope are OK). If absolutely necessary, try to use the same libraries which are already used in the project. Make sure you run `mvn dependency:analyze` to identify unnecessary dependencies.
- Write unit and/or integration tests for your code. This is critical: no tests, no review!
- Make sure you run all unit tests on all modules with `mvn clean test`
- Avoid making breaking changes. Always keep backward compatibility in mind. For example, instead of removing fields/methods/etc, mark them `@Deprecated` and make sure they still work as before.
- Follow existing naming conventions.
- Avoid using Lombok in the new code, and remove it from the old code if you get a chance.
Expand Down
6 changes: 0 additions & 6 deletions langchain4j-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,6 @@
<artifactId>slf4j-api</artifactId>
</dependency>

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

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.ElementType.CONSTRUCTOR;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE;

/**
* Indicates that a class/constructor/method is experimental and might change in the future.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
import dev.langchain4j.data.segment.TextSegment;
import dev.langchain4j.store.embedding.EmbeddingStore;

import java.util.*;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;

import static dev.langchain4j.internal.Exceptions.illegalArgument;
import static dev.langchain4j.internal.Exceptions.runtime;
Expand Down Expand Up @@ -67,8 +72,8 @@ public Metadata(Map<String, ?> metadata) {
validate(key, value);
if (!SUPPORTED_VALUE_TYPES.contains(value.getClass())) {
throw illegalArgument("The metadata key '%s' has the value '%s', which is of the unsupported type '%s'. " +
"Currently, the supported types are: %s",
key, value, value.getClass().getName(), SUPPORTED_VALUE_TYPES
"Currently, the supported types are: %s",
key, value, value.getClass().getName(), SUPPORTED_VALUE_TYPES
);
}
});
Expand Down Expand Up @@ -116,7 +121,7 @@ public String getString(String key) {
}

throw runtime("Metadata entry with the key '%s' has a value of '%s' and type '%s'. " +
"It cannot be returned as a String.", key, value, value.getClass().getName());
"It cannot be returned as a String.", key, value, value.getClass().getName());
}

/**
Expand All @@ -140,7 +145,7 @@ public UUID getUUID(String key) {
}

throw runtime("Metadata entry with the key '%s' has a value of '%s' and type '%s'. " +
"It cannot be returned as a UUID.", key, value, value.getClass().getName());
"It cannot be returned as a UUID.", key, value, value.getClass().getName());
}

/**
Expand Down Expand Up @@ -172,7 +177,7 @@ public Integer getInteger(String key) {
}

throw runtime("Metadata entry with the key '%s' has a value of '%s' and type '%s'. " +
"It cannot be returned as an Integer.", key, value, value.getClass().getName());
"It cannot be returned as an Integer.", key, value, value.getClass().getName());
}

/**
Expand Down Expand Up @@ -204,7 +209,7 @@ public Long getLong(String key) {
}

throw runtime("Metadata entry with the key '%s' has a value of '%s' and type '%s'. " +
"It cannot be returned as a Long.", key, value, value.getClass().getName());
"It cannot be returned as a Long.", key, value, value.getClass().getName());
}

/**
Expand Down Expand Up @@ -236,7 +241,7 @@ public Float getFloat(String key) {
}

throw runtime("Metadata entry with the key '%s' has a value of '%s' and type '%s'. " +
"It cannot be returned as a Float.", key, value, value.getClass().getName());
"It cannot be returned as a Float.", key, value, value.getClass().getName());
}

/**
Expand Down Expand Up @@ -268,7 +273,7 @@ public Double getDouble(String key) {
}

throw runtime("Metadata entry with the key '%s' has a value of '%s' and type '%s'. " +
"It cannot be returned as a Double.", key, value, value.getClass().getName());
"It cannot be returned as a Double.", key, value, value.getClass().getName());
}

/**
Expand Down Expand Up @@ -449,8 +454,8 @@ public int hashCode() {
@Override
public String toString() {
return "Metadata {" +
" metadata = " + metadata +
" }";
" metadata = " + metadata +
" }";
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
import static dev.langchain4j.data.message.ChatMessageType.AI;
import static dev.langchain4j.internal.Utils.isNullOrEmpty;
import static dev.langchain4j.internal.Utils.quoted;
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 java.util.Arrays.asList;

/**
Expand Down Expand Up @@ -90,7 +92,7 @@ public boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) return false;
AiMessage that = (AiMessage) o;
return Objects.equals(this.text, that.text)
&& Objects.equals(this.toolExecutionRequests, that.toolExecutionRequests);
&& Objects.equals(this.toolExecutionRequests, that.toolExecutionRequests);
}

@Override
Expand All @@ -101,9 +103,9 @@ public int hashCode() {
@Override
public String toString() {
return "AiMessage {" +
" text = " + quoted(text) +
" toolExecutionRequests = " + toolExecutionRequests +
" }";
" text = " + quoted(text) +
" toolExecutionRequests = " + toolExecutionRequests +
" }";
}

/**
Expand Down Expand Up @@ -139,7 +141,7 @@ public static AiMessage from(List<ToolExecutionRequest> toolExecutionRequests) {
/**
* Create a new {@link AiMessage} with the given text and tool execution requests.
*
* @param text the text of the message.
* @param text the text of the message.
* @param toolExecutionRequests the tool execution requests of the message.
* @return the new {@link AiMessage}.
*/
Expand Down Expand Up @@ -180,7 +182,7 @@ public static AiMessage aiMessage(List<ToolExecutionRequest> toolExecutionReques
/**
* Create a new {@link AiMessage} with the given text and tool execution requests.
*
* @param text the text of the message.
* @param text the text of the message.
* @param toolExecutionRequests the tool execution requests of the message.
* @return the new {@link AiMessage}.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package dev.langchain4j.data.message;

import static dev.langchain4j.data.message.ChatMessageSerializer.CODEC;
import java.util.List;

import static dev.langchain4j.data.message.ChatMessageSerializer.CODEC;

/**
* A deserializer for {@link ChatMessage} objects.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
package dev.langchain4j.data.message;

import com.google.gson.*;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;

import java.lang.reflect.Type;

class GsonChatMessageAdapter implements JsonDeserializer<ChatMessage>, JsonSerializer<ChatMessage> {

private static final Gson GSON = new GsonBuilder()
.registerTypeAdapter(Content.class, new GsonContentAdapter())
.registerTypeAdapter(TextContent.class, new GsonContentAdapter())
.registerTypeAdapter(ImageContent.class, new GsonContentAdapter())
.registerTypeAdapter(AudioContent.class, new GsonContentAdapter())
.registerTypeAdapter(VideoContent.class, new GsonContentAdapter())
.registerTypeAdapter(PdfFileContent.class, new GsonContentAdapter())
.create();
.registerTypeAdapter(Content.class, new GsonContentAdapter())
.registerTypeAdapter(TextContent.class, new GsonContentAdapter())
.registerTypeAdapter(ImageContent.class, new GsonContentAdapter())
.registerTypeAdapter(AudioContent.class, new GsonContentAdapter())
.registerTypeAdapter(VideoContent.class, new GsonContentAdapter())
.registerTypeAdapter(PdfFileContent.class, new GsonContentAdapter())
.create();

private static final String CHAT_MESSAGE_TYPE = "type"; // do not change, will break backward compatibility!

Expand All @@ -35,4 +42,4 @@ public ChatMessage deserialize(JsonElement messageJsonElement, Type ignored, Jso
}
return chatMessage;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package dev.langchain4j.data.message;

import static java.util.Collections.emptyList;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.List;

import static java.util.Collections.emptyList;

/**
* A codec for serializing and deserializing {@link ChatMessage} objects to and from JSON.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package dev.langchain4j.data.message;

import com.google.gson.*;
import com.google.gson.Gson;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;

import java.lang.reflect.Type;

Expand All @@ -23,4 +29,4 @@ public Content deserialize(JsonElement contentJsonElement, Type ignored, JsonDes
ContentType contentType = ContentType.valueOf(contentTypeString);
return GSON.fromJson(contentJsonElement, contentType.getContentClass());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import dev.langchain4j.data.message.ChatMessage;
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.chat.StreamingChatLanguageModel;
import lombok.Builder;

import java.util.List;

Expand All @@ -25,7 +24,6 @@ public class ChatModelRequest {
private final List<ChatMessage> messages;
private final List<ToolSpecification> toolSpecifications;

@Builder
public ChatModelRequest(String model,
Double temperature,
Double topP,
Expand All @@ -40,6 +38,10 @@ public ChatModelRequest(String model,
this.toolSpecifications = copyIfNotNull(toolSpecifications);
}

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

public String model() {
return model;
}
Expand All @@ -63,4 +65,54 @@ public List<ChatMessage> messages() {
public List<ToolSpecification> toolSpecifications() {
return toolSpecifications;
}

public static class ChatModelRequestBuilder {
private String model;
private Double temperature;
private Double topP;
private Integer maxTokens;
private List<ChatMessage> messages;
private List<ToolSpecification> toolSpecifications;

ChatModelRequestBuilder() {
}

public ChatModelRequestBuilder model(String model) {
this.model = model;
return this;
}

public ChatModelRequestBuilder temperature(Double temperature) {
this.temperature = temperature;
return this;
}

public ChatModelRequestBuilder topP(Double topP) {
this.topP = topP;
return this;
}

public ChatModelRequestBuilder maxTokens(Integer maxTokens) {
this.maxTokens = maxTokens;
return this;
}

public ChatModelRequestBuilder messages(List<ChatMessage> messages) {
this.messages = messages;
return this;
}

public ChatModelRequestBuilder toolSpecifications(List<ToolSpecification> toolSpecifications) {
this.toolSpecifications = toolSpecifications;
return this;
}

public ChatModelRequest build() {
return new ChatModelRequest(this.model, this.temperature, this.topP, this.maxTokens, this.messages, this.toolSpecifications);
}

public String toString() {
return "ChatModelRequest.ChatModelRequestBuilder(model=" + this.model + ", temperature=" + this.temperature + ", topP=" + this.topP + ", maxTokens=" + this.maxTokens + ", messages=" + this.messages + ", toolSpecifications=" + this.toolSpecifications + ")";
}
}
}
Loading

0 comments on commit 99dec54

Please sign in to comment.