Skip to content

Commit 698a531

Browse files
Seol-JYmarkpollack
authored andcommitted
Refactor ContentFormatTransformer for improved readability
- Extract Method - Replace explicit instanceof checks and casting with pattern matching - Change var to explicit type
1 parent fc7ee87 commit 698a531

File tree

2 files changed

+59
-37
lines changed

2 files changed

+59
-37
lines changed

models/spring-ai-openai/src/test/java/org/springframework/ai/openai/transformer/MetadataTransformerIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ public DefaultContentFormatter defaultContentFormatter() {
189189

190190
@Bean
191191
public ContentFormatTransformer contentFormatTransformer(DefaultContentFormatter defaultContentFormatter) {
192-
return new ContentFormatTransformer(defaultContentFormatter, false);
192+
return new ContentFormatTransformer(defaultContentFormatter);
193193
}
194194

195195
}

spring-ai-core/src/main/java/org/springframework/ai/transformer/ContentFormatTransformer.java

Lines changed: 58 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,38 @@
2424
import org.springframework.ai.document.DocumentTransformer;
2525

2626
/**
27+
* ContentFormatTransformer processes a list of documents by applying a content formatter
28+
* to each document.
29+
*
2730
* @author Christian Tzolov
31+
* @since 1.0.0
2832
*/
2933
public class ContentFormatTransformer implements DocumentTransformer {
3034

3135
/**
3236
* Disable the content-formatter template rewrite.
3337
*/
34-
private boolean disableTemplateRewrite = false;
38+
private final boolean disableTemplateRewrite;
3539

36-
private ContentFormatter contentFormatter;
40+
private final ContentFormatter contentFormatter;
3741

42+
/**
43+
* Creates a ContentFormatTransformer object with the given ContentFormatter.
44+
* @param contentFormatter the ContentFormatter to be used for transforming the
45+
* documents
46+
*/
3847
public ContentFormatTransformer(ContentFormatter contentFormatter) {
3948
this(contentFormatter, false);
4049
}
4150

51+
/**
52+
* The ContentFormatTransformer class is responsible for processing a list of
53+
* documents by applying a content formatter to each document.
54+
* @param contentFormatter The ContentFormatter to be used for transforming the
55+
* documents
56+
* @param disableTemplateRewrite Flag indicating whether to disable the
57+
* content-formatter template rewrite
58+
*/
4259
public ContentFormatTransformer(ContentFormatter contentFormatter, boolean disableTemplateRewrite) {
4360
this.contentFormatter = contentFormatter;
4461
this.disableTemplateRewrite = disableTemplateRewrite;
@@ -47,45 +64,50 @@ public ContentFormatTransformer(ContentFormatter contentFormatter, boolean disab
4764
/**
4865
* Post process documents chunked from loader. Allows extractors to be chained.
4966
* @param documents to post process.
50-
* @return
67+
* @return processed documents
5168
*/
5269
public List<Document> apply(List<Document> documents) {
53-
54-
if (this.contentFormatter != null) {
55-
56-
documents.forEach(document -> {
57-
// Update formatter
58-
if (document.getContentFormatter() instanceof DefaultContentFormatter
59-
&& this.contentFormatter instanceof DefaultContentFormatter) {
60-
61-
DefaultContentFormatter docFormatter = (DefaultContentFormatter) document.getContentFormatter();
62-
DefaultContentFormatter toUpdateFormatter = (DefaultContentFormatter) this.contentFormatter;
63-
64-
var updatedEmbedExcludeKeys = new ArrayList<>(docFormatter.getExcludedEmbedMetadataKeys());
65-
updatedEmbedExcludeKeys.addAll(toUpdateFormatter.getExcludedEmbedMetadataKeys());
66-
67-
var updatedInterfaceExcludeKeys = new ArrayList<>(docFormatter.getExcludedInferenceMetadataKeys());
68-
updatedInterfaceExcludeKeys.addAll(toUpdateFormatter.getExcludedInferenceMetadataKeys());
69-
70-
var builder = DefaultContentFormatter.builder()
71-
.withExcludedEmbedMetadataKeys(updatedEmbedExcludeKeys)
72-
.withExcludedInferenceMetadataKeys(updatedInterfaceExcludeKeys)
73-
.withMetadataTemplate(docFormatter.getMetadataTemplate())
74-
.withMetadataSeparator(docFormatter.getMetadataSeparator());
75-
76-
if (!this.disableTemplateRewrite) {
77-
builder.withTextTemplate(docFormatter.getTextTemplate());
78-
}
79-
document.setContentFormatter(builder.build());
80-
}
81-
else {
82-
// Override formatter
83-
document.setContentFormatter(this.contentFormatter);
84-
}
85-
});
70+
if (contentFormatter != null) {
71+
documents.forEach(this::processDocument);
8672
}
8773

8874
return documents;
8975
}
9076

77+
private void processDocument(Document document) {
78+
if (document.getContentFormatter() instanceof DefaultContentFormatter docFormatter
79+
&& contentFormatter instanceof DefaultContentFormatter toUpdateFormatter) {
80+
updateFormatter(document, docFormatter, toUpdateFormatter);
81+
82+
}
83+
else {
84+
overrideFormatter(document);
85+
}
86+
}
87+
88+
private void updateFormatter(Document document, DefaultContentFormatter docFormatter,
89+
DefaultContentFormatter toUpdateFormatter) {
90+
List<String> updatedEmbedExcludeKeys = new ArrayList<>(docFormatter.getExcludedEmbedMetadataKeys());
91+
updatedEmbedExcludeKeys.addAll(toUpdateFormatter.getExcludedEmbedMetadataKeys());
92+
93+
List<String> updatedInterfaceExcludeKeys = new ArrayList<>(docFormatter.getExcludedInferenceMetadataKeys());
94+
updatedInterfaceExcludeKeys.addAll(toUpdateFormatter.getExcludedInferenceMetadataKeys());
95+
96+
DefaultContentFormatter.Builder builder = DefaultContentFormatter.builder()
97+
.withExcludedEmbedMetadataKeys(updatedEmbedExcludeKeys)
98+
.withExcludedInferenceMetadataKeys(updatedInterfaceExcludeKeys)
99+
.withMetadataTemplate(docFormatter.getMetadataTemplate())
100+
.withMetadataSeparator(docFormatter.getMetadataSeparator());
101+
102+
if (!disableTemplateRewrite) {
103+
builder.withTextTemplate(docFormatter.getTextTemplate());
104+
}
105+
106+
document.setContentFormatter(builder.build());
107+
}
108+
109+
private void overrideFormatter(Document document) {
110+
document.setContentFormatter(contentFormatter);
111+
}
112+
91113
}

0 commit comments

Comments
 (0)