Skip to content

Commit bb7c52c

Browse files
magicgone-cnilayaperumalg
authored andcommitted
Add documentFormatter parameter to ContextualQueryAugmenter
This enhancement adds a documentFormatter parameter to ContextualQueryAugmenter, allowing users to customize how documents are formatted in the context. If not specified, the original document formatting logic is preserved. Signed-off-by: magicgone <[email protected]>
1 parent 747524b commit bb7c52c

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

Diff for: spring-ai-core/src/main/java/org/springframework/ai/rag/generation/augmentation/ContextualQueryAugmenter.java

+22-5
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.util.List;
2020
import java.util.Map;
21+
import java.util.function.Function;
2122
import java.util.stream.Collectors;
2223

2324
import org.slf4j.Logger;
@@ -75,18 +76,29 @@ public final class ContextualQueryAugmenter implements QueryAugmenter {
7576

7677
private static final boolean DEFAULT_ALLOW_EMPTY_CONTEXT = false;
7778

79+
/**
80+
* Default document formatter that just joins document text with newlines
81+
*/
82+
private static final Function<List<Document>, String> DEFAULT_DOCUMENT_FORMATTER = documents -> documents.stream()
83+
.map(Document::getText)
84+
.collect(Collectors.joining(System.lineSeparator()));
85+
7886
private final PromptTemplate promptTemplate;
7987

8088
private final PromptTemplate emptyContextPromptTemplate;
8189

8290
private final boolean allowEmptyContext;
8391

92+
private final Function<List<Document>, String> documentFormatter;
93+
8494
public ContextualQueryAugmenter(@Nullable PromptTemplate promptTemplate,
85-
@Nullable PromptTemplate emptyContextPromptTemplate, @Nullable Boolean allowEmptyContext) {
95+
@Nullable PromptTemplate emptyContextPromptTemplate, @Nullable Boolean allowEmptyContext,
96+
@Nullable Function<List<Document>, String> documentFormatter) {
8697
this.promptTemplate = promptTemplate != null ? promptTemplate : DEFAULT_PROMPT_TEMPLATE;
8798
this.emptyContextPromptTemplate = emptyContextPromptTemplate != null ? emptyContextPromptTemplate
8899
: DEFAULT_EMPTY_CONTEXT_PROMPT_TEMPLATE;
89100
this.allowEmptyContext = allowEmptyContext != null ? allowEmptyContext : DEFAULT_ALLOW_EMPTY_CONTEXT;
101+
this.documentFormatter = documentFormatter != null ? documentFormatter : DEFAULT_DOCUMENT_FORMATTER;
90102
PromptAssert.templateHasRequiredPlaceholders(this.promptTemplate, "query", "context");
91103
}
92104

@@ -102,9 +114,7 @@ public Query augment(Query query, List<Document> documents) {
102114
}
103115

104116
// 1. Collect content from documents.
105-
String documentContext = documents.stream()
106-
.map(Document::getText)
107-
.collect(Collectors.joining(System.lineSeparator()));
117+
String documentContext = this.documentFormatter.apply(documents);
108118

109119
// 2. Define prompt parameters.
110120
Map<String, Object> promptParameters = Map.of("query", query.text(), "context", documentContext);
@@ -134,6 +144,8 @@ public static class Builder {
134144

135145
private Boolean allowEmptyContext;
136146

147+
private Function<List<Document>, String> documentFormatter;
148+
137149
public Builder promptTemplate(PromptTemplate promptTemplate) {
138150
this.promptTemplate = promptTemplate;
139151
return this;
@@ -149,9 +161,14 @@ public Builder allowEmptyContext(Boolean allowEmptyContext) {
149161
return this;
150162
}
151163

164+
public Builder documentFormatter(Function<List<Document>, String> documentFormatter) {
165+
this.documentFormatter = documentFormatter;
166+
return this;
167+
}
168+
152169
public ContextualQueryAugmenter build() {
153170
return new ContextualQueryAugmenter(this.promptTemplate, this.emptyContextPromptTemplate,
154-
this.allowEmptyContext);
171+
this.allowEmptyContext, this.documentFormatter);
155172
}
156173

157174
}

0 commit comments

Comments
 (0)