Skip to content

Commit 3092efa

Browse files
quaffericbottard
authored andcommitted
Polishing
Signed-off-by: Yanming Zhou <[email protected]>
1 parent 1b69fd5 commit 3092efa

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

spring-ai-commons/src/main/java/org/springframework/ai/document/DefaultContentFormatter.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import java.util.ArrayList;
2020
import java.util.Arrays;
2121
import java.util.Collections;
22-
import java.util.HashMap;
2322
import java.util.HashSet;
2423
import java.util.List;
2524
import java.util.Map;
@@ -121,13 +120,13 @@ public String format(Document document, MetadataMode metadataMode) {
121120
* @param metadata Document metadata.
122121
* @return Returns the filtered by configured mode metadata.
123122
*/
124-
protected Map<String, Object> metadataFilter(Map<String, Object> metadata, MetadataMode metadataMode) {
123+
private Map<String, Object> metadataFilter(Map<String, Object> metadata, MetadataMode metadataMode) {
125124

126125
if (metadataMode == MetadataMode.ALL) {
127-
return new HashMap<>(metadata);
126+
return metadata;
128127
}
129128
if (metadataMode == MetadataMode.NONE) {
130-
return new HashMap<>(Collections.emptyMap());
129+
return Collections.emptyMap();
131130
}
132131

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

142-
return new HashMap<>(metadata.entrySet()
141+
return metadata.entrySet()
143142
.stream()
144143
.filter(e -> usableMetadataKeys.contains(e.getKey()))
145-
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)));
144+
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
146145
}
147146

148147
public String getMetadataTemplate() {

spring-ai-commons/src/main/java/org/springframework/ai/embedding/TokenCountBatchingStrategy.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
* @author Mark Pollack
5151
* @author Laura Trotta
5252
* @author Jihoon Kim
53+
* @author Yanming Zhou
5354
* @since 1.0.0
5455
*/
5556
public class TokenCountBatchingStrategy implements BatchingStrategy {
@@ -153,15 +154,15 @@ public List<List<Document>> batch(List<Document> documents) {
153154
documentTokens.put(document, tokenCount);
154155
}
155156

156-
for (Document document : documentTokens.keySet()) {
157-
Integer tokenCount = documentTokens.get(document);
158-
if (currentSize + tokenCount > this.maxInputTokenCount) {
157+
for (Map.Entry<Document, Integer> entry : documentTokens.entrySet()) {
158+
Document document = entry.getKey();
159+
currentSize += entry.getValue();
160+
if (currentSize > this.maxInputTokenCount) {
159161
batches.add(currentBatch);
160162
currentBatch = new ArrayList<>();
161163
currentSize = 0;
162164
}
163165
currentBatch.add(document);
164-
currentSize += tokenCount;
165166
}
166167
if (!currentBatch.isEmpty()) {
167168
batches.add(currentBatch);

0 commit comments

Comments
 (0)