Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_0146opDKBYtADXvkc39zdiyw
63 lines
2.9 KiB
Java
63 lines
2.9 KiB
Java
package com.ankurm.ragingestion;
|
|
|
|
import java.nio.file.Path;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
import com.ankurm.ragingestion.support.Eval;
|
|
import com.ankurm.ragingestion.support.Fixtures;
|
|
import com.ankurm.ragingestion.support.Transcript;
|
|
import org.junit.jupiter.api.Test;
|
|
import org.junit.jupiter.api.io.TempDir;
|
|
import org.springframework.ai.document.DefaultContentFormatter;
|
|
import org.springframework.ai.document.Document;
|
|
import org.springframework.ai.document.MetadataMode;
|
|
import org.springframework.ai.embedding.EmbeddingModel;
|
|
import org.springframework.ai.reader.tika.TikaDocumentReader;
|
|
import org.springframework.core.io.FileSystemResource;
|
|
|
|
/** Does metadata you add for filtering end up inside the vector? It depends on the embedding model's MetadataMode. */
|
|
class MetadataEmbeddingTest {
|
|
|
|
@TempDir
|
|
Path dir;
|
|
|
|
@Test
|
|
void metadataInTheEmbedding() throws Exception {
|
|
List<Document> word = new TikaDocumentReader(new FileSystemResource(Fixtures.docx(dir))).get();
|
|
List<Document> chunks = new MetadataEnricher("runbook.docx", "docx").apply(new HeadingAwareChunker(false).apply(word));
|
|
Document sample = chunks.get(0);
|
|
|
|
try (Transcript t = new Transcript("04-metadata-in-the-embedding.txt", "Metadata and the text that gets embedded")) {
|
|
EmbeddingModel none = MiniLm.create(MetadataMode.NONE);
|
|
EmbeddingModel embed = MiniLm.create(MetadataMode.EMBED);
|
|
t.line("chunk metadata: %s", new java.util.TreeMap<>(sample.getMetadata()));
|
|
|
|
t.section("the text each model would embed for the same chunk");
|
|
t.line("MetadataMode.NONE : %s", one(none.getEmbeddingContent(sample), 110));
|
|
t.line("MetadataMode.EMBED: %s", one(embed.getEmbeddingContent(sample), 110));
|
|
|
|
t.section("hit rate, one chunk per paragraph, 37 questions");
|
|
t.line("%-58s %s", "configuration", Eval.Score.HEADER);
|
|
t.line("%-58s %s", "MetadataMode.NONE", Eval.score(chunks, none).row());
|
|
t.line("%-58s %s", "MetadataMode.EMBED, every key embedded", Eval.score(chunks, embed).row());
|
|
|
|
DefaultContentFormatter keep = DefaultContentFormatter.builder()
|
|
.withExcludedEmbedMetadataKeys("source", "format", "chunk_index", "content_hash").build();
|
|
List<Document> filtered = new ArrayList<>();
|
|
for (Document c : chunks) {
|
|
Document copy = c.mutate().build();
|
|
copy.setContentFormatter(keep);
|
|
filtered.add(copy);
|
|
}
|
|
t.line("%-58s %s", "MetadataMode.EMBED, four keys in excludedEmbedMetadataKeys", Eval.score(filtered, embed).row());
|
|
t.line("embedded text with the four keys excluded: %s", one(embed.getEmbeddingContent(filtered.get(0)), 110));
|
|
}
|
|
}
|
|
|
|
private static String one(String s, int n) {
|
|
String x = s.replace("\n", "\\n");
|
|
return x.length() <= n ? x : x.substring(0, n);
|
|
}
|
|
}
|