Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add documentFormatter parameter to ContextualQueryAugmenter #2321

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import org.slf4j.Logger;
Expand Down Expand Up @@ -75,18 +76,30 @@ public final class ContextualQueryAugmenter implements QueryAugmenter {

private static final boolean DEFAULT_ALLOW_EMPTY_CONTEXT = false;

/**
* Default document formatter that just joins document text with newlines
*/
private static final Function<List<Document>, String> DEFAULT_DOCUMENT_FORMATTER = documents ->
documents.stream()
.map(Document::getText)
.collect(Collectors.joining(System.lineSeparator()));

private final PromptTemplate promptTemplate;

private final PromptTemplate emptyContextPromptTemplate;

private final boolean allowEmptyContext;

private final Function<List<Document>, String> documentFormatter;

public ContextualQueryAugmenter(@Nullable PromptTemplate promptTemplate,
@Nullable PromptTemplate emptyContextPromptTemplate, @Nullable Boolean allowEmptyContext) {
@Nullable PromptTemplate emptyContextPromptTemplate, @Nullable Boolean allowEmptyContext,
@Nullable Function<List<Document>, String> documentFormatter) {
this.promptTemplate = promptTemplate != null ? promptTemplate : DEFAULT_PROMPT_TEMPLATE;
this.emptyContextPromptTemplate = emptyContextPromptTemplate != null ? emptyContextPromptTemplate
: DEFAULT_EMPTY_CONTEXT_PROMPT_TEMPLATE;
this.allowEmptyContext = allowEmptyContext != null ? allowEmptyContext : DEFAULT_ALLOW_EMPTY_CONTEXT;
this.documentFormatter = documentFormatter != null ? documentFormatter : DEFAULT_DOCUMENT_FORMATTER;
PromptAssert.templateHasRequiredPlaceholders(this.promptTemplate, "query", "context");
}

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

// 1. Collect content from documents.
String documentContext = documents.stream()
.map(Document::getText)
.collect(Collectors.joining(System.lineSeparator()));
String documentContext = this.documentFormatter.apply(documents);

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

private Boolean allowEmptyContext;

private Function<List<Document>, String> documentFormatter;

public Builder promptTemplate(PromptTemplate promptTemplate) {
this.promptTemplate = promptTemplate;
return this;
Expand All @@ -149,9 +162,14 @@ public Builder allowEmptyContext(Boolean allowEmptyContext) {
return this;
}

public Builder documentFormatter(Function<List<Document>, String> documentFormatter) {
this.documentFormatter = documentFormatter;
return this;
}

public ContextualQueryAugmenter build() {
return new ContextualQueryAugmenter(this.promptTemplate, this.emptyContextPromptTemplate,
this.allowEmptyContext);
this.allowEmptyContext, this.documentFormatter);
}

}
Expand Down