Add rag-eval module: retrieval metrics, golden dataset and a JUnit regression gate
hit@k, recall@k, precision@k, MRR and nDCG@k in plain Java with their edge cases pinned by tests, a 30-question graded golden dataset over a 40-passage runbook, a paired bootstrap, and a gate (baseline + tolerance + no new misses) shown failing on a tokenizer that drops digits. BM25, a hashed-trigram vector (not a neural embedding) and RRF; no model, no network. Java 25, JUnit 6.1.3. Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_017ic4b7F47n8shUHpexxavn
This commit is contained in:
co-authored by
Claude Sonnet 5.5
parent
0bb301d361
commit
98ed878675
@@ -0,0 +1,62 @@
|
||||
package com.ankurm.rageval;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* A dense-vector stand-in: every token becomes character trigrams, each trigram is hashed into one of
|
||||
* 512 dimensions, and passages are ranked by cosine similarity.
|
||||
*
|
||||
* <p><strong>This is not a neural embedding model.</strong> It captures spelling overlap ("restore" and
|
||||
* "restoring"), not meaning, so it cannot connect "died" to "fails". It exists so the evaluation code
|
||||
* has a second, differently-behaved retriever to compare with BM25 without downloading a model. To
|
||||
* evaluate a real embedding model, implement {@link Retriever} around it; nothing else changes.
|
||||
*/
|
||||
public final class HashedNgramRetriever implements Retriever {
|
||||
|
||||
private static final int DIMS = 512;
|
||||
private final Tokenizer tokenizer = new Tokenizer(Tokenizer.Mode.WORDS);
|
||||
private final List<Passage> passages;
|
||||
private final List<double[]> vectors = new ArrayList<>();
|
||||
|
||||
public HashedNgramRetriever(List<Passage> passages) {
|
||||
this.passages = passages;
|
||||
passages.forEach(p -> vectors.add(embed(p.text())));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String name() {
|
||||
return "ngram";
|
||||
}
|
||||
|
||||
double[] embed(String text) {
|
||||
double[] v = new double[DIMS];
|
||||
for (String t : tokenizer.tokens(text)) {
|
||||
String padded = "#" + t + "#";
|
||||
for (int i = 0; i + 3 <= padded.length(); i++) {
|
||||
v[Math.floorMod(padded.substring(i, i + 3).hashCode(), DIMS)] += 1;
|
||||
}
|
||||
}
|
||||
double norm = 0;
|
||||
for (double x : v) norm += x * x;
|
||||
norm = Math.sqrt(norm);
|
||||
if (norm > 0) for (int i = 0; i < DIMS; i++) v[i] /= norm;
|
||||
return v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> retrieve(String query, int k) {
|
||||
double[] q = embed(query);
|
||||
record Scored(String id, double score) {}
|
||||
List<Scored> scored = new ArrayList<>();
|
||||
for (int i = 0; i < passages.size(); i++) {
|
||||
double dot = 0;
|
||||
double[] v = vectors.get(i);
|
||||
for (int d = 0; d < DIMS; d++) dot += q[d] * v[d];
|
||||
scored.add(new Scored(passages.get(i).id(), dot));
|
||||
}
|
||||
return scored.stream().sorted(Comparator.comparingDouble(Scored::score).reversed())
|
||||
.limit(k).map(Scored::id).toList();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user