Skip to content

Commit ba7f173

Browse files
committed
refactor(dotAI): convert ProviderConfig to Immutables interface
1 parent d9078de commit ba7f173

2 files changed

Lines changed: 47 additions & 175 deletions

File tree

dotCMS/src/main/java/com/dotcms/ai/client/langchain4j/LangChain4jModelFactory.java

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,15 @@ public static ImageModel buildImageModel(final ProviderConfig config) {
5858
private static <T> T build(final ProviderConfig config,
5959
final String modelType,
6060
final Function<ProviderConfig, T> openAiFn) {
61-
if (config == null || config.getProvider() == null) {
61+
if (config == null || config.provider() == null) {
6262
return null;
6363
}
64-
switch (config.getProvider().toLowerCase()) {
64+
switch (config.provider().toLowerCase()) {
6565
case "openai":
6666
return openAiFn.apply(config);
6767
default:
6868
throw new IllegalArgumentException("Unsupported " + modelType + " provider: "
69-
+ config.getProvider() + ". Supported in Phase 1: openai");
69+
+ config.provider() + ". Supported in Phase 1: openai");
7070
}
7171
}
7272

@@ -76,42 +76,42 @@ private static void applyCommonConfig(final ProviderConfig config,
7676
final Consumer<String> baseUrlFn,
7777
final Consumer<Integer> retriesFn,
7878
final Consumer<Duration> timeoutFn) {
79-
if (config.getEndpoint() != null) baseUrlFn.accept(config.getEndpoint());
80-
if (config.getMaxRetries() != null) retriesFn.accept(config.getMaxRetries());
81-
if (config.getTimeout() != null) timeoutFn.accept(Duration.ofSeconds(config.getTimeout()));
79+
if (config.endpoint() != null) baseUrlFn.accept(config.endpoint());
80+
if (config.maxRetries() != null) retriesFn.accept(config.maxRetries());
81+
if (config.timeout() != null) timeoutFn.accept(Duration.ofSeconds(config.timeout()));
8282
}
8383

8484
private static ChatModel buildOpenAiChatModel(final ProviderConfig config) {
8585
final OpenAiChatModel.OpenAiChatModelBuilder builder = OpenAiChatModel.builder()
86-
.apiKey(config.getApiKey())
87-
.modelName(config.getModel());
86+
.apiKey(config.apiKey())
87+
.modelName(config.model());
8888
applyCommonConfig(config, builder::baseUrl, builder::maxRetries, builder::timeout);
89-
if (config.getMaxCompletionTokens() != null) {
90-
builder.maxCompletionTokens(config.getMaxCompletionTokens());
91-
} else if (config.getMaxTokens() != null) {
92-
builder.maxTokens(config.getMaxTokens());
89+
if (config.maxCompletionTokens() != null) {
90+
builder.maxCompletionTokens(config.maxCompletionTokens());
91+
} else if (config.maxTokens() != null) {
92+
builder.maxTokens(config.maxTokens());
9393
}
94-
if (config.getTemperature() != null) {
95-
builder.temperature(config.getTemperature());
94+
if (config.temperature() != null) {
95+
builder.temperature(config.temperature());
9696
}
9797
return builder.build();
9898
}
9999

100100
private static EmbeddingModel buildOpenAiEmbeddingModel(final ProviderConfig config) {
101101
final OpenAiEmbeddingModel.OpenAiEmbeddingModelBuilder builder = OpenAiEmbeddingModel.builder()
102-
.apiKey(config.getApiKey())
103-
.modelName(config.getModel());
102+
.apiKey(config.apiKey())
103+
.modelName(config.model());
104104
applyCommonConfig(config, builder::baseUrl, builder::maxRetries, builder::timeout);
105105
return builder.build();
106106
}
107107

108108
private static ImageModel buildOpenAiImageModel(final ProviderConfig config) {
109109
final OpenAiImageModel.OpenAiImageModelBuilder builder = OpenAiImageModel.builder()
110-
.apiKey(config.getApiKey())
111-
.modelName(config.getModel());
110+
.apiKey(config.apiKey())
111+
.modelName(config.model());
112112
applyCommonConfig(config, builder::baseUrl, builder::maxRetries, builder::timeout);
113-
if (config.getSize() != null) {
114-
builder.size(config.getSize());
113+
if (config.size() != null) {
114+
builder.size(config.size());
115115
}
116116
return builder.build();
117117
}
Lines changed: 27 additions & 155 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
package com.dotcms.ai.client.langchain4j;
22

33
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
4+
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
5+
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
6+
import org.immutables.value.Value;
7+
8+
import javax.annotation.Nullable;
49

510
/**
6-
* Deserializable POJO for a single provider section in the {@code providerConfig} JSON.
11+
* Immutable representation of a single provider section in the {@code providerConfig} JSON.
712
*
813
* <p>Each section (chat, embeddings, image) in the JSON maps to one instance of this class.
914
* Unknown fields are ignored to allow forward-compatible configuration.
@@ -40,167 +45,34 @@
4045
* <li>{@code location}</li>
4146
* </ul>
4247
*/
48+
@Value.Immutable
49+
@JsonSerialize(as = ImmutableProviderConfig.class)
50+
@JsonDeserialize(as = ImmutableProviderConfig.class)
4351
@JsonIgnoreProperties(ignoreUnknown = true)
44-
public class ProviderConfig {
52+
public interface ProviderConfig {
4553

46-
private String provider;
47-
private String model;
48-
private Integer maxTokens;
49-
private Integer maxCompletionTokens;
50-
private Double temperature;
51-
private Integer maxRetries;
52-
private Integer timeout;
54+
@Nullable String provider();
55+
@Nullable String model();
56+
@Nullable Integer maxTokens();
57+
@Nullable Integer maxCompletionTokens();
58+
@Nullable Double temperature();
59+
@Nullable Integer maxRetries();
60+
@Nullable Integer timeout();
5361

5462
// OpenAI / Azure OpenAI
55-
private String apiKey;
56-
private String size;
57-
private String endpoint;
58-
private String deploymentName;
59-
private String apiVersion;
63+
@Nullable String apiKey();
64+
@Nullable String size();
65+
@Nullable String endpoint();
66+
@Nullable String deploymentName();
67+
@Nullable String apiVersion();
6068

6169
// AWS Bedrock
62-
private String region;
63-
private String accessKeyId;
64-
private String secretAccessKey;
70+
@Nullable String region();
71+
@Nullable String accessKeyId();
72+
@Nullable String secretAccessKey();
6573

6674
// Google Vertex AI
67-
private String projectId;
68-
private String location;
69-
70-
public String getProvider() {
71-
return provider;
72-
}
73-
74-
public void setProvider(final String provider) {
75-
this.provider = provider;
76-
}
77-
78-
public String getModel() {
79-
return model;
80-
}
81-
82-
public void setModel(final String model) {
83-
this.model = model;
84-
}
85-
86-
public Integer getMaxTokens() {
87-
return maxTokens;
88-
}
89-
90-
public void setMaxTokens(final Integer maxTokens) {
91-
this.maxTokens = maxTokens;
92-
}
93-
94-
public Integer getMaxCompletionTokens() {
95-
return maxCompletionTokens;
96-
}
97-
98-
public void setMaxCompletionTokens(final Integer maxCompletionTokens) {
99-
this.maxCompletionTokens = maxCompletionTokens;
100-
}
101-
102-
public Double getTemperature() {
103-
return temperature;
104-
}
105-
106-
public void setTemperature(final Double temperature) {
107-
this.temperature = temperature;
108-
}
109-
110-
public Integer getMaxRetries() {
111-
return maxRetries;
112-
}
113-
114-
public void setMaxRetries(final Integer maxRetries) {
115-
this.maxRetries = maxRetries;
116-
}
117-
118-
public Integer getTimeout() {
119-
return timeout;
120-
}
121-
122-
public void setTimeout(final Integer timeout) {
123-
this.timeout = timeout;
124-
}
125-
126-
public String getApiKey() {
127-
return apiKey;
128-
}
129-
130-
public void setApiKey(final String apiKey) {
131-
this.apiKey = apiKey;
132-
}
133-
134-
public String getSize() {
135-
return size;
136-
}
137-
138-
public void setSize(final String size) {
139-
this.size = size;
140-
}
141-
142-
public String getEndpoint() {
143-
return endpoint;
144-
}
145-
146-
public void setEndpoint(final String endpoint) {
147-
this.endpoint = endpoint;
148-
}
149-
150-
public String getDeploymentName() {
151-
return deploymentName;
152-
}
153-
154-
public void setDeploymentName(final String deploymentName) {
155-
this.deploymentName = deploymentName;
156-
}
157-
158-
public String getApiVersion() {
159-
return apiVersion;
160-
}
161-
162-
public void setApiVersion(final String apiVersion) {
163-
this.apiVersion = apiVersion;
164-
}
165-
166-
public String getRegion() {
167-
return region;
168-
}
169-
170-
public void setRegion(final String region) {
171-
this.region = region;
172-
}
173-
174-
public String getAccessKeyId() {
175-
return accessKeyId;
176-
}
177-
178-
public void setAccessKeyId(final String accessKeyId) {
179-
this.accessKeyId = accessKeyId;
180-
}
181-
182-
public String getSecretAccessKey() {
183-
return secretAccessKey;
184-
}
185-
186-
public void setSecretAccessKey(final String secretAccessKey) {
187-
this.secretAccessKey = secretAccessKey;
188-
}
189-
190-
public String getProjectId() {
191-
return projectId;
192-
}
193-
194-
public void setProjectId(final String projectId) {
195-
this.projectId = projectId;
196-
}
197-
198-
public String getLocation() {
199-
return location;
200-
}
201-
202-
public void setLocation(final String location) {
203-
this.location = location;
204-
}
75+
@Nullable String projectId();
76+
@Nullable String location();
20577

20678
}

0 commit comments

Comments
 (0)