Skip to content
Merged
Show file tree
Hide file tree
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 @@ -19,7 +19,6 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -121,13 +120,13 @@ public String format(Document document, MetadataMode metadataMode) {
* @param metadata Document metadata.
* @return Returns the filtered by configured mode metadata.
*/
protected Map<String, Object> metadataFilter(Map<String, Object> metadata, MetadataMode metadataMode) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

protected modifier is meaningless since the class is final.

private Map<String, Object> metadataFilter(Map<String, Object> metadata, MetadataMode metadataMode) {

if (metadataMode == MetadataMode.ALL) {
return new HashMap<>(metadata);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Creating new HashMap is unnecessary here

return metadata;
}
if (metadataMode == MetadataMode.NONE) {
return new HashMap<>(Collections.emptyMap());
return Collections.emptyMap();
}

Set<String> usableMetadataKeys = new HashSet<>(metadata.keySet());
Expand All @@ -139,10 +138,10 @@ else if (metadataMode == MetadataMode.EMBED) {
usableMetadataKeys.removeAll(this.excludedEmbedMetadataKeys);
}

return new HashMap<>(metadata.entrySet()
return metadata.entrySet()
.stream()
.filter(e -> usableMetadataKeys.contains(e.getKey()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)));
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}

public String getMetadataTemplate() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
* @author Mark Pollack
* @author Laura Trotta
* @author Jihoon Kim
* @author Yanming Zhou
* @since 1.0.0
*/
public class TokenCountBatchingStrategy implements BatchingStrategy {
Expand Down Expand Up @@ -153,15 +154,15 @@ public List<List<Document>> batch(List<Document> documents) {
documentTokens.put(document, tokenCount);
}

for (Document document : documentTokens.keySet()) {
Integer tokenCount = documentTokens.get(document);
if (currentSize + tokenCount > this.maxInputTokenCount) {
for (Map.Entry<Document, Integer> entry : documentTokens.entrySet()) {
Document document = entry.getKey();
currentSize += entry.getValue();
if (currentSize > this.maxInputTokenCount) {
batches.add(currentBatch);
currentBatch = new ArrayList<>();
currentSize = 0;
}
currentBatch.add(document);
currentSize += tokenCount;
}
if (!currentBatch.isEmpty()) {
batches.add(currentBatch);
Expand Down