Compare commits

...
2 Commits
60 changed files with 3207 additions and 0 deletions
+2
View File
@@ -25,5 +25,7 @@ Runnable companion code for the Spring AI articles on [ankurm.com](https://ankur
| [`semantic-cache/`](semantic-cache) | A `CallAdvisor` that serves a stored answer when a new question embeds close to an old one, over a Redis Stack vector index and a real in-process embedding model (all-MiniLM-L6-v2). A labelled set of 30 intents x 4 phrasings plus 20 off-topic questions swept across thresholds (hit rate against wrong answers), 600 requests replayed with an illustrative cost model, and the traps reproduced: Spring AI's score is `(1 + cosine) / 2`, reset-password and reset-2FA are close enough to answer for each other, a tenant filter, expiry, and an index built for another embedding model. The chat model is a script that counts calls; one test uses a real local model. Spring Boot 4.1.1, Spring AI 2.0.1, Java 25. | [Semantic Caching for LLM Calls in Spring Boot with Redis Vector Search](https://ankurm.com/semantic-caching-for-llm-calls-in-spring-boot-with-redis-vector-search/) |
| [`langchain4j-interop/`](langchain4j-interop) | The same order assistant in Spring AI 2.0.1 and LangChain4j 1.22.0 in one Spring Boot 4.1.1 app, against a local fake OpenAI server: the bean collision when both OpenAI starters are present, the two requests compared on the wire, a throwing tool and a looping model (what each framework bounds), the meters each records, one MCP tool server used from both, and which lines of the 2025 version of the article still compile. Java 25. | [Spring AI vs LangChain4j in 2026: Is the Pairing Still Worth It?](https://ankurm.com/spring-ai-langchain4j-production-ai-microservices-java/) |
| [`agent-skills/`](agent-skills) | `SKILL.md` folders loaded on demand by the `Skill` tool from `spring-ai-agent-utils` 0.13.0: what the model sees before it loads anything (name and description only), the two-call load round trip, unknown, duplicate and description-less skills, and listing size against inlining every body (character counts). Scripted chat model, no live model. Spring Boot 4.1.1, Spring AI 2.0.1, Java 25. | [Agent Skills in Spring AI: Packaging Reusable Capabilities for Your Agents](https://ankurm.com/agent-skills-spring-ai-packaging-reusable-capabilities/) |
| [`rag-ingestion/`](rag-ingestion) | PDF, Word and HTML versions of one 12-section runbook through `PagePdfDocumentReader`, `TikaDocumentReader` and `JsoupDocumentReader`, then `TokenTextSplitter` at six sizes, a paragraph chunker, a sentence splitter and a section chunker; retrieval hit@1/3/5 on 37 fixed questions, metadata in the embedding, content-hash idempotent ingestion into pgvector and the re-embedding cost of one edit. Local all-MiniLM-L6-v2; a synthetic corpus, so differences of two or three questions are noise. Spring Boot 4.1.2, Spring AI 2.0.2, Java 25. | [Spring AI RAG Ingestion Pipeline: PDF, Word and HTML Readers with Chunking Strategies Compared](https://ankurm.com/spring-ai-rag-ingestion-pipeline-pdf-word-html-chunking/) |
| [`hybrid-search/`](hybrid-search) | Vector, `tsvector` and a Java Okapi BM25 over one PostgreSQL table (pgvector HNSW + GIN), fused with Reciprocal Rank Fusion in Java and in a single SQL statement, behind a `DocumentRetriever` for `RetrievalAugmentationAdvisor`. 60 questions in four kinds over 94 passages: hybrid helps exact error codes and hurts paraphrases; `k` tuned on the evaluation set did not hold up on held-out folds; plans and latency at 94 and 50,094 rows. Local all-MiniLM-L6-v2. Spring Boot 4.1.2, Spring AI 2.0.2, Java 25. | [Hybrid Search in Java: BM25 + Vector with PostgreSQL and Reciprocal Rank Fusion](https://ankurm.com/hybrid-search-java-pgvector-tsvector-reciprocal-rank-fusion/) |
Upgrading from Spring AI 1.x: [migration guide](https://ankurm.com/spring-ai-1-to-2-migration-guide/).
+3
View File
@@ -0,0 +1,3 @@
target/
*.iml
.idea/
+7
View File
@@ -0,0 +1,7 @@
# hybrid-search
Companion code for [Hybrid Search in Java: BM25 + Vector with PostgreSQL and Reciprocal Rank Fusion](https://ankurm.com/hybrid-search-java-pgvector-tsvector-reciprocal-rank-fusion/).
One PostgreSQL table holds a `vector(384)` column (pgvector, HNSW) and a generated `tsvector` column (GIN). Vector search, keyword search and Reciprocal Rank Fusion are measured on 94 passages and 60 questions in four kinds. Spring Boot 4.1.2, Spring AI 2.0.2, Java 25.
Run `scripts/run-all.sh` (needs PostgreSQL 16 with pgvector; see the script header). Every number in the article is a line in `output/`.
+34
View File
@@ -0,0 +1,34 @@
# Reciprocal Rank Fusion on two short rankings
vector ranking : [A, B, C, D]
keyword ranking: [C, A, E, B]
--- k = 1 ---
A score 0.83333 (vector rank 1, keyword rank 2)
C score 0.75000 (vector rank 3, keyword rank 1)
B score 0.53333 (vector rank 2, keyword rank 4)
E score 0.25000 (vector rank 0, keyword rank 3)
D score 0.20000 (vector rank 4, keyword rank 0)
--- k = 60 ---
A score 0.03252 (vector rank 1, keyword rank 2)
C score 0.03227 (vector rank 3, keyword rank 1)
B score 0.03175 (vector rank 2, keyword rank 4)
E score 0.01587 (vector rank 0, keyword rank 3)
D score 0.01563 (vector rank 4, keyword rank 0)
--- k = 1000 ---
A score 0.00200 (vector rank 1, keyword rank 2)
C score 0.00200 (vector rank 3, keyword rank 1)
B score 0.00199 (vector rank 2, keyword rank 4)
E score 0.00100 (vector rank 0, keyword rank 3)
D score 0.00100 (vector rank 4, keyword rank 0)
--- the arithmetic for A at k = 60 ---
rank 1 in the vector list: 1/(60+1) = 0.01639
rank 2 in the keyword list: 1/(60+2) = 0.01613
sum = 0.03252
--- an item in one list only ---
E is rank 3 in the keyword list and absent from the vector list: 1/(60+3) = 0.01587
D is rank 4 in the vector list and absent from the keyword list: 1/(60+4) = 0.01563
@@ -0,0 +1,73 @@
# Six rankers on 60 labelled queries over 94 passages (all-MiniLM-L6-v2, PostgreSQL 16, pgvector 0.8.7)
fusion: RRF with k=60 over the top 20 of each list. hit@1 and hit@5: the answer passage is first / in the first five. MRR@10: mean of 1/rank.
--- exact error code (12 queries) ---
ranker hit@1 hit@5 MRR@10
vector (pgvector cosine) 2/12 8/12 0.374
keyword (tsvector, ts_rank_cd) 9/12 11/12 0.819
keyword (BM25, in Java) 9/12 12/12 0.875
hybrid: vector + tsvector, RRF 9/12 11/12 0.806
hybrid: vector + BM25, RRF 10/12 11/12 0.883
hybrid in one SQL statement 9/12 11/12 0.806
--- exact SKU or setting (12 queries) ---
ranker hit@1 hit@5 MRR@10
vector (pgvector cosine) 12/12 12/12 1.000
keyword (tsvector, ts_rank_cd) 12/12 12/12 1.000
keyword (BM25, in Java) 11/12 12/12 0.958
hybrid: vector + tsvector, RRF 12/12 12/12 1.000
hybrid: vector + BM25, RRF 12/12 12/12 1.000
hybrid in one SQL statement 12/12 12/12 1.000
--- paraphrase (24 queries) ---
ranker hit@1 hit@5 MRR@10
vector (pgvector cosine) 15/24 23/24 0.767
keyword (tsvector, ts_rank_cd) 5/24 11/24 0.296
keyword (BM25, in Java) 4/24 13/24 0.290
hybrid: vector + tsvector, RRF 13/24 20/24 0.663
hybrid: vector + BM25, RRF 11/24 21/24 0.626
hybrid in one SQL statement 13/24 20/24 0.663
--- code plus words (12 queries) ---
ranker hit@1 hit@5 MRR@10
vector (pgvector cosine) 12/12 12/12 1.000
keyword (tsvector, ts_rank_cd) 10/12 12/12 0.896
keyword (BM25, in Java) 11/12 12/12 0.958
hybrid: vector + tsvector, RRF 12/12 12/12 1.000
hybrid: vector + BM25, RRF 12/12 12/12 1.000
hybrid in one SQL statement 12/12 12/12 1.000
--- all 60 queries ---
ranker hit@1 hit@5 MRR@10
vector (pgvector cosine) 41/60 55/60 0.782
keyword (tsvector, ts_rank_cd) 36/60 46/60 0.662
keyword (BM25, in Java) 35/60 49/60 0.674
hybrid: vector + tsvector, RRF 46/60 55/60 0.826
hybrid: vector + BM25, RRF 45/60 56/60 0.827
hybrid in one SQL statement 46/60 55/60 0.826
--- the SQL version against the Java version ---
queries whose top 5 are identical, in the same order, from both implementations: 60 of 60
--- one query of each kind: the top 3 from each ranker ---
[exact error code] "What does error E-4106 mean?" -> answer is E-4106
vector : E-4108, E-4105, E-4114
tsvector : E-4106, E-4109, E-4139
BM25 : E-4109, E-4106, E-4140
hybrid : E-4108, E-4109, E-4111
[exact SKU or setting] "Does OP-TERM-5520 work when the internet is down?" -> answer is OP-TERM-5520
vector : OP-TERM-5520, OP-SUP-GOLD, G-30
tsvector : OP-TERM-5520, OP-TERM-6100, OP-TERM-5530
BM25 : OP-TERM-5520
hybrid : OP-TERM-5520, OP-SUP-GOLD, OP-TERM-5530
[paraphrase] "What happens when a customer's monthly payment does not go through?" -> answer is G-01
vector : G-15, G-04, G-16
tsvector : E-4110, E-4111, G-15
BM25 : G-04, G-06, orbit.receipts.locale
hybrid : G-15, G-16, G-04
[code plus words] "E-4106 keeps appearing in our Express webhook handler, is the body being parsed?" -> answer is E-4106
vector : E-4106, E-4107, E-4116
tsvector : E-4106, E-4102, E-4107
BM25 : E-4106, E-4102, E-4107
hybrid : E-4106, E-4107, E-4102
@@ -0,0 +1,26 @@
# RRF parameters on the 60 queries: k, candidates per list, list weights
--- k, with 20 candidates per list ---
k hit@1 hit@5 MRR@10
1 46/60 58/60 0.851
10 45/60 55/60 0.826
60 46/60 55/60 0.826
200 46/60 55/60 0.825
1000 46/60 55/60 0.825
--- candidates taken from each list, with k = 60 ---
cands hit@1 hit@5 MRR@10
3 41/60 57/60 0.802
5 43/60 59/60 0.828
10 45/60 57/60 0.823
20 46/60 55/60 0.826
50 46/60 55/60 0.829
--- weights (vector : keyword), k = 60, 20 candidates ---
weights hit@1 hit@5 MRR@10
1.0:0.0 41/60 55/60 0.782
1.0:0.5 45/60 56/60 0.822
1.0:1.0 46/60 55/60 0.826
0.5:1.0 42/60 54/60 0.781
0.0:1.0 37/60 50/60 0.692
+15
View File
@@ -0,0 +1,15 @@
# Choosing k on half of the queries and scoring it on the other half
candidates per list: 20. The 60 queries are split by position into two folds of 30; k comes from {1, 10, 60, 200}.
--- fold 1: train on 30, test on 30 ---
k chosen on the training half (best MRR@10 0.872): 1
held-out half, k=1 : 22/30 29/30 0.831
held-out half, k=60 : 24/30 28/30 0.859
held-out half, vector only: 21/30 30/30 0.831
--- fold 2: train on 30, test on 30 ---
k chosen on the training half (best MRR@10 0.859): 60
held-out half, k=60 : 22/30 27/30 0.794
held-out half, k=60 : 22/30 27/30 0.794
held-out half, vector only: 20/30 25/30 0.733
@@ -0,0 +1,43 @@
# HybridDocumentRetriever inside RetrievalAugmentationAdvisor (top 3 passages per question)
--- what the retriever returns for "Why am I getting E-4116?" ---
score 0.03279 vector rank 1 keyword rank 1 Error E-4116 - Charge already captured. A capture was attemp
score 0.03125 vector rank 4 keyword rank 4 Error E-4111 - Authentication challenge failed. The customer
score 0.02988 vector rank 9 keyword rank 5 Error E-4118 - Payout account not verified. Payouts are bloc
--- the prompt the model received ---
Context information is below.
---------------------
Error E-4116 - Charge already captured. A capture was attempted on a charge that has already been captured in full. Fetch the charge first and check its status instead of capturing blindly.
Error E-4111 - Authentication challenge failed. The customer abandoned or failed the 3-D Secure challenge. The payment cannot be completed with this card; offer another payment method.
Error E-4118 - Payout account not verified. Payouts are blocked because the bank account has not passed verification. Confirm the two micro-deposits shown in the dashboard to unlock transfers.
---------------------
Given the context information and no prior knowledge, answer the query.
Follow these rules:
1. If the answer is not in the context, just say that you don't know.
2. Avoid statements like "Based on the context..." or "The provided information...".
Query: Why am I getting E-4116?
Answer:
--- does the answer passage reach the prompt? the 12 exact-code questions, top 3 passages ---
E-4106 vector-only: false hybrid: false What does error E-4106 mean?
E-4116 vector-only: true hybrid: true Why am I getting E-4116?
E-4118 vector-only: false hybrid: true We see E-4118 in the logs, what should we do?
E-4120 vector-only: true hybrid: true Explain E-4120
E-4122 vector-only: false hybrid: true Support told me to look up E-4122
E-4126 vector-only: true hybrid: true What is E-4126?
E-4129 vector-only: false hybrid: true How do I fix E-4129?
E-4131 vector-only: false hybrid: true Our service started returning E-4131 this morning
E-4133 vector-only: false hybrid: true Meaning of E-4133
E-4135 vector-only: true hybrid: true Is E-4135 our fault?
E-4138 vector-only: false hybrid: true What causes E-4138 on the shop floor?
E-4140 vector-only: true hybrid: true Customer reports E-4140, is the money safe?
answer passage in the top 3: vector-only 5 of 12, hybrid 11 of 12
@@ -0,0 +1,45 @@
# 94 rows against 50,094 rows: plans, index sizes, latency (PostgreSQL 16, pgvector 0.8.7, 2 vCPU)
filler rows inserted: 50000 in 7819 ms. HNSW index built in 94632 ms, GIN index in 292 ms.
rows in hybrid_scale: 50094
heap 98 MB, HNSW index 98 MB, GIN index 2248 kB
--- plan for the vector query at 50,094 rows (scan nodes only) ---
Limit (actual rows=20 loops=1)
-> Index Scan using hybrid_scale_embedding_idx on hybrid_scale (actual rows=20 loops=1)
--- plan for the keyword query at 50,094 rows (scan nodes only) ---
Limit (actual rows=20 loops=1)
-> Sort (actual rows=20 loops=1)
Sort Key: (ts_rank_cd(d.tsv, '''get'' | ''e'' | ''-4116'''::tsquery)) DESC
Sort Method: quicksort Memory: 26kB
-> Bitmap Heap Scan on hybrid_scale d (actual rows=40 loops=1)
-> Bitmap Index Scan on hybrid_scale_tsv_idx (actual rows=40 loops=1)
--- plan for the same keyword query at 94 rows ---
Limit (actual rows=20 loops=1)
-> Sort (actual rows=20 loops=1)
Sort Key: (ts_rank_cd(d.tsv, '''get'' | ''e'' | ''-4116'''::tsquery)) DESC
Sort Method: quicksort Memory: 26kB
-> Seq Scan on hybrid_docs d (actual rows=40 loops=1)
Rows Removed by Filter: 54
--- rows the keyword query has to rank, by query kind (50,094 rows) ---
exact error code average rows matching the OR query: 1547
exact SKU or setting average rows matching the OR query: 1970
paraphrase average rows matching the OR query: 3781
code plus words average rows matching the OR query: 5220
--- latency in milliseconds over 60 queries x 3 runs after 2 warm-up passes (query embedding excluded unless stated) ---
94 p50 94 p95 50k p50 50k p95
embedding the question (MiniLM) 7.4 10.4 (same for both table sizes)
vector, top 20 25.3 32.2 28.0 36.4
keyword, top 20 27.0 39.3 85.5 177.1
hybrid in Java (2 queries + fuse) 51.6 65.3 111.2 208.2
hybrid in one SQL statement 31.5 40.1 132.3 226.4
--- keyword query latency at 50,094 rows by query kind (p50 / p95 ms) ---
exact error code 51.1 / 124.0
exact SKU or setting 35.7 / 223.0
paraphrase 85.9 / 169.6
code plus words 121.2 / 214.9
+64
View File
@@ -0,0 +1,64 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>4.1.2</version>
<relativePath/>
</parent>
<groupId>com.ankurm</groupId>
<artifactId>hybrid-search</artifactId>
<version>1.0.0</version>
<name>hybrid-search</name>
<description>Hybrid search on one PostgreSQL: pgvector and tsvector queries fused with Reciprocal Rank Fusion in Java, a real BM25 for comparison, and a Spring AI DocumentRetriever, measured on a labelled set of 60 queries.</description>
<properties>
<java.version>25</java.version>
<spring-ai.version>2.0.2</spring-ai.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-bom</artifactId>
<version>${spring-ai.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- Store module, not the starter: the table is created by the store and then extended by hand. -->
<dependency><groupId>org.springframework.ai</groupId><artifactId>spring-ai-pgvector-store</artifactId></dependency>
<!-- DocumentRetriever, Query and RetrievalAugmentationAdvisor. -->
<dependency><groupId>org.springframework.ai</groupId><artifactId>spring-ai-rag</artifactId></dependency>
<!-- all-MiniLM-L6-v2 in-process through ONNX Runtime: a real embedding model, no API key. -->
<dependency><groupId>org.springframework.ai</groupId><artifactId>spring-ai-transformers</artifactId></dependency>
<dependency><groupId>org.postgresql</groupId><artifactId>postgresql</artifactId></dependency>
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jdbc</artifactId></dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>-Duser.timezone=UTC -Dstdout.encoding=UTF-8 -Dfile.encoding=UTF-8 -Xmx3g</argLine>
</configuration>
</plugin>
</plugins>
</build>
</project>
+10
View File
@@ -0,0 +1,10 @@
#!/usr/bin/env bash
# Regenerates every file in output/ by running the test suite.
# Needs: JDK 25, Maven, internet for the first run (about 90 MB for the MiniLM model, cached in ~/.cache/minilm),
# and a PostgreSQL 16 with pgvector 0.8 on localhost:5432 (database ragdemo, user demo / demo).
# Set HYBRID_DB_URL to point somewhere else. ScaleTest inserts 50,000 rows and builds an HNSW index (about two minutes).
set -euo pipefail
cd "$(dirname "$0")/.."
rm -rf output
mvn -B -q test
ls output
@@ -0,0 +1,85 @@
package com.ankurm.hybridsearch;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Okapi BM25 over the passages, held in memory. PostgreSQL's ts_rank and ts_rank_cd are not BM25: they have no
* inverse document frequency, so a word found in every passage counts as much as a word found in one. This
* class exists to put a real BM25 next to them. Tokens are lower-case runs of letters and digits that may
* contain dots, underscores and hyphens, so "E-4116" and "orbit.retry.backoff_ms" each stay one token.
*/
public final class Bm25 {
private static final Pattern TOKEN = Pattern.compile("[a-z0-9]+(?:[._-][a-z0-9]+)*");
private static final Set<String> STOP = Set.of("a", "an", "the", "of", "to", "in", "is", "are", "and", "or", "for", "on", "at", "be", "by",
"do", "does", "how", "what", "why", "can", "it", "its", "we", "our", "i", "my", "me", "with", "was", "this", "that", "from", "as",
"have", "has", "when", "which", "if", "so", "not", "no", "am");
private final double k1;
private final double b;
private final List<String> ids = new ArrayList<>();
private final List<Map<String, Integer>> termFrequencies = new ArrayList<>();
private final List<Integer> lengths = new ArrayList<>();
private final Map<String, Integer> documentFrequency = new HashMap<>();
private final double averageLength;
public Bm25(List<KnowledgeBase.Passage> passages, double k1, double b) {
this.k1 = k1;
this.b = b;
long total = 0;
for (KnowledgeBase.Passage p : passages) {
List<String> tokens = tokens(p.text());
Map<String, Integer> tf = new LinkedHashMap<>();
tokens.forEach(t -> tf.merge(t, 1, Integer::sum));
ids.add(p.pid());
termFrequencies.add(tf);
lengths.add(tokens.size());
total += tokens.size();
tf.keySet().forEach(t -> documentFrequency.merge(t, 1, Integer::sum));
}
this.averageLength = (double) total / passages.size();
}
public List<String> search(String query, int n) {
List<String> terms = tokens(query);
List<double[]> scored = new ArrayList<>();
for (int d = 0; d < ids.size(); d++) {
double score = 0;
for (String term : terms) {
Integer f = termFrequencies.get(d).get(term);
if (f == null) {
continue;
}
int df = documentFrequency.get(term);
double idf = Math.log(1 + (ids.size() - df + 0.5) / (df + 0.5));
score += idf * f * (k1 + 1) / (f + k1 * (1 - b + b * lengths.get(d) / averageLength));
}
if (score > 0) {
scored.add(new double[] {d, score});
}
}
scored.sort((x, y) -> Double.compare(y[1], x[1]));
return scored.stream().limit(n).map(s -> ids.get((int) s[0])).toList();
}
static List<String> tokens(String text) {
List<String> out = new ArrayList<>();
Matcher m = TOKEN.matcher(text.toLowerCase(Locale.ROOT));
while (m.find()) {
String t = m.group();
if (STOP.contains(t)) {
continue;
}
out.add(t.length() > 3 && t.endsWith("s") && !t.contains("-") && !t.contains(".") ? t.substring(0, t.length() - 1) : t);
}
return out;
}
}
@@ -0,0 +1,35 @@
package com.ankurm.hybridsearch;
import java.util.List;
import java.util.Map;
import org.springframework.ai.document.Document;
import org.springframework.ai.rag.Query;
import org.springframework.ai.rag.retrieval.search.DocumentRetriever;
/**
* Spring AI's extension point for "where do the passages come from": give it to RetrievalAugmentationAdvisor
* and every question is answered from the fused ranking instead of from the vector store alone.
*/
public class HybridDocumentRetriever implements DocumentRetriever {
private final HybridSearch search;
private final int topK;
private final int candidates;
private final int k;
public HybridDocumentRetriever(HybridSearch search, int topK, int candidates, int k) {
this.search = search;
this.topK = topK;
this.candidates = candidates;
this.k = k;
}
@Override
public List<Document> retrieve(Query query) {
return search.hybrid(query.text(), topK, candidates, k).stream()
.map(f -> Document.builder().text(f.content()).score(f.score())
.metadata(Map.of("pid", f.pid(), "vector_rank", f.vectorRank(), "keyword_rank", f.keywordRank())).build())
.toList();
}
}
@@ -0,0 +1,155 @@
package com.ankurm.hybridsearch;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;
import org.springframework.ai.document.Document;
import org.springframework.ai.embedding.EmbeddingModel;
import org.springframework.ai.vectorstore.pgvector.PgVectorStore;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
/**
* Keyword search, vector search and their fusion over one PostgreSQL table.
*
* <p>The table is the one Spring AI's PgVectorStore creates (id, content, metadata, embedding). The only thing
* added by hand is a generated tsvector column over {@code content} and a GIN index on it, so a passage is
* stored once and searched two ways.
*/
public class HybridSearch {
public record Hit(String pid, String content) {
}
/** One fused result: the passage, its RRF score and the rank it had in each list (0 when absent). */
public record Fused(String pid, String content, double score, int vectorRank, int keywordRank) {
}
/** The English tsquery of a question with its AND turned into OR, so one rare word is enough to match. */
static final String OR_QUERY = "replace(plainto_tsquery('english', :text)::text, ' & ', ' | ')::tsquery";
private final JdbcTemplate jdbc;
private final NamedParameterJdbcTemplate named;
private final EmbeddingModel model;
private final String table;
public HybridSearch(JdbcTemplate jdbc, EmbeddingModel model, String table) {
this.jdbc = jdbc;
this.named = new NamedParameterJdbcTemplate(jdbc);
this.model = model;
this.table = table;
}
/** Creates the PgVectorStore table, loads the passages through it, then adds the tsvector column and its index. */
public static HybridSearch load(JdbcTemplate jdbc, EmbeddingModel model, String table, List<KnowledgeBase.Passage> passages) throws Exception {
PgVectorStore store = PgVectorStore.builder(jdbc, model).vectorTableName(table).dimensions(384)
.removeExistingVectorStoreTable(true).initializeSchema(true).build();
store.afterPropertiesSet();
List<Document> docs = new ArrayList<>();
for (KnowledgeBase.Passage p : passages) {
docs.add(Document.builder().id(UUID.nameUUIDFromBytes(p.pid().getBytes(StandardCharsets.UTF_8)).toString())
.text(p.text()).metadata(Map.of("pid", p.pid(), "kind", p.kind())).build());
}
store.add(docs);
addKeywordIndex(jdbc, table);
return new HybridSearch(jdbc, model, table);
}
public static void addKeywordIndex(JdbcTemplate jdbc, String table) {
jdbc.execute("alter table " + table + " add column if not exists tsv tsvector generated always as (to_tsvector('english', content)) stored");
jdbc.execute("create index if not exists " + table + "_tsv_idx on " + table + " using gin (tsv)");
jdbc.execute("analyze " + table);
}
public List<Hit> vector(String text, int n) {
return vectorByLiteral(literal(model.embed(text)), n);
}
/** The same query with the question already embedded, so a timing can leave the embedding model out. */
public List<Hit> vectorByLiteral(String vectorLiteral, int n) {
return named.query("select metadata->>'pid' as pid, content from " + table
+ " order by embedding <=> cast(:vec as vector) limit :n",
new MapSqlParameterSource("vec", vectorLiteral).addValue("n", n),
(rs, i) -> new Hit(rs.getString("pid"), rs.getString("content")));
}
public List<Hit> keyword(String text, int n) {
return named.query("with q as (select " + OR_QUERY + " as query) select d.metadata->>'pid' as pid, d.content, "
+ "ts_rank_cd(d.tsv, q.query) as score from " + table + " d, q where d.tsv @@ q.query "
+ "order by score desc, d.id limit :n",
new MapSqlParameterSource("text", text).addValue("n", n),
(rs, i) -> new Hit(rs.getString("pid"), rs.getString("content")));
}
/** Two queries, fused in Java: this is what a DocumentRetriever does. */
public List<Fused> hybrid(String text, int n, int candidates, int k) {
List<Hit> vector = vector(text, candidates);
List<Hit> keyword = keyword(text, candidates);
return fuse(vector, keyword, n, k);
}
public static List<Fused> fuse(List<Hit> vector, List<Hit> keyword, int n, int k) {
List<String> v = vector.stream().map(Hit::pid).toList();
List<String> w = keyword.stream().map(Hit::pid).toList();
Map<String, String> content = new LinkedHashMap<>();
vector.forEach(h -> content.putIfAbsent(h.pid(), h.content()));
keyword.forEach(h -> content.putIfAbsent(h.pid(), h.content()));
return ReciprocalRankFusion.fuse(List.of(v, w), k).stream().limit(n)
.map(f -> new Fused(f.id(), content.get(f.id()), f.score(), v.indexOf(f.id()) + 1, w.indexOf(f.id()) + 1))
.collect(Collectors.toList());
}
/**
* The same fusion written as one SQL statement. Equal scores are common in RRF (rank 1 in one list only
* equals rank 1 in the other), so the tie-break is spelled out to match fuse(): vector-list order first,
* then keyword-only items in keyword order.
*/
public List<String> hybridSql(String text, int n, int candidates, int k) {
return hybridSqlByLiteral(text, literal(model.embed(text)), n, candidates, k);
}
public List<String> hybridSqlByLiteral(String text, String vectorLiteral, int n, int candidates, int k) {
return named.query("""
with q as (select %s as tsq, cast(:vec as vector) as v),
sem as (select id, row_number() over (order by dist) as r from
(select d.id, d.embedding <=> q.v as dist from %s d, q order by dist limit :cand) s),
kw as (select id, row_number() over (order by score desc, id) as r from
(select d.id, ts_rank_cd(d.tsv, q.tsq) as score from %s d, q where d.tsv @@ q.tsq
order by score desc, d.id limit :cand) s)
select d.metadata->>'pid' as pid,
coalesce(1.0 / (:k + sem.r), 0) + coalesce(1.0 / (:k + kw.r), 0) as score
from sem full outer join kw on sem.id = kw.id
join %s d on d.id = coalesce(sem.id, kw.id)
order by score desc, sem.r nulls last, kw.r nulls last
limit :n
""".formatted(OR_QUERY, table, table, table),
new MapSqlParameterSource("text", text).addValue("vec", vectorLiteral)
.addValue("cand", candidates).addValue("k", k).addValue("n", n),
(rs, i) -> rs.getString("pid"));
}
public JdbcTemplate jdbc() {
return jdbc;
}
public String table() {
return table;
}
public static String literal(float[] v) {
StringBuilder sb = new StringBuilder("[");
for (int i = 0; i < v.length; i++) {
if (i > 0) {
sb.append(',');
}
sb.append(v[i]);
}
return sb.append(']').toString();
}
}
@@ -0,0 +1,222 @@
package com.ankurm.hybridsearch;
import java.util.ArrayList;
import java.util.List;
/**
* A made-up support knowledge base for a payments API called Orbit: 94 passages and 60 labelled queries.
* Every query has exactly one passage that answers it, named by {@code pid}.
*
* <p>The passages come in three kinds, because the three kinds are where the two search methods differ:
* 40 error codes whose texts all look alike and differ in the identifier and one fact, 24 hardware and
* configuration identifiers (SKUs and setting names), and 30 how-to guides written in plain language.
* The corpus is synthetic and small. It shows how the methods behave, not how any of them scales.
*/
public final class KnowledgeBase {
public record Passage(String pid, String kind, String text) {
}
public record Query(String text, String pid, String category) {
}
public static final String EXACT_CODE = "exact error code";
public static final String EXACT_ID = "exact SKU or setting";
public static final String PARAPHRASE = "paraphrase";
public static final String MIXED = "code plus words";
public static final List<Passage> PASSAGES = new ArrayList<>();
public static final List<Query> QUERIES = new ArrayList<>();
private static void error(String code, String title, String body) {
PASSAGES.add(new Passage(code, "error", "Error " + code + " - " + title + ". " + body));
}
private static void item(String id, String kind, String text) {
PASSAGES.add(new Passage(id, kind, text));
}
private static void guide(String id, String title, String body) {
PASSAGES.add(new Passage(id, "guide", title + ". " + body));
}
private static void q(String category, String pid, String text) {
QUERIES.add(new Query(text, pid, category));
}
static {
// ---- 40 error codes -------------------------------------------------------------------------------
error("E-4101", "Missing idempotency key", "Requests that create a charge must send an Idempotency-Key header. A request without one is rejected before any money moves. Generate a fresh UUID for each logical payment attempt.");
error("E-4102", "Idempotency key reused with a different body", "The same Idempotency-Key arrived with a different request body. Orbit replays the stored response only when the body is identical, and otherwise refuses the call.");
error("E-4103", "Unsupported currency", "The currency code is not enabled for this merchant account. Enable it in the dashboard under Settings, or convert the amount before calling the API.");
error("E-4104", "Amount below minimum", "The charge is smaller than the minimum for its currency, which is fifty cents for dollars. Orbit does not process smaller amounts.");
error("E-4105", "Amount above limit", "The amount is higher than the per-transaction ceiling set for the merchant. Ask support to raise the ceiling or split the order into several charges.");
error("E-4106", "Webhook signature mismatch", "The signature header on a webhook did not match the value computed with the endpoint secret. Verify against the raw request body, not the parsed JSON, because re-serialising changes the bytes.");
error("E-4107", "Webhook timestamp too old", "The timestamp inside the webhook signature is more than five minutes in the past. This guards against replays. Check for clock drift on the receiving server.");
error("E-4108", "Card expired", "The expiry date on the card has passed. Ask the customer for a different card or for the new expiry date before trying again.");
error("E-4109", "Card number invalid", "The card number fails the Luhn checksum, which usually means a typing mistake. Orbit rejects it locally without contacting the issuer.");
error("E-4110", "Strong customer authentication required", "The issuer demands a 3-D Secure challenge before it will approve the payment. Redirect the customer to the authentication page and confirm the payment afterwards.");
error("E-4111", "Authentication challenge failed", "The customer abandoned or failed the 3-D Secure challenge. The payment cannot be completed with this card; offer another payment method.");
error("E-4112", "Issuer declined without a reason", "The card issuer returned a generic do-not-honor response. Orbit cannot see the real cause. The customer should call their bank or use a different card.");
error("E-4113", "Insufficient funds", "The account behind the card does not have enough available balance for the amount. A later retry may succeed once the customer tops up.");
error("E-4114", "Refund larger than the captured amount", "The refund requested is more than what was captured on the charge, after earlier partial refunds are taken off. Lower the refund amount.");
error("E-4115", "Refund window closed", "Refunds to cards are possible for 180 days after capture. After that the money must be returned by bank transfer outside Orbit.");
error("E-4116", "Charge already captured", "A capture was attempted on a charge that has already been captured in full. Fetch the charge first and check its status instead of capturing blindly.");
error("E-4117", "Authorization expired", "An authorization that is not captured within seven days lapses and the hold is released. Create a new authorization if the goods ship later.");
error("E-4118", "Payout account not verified", "Payouts are blocked because the bank account has not passed verification. Confirm the two micro-deposits shown in the dashboard to unlock transfers.");
error("E-4119", "Payout below threshold", "The available balance is under ten units of currency, so the scheduled payout was skipped. It will be included in the next run once the balance grows.");
error("E-4120", "Dispute evidence deadline passed", "Evidence for a chargeback must be submitted before the deadline printed on the dispute. After it passes the dispute is lost automatically.");
error("E-4121", "Customer not found", "No customer exists with the identifier supplied. It may belong to the other mode, since test and live objects are kept apart.");
error("E-4122", "Payment method not attached", "The payment method is not attached to this customer. Attach it first, or pass it directly on a one-off charge.");
error("E-4123", "Duplicate customer email", "A customer with this email address already exists and the account is configured to enforce unique emails. Reuse the existing customer.");
error("E-4124", "Plan archived", "The subscription plan was archived and cannot be chosen by new subscribers. Existing subscribers keep it until they cancel.");
error("E-4125", "Proration not allowed during trial", "Prorated billing cannot be applied while the subscription is still in its free trial. Change the plan at the end of the trial instead.");
error("E-4126", "Coupon expired", "The coupon passed its redemption end date. Create a new coupon or extend the validity in the dashboard.");
error("E-4127", "Coupon already redeemed", "This single-use coupon was already redeemed by another order. Issue a fresh code for each customer.");
error("E-4128", "Tax identifier invalid", "The tax registration number does not match the format for its country. Check the prefix and the number of digits.");
error("E-4129", "API key lacks permission", "The key in use does not carry the scope needed for this endpoint. Create a restricted key that includes it.");
error("E-4130", "API key revoked", "The secret key was revoked, either by a team member or automatically after it was found in a public repository. Issue a new key and rotate it in your deployment.");
error("E-4131", "Rate limit exceeded", "More than one hundred requests per second arrived on this key. Back off with jitter and respect the Retry-After header.");
error("E-4132", "Request body too large", "The request payload is bigger than one megabyte. Upload large files through the file endpoint and reference them by identifier.");
error("E-4133", "Unknown parameter", "The request contains a field that this API version does not define. Remove it or pin a newer API version.");
error("E-4134", "Metadata key too long", "A metadata key is longer than forty characters. Shorten the key names.");
error("E-4135", "Too many metadata keys", "An object may carry at most fifty metadata keys. Store bulky data in your own database and keep only a reference.");
error("E-4136", "Test card used in live mode", "One of the published test card numbers was used with a live key. Switch to the test key, or use a real card.");
error("E-4137", "Live card used in test mode", "A real card number was sent with a test key. Orbit never charges real cards in test mode, so use a test card.");
error("E-4138", "Terminal offline", "The card terminal has not sent a heartbeat and cannot take payments. Check its network connection and power.");
error("E-4139", "Terminal firmware outdated", "The terminal runs firmware older than the minimum supported release. Connect it to power and wifi so it can update overnight.");
error("E-4140", "Receipt email bounced", "The receipt could not be delivered because the address rejected it. The payment itself is unaffected.");
// ---- 12 hardware, plan and support identifiers ------------------------------------------------------
item("OP-TERM-5520", "sku", "OP-TERM-5520 countertop card terminal with a 4G fallback modem. Accepts chip, tap and swipe, and keeps working for up to eight hours of sales if the shop network drops.");
item("OP-TERM-5530", "sku", "OP-TERM-5530 handheld card terminal with a built-in receipt printer and a six hour battery. Intended for table service in restaurants.");
item("OP-TERM-6100", "sku", "OP-TERM-6100 unattended card reader for kiosks and vending machines. Vandal-resistant housing, no display, powered over the serial port.");
item("OP-DOCK-210", "sku", "OP-DOCK-210 charging dock for the handheld terminals. Holds a single unit and charges it to full in about ninety minutes.");
item("OP-PRNT-77", "sku", "OP-PRNT-77 thermal paper rolls, 58 millimetres wide, sold in boxes of fifty. Fits the handheld terminal.");
item("OP-SIM-EU1", "sku", "OP-SIM-EU1 data SIM for terminals used across the European Union, with roaming included and no monthly minimum.");
item("OP-STND-45", "sku", "OP-STND-45 adjustable stand for countertop terminals. Tilts from zero to forty-five degrees and has a lockable base.");
item("OP-PLAN-STD", "plan", "OP-PLAN-STD standard pricing plan: 2.4 percent plus twenty cents per successful card charge, no monthly fee, payouts every two business days.");
item("OP-PLAN-PLUS", "plan", "OP-PLAN-PLUS pricing plan for growing merchants: 1.9 percent plus twenty cents per charge, a monthly fee, and next business day payouts.");
item("OP-PLAN-ENT", "plan", "OP-PLAN-ENT enterprise plan with negotiated rates, a named account manager and a custom payout calendar.");
item("OP-SUP-GOLD", "support", "OP-SUP-GOLD gold support tier: a human answers within one hour at any time of day, with a phone line for outages.");
item("OP-SUP-SILV", "support", "OP-SUP-SILV silver support tier: email answers within one business day, no phone line.");
// ---- 12 configuration settings ---------------------------------------------------------------------
item("orbit.retry.max_attempts", "setting", "Setting orbit.retry.max_attempts is the number of times the client library repeats a failed request before giving up. The default is 5.");
item("orbit.retry.backoff_ms", "setting", "Setting orbit.retry.backoff_ms is the initial wait between retries in milliseconds. It doubles after each attempt. The default is 250.");
item("orbit.webhook.tolerance_seconds", "setting", "Setting orbit.webhook.tolerance_seconds is how old a webhook signature timestamp may be before verification fails. The default is 300.");
item("orbit.http.timeout_ms", "setting", "Setting orbit.http.timeout_ms is the total time the client waits for a response. The default is 30000.");
item("orbit.capture.mode", "setting", "Setting orbit.capture.mode chooses between automatic capture at authorization and manual capture later. The default is automatic.");
item("orbit.idempotency.ttl_hours", "setting", "Setting orbit.idempotency.ttl_hours is how long Orbit remembers an idempotency key and replays its response. The default is 24.");
item("orbit.radar.threshold", "setting", "Setting orbit.radar.threshold is the risk score from 0 to 100 above which a payment is blocked. The default is 75.");
item("orbit.payout.schedule", "setting", "Setting orbit.payout.schedule picks daily, weekly or monthly payouts. The default is daily.");
item("orbit.receipts.locale", "setting", "Setting orbit.receipts.locale is the language of emailed receipts. The default follows the customer's browser.");
item("orbit.terminal.heartbeat_seconds", "setting", "Setting orbit.terminal.heartbeat_seconds is how often a terminal reports in. After three missed beats it is shown as offline. The default is 60.");
item("orbit.log.level", "setting", "Setting orbit.log.level controls how much the client library writes to its log. The default is warn.");
item("orbit.cache.customer_ttl_s", "setting", "Setting orbit.cache.customer_ttl_s is how long customer objects are cached inside the client. The default is 120.");
// ---- 30 guides in plain language --------------------------------------------------------------------
guide("G-01", "Recovering failed subscription renewals", "When a renewal charge fails, Orbit runs a dunning schedule: it retries after one, three and five days, then marks the subscription past due and emails the customer a link to update their card.");
guide("G-02", "Paying several sellers from one order", "A marketplace can divide the proceeds of a single charge between sellers using connected accounts and transfers. Each seller is paid out on their own schedule and the platform keeps its fee.");
guide("G-03", "Authorise now, capture on dispatch", "Place a hold on the card when the order is made and capture it when the goods ship. The hold guarantees the funds for seven days and can be released without any fee.");
guide("G-04", "Responding to a chargeback", "When a cardholder disputes a payment their bank takes the money back at once. Gather proof of delivery and customer communication and submit it through the dispute page before the deadline.");
guide("G-05", "Trying the integration without real money", "Test mode mirrors live mode with separate keys and published card numbers that simulate approvals, declines and challenges. Nothing in test mode moves real funds.");
guide("G-06", "Remembering a shopper's card", "Attach a payment method to a customer object after the first purchase. Later orders can then reuse it, and the shopper never types the number again.");
guide("G-07", "Taking payment from people without cards", "Orbit supports bank redirects, wallets and instant bank transfers. Enable the methods in the dashboard and they appear in the checkout automatically for eligible countries.");
guide("G-08", "When refunded money arrives", "A refund is sent to the bank immediately but most issuers need five to ten business days to show it on the customer's statement.");
guide("G-09", "What shows on the cardholder's statement", "The statement descriptor is the short text a cardholder sees next to the charge. Choose one customers will recognise to avoid unnecessary disputes.");
guide("G-10", "Blocking suspicious orders", "Radar scores every payment for fraud risk using signals across the network. Rules let you block, review or allow payments based on the score, the country or the card type.");
guide("G-11", "When funds reach your bank account", "Settled funds are paid to your bank on a rolling schedule. New accounts wait longer for the first payout while Orbit builds a history.");
guide("G-12", "If your server is unreachable", "When your endpoint does not answer, Orbit redelivers the event with increasing gaps for up to three days. You can also list missed events through the events endpoint and replay them.");
guide("G-13", "Charging in one currency and settling in another", "Present prices in the shopper's currency and Orbit converts to your settlement currency at the daily rate, adding a small conversion fee to the payout.");
guide("G-14", "Adding sales tax to bills", "Orbit Tax calculates the rate from the customer's address and the product category, adds it to the invoice and keeps the reports you need for filing.");
guide("G-15", "Giving new customers a free period", "Set a trial length on the plan. The customer is not charged until the trial ends, and a reminder is emailed three days before the first payment.");
guide("G-16", "Putting a subscription on hold", "Pausing stops invoices while keeping the customer and their payment method. Billing resumes on the date you choose and the cycle is shifted accordingly.");
guide("G-17", "Handing transactions to the accountants", "Export every transaction, fee and payout for a date range as a spreadsheet. The report reconciles to the bank deposits line by line.");
guide("G-18", "Restricting what a team member can see", "Dashboard roles control who can view balances, issue refunds or read customer details. Give support staff the limited role so they cannot see payout accounts.");
guide("G-19", "Keeping European data in Europe", "Choose the EU region when you create the account and customer and card data are stored and processed only in European data centres.");
guide("G-20", "Staying out of card data rules", "Hosted fields and the checkout page collect card numbers on Orbit's servers, so they never touch yours and your compliance questionnaire stays short.");
guide("G-21", "Accepting taps on a phone", "The mobile SDK turns a supported phone into a contactless reader, with no extra hardware, for cards and wallets.");
guide("G-22", "Customising the emailed receipt", "Add your logo, brand colour and a footer message to receipts under Settings. Receipts are sent automatically after each successful payment.");
guide("G-23", "Resending a request that timed out", "If a call times out you cannot know whether it succeeded. Send it again with the same idempotency key and Orbit returns the original result instead of creating a second charge.");
guide("G-24", "Taking part of an authorised amount", "You may capture less than was authorised, for example when one item of an order is out of stock. The rest of the hold is released straight away.");
guide("G-25", "Letting customers add a tip", "Collect a gratuity on the terminal after the amount is shown. The tip is added to the same charge and reported separately.");
guide("G-26", "Showing local prices", "Define a price per currency on each product so shoppers see round local amounts instead of converted ones.");
guide("G-27", "Clearing out sandbox data", "Reset the test environment from the developer settings page to delete test customers, charges and subscriptions and start clean.");
guide("G-28", "Seeing who changed what", "The audit log records every dashboard action with the person, time and address, and can be exported for your security team.");
guide("G-29", "Signing in with your company directory", "Single sign-on lets the team log in to the dashboard through your identity provider, and access is removed automatically when someone leaves.");
guide("G-30", "Checking whether Orbit is having problems", "The status page lists current incidents and past outages per region, and you can subscribe to updates by email or webhook.");
// ---- 60 queries --------------------------------------------------------------------------------------
// 12 where the only useful words are an error code
q(EXACT_CODE, "E-4106", "What does error E-4106 mean?");
q(EXACT_CODE, "E-4116", "Why am I getting E-4116?");
q(EXACT_CODE, "E-4118", "We see E-4118 in the logs, what should we do?");
q(EXACT_CODE, "E-4120", "Explain E-4120");
q(EXACT_CODE, "E-4122", "Support told me to look up E-4122");
q(EXACT_CODE, "E-4126", "What is E-4126?");
q(EXACT_CODE, "E-4129", "How do I fix E-4129?");
q(EXACT_CODE, "E-4131", "Our service started returning E-4131 this morning");
q(EXACT_CODE, "E-4133", "Meaning of E-4133");
q(EXACT_CODE, "E-4135", "Is E-4135 our fault?");
q(EXACT_CODE, "E-4138", "What causes E-4138 on the shop floor?");
q(EXACT_CODE, "E-4140", "Customer reports E-4140, is the money safe?");
// 12 where the identifier is a SKU, plan or setting name
q(EXACT_ID, "OP-TERM-5520", "Does OP-TERM-5520 work when the internet is down?");
q(EXACT_ID, "OP-TERM-5530", "Tell me about OP-TERM-5530");
q(EXACT_ID, "OP-TERM-6100", "Can OP-TERM-6100 be used in a vending machine?");
q(EXACT_ID, "OP-DOCK-210", "How long does OP-DOCK-210 take to charge a unit?");
q(EXACT_ID, "OP-PRNT-77", "What size is OP-PRNT-77?");
q(EXACT_ID, "OP-PLAN-PLUS", "What are the rates on OP-PLAN-PLUS?");
q(EXACT_ID, "OP-SUP-GOLD", "Response time for OP-SUP-GOLD");
q(EXACT_ID, "orbit.retry.backoff_ms", "What is the default for orbit.retry.backoff_ms?");
q(EXACT_ID, "orbit.radar.threshold", "Value of orbit.radar.threshold");
q(EXACT_ID, "orbit.idempotency.ttl_hours", "What does orbit.idempotency.ttl_hours do?");
q(EXACT_ID, "orbit.terminal.heartbeat_seconds", "Default of orbit.terminal.heartbeat_seconds");
q(EXACT_ID, "orbit.cache.customer_ttl_s", "Explain orbit.cache.customer_ttl_s");
// 24 plain-language questions written without the passage's own distinctive words
q(PARAPHRASE, "G-01", "What happens when a customer's monthly payment does not go through?");
q(PARAPHRASE, "G-02", "How can an online mall split one shopper's purchase among several vendors?");
q(PARAPHRASE, "G-03", "Can I reserve money on a card and only take it once the parcel leaves?");
q(PARAPHRASE, "G-04", "A buyer told their bank they never got the item. What now?");
q(PARAPHRASE, "G-05", "How do I experiment safely before launching?");
q(PARAPHRASE, "G-06", "Store a shopper's card so they do not re-enter it next time");
q(PARAPHRASE, "G-07", "Accept money from people who do not own a credit card");
q(PARAPHRASE, "G-08", "How long until the buyer sees their money back?");
q(PARAPHRASE, "G-09", "Which words appear on the customer's bank statement?");
q(PARAPHRASE, "G-10", "Stop dodgy purchases automatically");
q(PARAPHRASE, "G-11", "When will the money land in our account?");
q(PARAPHRASE, "G-12", "What if our website is down when you send us news?");
q(PARAPHRASE, "G-13", "Show prices in euros but receive dollars");
q(PARAPHRASE, "G-14", "Include VAT on our bills without doing sums");
q(PARAPHRASE, "G-15", "Let new users try the product for two weeks at no cost");
q(PARAPHRASE, "G-16", "A client wants to stop being billed for the summer");
q(PARAPHRASE, "G-17", "Give our bookkeeper everything that came in last quarter");
q(PARAPHRASE, "G-18", "Limit what a helpdesk employee can look at");
q(PARAPHRASE, "G-19", "Our lawyers want shopper information to stay on this continent");
q(PARAPHRASE, "G-20", "How do we avoid ever handling raw card numbers ourselves?");
q(PARAPHRASE, "G-21", "Take contactless payments with just a smartphone");
q(PARAPHRASE, "G-23", "Our call hung and we do not know if it worked. Is it safe to send it again?");
q(PARAPHRASE, "G-24", "One item in the basket is sold out, can we bill only for the rest?");
q(PARAPHRASE, "G-28", "Which colleague altered the settings last Tuesday?");
// 12 that carry a code and also say what it is about
q(MIXED, "E-4106", "E-4106 keeps appearing in our Express webhook handler, is the body being parsed?");
q(MIXED, "E-4113", "Customer card gets E-4113 although they swear there is money on it");
q(MIXED, "E-4117", "E-4117 on capture, the goods shipped ten days after the order");
q(MIXED, "E-4125", "Plan change during trial returns E-4125");
q(MIXED, "E-4130", "E-4130 on every call since the key leaked into our repository");
q(MIXED, "E-4115", "Cannot refund a purchase from last year, E-4115");
q(MIXED, "E-4110", "E-4110 and the customer is asked for a bank challenge");
q(MIXED, "E-4127", "Promo code gives E-4127 for the second shopper");
q(MIXED, "E-4139", "Terminal says E-4139 and needs an update");
q(MIXED, "E-4101", "Getting E-4101 on the charge endpoint, our client does not set the idempotency header");
q(MIXED, "E-4131", "E-4131 during the flash sale, too many requests per second");
q(MIXED, "E-4119", "Why was our payout skipped, E-4119, the balance was small");
}
private KnowledgeBase() {
}
}
@@ -0,0 +1,44 @@
package com.ankurm.hybridsearch;
import java.util.Map;
import org.springframework.ai.document.MetadataMode;
import org.springframework.ai.embedding.EmbeddingModel;
import org.springframework.ai.transformers.TransformersEmbeddingModel;
/**
* all-MiniLM-L6-v2 (384 dimensions) running inside the JVM through ONNX Runtime. Both model files come from
* Hugging Face and are cached under the user's home directory, so the first run downloads about 90 MB and
* later runs are offline. No API key is involved, which is what lets anyone repeat the measurements.
*/
public final class MiniLm {
private static final String HF = "https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2/resolve/main/";
private static EmbeddingModel shared;
private MiniLm() {
}
/** The shared instance, which embeds the text of a Document only (MetadataMode.NONE, the constructor default). */
public static synchronized EmbeddingModel get() {
if (shared == null) {
shared = create(MetadataMode.NONE);
}
return shared;
}
/** A fresh instance whose Document embedding content is built with the given metadata mode. */
public static EmbeddingModel create(MetadataMode mode) {
try {
TransformersEmbeddingModel m = new TransformersEmbeddingModel(mode);
m.setTokenizerResource(HF + "tokenizer.json");
m.setModelResource(HF + "onnx/model.onnx");
m.setResourceCacheDirectory(System.getProperty("user.home") + "/.cache/minilm");
m.setTokenizerOptions(Map.of("padding", "true", "truncation", "true", "maxLength", "256"));
m.afterPropertiesSet();
return m;
} catch (Exception e) {
throw new IllegalStateException(e);
}
}
}
@@ -0,0 +1,43 @@
package com.ankurm.hybridsearch;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* Reciprocal Rank Fusion: every ranking gives each item {@code weight / (k + rank)} (rank starts at 1) and the
* item's fused score is the sum over the rankings it appears in. Only ranks are used, never the scores
* the rankings were built from, which is why it can join a cosine similarity and a text rank that live on
* different scales without any normalisation. {@code k} (60 in the original paper) flattens the curve: the
* larger it is, the less the very top places matter.
*/
public final class ReciprocalRankFusion {
public record Fused(String id, double score) {
}
private ReciprocalRankFusion() {
}
public static List<Fused> fuse(List<List<String>> rankings, int k) {
double[] ones = new double[rankings.size()];
java.util.Arrays.fill(ones, 1.0);
return fuse(rankings, ones, k);
}
public static List<Fused> fuse(List<List<String>> rankings, double[] weights, int k) {
Map<String, Double> scores = new LinkedHashMap<>();
for (int r = 0; r < rankings.size(); r++) {
List<String> ranking = rankings.get(r);
for (int i = 0; i < ranking.size(); i++) {
scores.merge(ranking.get(i), weights[r] / (k + i + 1), Double::sum);
}
}
List<Fused> out = new ArrayList<>();
scores.forEach((id, score) -> out.add(new Fused(id, score)));
// equal scores keep the order in which the ids first appeared, so the result is deterministic
out.sort((a, b) -> Double.compare(b.score(), a.score()));
return out;
}
}
@@ -0,0 +1,24 @@
package com.ankurm.hybridsearch;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;
import org.junit.jupiter.api.Test;
class KnowledgeBaseTest {
@Test
void sizesAndLabels() {
assertThat(KnowledgeBase.PASSAGES).hasSize(94);
assertThat(KnowledgeBase.QUERIES).hasSize(60);
Set<String> pids = new HashSet<>();
KnowledgeBase.PASSAGES.forEach(p -> assertThat(pids.add(p.pid())).as("unique pid %s", p.pid()).isTrue());
KnowledgeBase.QUERIES.forEach(q -> assertThat(pids).as("answer of: %s", q.text()).contains(q.pid()));
assertThat(KnowledgeBase.QUERIES.stream().collect(Collectors.groupingBy(KnowledgeBase.Query::category, Collectors.counting())))
.containsEntry(KnowledgeBase.EXACT_CODE, 12L).containsEntry(KnowledgeBase.EXACT_ID, 12L)
.containsEntry(KnowledgeBase.PARAPHRASE, 24L).containsEntry(KnowledgeBase.MIXED, 12L);
}
}
@@ -0,0 +1,135 @@
package com.ankurm.hybridsearch;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
import com.ankurm.hybridsearch.support.Fixture;
import com.ankurm.hybridsearch.support.Metrics;
import com.ankurm.hybridsearch.support.Transcript;
import org.junit.jupiter.api.Test;
/** Six rankers on 60 labelled queries, split by the kind of query, plus what k and the candidate count change. */
class QualityTest {
static final int CANDIDATES = 20;
@Test
void rankersByCategory() {
HybridSearch hs = Fixture.search();
Bm25 bm25 = Fixture.bm25();
Function<String, List<String>> vector = q -> hs.vector(q, 10).stream().map(HybridSearch.Hit::pid).toList();
Function<String, List<String>> tsvector = q -> hs.keyword(q, 10).stream().map(HybridSearch.Hit::pid).toList();
Function<String, List<String>> bm25Only = q -> bm25.search(q, 10);
Function<String, List<String>> rrfTs = q -> hs.hybrid(q, 10, CANDIDATES, 60).stream().map(HybridSearch.Fused::pid).toList();
Function<String, List<String>> rrfBm25 = q -> ReciprocalRankFusion.fuse(List.of(
hs.vector(q, CANDIDATES).stream().map(HybridSearch.Hit::pid).toList(), bm25.search(q, CANDIDATES)), 60)
.stream().limit(10).map(ReciprocalRankFusion.Fused::id).toList();
Function<String, List<String>> rrfSql = q -> hs.hybridSql(q, 10, CANDIDATES, 60);
String[] names = {"vector (pgvector cosine)", "keyword (tsvector, ts_rank_cd)", "keyword (BM25, in Java)",
"hybrid: vector + tsvector, RRF", "hybrid: vector + BM25, RRF", "hybrid in one SQL statement"};
List<Function<String, List<String>>> rankers = List.of(vector, tsvector, bm25Only, rrfTs, rrfBm25, rrfSql);
try (Transcript t = new Transcript("02-quality-by-query-type.txt", "Six rankers on 60 labelled queries over 94 passages (all-MiniLM-L6-v2, PostgreSQL 16, pgvector 0.8.7)")) {
t.line("fusion: RRF with k=60 over the top %d of each list. hit@1 and hit@5: the answer passage is first / in the first five. MRR@10: mean of 1/rank.", CANDIDATES);
for (String category : List.of(KnowledgeBase.EXACT_CODE, KnowledgeBase.EXACT_ID, KnowledgeBase.PARAPHRASE, KnowledgeBase.MIXED)) {
t.section(category + " (" + Metrics.category(category).size() + " queries)");
t.line("%-34s %s", "ranker", "hit@1 hit@5 MRR@10");
for (int i = 0; i < names.length; i++) {
t.line("%-34s %s", names[i], Metrics.score(Metrics.category(category), rankers.get(i)).cells());
}
}
t.section("all 60 queries");
t.line("%-34s %s", "ranker", "hit@1 hit@5 MRR@10");
for (int i = 0; i < names.length; i++) {
t.line("%-34s %s", names[i], Metrics.score(KnowledgeBase.QUERIES, rankers.get(i)).cells());
}
t.section("the SQL version against the Java version");
int same = 0;
for (KnowledgeBase.Query q : KnowledgeBase.QUERIES) {
if (rrfTs.apply(q.text()).subList(0, 5).equals(rrfSql.apply(q.text()).subList(0, 5))) {
same++;
}
}
t.line("queries whose top 5 are identical, in the same order, from both implementations: %d of %d", same, KnowledgeBase.QUERIES.size());
t.section("one query of each kind: the top 3 from each ranker");
for (String category : List.of(KnowledgeBase.EXACT_CODE, KnowledgeBase.EXACT_ID, KnowledgeBase.PARAPHRASE, KnowledgeBase.MIXED)) {
KnowledgeBase.Query q = Metrics.category(category).get(0);
t.line("[%s] \"%s\" -> answer is %s", category, q.text(), q.pid());
t.line(" vector : %s", top(vector.apply(q.text())));
t.line(" tsvector : %s", top(tsvector.apply(q.text())));
t.line(" BM25 : %s", top(bm25Only.apply(q.text())));
t.line(" hybrid : %s", top(rrfTs.apply(q.text())));
}
}
}
@Test
void whatKAndTheCandidateCountChange() {
HybridSearch hs = Fixture.search();
try (Transcript t = new Transcript("03-rrf-parameters.txt", "RRF parameters on the 60 queries: k, candidates per list, list weights")) {
t.section("k, with 20 candidates per list");
t.line("%-6s %s", "k", "hit@1 hit@5 MRR@10");
for (int k : new int[] {1, 10, 60, 200, 1000}) {
t.line("%-6d %s", k, Metrics.score(KnowledgeBase.QUERIES,
q -> hs.hybrid(q, 10, 20, k).stream().map(HybridSearch.Fused::pid).toList()).cells());
}
t.section("candidates taken from each list, with k = 60");
t.line("%-6s %s", "cands", "hit@1 hit@5 MRR@10");
for (int c : new int[] {3, 5, 10, 20, 50}) {
t.line("%-6d %s", c, Metrics.score(KnowledgeBase.QUERIES,
q -> hs.hybrid(q, 10, c, 60).stream().map(HybridSearch.Fused::pid).toList()).cells());
}
t.section("weights (vector : keyword), k = 60, 20 candidates");
t.line("%-10s %s", "weights", "hit@1 hit@5 MRR@10");
for (double[] w : new double[][] {{1, 0}, {1, 0.5}, {1, 1}, {0.5, 1}, {0, 1}}) {
t.line("%-10s %s", w[0] + ":" + w[1], Metrics.score(KnowledgeBase.QUERIES, q -> {
List<String> v = hs.vector(q, 20).stream().map(HybridSearch.Hit::pid).toList();
List<String> kw = hs.keyword(q, 20).stream().map(HybridSearch.Hit::pid).toList();
return ReciprocalRankFusion.fuse(List.of(v, kw), w, 60).stream().limit(10).map(ReciprocalRankFusion.Fused::id).toList();
}).cells());
}
}
}
@Test
void doesTheBestKSurviveHeldOutQueries() {
HybridSearch hs = Fixture.search();
List<KnowledgeBase.Query> even = new ArrayList<>();
List<KnowledgeBase.Query> odd = new ArrayList<>();
for (int i = 0; i < KnowledgeBase.QUERIES.size(); i++) {
(i % 2 == 0 ? even : odd).add(KnowledgeBase.QUERIES.get(i));
}
try (Transcript t = new Transcript("04-held-out-k.txt", "Choosing k on half of the queries and scoring it on the other half")) {
t.line("candidates per list: 20. The 60 queries are split by position into two folds of 30; k comes from {1, 10, 60, 200}.");
for (int fold = 0; fold < 2; fold++) {
List<KnowledgeBase.Query> train = fold == 0 ? even : odd;
List<KnowledgeBase.Query> test = fold == 0 ? odd : even;
int best = -1;
double bestMrr = -1;
for (int k : new int[] {1, 10, 60, 200}) {
double mrr = Metrics.score(train, q -> hs.hybrid(q, 10, 20, k).stream().map(HybridSearch.Fused::pid).toList()).mrr();
if (mrr > bestMrr) {
bestMrr = mrr;
best = k;
}
}
final int chosen = best;
t.section("fold " + (fold + 1) + ": train on " + train.size() + ", test on " + test.size());
t.line("k chosen on the training half (best MRR@10 %.3f): %d", bestMrr, chosen);
t.line("held-out half, k=%-3d : %s", chosen, Metrics.score(test, q -> hs.hybrid(q, 10, 20, chosen).stream().map(HybridSearch.Fused::pid).toList()).cells());
t.line("held-out half, k=60 : %s", Metrics.score(test, q -> hs.hybrid(q, 10, 20, 60).stream().map(HybridSearch.Fused::pid).toList()).cells());
t.line("held-out half, vector only: %s", Metrics.score(test, q -> hs.vector(q, 10).stream().map(HybridSearch.Hit::pid).toList()).cells());
}
}
}
private static String top(List<String> ranking) {
return ranking.stream().limit(3).collect(Collectors.joining(", "));
}
}
@@ -0,0 +1,64 @@
package com.ankurm.hybridsearch;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.List;
import com.ankurm.hybridsearch.support.Fixture;
import com.ankurm.hybridsearch.support.Metrics;
import com.ankurm.hybridsearch.support.RecordingChatModel;
import com.ankurm.hybridsearch.support.Transcript;
import org.junit.jupiter.api.Test;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.document.Document;
import org.springframework.ai.rag.advisor.RetrievalAugmentationAdvisor;
import org.springframework.ai.rag.retrieval.search.DocumentRetriever;
import org.springframework.ai.rag.retrieval.search.VectorStoreDocumentRetriever;
import org.springframework.ai.vectorstore.pgvector.PgVectorStore;
/** The fused ranking behind Spring AI's RetrievalAugmentationAdvisor, and what reaches the prompt. */
class RetrieverTest {
@Test
void hybridRetrieverInTheAdvisor() throws Exception {
HybridSearch hs = Fixture.search();
DocumentRetriever hybrid = new HybridDocumentRetriever(hs, 3, 20, 60);
PgVectorStore store = PgVectorStore.builder(Fixture.jdbc(), MiniLm.get()).vectorTableName(Fixture.TABLE).dimensions(384)
.initializeSchema(false).build();
DocumentRetriever vectorOnly = VectorStoreDocumentRetriever.builder().vectorStore(store).topK(3).build();
try (Transcript t = new Transcript("05-retriever-and-advisor.txt", "HybridDocumentRetriever inside RetrievalAugmentationAdvisor (top 3 passages per question)")) {
String question = "Why am I getting E-4116?";
t.section("what the retriever returns for \"" + question + "\"");
for (Document d : hybrid.retrieve(new org.springframework.ai.rag.Query(question))) {
t.line("score %.5f vector rank %-2s keyword rank %-2s %s", d.getScore(),
zero(d.getMetadata().get("vector_rank")), zero(d.getMetadata().get("keyword_rank")), d.getText().substring(0, 60));
}
RecordingChatModel chat = new RecordingChatModel();
ChatClient client = ChatClient.builder(chat)
.defaultAdvisors(RetrievalAugmentationAdvisor.builder().documentRetriever(hybrid).build()).build();
client.prompt().user(question).call().content();
String prompt = chat.prompts().getFirst().getContents();
t.section("the prompt the model received");
t.line(prompt);
assertThat(prompt).contains("Error E-4116");
t.section("does the answer passage reach the prompt? the 12 exact-code questions, top 3 passages");
int hybridHits = 0, vectorHits = 0;
for (KnowledgeBase.Query q : Metrics.category(KnowledgeBase.EXACT_CODE)) {
String answer = KnowledgeBase.PASSAGES.stream().filter(p -> p.pid().equals(q.pid())).findFirst().orElseThrow().text();
boolean h = hybrid.retrieve(new org.springframework.ai.rag.Query(q.text())).stream().anyMatch(d -> d.getText().equals(answer));
boolean v = vectorOnly.retrieve(new org.springframework.ai.rag.Query(q.text())).stream().anyMatch(d -> d.getText().equals(answer));
hybridHits += h ? 1 : 0;
vectorHits += v ? 1 : 0;
t.line("%-7s vector-only: %-5s hybrid: %-5s %s", q.pid(), v, h, q.text());
}
t.line("answer passage in the top 3: vector-only %d of 12, hybrid %d of 12", vectorHits, hybridHits);
}
}
private static Object zero(Object rank) {
return ((Integer) rank) == 0 ? "-" : rank;
}
}
@@ -0,0 +1,42 @@
package com.ankurm.hybridsearch;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.within;
import java.util.List;
import com.ankurm.hybridsearch.support.Transcript;
import org.junit.jupiter.api.Test;
/** Reciprocal Rank Fusion worked by hand and then by code, with the effect of k. */
class RrfTest {
@Test
void workedExample() {
List<String> vector = List.of("A", "B", "C", "D");
List<String> keyword = List.of("C", "A", "E", "B");
try (Transcript t = new Transcript("01-rrf-by-hand.txt", "Reciprocal Rank Fusion on two short rankings")) {
t.line("vector ranking : %s", vector);
t.line("keyword ranking: %s", keyword);
for (int k : new int[] {1, 60, 1000}) {
t.section("k = " + k);
for (ReciprocalRankFusion.Fused f : ReciprocalRankFusion.fuse(List.of(vector, keyword), k)) {
t.line("%s score %.5f (vector rank %d, keyword rank %d)", f.id(), f.score(), vector.indexOf(f.id()) + 1, keyword.indexOf(f.id()) + 1);
}
}
t.section("the arithmetic for A at k = 60");
t.line("rank 1 in the vector list: 1/(60+1) = %.5f", 1.0 / 61);
t.line("rank 2 in the keyword list: 1/(60+2) = %.5f", 1.0 / 62);
t.line("sum = %.5f", 1.0 / 61 + 1.0 / 62);
t.section("an item in one list only");
t.line("E is rank 3 in the keyword list and absent from the vector list: 1/(60+3) = %.5f", 1.0 / 63);
t.line("D is rank 4 in the vector list and absent from the keyword list: 1/(60+4) = %.5f", 1.0 / 64);
}
var fused = ReciprocalRankFusion.fuse(List.of(vector, keyword), 60);
assertThat(fused.get(0).id()).isEqualTo("A");
assertThat(fused.get(0).score()).isCloseTo(1.0 / 61 + 1.0 / 62, within(1e-12));
assertThat(ReciprocalRankFusion.fuse(List.of(vector, keyword), 1).get(0).id()).isEqualTo("A");
}
}
@@ -0,0 +1,174 @@
package com.ankurm.hybridsearch;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;
import com.ankurm.hybridsearch.support.Fixture;
import com.ankurm.hybridsearch.support.Metrics;
import com.ankurm.hybridsearch.support.Transcript;
import org.junit.jupiter.api.Test;
import org.springframework.jdbc.core.JdbcTemplate;
/**
* The same four queries at 94 rows and at 50,094: query plans, index sizes and latency. The 50,000 extra rows are
* generated in SQL from the knowledge base's own vocabulary (14 random words each) with random 384-dimension vectors,
* so keyword posting lists are long and the vector index has work to do. They say nothing about relevance.
*/
class ScaleTest {
static final int FILLER = 50_000;
@Test
void scale() throws Exception {
JdbcTemplate jdbc = Fixture.jdbc();
Fixture.search(); // makes sure hybrid_docs exists
Set<String> vocabulary = new LinkedHashSet<>();
KnowledgeBase.PASSAGES.forEach(p -> vocabulary.addAll(Bm25.tokens(p.text())));
String array = vocabulary.stream().filter(w -> w.matches("[a-z0-9]+")).map(w -> "'" + w + "'")
.collect(java.util.stream.Collectors.joining(",", "array[", "]::text[]"));
jdbc.execute("drop table if exists hybrid_scale");
jdbc.execute("create table hybrid_scale (id uuid primary key, content text, metadata json, embedding vector(384), "
+ "tsv tsvector generated always as (to_tsvector('english', content)) stored)");
jdbc.execute("insert into hybrid_scale (id, content, metadata, embedding) select id, content, metadata, embedding from hybrid_docs");
long t0 = System.nanoTime();
jdbc.update("with v as (select " + array + " as w) insert into hybrid_scale (id, content, metadata, embedding) "
+ "select gen_random_uuid(), c.t, '{\"pid\":\"filler\",\"kind\":\"filler\"}'::json, e.v "
+ "from generate_series(1, " + FILLER + ") s(i) cross join v "
+ "cross join lateral (select string_agg(v.w[1 + floor(random() * array_length(v.w, 1))::int + 0 * g], ' ') as t "
+ "from generate_series(1 + 0 * s.i, 14) g) c "
+ "cross join lateral (select array_agg(random() - 0.5 + 0 * g)::vector as v from generate_series(1 + 0 * s.i, 384) g) e");
long loadMs = (System.nanoTime() - t0) / 1_000_000;
jdbc.execute("set maintenance_work_mem = '512MB'");
// the PgVectorStore index: HNSW with cosine distance
HybridSearchIndexes small = new HybridSearchIndexes(jdbc);
t0 = System.nanoTime();
jdbc.execute("create index hybrid_scale_embedding_idx on hybrid_scale using hnsw (embedding vector_cosine_ops)");
long hnswMs = (System.nanoTime() - t0) / 1_000_000;
t0 = System.nanoTime();
jdbc.execute("create index hybrid_scale_tsv_idx on hybrid_scale using gin (tsv)");
long ginMs = (System.nanoTime() - t0) / 1_000_000;
jdbc.execute("analyze hybrid_scale");
HybridSearch tiny = Fixture.search();
HybridSearch big = new HybridSearch(jdbc, MiniLm.get(), "hybrid_scale");
try (Transcript t = new Transcript("06-scale-plans-and-latency.txt", "94 rows against 50,094 rows: plans, index sizes, latency (PostgreSQL 16, pgvector 0.8.7, 2 vCPU)")) {
t.line("filler rows inserted: %d in %d ms. HNSW index built in %d ms, GIN index in %d ms.", FILLER, loadMs, hnswMs, ginMs);
t.line("rows in hybrid_scale: %d", jdbc.queryForObject("select count(*) from hybrid_scale", Integer.class));
t.line("heap %s, HNSW index %s, GIN index %s",
jdbc.queryForObject("select pg_size_pretty(pg_table_size('hybrid_scale'))", String.class),
jdbc.queryForObject("select pg_size_pretty(pg_relation_size('hybrid_scale_embedding_idx'))", String.class),
jdbc.queryForObject("select pg_size_pretty(pg_relation_size('hybrid_scale_tsv_idx'))", String.class));
String sample = "Why am I getting E-4116?";
String lit = HybridSearch.literal(MiniLm.get().embed(sample));
t.section("plan for the vector query at 50,094 rows (scan nodes only)");
plan(jdbc, t, "explain (analyze, costs off, timing off, summary off) select metadata->>'pid' from hybrid_scale order by embedding <=> '" + lit + "'::vector limit 20");
t.section("plan for the keyword query at 50,094 rows (scan nodes only)");
plan(jdbc, t, "explain (analyze, costs off, timing off, summary off) with q as (select replace(plainto_tsquery('english', '" + sample
+ "')::text, ' & ', ' | ')::tsquery as query) select d.metadata->>'pid', ts_rank_cd(d.tsv, q.query) as s from hybrid_scale d, q "
+ "where d.tsv @@ q.query order by s desc limit 20");
t.section("plan for the same keyword query at 94 rows");
plan(jdbc, t, "explain (analyze, costs off, timing off, summary off) with q as (select replace(plainto_tsquery('english', '" + sample
+ "')::text, ' & ', ' | ')::tsquery as query) select d.metadata->>'pid', ts_rank_cd(d.tsv, q.query) as s from hybrid_docs d, q "
+ "where d.tsv @@ q.query order by s desc limit 20");
t.section("rows the keyword query has to rank, by query kind (50,094 rows)");
for (String category : List.of(KnowledgeBase.EXACT_CODE, KnowledgeBase.EXACT_ID, KnowledgeBase.PARAPHRASE, KnowledgeBase.MIXED)) {
double avg = Metrics.category(category).stream().mapToInt(q -> jdbc.queryForObject(
"select count(*) from hybrid_scale where tsv @@ replace(plainto_tsquery('english', ?)::text, ' & ', ' | ')::tsquery",
Integer.class, q.text())).average().orElse(0);
t.line("%-24s average rows matching the OR query: %8.0f", category, avg);
}
List<String> texts = KnowledgeBase.QUERIES.stream().map(KnowledgeBase.Query::text).toList();
List<String> lits = texts.stream().map(x -> HybridSearch.literal(MiniLm.get().embed(x))).toList();
t.section("latency in milliseconds over 60 queries x 3 runs after 2 warm-up passes (query embedding excluded unless stated)");
t.line("%-34s %9s %9s %9s %9s", "", "94 p50", "94 p95", "50k p50", "50k p95");
t.line("%-34s %s", "embedding the question (MiniLM)", embedTiming(texts));
row(t, "vector, top 20", texts, lits, (i) -> tiny.vectorByLiteral(lits.get(i), 20), (i) -> big.vectorByLiteral(lits.get(i), 20));
row(t, "keyword, top 20", texts, lits, (i) -> tiny.keyword(texts.get(i), 20), (i) -> big.keyword(texts.get(i), 20));
row(t, "hybrid in Java (2 queries + fuse)", texts, lits,
(i) -> HybridSearch.fuse(tiny.vectorByLiteral(lits.get(i), 20), tiny.keyword(texts.get(i), 20), 10, 60),
(i) -> HybridSearch.fuse(big.vectorByLiteral(lits.get(i), 20), big.keyword(texts.get(i), 20), 10, 60));
row(t, "hybrid in one SQL statement", texts, lits, (i) -> tiny.hybridSqlByLiteral(texts.get(i), lits.get(i), 10, 20, 60),
(i) -> big.hybridSqlByLiteral(texts.get(i), lits.get(i), 10, 20, 60));
t.section("keyword query latency at 50,094 rows by query kind (p50 / p95 ms)");
for (String category : List.of(KnowledgeBase.EXACT_CODE, KnowledgeBase.EXACT_ID, KnowledgeBase.PARAPHRASE, KnowledgeBase.MIXED)) {
List<String> qs = Metrics.category(category).stream().map(KnowledgeBase.Query::text).toList();
List<Double> samples = new ArrayList<>();
for (int pass = 0; pass < 5; pass++) {
for (String q : qs) {
long s = System.nanoTime();
big.keyword(q, 20);
if (pass >= 2) {
samples.add((System.nanoTime() - s) / 1e6);
}
}
}
t.line("%-24s %7.1f / %7.1f", category, percentile(samples, 50), percentile(samples, 95));
}
}
jdbc.execute("drop table if exists hybrid_scale");
}
/** Placeholder holder so the maintenance setting above is visibly a separate step. */
private record HybridSearchIndexes(JdbcTemplate jdbc) {
}
private static void plan(JdbcTemplate jdbc, Transcript t, String sql) {
for (Map<String, Object> r : jdbc.queryForList(sql)) {
String line = r.values().iterator().next().toString();
if (line.contains("Scan") || line.contains("Limit") || line.contains("Sort") || line.contains("Rows Removed")) {
t.line("%s", line.length() > 118 ? line.substring(0, 118) + "..." : line);
}
}
}
private static String embedTiming(List<String> texts) {
List<Double> samples = new ArrayList<>();
for (int pass = 0; pass < 3; pass++) {
for (String q : texts) {
long s = System.nanoTime();
MiniLm.get().embed(q);
if (pass >= 1) {
samples.add((System.nanoTime() - s) / 1e6);
}
}
}
return String.format("%9.1f %9.1f (same for both table sizes)", percentile(samples, 50), percentile(samples, 95));
}
private static void row(Transcript t, String name, List<String> texts, List<String> lits, java.util.function.IntConsumer small,
java.util.function.IntConsumer large) {
List<Double> a = time(texts.size(), small);
List<Double> b = time(texts.size(), large);
t.line("%-34s %9.1f %9.1f %9.1f %9.1f", name, percentile(a, 50), percentile(a, 95), percentile(b, 50), percentile(b, 95));
}
private static List<Double> time(int n, java.util.function.IntConsumer call) {
List<Double> samples = new ArrayList<>();
for (int pass = 0; pass < 5; pass++) {
for (int i = 0; i < n; i++) {
long s = System.nanoTime();
call.accept(i);
if (pass >= 2) {
samples.add((System.nanoTime() - s) / 1e6);
}
}
}
return samples;
}
private static double percentile(List<Double> samples, int p) {
List<Double> sorted = samples.stream().sorted().toList();
return sorted.get(Math.min(sorted.size() - 1, (int) Math.ceil(p / 100.0 * sorted.size()) - 1));
}
}
@@ -0,0 +1,43 @@
package com.ankurm.hybridsearch.support;
import com.ankurm.hybridsearch.Bm25;
import com.ankurm.hybridsearch.HybridSearch;
import com.ankurm.hybridsearch.KnowledgeBase;
import com.ankurm.hybridsearch.MiniLm;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
/** One table, loaded once per test run: 94 passages embedded by all-MiniLM-L6-v2 and indexed for keyword search. */
public final class Fixture {
public static final String URL = System.getenv().getOrDefault("HYBRID_DB_URL", "jdbc:postgresql://localhost:5432/ragdemo");
public static final String TABLE = "hybrid_docs";
private static HybridSearch search;
private static Bm25 bm25;
private Fixture() {
}
public static JdbcTemplate jdbc() {
return new JdbcTemplate(new DriverManagerDataSource(URL, "demo", "demo"));
}
public static synchronized HybridSearch search() {
if (search == null) {
try {
search = HybridSearch.load(jdbc(), MiniLm.get(), TABLE, KnowledgeBase.PASSAGES);
} catch (Exception e) {
throw new IllegalStateException(e);
}
}
return search;
}
public static synchronized Bm25 bm25() {
if (bm25 == null) {
bm25 = new Bm25(KnowledgeBase.PASSAGES, 1.2, 0.75);
}
return bm25;
}
}
@@ -0,0 +1,43 @@
package com.ankurm.hybridsearch.support;
import java.util.List;
import java.util.function.Function;
import com.ankurm.hybridsearch.KnowledgeBase;
/** hit@1, hit@5 and MRR@10 of a ranker over a set of labelled queries. */
public final class Metrics {
public record Score(int n, int hit1, int hit5, double mrr) {
public String cells() {
return String.format("%2d/%-2d %2d/%-2d %.3f", hit1, n, hit5, n, mrr);
}
}
private Metrics() {
}
public static Score score(List<KnowledgeBase.Query> queries, Function<String, List<String>> ranker) {
int h1 = 0, h5 = 0;
double mrr = 0;
for (KnowledgeBase.Query q : queries) {
List<String> ranking = ranker.apply(q.text());
int rank = ranking.subList(0, Math.min(10, ranking.size())).indexOf(q.pid()) + 1;
if (rank == 1) {
h1++;
}
if (rank >= 1 && rank <= 5) {
h5++;
}
if (rank >= 1) {
mrr += 1.0 / rank;
}
}
return new Score(queries.size(), h1, h5, mrr / queries.size());
}
public static List<KnowledgeBase.Query> category(String name) {
return KnowledgeBase.QUERIES.stream().filter(q -> q.category().equals(name)).toList();
}
}
@@ -0,0 +1,26 @@
package com.ankurm.hybridsearch.support;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import org.springframework.ai.chat.messages.AssistantMessage;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.model.Generation;
import org.springframework.ai.chat.prompt.Prompt;
/** Records the prompts it is sent and answers "ok". What Spring AI sends is the part this module is responsible for. */
public class RecordingChatModel implements ChatModel {
private final List<Prompt> prompts = new CopyOnWriteArrayList<>();
public List<Prompt> prompts() {
return prompts;
}
@Override
public ChatResponse call(Prompt prompt) {
prompts.add(prompt);
return new ChatResponse(List.of(new Generation(new AssistantMessage("ok"))));
}
}
@@ -0,0 +1,52 @@
package com.ankurm.hybridsearch.support;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.nio.file.Files;
import java.nio.file.Path;
/**
* Writes a numbered transcript under {@code output/} and echoes it to the console. Every console block
* quoted in the article comes out of one of these files verbatim.
*/
public final class Transcript implements AutoCloseable {
private final Path path;
private final StringWriter buffer = new StringWriter();
private final PrintWriter out = new PrintWriter(buffer);
public Transcript(String fileName, String title) {
this.path = Path.of("output", fileName);
out.println("# " + title);
out.println();
}
public Transcript line(String format, Object... args) {
out.println(args.length == 0 ? format : String.format(format, args));
return this;
}
public Transcript blank() {
out.println();
return this;
}
public Transcript section(String heading) {
out.println();
out.println("--- " + heading + " ---");
return this;
}
@Override
public void close() {
out.flush();
try {
Files.createDirectories(path.getParent());
Files.writeString(path, buffer.toString());
} catch (IOException e) {
throw new IllegalStateException("could not write " + path, e);
}
System.out.print(buffer);
}
}
+3
View File
@@ -0,0 +1,3 @@
target/
*.iml
.idea/
+7
View File
@@ -0,0 +1,7 @@
# rag-ingestion
Companion code for [Spring AI RAG Ingestion Pipeline: PDF, Word and HTML Readers with Chunking Strategies Compared](https://ankurm.com/spring-ai-rag-ingestion-pipeline-pdf-word-html-chunking/).
PDF, Word and HTML versions of one 12-section runbook go through `PagePdfDocumentReader`, `TikaDocumentReader` and `JsoupDocumentReader`, then through several chunkers, and retrieval hit rate is measured on 37 fixed questions. Also: metadata in the embedding, content-hash ingestion, and what one edit costs to re-embed. Spring Boot 4.1.2, Spring AI 2.0.2, Java 25.
Run `scripts/run-all.sh` (needs PostgreSQL 16 with pgvector; see the script header). Every number in the article is a line in `output/`.
+38
View File
@@ -0,0 +1,38 @@
# What each reader returns for the same 36-paragraph runbook
corpus: 10665 characters of body text (title, 12 headings, 36 paragraphs)
--- PagePdfDocumentReader, default config ---
Documents returned: 4 (one per PDF page)
metadata of page 1: {file_name=runbook.pdf, page_number=1}
pages whose text contains the running footer: 4 of 4
characters as read: 30358, after collapsing runs of whitespace: 10812 (the corpus itself is 10665)
page 1 ends with: " Helios Operations Runbook - Confidential - Page 1"
page 2 starts with: "they receive every page but are not expected to act, and they write up what"
paragraphs that no single page contains whole, because a page break cuts them: 2 of 36
--- PagePdfDocumentReader with pageBottomMargin ---
pageBottomMargin= 30: footer on 4 of 4 pages, answer fragments still in the text: 37 of 37
pageBottomMargin= 60: footer on 0 of 4 pages, answer fragments still in the text: 37 of 37
pageBottomMargin=100: footer on 0 of 4 pages, answer fragments still in the text: 36 of 37
no margin: footer on 4 of 4 pages, answer fragments still in the text: 37 of 37
--- TikaDocumentReader on the .docx ---
Documents returned: 1
metadata: {source=runbook.docx}
characters: 10657, contains the footer text: true
starts with: "Helios Cloud Platform Operations Runbook\n1. Backups and Restore\nEvery production databas"
blank-line-separated blocks in the text: 1, newline-separated lines: 50
answer fragments in the text: 37 of 37
--- TikaDocumentReader on the same PDF and HTML ---
.pdf -> 1 Document, 10861 characters, metadata keys [source]
.html -> 1 Document, 10764 characters, contains "Cookie settings": true, contains tracking script text: false
--- JsoupDocumentReader on the .html ---
default config: 1 Document, 10712 characters, contains nav link text "Status": true, contains "Cookie settings": true
default metadata: {description=How Helios runs production., title=Helios Cloud Platform Operations Runbook}
selector=article: 1 Document, 10615 characters, nav text: false, footer text: false
selector=section, groupByElement=true: 12 Documents
first section Document: 1188 characters, starts "1. Backups and Restore Every production database is snapshot"
with metadataTag("description") and additionalMetadata: {description=How Helios runs production., format=html, title=Helios Cloud Platform Operations Runbook}
@@ -0,0 +1,23 @@
# TokenTextSplitter chunk size against retrieval hit rate (all-MiniLM-L6-v2, 37 questions)
source: runbook.docx read by TikaDocumentReader, 2172 cl100k tokens, 37 questions, embedding model all-MiniLM-L6-v2 (max 256 WordPiece tokens)
hit@k: the question's answer is inside one of the k best chunks. cut: answers that no chunk contains whole.
top3Toks: tokens in the three best chunks, the context a prompt would carry per question.
--- defaults except chunkSize (minChunkSizeChars=350) ---
chunkSize chunks meanToks hit@1 hit@3 hit@5 cut top3Toks
32 68 31.9 22/37 29/37 30/37 3 96
64 34 63.9 27/37 33/37 33/37 2 191
128 19 114.1 31/37 35/37 36/37 0 335
256 9 241.1 24/37 31/37 36/37 0 720
512 5 434.0 21/37 33/37 37/37 0 1278
800 3 723.3 17/37 37/37 37/37 0 2170
--- same sizes with minChunkSizeChars=10 ---
chunkSize chunks meanToks hit@1 hit@3 hit@5 cut top3Toks
32 88 24.6 31/37 32/37 32/37 0 76
64 42 51.7 27/37 32/37 34/37 1 160
128 19 114.1 31/37 35/37 36/37 0 335
256 9 241.1 24/37 31/37 36/37 0 720
512 5 434.0 21/37 33/37 37/37 0 1278
800 3 723.3 17/37 37/37 37/37 0 2170
@@ -0,0 +1,18 @@
# Chunking strategies on the same runbook (all-MiniLM-L6-v2, 37 questions)
strategy chunks meanToks hit@1 hit@3 hit@5 cut top3Toks
TokenTextSplitter 128 tokens 19 114.1 31/37 35/37 36/37 0 335
TokenTextSplitter 64 tokens, minChars=10 42 51.7 27/37 32/37 34/37 1 160
SentenceSplitter 128 tokens, no overlap 20 108.5 24/37 33/37 34/37 0 341
SentenceSplitter 128 tokens, 1 sentence overlap 24 113.5 30/37 33/37 36/37 0 337
SentenceSplitter 64 tokens, no overlap 41 52.8 30/37 34/37 34/37 0 161
one chunk per paragraph 37 56.4 34/37 36/37 36/37 0 176
one chunk per paragraph + section heading 37 62.7 32/37 35/37 36/37 0 194
one chunk per section (JsoupDocumentReader) 12 178.7 31/37 37/37 37/37 0 529
--- 37 chunks from 36 paragraphs ---
last paragraph chunk: section=12. Decommissioning, text="Helios Operations Runbook - Confidential"
--- what a paragraph chunk with its heading looks like ---
metadata: {chunk_index=2, section=1. Backups and Restore, source=runbook.docx}
text: 1. Backups and Restore\nPoint-in-time recovery is available for the last 7 days. To request a recovery, the service owne
@@ -0,0 +1,14 @@
# Metadata and the text that gets embedded
chunk metadata: {chunk_index=0, content_hash=86f5d5577cede265, format=docx, section=1. Backups and Restore, source=runbook.docx}
--- the text each model would embed for the same chunk ---
MetadataMode.NONE : \n\nEvery production database is snapshotted once a day at 01:30 UTC. Daily snapshots are kept for 35 days, af
MetadataMode.EMBED: chunk_index: 0\nformat: docx\nsection: 1. Backups and Restore\nsource: runbook.docx\ncontent_hash: 86f5d5577ce
--- hit rate, one chunk per paragraph, 37 questions ---
configuration chunks meanToks hit@1 hit@3 hit@5 cut top3Toks
MetadataMode.NONE 37 56.4 34/37 36/37 36/37 0 176
MetadataMode.EMBED, every key embedded 37 56.4 29/37 36/37 36/37 0 175
MetadataMode.EMBED, four keys in excludedEmbedMetadataKeys 37 56.4 31/37 35/37 36/37 0 174
embedded text with the four keys excluded: section: 1. Backups and Restore\n\nEvery production database is snapshotted once a day at 01:30 UTC. Daily sna
+15
View File
@@ -0,0 +1,15 @@
# One runbook, three formats, TokenTextSplitter(128), 37 questions
reader and configuration chunks meanToks hit@1 hit@3 hit@5 cut top3Toks
PDF PagePdfDocumentReader, each page split alone 36 106.6 24/37 32/37 33/37 1 333
PDF pages joined first, footer still there 34 112.9 26/37 33/37 34/37 1 338
PDF pages joined, pageBottomMargin=60 34 110.5 27/37 33/37 34/37 1 330
PDF pages joined, margin 60, whitespace collapsed 19 117.7 27/37 36/37 37/37 0 348
DOCX TikaDocumentReader 19 114.1 31/37 35/37 36/37 0 335
HTML JsoupDocumentReader, default selector (body) 19 114.8 30/37 34/37 36/37 0 342
HTML JsoupDocumentReader, selector=article 19 113.8 29/37 34/37 36/37 0 342
--- tokens the splitter had to cut, by reader ---
PDF pages as read : 30358 characters, 3933 tokens
PDF whitespace collapsed: 10655 characters, 2251 tokens
DOCX : 10657 characters, 2172 tokens
@@ -0,0 +1,18 @@
# Re-ingesting runbook.docx into pgvector 0.8.7 (all-MiniLM-L6-v2, 384 dims)
--- a naive loop: read, split, add, with random chunk ids ---
run 1: rows in table = 19, texts embedded so far = 19
run 2: rows in table = 38, texts embedded so far = 38
run 3: rows in table = 57, texts embedded so far = 57
rows saying "kept for 35 days": 3
--- TokenTextSplitter(128): windows of tokens ---
1. first ingest of v1 chunks=19 embedded=19 unchanged= 0 deleted= 0 | rows=19, texts embedded in total=19
2. same file again chunks=19 embedded= 0 unchanged=19 deleted= 0 | rows=19, texts embedded in total=19
3. same file a third time chunks=19 embedded= 0 unchanged=19 deleted= 0 | rows=19, texts embedded in total=19
4. v2: one paragraph edited chunks=19 embedded= 1 unchanged=18 deleted= 1 | rows=19, texts embedded in total=20
rows saying "kept for 35 days": 0, rows saying "kept for 45 days": 1
best match for the snapshot question now contains "45 days": true
5. v3: section 12 deleted from the file chunks=17 embedded= 1 unchanged=16 deleted= 3 | rows=17, texts embedded in total=22
rows saying "180 days in advance" (a fact only section 12 holds): 0
@@ -0,0 +1,12 @@
# Same sequence with one chunk per paragraph
--- HeadingAwareChunker: one chunk per paragraph ---
1. first ingest of v1 chunks=37 embedded=37 unchanged= 0 deleted= 0 | rows=37, texts embedded in total=37
2. same file again chunks=37 embedded= 0 unchanged=37 deleted= 0 | rows=37, texts embedded in total=37
3. same file a third time chunks=37 embedded= 0 unchanged=37 deleted= 0 | rows=37, texts embedded in total=37
4. v2: one paragraph edited chunks=37 embedded= 1 unchanged=36 deleted= 1 | rows=37, texts embedded in total=38
rows saying "kept for 35 days": 0, rows saying "kept for 45 days": 1
best match for the snapshot question now contains "45 days": true
5. v3: section 12 deleted from the file chunks=34 embedded= 0 unchanged=34 deleted= 3 | rows=34, texts embedded in total=39
rows saying "180 days in advance" (a fact only section 12 holds): 0
+9
View File
@@ -0,0 +1,9 @@
# Chunks to embed again after appending one sentence to one paragraph (all 36 paragraphs tried)
splitter chunks mean median max
TokenTextSplitter 128 tokens 19 5.0 2 17
TokenTextSplitter 256 tokens 9 2.5 2 10
SentenceSplitter 128, no overlap 19 2.4 2 8
SentenceSplitter 128, 1 sentence overlap 24 3.1 2 13
one chunk per paragraph 36 1.0 1 1
columns: chunks in the unedited file; mean, median and max chunks re-embedded over the 36 edits
@@ -0,0 +1,29 @@
# Filtering retrieval by the metadata written at ingestion
rows in table: 73 (runbook.docx and runbook.html, one chunk per paragraph)
JsoupDocumentReader selector=article: 1 line(s) of text -> 0 chunks from the paragraph chunker
selector="article h1, article h2, article p" with separator "\n": 49 lines -> 36 chunks
rows per format: [docx=37, html=36]
question: "How long is data kept?"
--- no filter, top 5 ---
0.640 html 12. Decommissioning After a customer contract ends, the customer's data is del
0.640 docx 12. Decommissioning After a customer contract ends, the customer's data is del
0.588 html 1. Backups and Restore Every production database is snapshotted once a day at 01:
0.588 docx 1. Backups and Restore Every production database is snapshotted once a day at 01:
0.569 html 9. Logging and Monitoring Metrics are stored at 10 second resolution for 7 days, the
(5 results)
--- filter: section == '9. Logging and Monitoring' ---
0.569 docx 9. Logging and Monitoring Metrics are stored at 10 second resolution for 7 days, the
0.569 html 9. Logging and Monitoring Metrics are stored at 10 second resolution for 7 days, the
0.514 docx 9. Logging and Monitoring Application logs are searchable for 90 days in the hot tie
0.514 html 9. Logging and Monitoring Application logs are searchable for 90 days in the hot tie
0.188 docx 9. Logging and Monitoring To keep paging humane, each on-call shift has a noise budg
(5 results)
--- filter: format == 'html' && section == '1. Backups and Restore' ---
0.588 html 1. Backups and Restore Every production database is snapshotted once a day at 01:
0.468 html 1. Backups and Restore Point-in-time recovery is available for the last 7 days. T
0.254 html 1. Backups and Restore A backup that has never been restored is only a hope. On t
(3 results)
+66
View File
@@ -0,0 +1,66 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>4.1.2</version>
<relativePath/>
</parent>
<groupId>com.ankurm</groupId>
<artifactId>rag-ingestion</artifactId>
<version>1.0.0</version>
<name>rag-ingestion</name>
<description>The ETL side of RAG on Spring AI 2.0: PDF, Word and HTML readers, chunking strategies compared by retrieval hit rate on one fixed question set, metadata enrichment and idempotent re-ingestion into pgvector.</description>
<properties>
<java.version>25</java.version>
<spring-ai.version>2.0.2</spring-ai.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-bom</artifactId>
<version>${spring-ai.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency><groupId>org.springframework.ai</groupId><artifactId>spring-ai-pdf-document-reader</artifactId></dependency>
<dependency><groupId>org.springframework.ai</groupId><artifactId>spring-ai-tika-document-reader</artifactId></dependency>
<dependency><groupId>org.springframework.ai</groupId><artifactId>spring-ai-jsoup-document-reader</artifactId></dependency>
<!-- Store module, not the starter: the store is built by hand so every setting is visible. -->
<dependency><groupId>org.springframework.ai</groupId><artifactId>spring-ai-pgvector-store</artifactId></dependency>
<!-- all-MiniLM-L6-v2 in-process through ONNX Runtime: a real embedding model, no API key. -->
<dependency><groupId>org.springframework.ai</groupId><artifactId>spring-ai-transformers</artifactId></dependency>
<dependency><groupId>org.postgresql</groupId><artifactId>postgresql</artifactId></dependency>
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jdbc</artifactId></dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>-Duser.timezone=UTC -Dstdout.encoding=UTF-8 -Dfile.encoding=UTF-8 -Xmx3g</argLine>
</configuration>
</plugin>
</plugins>
</build>
</project>
+10
View File
@@ -0,0 +1,10 @@
#!/usr/bin/env bash
# Regenerates every file in output/ by running the test suite.
# Needs: JDK 25, Maven, internet for the first run (about 90 MB for the MiniLM model, cached in ~/.cache/minilm),
# and a PostgreSQL 16 with pgvector 0.8 on localhost:5432 (database ragdemo, user demo / demo) for the two ingestion tests.
# Set RAG_DB_URL to point somewhere else.
set -euo pipefail
cd "$(dirname "$0")/.."
rm -rf output
mvn -B -q test
ls output
+7
View File
@@ -0,0 +1,7 @@
#!/usr/bin/env bash
# One way to get the database the ingestion tests expect: a pgvector container on :5432.
set -euo pipefail
docker run -d --name ragdemo-pg -e POSTGRES_USER=demo -e POSTGRES_PASSWORD=demo -e POSTGRES_DB=ragdemo \
-p 5432:5432 pgvector/pgvector:0.8.1-pg16
until docker exec ragdemo-pg pg_isready -U demo -d ragdemo >/dev/null 2>&1; do sleep 1; done
echo "ready: jdbc:postgresql://localhost:5432/ragdemo (demo / demo)"
@@ -0,0 +1,137 @@
package com.ankurm.ragingestion;
import java.util.List;
/**
* The one document every experiment in this module reads: a made-up operations runbook with 12 sections
* and 36 paragraphs, plus 37 questions whose answers sit in exactly one paragraph each.
*
* <p>It is written out as a PDF, a Word file and an HTML page (see the test fixtures), so the readers and
* the chunkers are compared on identical content. A question counts as answered when a retrieved chunk
* contains its {@code answer} fragment, and {@code CorpusTest} proves every fragment occurs exactly once.
*/
public final class Corpus {
public static final String TITLE = "Helios Cloud Platform Operations Runbook";
public record Section(String title, List<String> paragraphs) {
}
public record Question(String text, String answer) {
}
public static final List<Section> SECTIONS = List.of(
new Section("1. Backups and Restore", List.of(
"Every production database is snapshotted once a day at 01:30 UTC. Daily snapshots are kept for 35 days, after which they are deleted automatically. Weekly snapshots, taken on Sundays, are kept for 12 weeks. All snapshots are encrypted with AES-256 using keys held in the regional key management service, and each snapshot is copied to the eu-west-2 region within one hour of completion.",
"A backup that has never been restored is only a hope. On the first Monday of every quarter the storage team restores the newest snapshot of three randomly chosen databases into an isolated account and runs the integrity checklist against it. A restore test passes only if the restored database starts, replays its write-ahead log without errors and returns the expected row counts for the canary tables.",
"Point-in-time recovery is available for the last 7 days. To request a recovery, the service owner opens a ticket in the STORAGE queue with the target timestamp in UTC. The storage team aims to start the recovery within 2 hours of a request and will always restore into a new instance, never over the live one, so that the owner can compare the data before switching traffic.")),
new Section("2. Incident Severity and Response", List.of(
"Incidents are classified by customer impact. A Sev1 is a full outage of a customer-facing service or any confirmed loss of customer data. A Sev2 is a partial outage or a degradation that affects more than 10 percent of requests. A Sev3 is anything else that needs a human but does not hurt customers right now. The incident commander decides the severity and may raise or lower it at any time during the incident.",
"The response targets are fixed: for a Sev1 a responder must be working within 10 minutes of the page, for a Sev2 within 30 minutes and for a Sev3 by the end of the next business day. Sev1 incidents get a status page update within 20 minutes and then every 30 minutes until resolved. The communications lead, not the incident commander, writes those updates.",
"Every Sev1 and Sev2 gets a written post-incident review within 5 business days. The review is blameless, lists the contributing factors, and ends with action items that each have an owner and a due date. Action items that are still open after 60 days are escalated to the director of engineering.")),
new Section("3. On-Call Rotation", List.of(
"Each service has a primary and a secondary on-call engineer. The rotation changes every Thursday at 10:00 UTC, and the outgoing engineer gives a 30 minute verbal handover covering open alerts, risky changes and anything that felt unusual during the week. Nobody may be on call for more than 2 consecutive weeks without written approval from their manager.",
"If the primary does not acknowledge a page within 5 minutes it escalates to the secondary, and after a further 5 minutes to the engineering manager. On-call engineers are paid an allowance of 120 euros for each week on the rotation, plus time off in lieu for any night call that lasts longer than 1 hour.",
"New engineers shadow the rotation for two full weeks before taking the pager. During shadowing they receive every page but are not expected to act, and they write up what they would have done so that the primary can review it afterwards.")),
new Section("4. Deployments and Releases", List.of(
"Production deployments are blocked every Friday after 14:00 UTC and for the whole period from 20 December to 3 January. An emergency change during a freeze needs approval from the on-call incident commander and is recorded in the change log with the reason for the exception.",
"Every release starts as a canary that receives 5 percent of traffic for 20 minutes. If the error rate of the canary rises more than 2 percent above the baseline in that window the pipeline rolls back automatically without waiting for a human. If the canary stays healthy it is promoted in steps of 25 percent, with a 10 minute pause between steps.",
"Database migrations are shipped separately from application code and must be backwards compatible with the previous release, because a rollback restores only the code. A migration that rewrites more than 1 million rows must run as a background job with a throttle and must be announced in the platform channel one day in advance.")),
new Section("5. Access Control", List.of(
"Engineers reach production only through short-lived certificates issued by the access broker. A certificate is valid for 8 hours and cannot be renewed without signing in again with a hardware security key. Passwords and SMS codes are not accepted for any production system.",
"Access rights are reviewed every quarter by the owning team lead, and rights that were not used in the previous 90 days are removed automatically. When an engineer changes teams, their previous team's access is removed on the same day and not at the next review.",
"The break-glass account exists for the case where the access broker itself is down. Its token expires after 60 minutes, every use pages the security team immediately, and the engineer who used it must file a written justification within 24 hours.")),
new Section("6. Capacity and Autoscaling", List.of(
"Services scale on CPU utilisation with a target of 65 percent. After a scale-out the autoscaler waits 180 seconds before it considers another one, and after a scale-in it waits 600 seconds, so that a short burst cannot make the fleet flap up and down.",
"Each region keeps reserved headroom of 30 percent above the busiest hour of the last month. Capacity is validated by a load test on the first Wednesday of the month at 3 times the observed peak, and a service that cannot sustain it for 15 minutes gets a capacity ticket against the owning team.",
"Stateful services are scaled by hand because adding a node triggers data rebalancing. The owner schedules the change outside peak hours, watches rebalancing progress on the storage dashboard and does not start a second change until the first one has finished.")),
new Section("7. Databases", List.of(
"The standard database is PostgreSQL 16 with one primary and two replicas in separate availability zones. Replication lag above 15 seconds raises a warning and lag above 60 seconds pages the on-call engineer, because a replica that far behind cannot be promoted safely.",
"Automatic failover must complete within 90 seconds. Services reach the database through a connection pooler, so they reconnect on their own, and each service may open at most 200 connections in total. A service that needs more must use the pooler's transaction mode instead of raising the limit.",
"Maintenance tasks such as vacuum and index rebuilds run only in the window from 02:00 to 04:00 UTC. Schema changes that need an exclusive lock are not allowed in production at all; the owner must use the online migration tool, which copies the table in the background and swaps it atomically.")),
new Section("8. Networking and Certificates", List.of(
"All workloads run in private subnets. Outbound traffic leaves through NAT gateways with fixed addresses so that partners can allow-list them, and inbound traffic arrives only through the shared load balancers. The minimum accepted protocol version is TLS 1.3 for every internal and external endpoint.",
"Certificates are renewed automatically 30 days before they expire. If a renewal fails, an alert goes to the platform team on the first day and pages them when fewer than 10 days remain, because an expired certificate is the most common cause of a self-inflicted outage.",
"DNS records used for failover have a time to live of 60 seconds. All other records use 3600 seconds. Changing a TTL ahead of a planned migration must be done at least one full old-TTL period before the migration starts, so that resolvers have picked up the shorter value.")),
new Section("9. Logging and Monitoring", List.of(
"Application logs are searchable for 90 days in the hot tier and then archived for a further 400 days in object storage, where they can be restored on request. Logs that contain personal data are redacted at the agent before they leave the host.",
"Metrics are stored at 10 second resolution for 7 days, then downsampled to 1 minute resolution for 13 months. Dashboards belong to the service teams, and a dashboard that nobody has opened for 6 months is archived.",
"To keep paging humane, each on-call shift has a noise budget of no more than 5 pages. A team that exceeds the budget two shifts in a row must spend the next sprint reducing alert noise before taking new feature work.")),
new Section("10. Cost Management", List.of(
"Finance and engineering review cloud spend together on the third working day of each month. Any service whose spend is more than 20 percent above its forecast must present a short explanation and a plan at that review.",
"Resources without an owner tag are shut down after 14 days of being idle and deleted after another 14 days. The tagging policy is enforced at provisioning time, so an untagged resource can only exist if someone bypassed the pipeline.",
"The target for committed-use discounts is to cover 70 percent of steady-state compute. Coverage above 85 percent is discouraged because it removes the ability to shrink the fleet when demand falls.")),
new Section("11. Compliance and Vendors", List.of(
"The platform is audited for SOC 2 Type II every October, and the auditors receive read-only access to the evidence repository for the whole of that month. Penetration tests by an external firm are carried out twice a year, and every finding rated high or critical must be fixed within 30 days.",
"Customers on the Regulated tier have their data stored and processed only in EU regions. A new subprocessor may not receive customer data until a data processing agreement is signed, and the public subprocessor list is updated within 10 business days of any change.",
"Employees complete the security awareness course in their first week and again every 12 months. Completion is tracked in the learning system, and access to production is suspended for anyone more than 30 days overdue.")),
new Section("12. Decommissioning", List.of(
"A service can be deprecated only after its owner has announced the retirement date at least 180 days in advance and has contacted every known consumer directly. The platform team keeps a calendar of retirement dates so that two large migrations do not land in the same month.",
"After a customer contract ends, the customer's data is deleted within 30 days and the deletion is confirmed to the customer in writing. Backups that contain the data age out under the normal retention schedule and are not restored for any reason in the meantime.",
"When a service is switched off, its DNS records are removed after 14 days of zero traffic, and its credentials are revoked on the same day it stops serving. A final check confirms that no alerts, dashboards or scheduled jobs still reference the retired service.")));
public static final List<Question> QUESTIONS = List.of(
new Question("How long do we keep the daily database snapshots before they are deleted?", "kept for 35 days"),
new Question("How often is a restore test performed?", "first Monday of every quarter"),
new Question("How far back can a database be recovered to a chosen moment?", "last 7 days"),
new Question("How quickly does someone have to start working on the most serious kind of incident?", "within 10 minutes of the page"),
new Question("By when must a post-incident review be written?", "within 5 business days"),
new Question("How frequently are customers updated on the status page during a major outage?", "every 30 minutes until resolved"),
new Question("At what time does the on-call duty change hands each week?", "every Thursday at 10:00 UTC"),
new Question("What extra money does an on-call engineer receive?", "120 euros"),
new Question("What happens if nobody answers a page for five minutes?", "does not acknowledge a page within 5 minutes"),
new Question("On which days and times are releases to production not permitted?", "every Friday after 14:00 UTC"),
new Question("What proportion of users sees a new release first, and for how long?", "5 percent of traffic for 20 minutes"),
new Question("When does a release get undone without human involvement?", "rises more than 2 percent above the baseline"),
new Question("How long does a production login certificate last?", "valid for 8 hours"),
new Question("What is the consequence of using the emergency account?", "every use pages the security team"),
new Question("After how long does the emergency account's token stop working?", "expires after 60 minutes"),
new Question("What CPU load do we aim for when scaling services automatically?", "target of 65 percent"),
new Question("How much spare capacity does each region hold?", "reserved headroom of 30 percent"),
new Question("How long does the autoscaler wait after removing instances?", "waits 600 seconds"),
new Question("When does database replication delay start to raise a warning?", "lag above 15 seconds raises a warning"),
new Question("How quickly must a database failover complete?", "within 90 seconds"),
new Question("What is the cap on database connections for one service?", "at most 200 connections"),
new Question("How long before expiry are TLS certificates replaced?", "renewed automatically 30 days before"),
new Question("How long can resolvers cache DNS answers for failover names?", "time to live of 60 seconds"),
new Question("What is the oldest TLS version we accept?", "TLS 1.3 for every"),
new Question("For how long can I search recent application logs?", "searchable for 90 days"),
new Question("What is the page limit for a single on-call shift?", "no more than 5 pages"),
new Question("How fine-grained are metrics during their first week?", "10 second resolution"),
new Question("When do finance and engineering meet to discuss cloud spend?", "third working day of each month"),
new Question("How far above forecast does spending have to go before a team must explain it?", "more than 20 percent above its forecast"),
new Question("How much of our steady compute should be covered by committed-use discounts?", "cover 70 percent of steady-state compute"),
new Question("In which month does the SOC 2 audit take place?", "SOC 2 Type II every October"),
new Question("What is the deadline for fixing serious penetration test findings?", "fixed within 30 days"),
new Question("Where may data of customers on the Regulated tier live?", "only in EU regions"),
new Question("How much warning must be given before a service is retired?", "at least 180 days in advance"),
new Question("How soon after a contract ends is the customer's data erased?", "deleted within 30 days and"),
new Question("When are the DNS records of a switched-off service removed?", "removed after 14 days of zero traffic"),
new Question("What kind of new subprocessors need a signed agreement before getting data?", "data processing agreement is signed"));
/** The whole runbook as one string: title, then each section heading and paragraphs separated by blank lines. */
public static String fullText() {
StringBuilder sb = new StringBuilder(TITLE).append("\n\n");
for (Section s : SECTIONS) {
sb.append(s.title()).append("\n\n");
for (String p : s.paragraphs()) {
sb.append(p).append("\n\n");
}
}
return sb.toString();
}
/** Title, then each section heading followed by its paragraphs, in reading order. */
public static List<String> blocks() {
java.util.List<String> blocks = new java.util.ArrayList<>();
blocks.add(TITLE);
for (Section s : SECTIONS) {
blocks.add(s.title());
blocks.addAll(s.paragraphs());
}
return blocks;
}
private Corpus() {
}
}
@@ -0,0 +1,54 @@
package com.ankurm.ragingestion;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import org.springframework.ai.document.Document;
import org.springframework.ai.document.DocumentTransformer;
/**
* One chunk per paragraph, each tagged with the section heading it sits under. Line-oriented text (what the
* Tika reader returns for a Word file) is read as: a short numbered line with no full stop is a heading, any
* other line is a paragraph. With {@code prefixHeading} the heading is also written in front of the paragraph,
* so the embedding sees which section the paragraph belongs to.
*/
public class HeadingAwareChunker implements DocumentTransformer {
private static final Pattern HEADING = Pattern.compile("^\\d+\\. [^.]{1,60}$");
private final boolean prefixHeading;
public HeadingAwareChunker(boolean prefixHeading) {
this.prefixHeading = prefixHeading;
}
@Override
public List<Document> apply(List<Document> documents) {
List<Document> out = new ArrayList<>();
for (Document doc : documents) {
String section = "";
int index = 0;
for (String raw : doc.getText().split("\\R")) {
String line = raw.replaceAll("\\s+", " ").strip();
if (line.isEmpty()) {
continue;
}
if (HEADING.matcher(line).matches()) {
section = line;
continue;
}
if (section.isEmpty()) {
continue; // the document title, before the first section
}
Map<String, Object> metadata = new LinkedHashMap<>(doc.getMetadata());
metadata.put("section", section);
metadata.put("chunk_index", index++);
out.add(Document.builder().text(prefixHeading ? section + "\n" + line : line).metadata(metadata).build());
}
}
return out;
}
}
@@ -0,0 +1,90 @@
package com.ankurm.ragingestion;
import java.nio.file.Path;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.stream.Collectors;
import org.springframework.ai.document.Document;
import org.springframework.ai.document.DocumentReader;
import org.springframework.ai.document.DocumentTransformer;
import org.springframework.ai.reader.jsoup.JsoupDocumentReader;
import org.springframework.ai.reader.jsoup.config.JsoupDocumentReaderConfig;
import org.springframework.ai.reader.pdf.PagePdfDocumentReader;
import org.springframework.ai.reader.pdf.config.PdfDocumentReaderConfig;
import org.springframework.ai.reader.tika.TikaDocumentReader;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.core.io.FileSystemResource;
import org.springframework.jdbc.core.JdbcTemplate;
/**
* Reads one file, splits it, tags the chunks and writes them to a pgvector table so that running it again
* on the same file is harmless:
* <ul>
* <li>chunks that already exist (same id) are skipped, so nothing is embedded twice;</li>
* <li>chunks that exist in the table for this source but not in the new version are deleted;</li>
* <li>only the new chunks are embedded and written.</li>
* </ul>
*/
public class IngestionPipeline {
public record Result(int chunks, int embedded, int unchanged, int deleted) {
}
private final VectorStore store;
private final JdbcTemplate jdbc;
private final String table;
private final DocumentTransformer splitter;
public IngestionPipeline(VectorStore store, JdbcTemplate jdbc, String table, DocumentTransformer splitter) {
this.store = store;
this.jdbc = jdbc;
this.table = table;
this.splitter = splitter;
}
public Result ingest(Path file) {
String name = file.getFileName().toString();
String format = name.substring(name.lastIndexOf('.') + 1).toLowerCase(Locale.ROOT);
// 1. read, then join every page or element into one text so chunk boundaries do not depend on the file's layout
List<Document> read = reader(file, format).get();
String text = read.stream().map(Document::getText).collect(Collectors.joining("\n"));
List<Document> whole = List.of(new Document(text));
// 2. split and tag
List<Document> chunks = new MetadataEnricher(name, format).apply(splitter.apply(whole));
if (chunks.isEmpty() && !text.isBlank()) {
// a splitter that does not understand the reader's text shape returns nothing; say so instead of writing zero rows
throw new IllegalStateException(name + ": " + text.length() + " characters read, 0 chunks produced");
}
// 3. work out what is already stored for this source
Set<String> existing = new HashSet<>(jdbc.queryForList(
"select id::text from " + table + " where metadata->>'source' = ?", String.class, name));
Set<String> wanted = chunks.stream().map(Document::getId).collect(Collectors.toSet());
List<String> stale = existing.stream().filter(id -> !wanted.contains(id)).toList();
if (!stale.isEmpty()) {
store.delete(stale);
}
List<Document> fresh = chunks.stream().filter(c -> !existing.contains(c.getId())).toList();
if (!fresh.isEmpty()) {
store.add(fresh);
}
return new Result(chunks.size(), fresh.size(), chunks.size() - fresh.size(), stale.size());
}
static DocumentReader reader(Path file, String format) {
FileSystemResource resource = new FileSystemResource(file);
return switch (format) {
case "pdf" -> new PagePdfDocumentReader(resource, PdfDocumentReaderConfig.builder().withPageBottomMargin(60).build());
// headings and paragraphs as separate lines: JsoupDocumentReader joins elements with the separator, and a line-oriented chunker needs the lines
case "html", "htm" -> new JsoupDocumentReader(resource, JsoupDocumentReaderConfig.builder()
.selector("article h1, article h2, article p").separator("\n").build());
default -> new TikaDocumentReader(resource);
};
}
}
@@ -0,0 +1,56 @@
package com.ankurm.ragingestion;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.HexFormat;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.springframework.ai.document.Document;
import org.springframework.ai.document.DocumentTransformer;
/**
* Gives every chunk the same metadata whatever reader produced it: {@code source}, {@code format},
* {@code chunk_index} and a {@code content_hash}, and a deterministic id derived from them. The id is the
* whole idempotency story: the same chunk of the same file always gets the same id, so writing it twice
* is an upsert, and a chunk whose text changed gets a new id.
*/
public class MetadataEnricher implements DocumentTransformer {
private final String source;
private final String format;
public MetadataEnricher(String source, String format) {
this.source = source;
this.format = format;
}
@Override
public List<Document> apply(List<Document> chunks) {
List<Document> out = new ArrayList<>();
for (int i = 0; i < chunks.size(); i++) {
Document chunk = chunks.get(i);
String hash = sha256(chunk.getText()).substring(0, 16);
Map<String, Object> metadata = new LinkedHashMap<>(chunk.getMetadata());
metadata.put("source", source);
metadata.put("format", format);
metadata.put("chunk_index", i);
metadata.put("content_hash", hash);
String id = UUID.nameUUIDFromBytes((source + "#" + hash).getBytes(StandardCharsets.UTF_8)).toString();
out.add(Document.builder().id(id).text(chunk.getText()).metadata(metadata).build());
}
return out;
}
static String sha256(String text) {
try {
return HexFormat.of().formatHex(MessageDigest.getInstance("SHA-256").digest(text.getBytes(StandardCharsets.UTF_8)));
} catch (NoSuchAlgorithmException e) {
throw new IllegalStateException(e);
}
}
}
@@ -0,0 +1,44 @@
package com.ankurm.ragingestion;
import java.util.Map;
import org.springframework.ai.document.MetadataMode;
import org.springframework.ai.embedding.EmbeddingModel;
import org.springframework.ai.transformers.TransformersEmbeddingModel;
/**
* all-MiniLM-L6-v2 (384 dimensions) running inside the JVM through ONNX Runtime. Both model files come from
* Hugging Face and are cached under the user's home directory, so the first run downloads about 90 MB and
* later runs are offline. No API key is involved, which is what lets anyone repeat the measurements.
*/
public final class MiniLm {
private static final String HF = "https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2/resolve/main/";
private static EmbeddingModel shared;
private MiniLm() {
}
/** The shared instance, which embeds the text of a Document only (MetadataMode.NONE, the constructor default). */
public static synchronized EmbeddingModel get() {
if (shared == null) {
shared = create(MetadataMode.NONE);
}
return shared;
}
/** A fresh instance whose Document embedding content is built with the given metadata mode. */
public static EmbeddingModel create(MetadataMode mode) {
try {
TransformersEmbeddingModel m = new TransformersEmbeddingModel(mode);
m.setTokenizerResource(HF + "tokenizer.json");
m.setModelResource(HF + "onnx/model.onnx");
m.setResourceCacheDirectory(System.getProperty("user.home") + "/.cache/minilm");
m.setTokenizerOptions(Map.of("padding", "true", "truncation", "true", "maxLength", "256"));
m.afterPropertiesSet();
return m;
} catch (Exception e) {
throw new IllegalStateException(e);
}
}
}
@@ -0,0 +1,67 @@
package com.ankurm.ragingestion;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.springframework.ai.document.Document;
import org.springframework.ai.document.DocumentTransformer;
/**
* Groups whole sentences into chunks of about {@code targetTokens}, repeating the last
* {@code overlapSentences} sentences at the start of the next chunk. Unlike TokenTextSplitter it never cuts
* inside a sentence, so an answer that sits in one sentence cannot be torn in half by a chunk boundary.
*/
public class SentenceSplitter implements DocumentTransformer {
/** A full stop after a letter, followed by a capital: "UTC. Daily" splits, "1. Backups" does not. */
private static final String SENTENCE_END = "(?<=[a-z\\)][.!?])\\s+(?=[A-Z])";
private final int targetTokens;
private final int overlapSentences;
public SentenceSplitter(int targetTokens, int overlapSentences) {
this.targetTokens = targetTokens;
this.overlapSentences = overlapSentences;
}
@Override
public List<Document> apply(List<Document> documents) {
List<Document> out = new ArrayList<>();
for (Document doc : documents) {
List<String> sentences = new ArrayList<>();
for (String line : doc.getText().split("\\R")) {
String clean = line.replaceAll("\\s+", " ").strip();
if (!clean.isEmpty()) {
sentences.addAll(List.of(clean.split(SENTENCE_END)));
}
}
List<String> window = new ArrayList<>();
int tokens = 0;
int index = 0;
for (String sentence : sentences) {
int t = Tokens.count(sentence);
if (!window.isEmpty() && tokens + t > targetTokens) {
out.add(chunk(doc, window, index++));
List<String> keep = overlapSentences == 0 ? List.of()
: window.subList(Math.max(0, window.size() - overlapSentences), window.size());
window = new ArrayList<>(keep);
tokens = window.stream().mapToInt(Tokens::count).sum();
}
window.add(sentence);
tokens += t;
}
if (!window.isEmpty()) {
out.add(chunk(doc, window, index));
}
}
return out;
}
private static Document chunk(Document parent, List<String> sentences, int index) {
Map<String, Object> metadata = new LinkedHashMap<>(parent.getMetadata());
metadata.put("chunk_index", index);
return Document.builder().text(String.join(" ", sentences)).metadata(metadata).build();
}
}
@@ -0,0 +1,18 @@
package com.ankurm.ragingestion;
import com.knuddels.jtokkit.Encodings;
import com.knuddels.jtokkit.api.Encoding;
import com.knuddels.jtokkit.api.EncodingType;
/** Token counts in the same encoding TokenTextSplitter uses by default (cl100k_base). */
public final class Tokens {
private static final Encoding ENCODING = Encodings.newDefaultEncodingRegistry().getEncoding(EncodingType.CL100K_BASE);
private Tokens() {
}
public static int count(String text) {
return ENCODING.countTokens(text);
}
}
@@ -0,0 +1,47 @@
package com.ankurm.ragingestion;
import java.nio.file.Path;
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.Document;
import org.springframework.ai.reader.tika.TikaDocumentReader;
import org.springframework.ai.transformer.splitter.TokenTextSplitter;
import org.springframework.core.io.FileSystemResource;
/** Retrieval hit rate of TokenTextSplitter at six chunk sizes, with its default minChunkSizeChars and with a small one. */
class ChunkSizeSweepTest {
@TempDir
Path dir;
@Test
void chunkSizeSweep() throws Exception {
List<Document> text = new TikaDocumentReader(new FileSystemResource(Fixtures.docx(dir))).get();
int total = Tokens.count(text.get(0).getText());
try (Transcript t = new Transcript("02-chunk-size-sweep.txt", "TokenTextSplitter chunk size against retrieval hit rate (all-MiniLM-L6-v2, 37 questions)")) {
t.line("source: runbook.docx read by TikaDocumentReader, %d cl100k tokens, 37 questions, embedding model all-MiniLM-L6-v2 (max 256 WordPiece tokens)", total);
t.line("hit@k: the question's answer is inside one of the k best chunks. cut: answers that no chunk contains whole.");
t.line("top3Toks: tokens in the three best chunks, the context a prompt would carry per question.");
t.section("defaults except chunkSize (minChunkSizeChars=350)");
t.line("chunkSize %s", Eval.Score.HEADER);
for (int size : new int[] {32, 64, 128, 256, 512, 800}) {
List<Document> chunks = TokenTextSplitter.builder().withChunkSize(size).build().apply(text);
t.line("%9d %s", size, Eval.score(chunks, MiniLm.get()).row());
}
t.section("same sizes with minChunkSizeChars=10");
t.line("chunkSize %s", Eval.Score.HEADER);
for (int size : new int[] {32, 64, 128, 256, 512, 800}) {
List<Document> chunks = TokenTextSplitter.builder().withChunkSize(size).withMinChunkSizeChars(10).build().apply(text);
t.line("%9d %s", size, Eval.score(chunks, MiniLm.get()).row());
}
}
}
}
@@ -0,0 +1,20 @@
package com.ankurm.ragingestion;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.jupiter.api.Test;
class CorpusTest {
@Test
void everyAnswerFragmentOccursExactlyOnce() {
String text = Corpus.fullText();
for (Corpus.Question q : Corpus.QUESTIONS) {
int first = text.indexOf(q.answer());
assertThat(first).as("fragment present: %s", q.answer()).isGreaterThanOrEqualTo(0);
assertThat(text.indexOf(q.answer(), first + 1)).as("fragment unique: %s", q.answer()).isEqualTo(-1);
}
assertThat(Corpus.QUESTIONS).hasSize(37);
assertThat(Corpus.SECTIONS.stream().mapToInt(s -> s.paragraphs().size()).sum()).isEqualTo(36);
}
}
@@ -0,0 +1,68 @@
package com.ankurm.ragingestion;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import com.ankurm.ragingestion.support.Transcript;
import org.junit.jupiter.api.Test;
import org.springframework.ai.document.Document;
import org.springframework.ai.document.DocumentTransformer;
import org.springframework.ai.transformer.splitter.TokenTextSplitter;
/**
* For each of the 36 paragraphs in turn, append one sentence to it and count how many chunks get a new id,
* which is how many texts must be embedded again. No database and no model: ids are hashes of chunk text.
*/
class EditCostTest {
private static final String ADDED = " This paragraph was revised in the latest edition of the runbook.";
@Test
void whatOneEditCosts() {
List<String> blocks = Corpus.blocks();
List<Integer> paragraphBlocks = new ArrayList<>();
for (int i = 0; i < blocks.size(); i++) {
if (blocks.get(i).length() > 100) {
paragraphBlocks.add(i);
}
}
Map<String, Supplier<DocumentTransformer>> splitters = new LinkedHashMap<>();
splitters.put("TokenTextSplitter 128 tokens", () -> TokenTextSplitter.builder().withChunkSize(128).build());
splitters.put("TokenTextSplitter 256 tokens", () -> TokenTextSplitter.builder().withChunkSize(256).build());
splitters.put("SentenceSplitter 128, no overlap", () -> new SentenceSplitter(128, 0));
splitters.put("SentenceSplitter 128, 1 sentence overlap", () -> new SentenceSplitter(128, 1));
splitters.put("one chunk per paragraph", () -> new HeadingAwareChunker(false));
try (Transcript t = new Transcript("08-edit-cost.txt", "Chunks to embed again after appending one sentence to one paragraph (all 36 paragraphs tried)")) {
t.line("%-42s %6s %6s %6s %6s", "splitter", "chunks", "mean", "median", "max");
for (var entry : splitters.entrySet()) {
DocumentTransformer splitter = entry.getValue().get();
Set<String> before = ids(splitter, blocks);
List<Integer> costs = new ArrayList<>();
for (int index : paragraphBlocks) {
List<String> edited = new ArrayList<>(blocks);
edited.set(index, blocks.get(index) + ADDED);
Set<String> after = ids(splitter, edited);
costs.add((int) after.stream().filter(id -> !before.contains(id)).count());
}
List<Integer> sorted = costs.stream().sorted().toList();
t.line("%-42s %6d %6.1f %6d %6d", entry.getKey(), before.size(),
costs.stream().mapToInt(Integer::intValue).average().orElse(0), sorted.get(sorted.size() / 2),
sorted.get(sorted.size() - 1));
}
t.line("columns: chunks in the unedited file; mean, median and max chunks re-embedded over the 36 edits");
}
}
private static Set<String> ids(DocumentTransformer splitter, List<String> blocks) {
Document whole = new Document(String.join("\n", blocks));
return new MetadataEnricher("runbook.docx", "docx").apply(splitter.apply(List.of(whole))).stream()
.map(Document::getId).collect(Collectors.toSet());
}
}
@@ -0,0 +1,81 @@
package com.ankurm.ragingestion;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
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.Document;
import org.springframework.ai.reader.jsoup.JsoupDocumentReader;
import org.springframework.ai.reader.jsoup.config.JsoupDocumentReaderConfig;
import org.springframework.ai.reader.pdf.PagePdfDocumentReader;
import org.springframework.ai.reader.pdf.config.PdfDocumentReaderConfig;
import org.springframework.ai.reader.tika.TikaDocumentReader;
import org.springframework.ai.transformer.splitter.TokenTextSplitter;
import org.springframework.core.io.FileSystemResource;
/** The same words in three file formats, read seven ways, cut by the same 128-token splitter. */
class FormatParityTest {
@TempDir
Path dir;
@Test
void sameContentThreeFormats() throws Exception {
FileSystemResource pdf = new FileSystemResource(Fixtures.pdf(dir));
FileSystemResource docx = new FileSystemResource(Fixtures.docx(dir));
FileSystemResource html = new FileSystemResource(Fixtures.html(dir));
TokenTextSplitter splitter = TokenTextSplitter.builder().withChunkSize(128).build();
try (Transcript t = new Transcript("05-format-parity.txt", "One runbook, three formats, TokenTextSplitter(128), 37 questions")) {
t.line("%-52s %s", "reader and configuration", Eval.Score.HEADER);
List<Document> pages = new PagePdfDocumentReader(pdf).get();
row(t, "PDF PagePdfDocumentReader, each page split alone", splitter.apply(pages));
row(t, "PDF pages joined first, footer still there", splitter.apply(joined(pages)));
List<Document> trimmed = new PagePdfDocumentReader(pdf, PdfDocumentReaderConfig.builder().withPageBottomMargin(60).build()).get();
row(t, "PDF pages joined, pageBottomMargin=60", splitter.apply(joined(trimmed)));
row(t, "PDF pages joined, margin 60, whitespace collapsed", splitter.apply(joined(collapse(trimmed))));
row(t, "DOCX TikaDocumentReader", splitter.apply(new TikaDocumentReader(docx).get()));
row(t, "HTML JsoupDocumentReader, default selector (body)", splitter.apply(new JsoupDocumentReader(html).get()));
row(t, "HTML JsoupDocumentReader, selector=article",
splitter.apply(new JsoupDocumentReader(html, JsoupDocumentReaderConfig.builder().selector("article").build()).get()));
t.section("tokens the splitter had to cut, by reader");
t.line("PDF pages as read : %d characters, %d tokens", chars(pages), tokens(pages));
t.line("PDF whitespace collapsed: %d characters, %d tokens", chars(collapse(trimmed)), tokens(collapse(trimmed)));
t.line("DOCX : %d characters, %d tokens", chars(new TikaDocumentReader(docx).get()), tokens(new TikaDocumentReader(docx).get()));
}
}
private static List<Document> joined(List<Document> docs) {
return List.of(new Document(docs.stream().map(Document::getText).collect(Collectors.joining("\n"))));
}
private static List<Document> collapse(List<Document> docs) {
List<Document> out = new ArrayList<>();
for (Document d : docs) {
out.add(new Document(d.getText().replaceAll("[ \\t]+", " ").replaceAll(" ?\\n ?", "\n"), d.getMetadata()));
}
return out;
}
private static int chars(List<Document> docs) {
return docs.stream().mapToInt(d -> d.getText().length()).sum();
}
private static int tokens(List<Document> docs) {
return docs.stream().mapToInt(d -> Tokens.count(d.getText())).sum();
}
private static void row(Transcript t, String name, List<Document> chunks) {
t.line("%-52s %s", name, Eval.score(chunks, MiniLm.get()).row());
}
}
@@ -0,0 +1,102 @@
package com.ankurm.ragingestion;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import com.ankurm.ragingestion.support.CountingEmbeddingModel;
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.Document;
import org.springframework.ai.document.DocumentTransformer;
import org.springframework.ai.reader.tika.TikaDocumentReader;
import org.springframework.ai.transformer.splitter.TokenTextSplitter;
import org.springframework.ai.vectorstore.SearchRequest;
import org.springframework.ai.vectorstore.pgvector.PgVectorStore;
import org.springframework.core.io.FileSystemResource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
/** Re-ingesting into a real pgvector table: what a naive loop does, what stable chunk ids do, and what an edit costs. */
class IdempotentIngestionTest {
static final String URL = System.getenv().getOrDefault("RAG_DB_URL", "jdbc:postgresql://localhost:5432/ragdemo");
@TempDir
Path dir;
@Test
void reingestion() throws Exception {
JdbcTemplate jdbc = new JdbcTemplate(new DriverManagerDataSource(URL, "demo", "demo"));
List<String> blocks = Corpus.blocks();
List<String> edited = new ArrayList<>(blocks);
// block 2 is the first paragraph of section 1; change the number and add a sentence, the way a real edit looks
edited.set(2, blocks.get(2).replace("kept for 35 days", "kept for 45 days")
+ " Snapshots of databases tagged restricted are kept for 90 days instead.");
List<String> shorter = new ArrayList<>(edited.subList(0, edited.size() - 4)); // drop section 12
Path v1 = Fixtures.docx(dir, "runbook.docx", blocks);
try (Transcript t = new Transcript("06-idempotent-reingestion.txt", "Re-ingesting runbook.docx into pgvector 0.8.7 (all-MiniLM-L6-v2, 384 dims)")) {
t.section("a naive loop: read, split, add, with random chunk ids");
CountingEmbeddingModel naiveModel = new CountingEmbeddingModel(MiniLm.get());
PgVectorStore naive = store(jdbc, "ingest_naive", naiveModel);
List<Document> read = new TikaDocumentReader(new FileSystemResource(v1)).get();
for (int run = 1; run <= 3; run++) {
naive.add(TokenTextSplitter.builder().withChunkSize(128).build().apply(read));
t.line("run %d: rows in table = %d, texts embedded so far = %d", run, count(jdbc, "ingest_naive"), naiveModel.texts());
}
t.line("rows saying \"kept for 35 days\": %d", rows(jdbc, "ingest_naive", "%kept for 35 days%"));
scenario(t, jdbc, "TokenTextSplitter(128): windows of tokens", "ingest_tokens", new TokenTextSplitter(), blocks, edited, shorter);
}
try (Transcript t = new Transcript("07-paragraph-chunks-reingestion.txt", "Same sequence with one chunk per paragraph")) {
scenario(t, jdbc, "HeadingAwareChunker: one chunk per paragraph", "ingest_paragraphs", new HeadingAwareChunker(false), blocks, edited, shorter);
}
}
private void scenario(Transcript t, JdbcTemplate jdbc, String title, String table, DocumentTransformer splitter,
List<String> v1Blocks, List<String> v2Blocks, List<String> v3Blocks) throws Exception {
DocumentTransformer chosen = splitter instanceof TokenTextSplitter ? TokenTextSplitter.builder().withChunkSize(128).build() : splitter;
CountingEmbeddingModel model = new CountingEmbeddingModel(MiniLm.get());
PgVectorStore store = store(jdbc, table, model);
IngestionPipeline pipeline = new IngestionPipeline(store, jdbc, table, chosen);
t.section(title);
step(t, jdbc, table, model, "1. first ingest of v1", pipeline.ingest(Fixtures.docx(dir, "runbook.docx", v1Blocks)));
step(t, jdbc, table, model, "2. same file again", pipeline.ingest(Fixtures.docx(dir, "runbook.docx", v1Blocks)));
step(t, jdbc, table, model, "3. same file a third time", pipeline.ingest(Fixtures.docx(dir, "runbook.docx", v1Blocks)));
step(t, jdbc, table, model, "4. v2: one paragraph edited", pipeline.ingest(Fixtures.docx(dir, "runbook.docx", v2Blocks)));
t.line(" rows saying \"kept for 35 days\": %d, rows saying \"kept for 45 days\": %d",
rows(jdbc, table, "%kept for 35 days%"), rows(jdbc, table, "%kept for 45 days%"));
List<Document> top = store.similaritySearch(SearchRequest.builder()
.query("How long do we keep the daily database snapshots before they are deleted?").topK(1).build());
t.line(" best match for the snapshot question now contains \"45 days\": %s", top.get(0).getText().contains("45 days"));
step(t, jdbc, table, model, "5. v3: section 12 deleted from the file", pipeline.ingest(Fixtures.docx(dir, "runbook.docx", v3Blocks)));
t.line(" rows saying \"180 days in advance\" (a fact only section 12 holds): %d", rows(jdbc, table, "%180 days in advance%"));
}
private static void step(Transcript t, JdbcTemplate jdbc, String table, CountingEmbeddingModel model, String label, IngestionPipeline.Result r) {
t.line("%-40s chunks=%2d embedded=%2d unchanged=%2d deleted=%2d | rows=%2d, texts embedded in total=%d", label, r.chunks(),
r.embedded(), r.unchanged(), r.deleted(), count(jdbc, table), model.texts());
}
private static PgVectorStore store(JdbcTemplate jdbc, String table, CountingEmbeddingModel model) throws Exception {
PgVectorStore store = PgVectorStore.builder(jdbc, model).vectorTableName(table).dimensions(384)
.removeExistingVectorStoreTable(true).initializeSchema(true).build();
store.afterPropertiesSet();
return store;
}
static int count(JdbcTemplate jdbc, String table) {
return jdbc.queryForObject("select count(*) from " + table, Integer.class);
}
static int rows(JdbcTemplate jdbc, String table, String like) {
return jdbc.queryForObject("select count(*) from " + table + " where content like ?", Integer.class, like);
}
}
@@ -0,0 +1,62 @@
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);
}
}
@@ -0,0 +1,68 @@
package com.ankurm.ragingestion;
import java.nio.file.Path;
import java.util.List;
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.Document;
import org.springframework.ai.vectorstore.SearchRequest;
import org.springframework.ai.vectorstore.pgvector.PgVectorStore;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
/** What the metadata added at ingestion buys at query time: a filter on section or format, run inside pgvector. */
class MetadataFilterTest {
@TempDir
Path dir;
@Test
void filterBySectionAndFormat() throws Exception {
JdbcTemplate jdbc = new JdbcTemplate(new DriverManagerDataSource(IdempotentIngestionTest.URL, "demo", "demo"));
PgVectorStore store = PgVectorStore.builder(jdbc, MiniLm.get()).vectorTableName("ingest_filter").dimensions(384)
.removeExistingVectorStoreTable(true).initializeSchema(true).build();
store.afterPropertiesSet();
IngestionPipeline pipeline = new IngestionPipeline(store, jdbc, "ingest_filter", new HeadingAwareChunker(false));
Path html = Fixtures.html(dir);
// the first configuration that came to mind: selector=article. Jsoup flattens it to one line.
String flat = new org.springframework.ai.reader.jsoup.JsoupDocumentReader(new org.springframework.core.io.FileSystemResource(html),
org.springframework.ai.reader.jsoup.config.JsoupDocumentReaderConfig.builder().selector("article").build()).get().get(0).getText();
int flatChunks = new HeadingAwareChunker(false).apply(List.of(new Document(flat))).size();
String lined = IngestionPipeline.reader(html, "html").get().get(0).getText();
int linedChunks = new HeadingAwareChunker(false).apply(List.of(new Document(lined))).size();
pipeline.ingest(Fixtures.docx(dir));
pipeline.ingest(html);
String question = "How long is data kept?";
try (Transcript t = new Transcript("09-metadata-filter.txt", "Filtering retrieval by the metadata written at ingestion")) {
t.line("rows in table: %d (runbook.docx and runbook.html, one chunk per paragraph)", IdempotentIngestionTest.count(jdbc, "ingest_filter"));
t.line("JsoupDocumentReader selector=article: %d line(s) of text -> %d chunks from the paragraph chunker", flat.split("\\R").length, flatChunks);
t.line("selector=\"article h1, article h2, article p\" with separator \"\\n\": %d lines -> %d chunks", lined.split("\\R").length, linedChunks);
t.line("rows per format: %s", jdbc.queryForList("select metadata->>'format' as f, count(*) as n from ingest_filter group by 1 order by 1")
.stream().map(r -> r.get("f") + "=" + r.get("n")).toList());
t.line("question: \"%s\"", question);
t.section("no filter, top 5");
show(t, store.similaritySearch(SearchRequest.builder().query(question).topK(5).build()));
t.section("filter: section == '9. Logging and Monitoring'");
show(t, store.similaritySearch(SearchRequest.builder().query(question).topK(5)
.filterExpression("section == '9. Logging and Monitoring'").build()));
t.section("filter: format == 'html' && section == '1. Backups and Restore'");
show(t, store.similaritySearch(SearchRequest.builder().query(question).topK(5)
.filterExpression("format == 'html' && section == '1. Backups and Restore'").build()));
}
}
private static void show(Transcript t, List<Document> docs) {
for (Document d : docs) {
t.line(" %.3f %-5s %-34s %s", d.getScore(), d.getMetadata().get("format"), d.getMetadata().get("section"),
d.getText().substring(0, 58).replace("\n", " "));
}
t.line(" (%d results)", docs.size());
}
}
@@ -0,0 +1,143 @@
package com.ankurm.ragingestion;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
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.Document;
import org.springframework.ai.reader.jsoup.JsoupDocumentReader;
import org.springframework.ai.reader.jsoup.config.JsoupDocumentReaderConfig;
import org.springframework.ai.reader.pdf.PagePdfDocumentReader;
import org.springframework.ai.reader.pdf.config.PdfDocumentReaderConfig;
import org.springframework.ai.reader.tika.TikaDocumentReader;
import org.springframework.core.io.FileSystemResource;
/** What each reader hands back for the same runbook, written as PDF, Word and HTML. */
class ReadersTest {
@TempDir
Path dir;
@Test
void whatEachReaderReturns() throws Exception {
Path pdf = Fixtures.pdf(dir);
Path docx = Fixtures.docx(dir);
Path html = Fixtures.html(dir);
String footerMarker = "Confidential";
try (Transcript t = new Transcript("01-readers.txt", "What each reader returns for the same 36-paragraph runbook")) {
t.line("corpus: %d characters of body text (title, 12 headings, 36 paragraphs)", Corpus.fullText().length());
t.section("PagePdfDocumentReader, default config");
List<Document> pages = new PagePdfDocumentReader(new FileSystemResource(pdf)).get();
t.line("Documents returned: %d (one per PDF page)", pages.size());
t.line("metadata of page 1: %s", new TreeMap<>(pages.get(0).getMetadata()));
t.line("pages whose text contains the running footer: %d of %d", count(pages, footerMarker), pages.size());
t.line("characters as read: %d, after collapsing runs of whitespace: %d (the corpus itself is %d)", total(pages),
pages.stream().mapToInt(d -> norm(d.getText()).length()).sum(), Corpus.fullText().length());
t.line("page 1 ends with: \"%s\"", tail(pages.get(0).getText(), 70));
t.line("page 2 starts with: \"%s\"", norm(head(pages.get(1).getText(), 90)));
t.line("paragraphs that no single page contains whole, because a page break cuts them: %d of 36", cutParagraphs(pages));
t.section("PagePdfDocumentReader with pageBottomMargin");
for (int margin : new int[] {30, 60, 100}) {
PdfDocumentReaderConfig cfg = PdfDocumentReaderConfig.builder().withPageBottomMargin(margin).build();
List<Document> cut = new PagePdfDocumentReader(new FileSystemResource(pdf), cfg).get();
t.line("pageBottomMargin=%3d: footer on %d of %d pages, answer fragments still in the text: %d of %d", margin,
count(cut, footerMarker), cut.size(), fragments(cut), Corpus.QUESTIONS.size());
}
t.line("no margin: footer on %d of %d pages, answer fragments still in the text: %d of %d",
count(pages, footerMarker), pages.size(), fragments(pages), Corpus.QUESTIONS.size());
t.section("TikaDocumentReader on the .docx");
List<Document> word = new TikaDocumentReader(new FileSystemResource(docx)).get();
t.line("Documents returned: %d", word.size());
t.line("metadata: %s", new TreeMap<>(word.get(0).getMetadata()));
t.line("characters: %d, contains the footer text: %s", word.get(0).getText().length(),
word.get(0).getText().contains(footerMarker));
t.line("starts with: \"%s\"", head(word.get(0).getText(), 90));
t.line("blank-line-separated blocks in the text: %d, newline-separated lines: %d", word.get(0).getText().split("\n\n+").length,
word.get(0).getText().split("\n").length);
t.line("answer fragments in the text: %d of %d", fragments(word), Corpus.QUESTIONS.size());
t.section("TikaDocumentReader on the same PDF and HTML");
List<Document> tikaPdf = new TikaDocumentReader(new FileSystemResource(pdf)).get();
List<Document> tikaHtml = new TikaDocumentReader(new FileSystemResource(html)).get();
t.line(".pdf -> %d Document, %d characters, metadata keys %s", tikaPdf.size(), tikaPdf.get(0).getText().length(),
tikaPdf.get(0).getMetadata().keySet());
t.line(".html -> %d Document, %d characters, contains \"Cookie settings\": %s, contains tracking script text: %s",
tikaHtml.size(), tikaHtml.get(0).getText().length(), tikaHtml.get(0).getText().contains("Cookie settings"),
tikaHtml.get(0).getText().contains("noise noise"));
t.section("JsoupDocumentReader on the .html");
List<Document> body = new JsoupDocumentReader(new FileSystemResource(html)).get();
t.line("default config: %d Document, %d characters, contains nav link text \"Status\": %s, contains \"Cookie settings\": %s",
body.size(), body.get(0).getText().length(), body.get(0).getText().contains("Status"),
body.get(0).getText().contains("Cookie settings"));
t.line("default metadata: %s", new TreeMap<>(body.get(0).getMetadata()));
JsoupDocumentReaderConfig article = JsoupDocumentReaderConfig.builder().selector("article").build();
List<Document> art = new JsoupDocumentReader(new FileSystemResource(html), article).get();
t.line("selector=article: %d Document, %d characters, nav text: %s, footer text: %s", art.size(),
art.get(0).getText().length(), art.get(0).getText().contains("Status"),
art.get(0).getText().contains("Cookie settings"));
JsoupDocumentReaderConfig bySection = JsoupDocumentReaderConfig.builder().selector("section").allElements(false)
.groupByElement(true).build();
List<Document> sections = new JsoupDocumentReader(new FileSystemResource(html), bySection).get();
t.line("selector=section, groupByElement=true: %d Documents", sections.size());
t.line("first section Document: %d characters, starts \"%s\"", sections.get(0).getText().length(),
head(sections.get(0).getText(), 60));
JsoupDocumentReaderConfig meta = JsoupDocumentReaderConfig.builder().selector("article")
.metadataTag("description").additionalMetadata("format", "html").build();
Map<String, Object> m = new TreeMap<>(new JsoupDocumentReader(new FileSystemResource(html), meta).get().get(0).getMetadata());
t.line("with metadataTag(\"description\") and additionalMetadata: %s", m);
}
}
private static int count(List<Document> docs, String needle) {
return (int) docs.stream().filter(d -> d.getText().contains(needle)).count();
}
/** Collapse every run of whitespace to one space, the same normalisation the hit-rate check applies. */
static String norm(String s) {
return s.replaceAll("\\s+", " ").strip();
}
private static int fragments(List<Document> docs) {
String all = norm(docs.stream().map(Document::getText).collect(java.util.stream.Collectors.joining(" ")));
return (int) Corpus.QUESTIONS.stream().filter(q -> all.contains(q.answer())).count();
}
private static int cutParagraphs(List<Document> pages) {
int cut = 0;
for (Corpus.Section s : Corpus.SECTIONS) {
for (String p : s.paragraphs()) {
if (pages.stream().noneMatch(d -> norm(d.getText()).contains(p))) {
cut++;
}
}
}
return cut;
}
private static int total(List<Document> docs) {
return docs.stream().mapToInt(d -> d.getText().length()).sum();
}
private static String head(String s, int n) {
String one = s.strip().replace("\n", "\\n");
return one.length() <= n ? one : one.substring(0, n);
}
private static String tail(String s, int n) {
String one = s.strip().replace("\n", "\\n");
return one.length() <= n ? one : one.substring(one.length() - n);
}
}
@@ -0,0 +1,55 @@
package com.ankurm.ragingestion;
import java.nio.file.Path;
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.Document;
import org.springframework.ai.reader.jsoup.JsoupDocumentReader;
import org.springframework.ai.reader.jsoup.config.JsoupDocumentReaderConfig;
import org.springframework.ai.reader.tika.TikaDocumentReader;
import org.springframework.ai.transformer.splitter.TokenTextSplitter;
import org.springframework.core.io.FileSystemResource;
/** Six ways to cut the same runbook, scored on the same 37 questions with the same embedding model. */
class StrategyTest {
@TempDir
Path dir;
@Test
void strategies() throws Exception {
List<Document> word = new TikaDocumentReader(new FileSystemResource(Fixtures.docx(dir))).get();
List<Document> sections = new JsoupDocumentReader(new FileSystemResource(Fixtures.html(dir)),
JsoupDocumentReaderConfig.builder().selector("section").allElements(false).groupByElement(true).build()).get();
try (Transcript t = new Transcript("03-chunking-strategies.txt", "Chunking strategies on the same runbook (all-MiniLM-L6-v2, 37 questions)")) {
t.line("%-44s %s", "strategy", Eval.Score.HEADER);
row(t, "TokenTextSplitter 128 tokens", TokenTextSplitter.builder().withChunkSize(128).build().apply(word));
row(t, "TokenTextSplitter 64 tokens, minChars=10", TokenTextSplitter.builder().withChunkSize(64).withMinChunkSizeChars(10).build().apply(word));
row(t, "SentenceSplitter 128 tokens, no overlap", new SentenceSplitter(128, 0).apply(word));
row(t, "SentenceSplitter 128 tokens, 1 sentence overlap", new SentenceSplitter(128, 1).apply(word));
row(t, "SentenceSplitter 64 tokens, no overlap", new SentenceSplitter(64, 0).apply(word));
row(t, "one chunk per paragraph", new HeadingAwareChunker(false).apply(word));
row(t, "one chunk per paragraph + section heading", new HeadingAwareChunker(true).apply(word));
row(t, "one chunk per section (JsoupDocumentReader)", sections);
List<Document> paragraphs = new HeadingAwareChunker(false).apply(word);
t.section("37 chunks from 36 paragraphs");
t.line("last paragraph chunk: section=%s, text=\"%s\"", paragraphs.get(36).getMetadata().get("section"), paragraphs.get(36).getText());
t.section("what a paragraph chunk with its heading looks like");
Document sample = new HeadingAwareChunker(true).apply(word).get(2);
t.line("metadata: %s", new java.util.TreeMap<>(sample.getMetadata()));
t.line("text: %s", sample.getText().replace("\n", "\\n").substring(0, 120));
}
}
private static void row(Transcript t, String name, List<Document> chunks) {
t.line("%-44s %s", name, Eval.score(chunks, MiniLm.get()).row());
}
}
@@ -0,0 +1,58 @@
package com.ankurm.ragingestion.support;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import org.springframework.ai.document.Document;
import org.springframework.ai.embedding.EmbeddingModel;
import org.springframework.ai.embedding.EmbeddingRequest;
import org.springframework.ai.embedding.EmbeddingResponse;
/** Wraps a model and counts how many texts it was asked to embed, which is what an embedding API bills for. */
public class CountingEmbeddingModel implements EmbeddingModel {
private final EmbeddingModel delegate;
private final AtomicInteger texts = new AtomicInteger();
public CountingEmbeddingModel(EmbeddingModel delegate) {
this.delegate = delegate;
}
public int texts() {
return texts.get();
}
@Override
public EmbeddingResponse call(EmbeddingRequest request) {
texts.addAndGet(request.getInstructions().size());
return delegate.call(request);
}
@Override
public float[] embed(Document document) {
texts.incrementAndGet();
return delegate.embed(document);
}
@Override
public float[] embed(String text) {
texts.incrementAndGet();
return delegate.embed(text);
}
@Override
public List<float[]> embed(List<String> input) {
texts.addAndGet(input.size());
return delegate.embed(input);
}
@Override
public String getEmbeddingContent(Document document) {
return delegate.getEmbeddingContent(document);
}
@Override
public int dimensions() {
return delegate.dimensions();
}
}
@@ -0,0 +1,66 @@
package com.ankurm.ragingestion.support;
import java.util.List;
import com.ankurm.ragingestion.Corpus;
import com.ankurm.ragingestion.Tokens;
import org.springframework.ai.document.Document;
import org.springframework.ai.embedding.EmbeddingModel;
import org.springframework.ai.vectorstore.SearchRequest;
import org.springframework.ai.vectorstore.SimpleVectorStore;
/**
* Scores a set of chunks against the 37 questions. A question is answered at rank r when the r-th retrieved
* chunk is the first one whose text, with whitespace collapsed, contains the question's answer fragment.
*/
public final class Eval {
public record Score(int chunks, double meanTokens, int hit1, int hit3, int hit5, int cut, double contextTokens) {
public String row() {
int n = Corpus.QUESTIONS.size();
return String.format("%6d %8.1f %2d/%d %2d/%d %2d/%d %4d %9.0f", chunks, meanTokens, hit1, n, hit3, n, hit5, n,
cut, contextTokens);
}
public static final String HEADER = "chunks meanToks hit@1 hit@3 hit@5 cut top3Toks";
}
private Eval() {
}
public static String norm(String s) {
return s.replaceAll("\\s+", " ").strip();
}
public static Score score(List<Document> chunks, EmbeddingModel model) {
SimpleVectorStore store = SimpleVectorStore.builder(model).build();
store.add(chunks);
int h1 = 0, h3 = 0, h5 = 0, cut = 0;
long ctx = 0;
for (Corpus.Question q : Corpus.QUESTIONS) {
boolean exists = chunks.stream().anyMatch(c -> norm(c.getText()).contains(q.answer()));
if (!exists) {
cut++;
}
List<Document> top = store.similaritySearch(SearchRequest.builder().query(q.text()).topK(5).similarityThresholdAll().build());
for (int i = 0; i < top.size(); i++) {
if (norm(top.get(i).getText()).contains(q.answer())) {
if (i == 0) {
h1++;
}
if (i < 3) {
h3++;
}
h5++;
break;
}
}
for (int i = 0; i < Math.min(3, top.size()); i++) {
ctx += Tokens.count(top.get(i).getText());
}
}
double mean = chunks.stream().mapToInt(c -> Tokens.count(c.getText())).average().orElse(0);
return new Score(chunks.size(), mean, h1, h3, h5, cut, (double) ctx / Corpus.QUESTIONS.size());
}
}
@@ -0,0 +1,141 @@
package com.ankurm.ragingestion.support;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import com.ankurm.ragingestion.Corpus;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
import org.apache.pdfbox.pdmodel.font.Standard14Fonts;
import org.apache.poi.wp.usermodel.HeaderFooterType;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
/** Writes the runbook from {@link Corpus} as a PDF, a Word file and an HTML page, so no binary is committed. */
public final class Fixtures {
public static final String FOOTER = "Helios Operations Runbook - Confidential - Page ";
private Fixtures() {
}
public static Path pdf(Path dir) throws IOException {
Path file = dir.resolve("runbook.pdf");
try (PDDocument doc = new PDDocument()) {
PDType1Font body = new PDType1Font(Standard14Fonts.FontName.HELVETICA);
PDType1Font bold = new PDType1Font(Standard14Fonts.FontName.HELVETICA_BOLD);
float width = 480, lineHeight = 15, top = 790, bottom = 80;
List<String[]> lines = new ArrayList<>(); // {font, text}
lines.add(new String[] {"B", Corpus.TITLE});
lines.add(new String[] {"P", ""});
for (Corpus.Section s : Corpus.SECTIONS) {
lines.add(new String[] {"B", s.title()});
for (String paragraph : s.paragraphs()) {
for (String l : wrap(paragraph, body, 11, width)) {
lines.add(new String[] {"P", l});
}
lines.add(new String[] {"P", ""});
}
}
int pageNo = 0;
int i = 0;
while (i < lines.size()) {
PDPage page = new PDPage();
doc.addPage(page);
pageNo++;
try (PDPageContentStream cs = new PDPageContentStream(doc, page)) {
float y = top;
while (i < lines.size() && y > bottom) {
String[] l = lines.get(i++);
if (!l[1].isEmpty()) {
cs.beginText();
cs.setFont("B".equals(l[0]) ? bold : body, "B".equals(l[0]) ? 13 : 11);
cs.newLineAtOffset(60, y);
cs.showText(l[1]);
cs.endText();
}
y -= lineHeight;
}
cs.beginText();
cs.setFont(body, 9);
cs.newLineAtOffset(60, 40);
cs.showText(FOOTER + pageNo);
cs.endText();
}
}
doc.save(file.toFile());
}
return file;
}
public static Path docx(Path dir) throws IOException {
return docx(dir, "runbook.docx", Corpus.blocks());
}
/** Writes a Word file from blocks: the first is the title, a block starting "N. " is a heading, the rest paragraphs. */
public static Path docx(Path dir, String name, java.util.List<String> blocks) throws IOException {
Path file = dir.resolve(name);
try (XWPFDocument doc = new XWPFDocument(); OutputStream out = Files.newOutputStream(file)) {
for (int i = 0; i < blocks.size(); i++) {
String block = blocks.get(i);
XWPFRun run = doc.createParagraph().createRun();
if (i == 0) {
run.setBold(true);
run.setFontSize(18);
} else if (block.matches("^\\d+\\. [^.]{1,60}$")) {
run.setBold(true);
run.setFontSize(14);
}
run.setText(block);
}
doc.createFooter(HeaderFooterType.DEFAULT).createParagraph().createRun().setText("Helios Operations Runbook - Confidential");
doc.write(out);
}
return file;
}
public static Path html(Path dir) throws IOException {
Path file = dir.resolve("runbook.html");
StringBuilder sb = new StringBuilder("<!DOCTYPE html>\n<html lang=\"en\"><head><meta charset=\"utf-8\">")
.append("<title>").append(Corpus.TITLE).append("</title>")
.append("<meta name=\"description\" content=\"How Helios runs production.\">")
.append("<script>var tracking = 'noise noise noise';</script></head><body>\n")
.append("<header><nav><a href=\"/\">Home</a> | <a href=\"/docs\">Docs</a> | <a href=\"/status\">Status</a></nav></header>\n")
.append("<main><article>\n<h1>").append(Corpus.TITLE).append("</h1>\n");
for (Corpus.Section s : Corpus.SECTIONS) {
sb.append("<section><h2>").append(s.title()).append("</h2>\n");
for (String p : s.paragraphs()) {
sb.append("<p>").append(p.replace("&", "&amp;")).append("</p>\n");
}
sb.append("</section>\n");
}
sb.append("</article></main>\n<footer>Helios Operations Runbook - Confidential. Cookie settings | Privacy | Terms</footer>\n</body></html>\n");
Files.writeString(file, sb.toString());
return file;
}
static List<String> wrap(String text, PDType1Font font, float size, float width) throws IOException {
List<String> out = new ArrayList<>();
StringBuilder line = new StringBuilder();
for (String word : text.split(" ")) {
String candidate = line.isEmpty() ? word : line + " " + word;
if (font.getStringWidth(candidate) / 1000 * size > width && !line.isEmpty()) {
out.add(line.toString());
line = new StringBuilder(word);
} else {
line = new StringBuilder(candidate);
}
}
if (!line.isEmpty()) {
out.add(line.toString());
}
return out;
}
}
@@ -0,0 +1,52 @@
package com.ankurm.ragingestion.support;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.nio.file.Files;
import java.nio.file.Path;
/**
* Writes a numbered transcript under {@code output/} and echoes it to the console. Every console block
* quoted in the article comes out of one of these files verbatim.
*/
public final class Transcript implements AutoCloseable {
private final Path path;
private final StringWriter buffer = new StringWriter();
private final PrintWriter out = new PrintWriter(buffer);
public Transcript(String fileName, String title) {
this.path = Path.of("output", fileName);
out.println("# " + title);
out.println();
}
public Transcript line(String format, Object... args) {
out.println(args.length == 0 ? format : String.format(format, args));
return this;
}
public Transcript blank() {
out.println();
return this;
}
public Transcript section(String heading) {
out.println();
out.println("--- " + heading + " ---");
return this;
}
@Override
public void close() {
out.flush();
try {
Files.createDirectories(path.getParent());
Files.writeString(path, buffer.toString());
} catch (IOException e) {
throw new IllegalStateException("could not write " + path, e);
}
System.out.print(buffer);
}
}