Skip to content

Commit 027b573

Browse files
committed
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 70fe412 commit 027b573

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

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

Lines changed: 23 additions & 5 deletions
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,30 @@ 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 ->
83+
documents.stream()
84+
.map(Document::getText)
85+
.collect(Collectors.joining(System.lineSeparator()));
86+
7887
private final PromptTemplate promptTemplate;
7988

8089
private final PromptTemplate emptyContextPromptTemplate;
8190

8291
private final boolean allowEmptyContext;
8392

93+
private final Function<List<Document>, String> documentFormatter;
94+
8495
public ContextualQueryAugmenter(@Nullable PromptTemplate promptTemplate,
85-
@Nullable PromptTemplate emptyContextPromptTemplate, @Nullable Boolean allowEmptyContext) {
96+
@Nullable PromptTemplate emptyContextPromptTemplate, @Nullable Boolean allowEmptyContext,
97+
@Nullable Function<List<Document>, String> documentFormatter) {
8698
this.promptTemplate = promptTemplate != null ? promptTemplate : DEFAULT_PROMPT_TEMPLATE;
8799
this.emptyContextPromptTemplate = emptyContextPromptTemplate != null ? emptyContextPromptTemplate
88100
: DEFAULT_EMPTY_CONTEXT_PROMPT_TEMPLATE;
89101
this.allowEmptyContext = allowEmptyContext != null ? allowEmptyContext : DEFAULT_ALLOW_EMPTY_CONTEXT;
102+
this.documentFormatter = documentFormatter != null ? documentFormatter : DEFAULT_DOCUMENT_FORMATTER;
90103
PromptAssert.templateHasRequiredPlaceholders(this.promptTemplate, "query", "context");
91104
}
92105

@@ -102,9 +115,7 @@ public Query augment(Query query, List<Document> documents) {
102115
}
103116

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

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

135146
private Boolean allowEmptyContext;
136147

148+
private Function<List<Document>, String> documentFormatter;
149+
137150
public Builder promptTemplate(PromptTemplate promptTemplate) {
138151
this.promptTemplate = promptTemplate;
139152
return this;
@@ -149,9 +162,14 @@ public Builder allowEmptyContext(Boolean allowEmptyContext) {
149162
return this;
150163
}
151164

165+
public Builder documentFormatter(Function<List<Document>, String> documentFormatter) {
166+
this.documentFormatter = documentFormatter;
167+
return this;
168+
}
169+
152170
public ContextualQueryAugmenter build() {
153171
return new ContextualQueryAugmenter(this.promptTemplate, this.emptyContextPromptTemplate,
154-
this.allowEmptyContext);
172+
this.allowEmptyContext, this.documentFormatter);
155173
}
156174

157175
}

0 commit comments

Comments
 (0)