papertrail/lib/agents/retrievalAgent.ts
caelum0x b5274e1bb7 fix: calibrate retrieval similarity threshold for voyage-3 (0.72 -> 0.6, env-tunable)
Dogfooding the live app surfaced that verify returned 'no confident match' even when
a claim quoted its own cached primary source: the hardcoded 0.72 cosine floor was too
high for voyage-3, whose claim-vs-source-abstract similarities land ~0.6-0.72. The 95%
benchmark never caught this because it runs the mock/fixture path, bypassing live
retrieval entirely.

- Lower default floor to 0.6; make it configurable via RETRIEVAL_SIMILARITY_THRESHOLD
  (clamped [0,1], malformed values fall back to default) so it can be tuned per model
  without a redeploy.
- Log the top pre-filter similarity on a miss (scores/ids only, no claim/source text)
  so the floor can be calibrated against real data.

tsc + build green.
2026-07-11 23:04:21 +03:00

204 lines
8.0 KiB
TypeScript

import { getPool } from "../db";
import { embed, toPgVectorLiteral } from "../embeddings";
import { searchPubmed, fetchPubmedRecords } from "../sources/pubmed";
import { searchTrials, fetchTrialResults } from "../sources/clinicaltrials";
import { SourceCandidate } from "../schemas";
// Cosine-similarity floor below which a cached source is treated as "no confident match".
// Configurable via RETRIEVAL_SIMILARITY_THRESHOLD so it can be calibrated per embedding
// model without a code change. Default 0.6: voyage-3 cosine similarity for a claim vs its
// own primary-source abstract typically lands in the ~0.6-0.72 band, so the previous 0.72
// floor rejected genuine direct-quote matches (a claim quoting its own trial scored below
// it). 0.6 admits those true matches while still rejecting off-topic sources, which score
// materially lower. Clamped to [0,1]; a malformed env value falls back to the default.
const DEFAULT_SIMILARITY_THRESHOLD = 0.6;
const SIMILARITY_THRESHOLD = ((): number => {
const raw = process.env.RETRIEVAL_SIMILARITY_THRESHOLD;
if (raw === undefined) return DEFAULT_SIMILARITY_THRESHOLD;
const parsed = Number.parseFloat(raw);
if (!Number.isFinite(parsed) || parsed < 0 || parsed > 1) {
return DEFAULT_SIMILARITY_THRESHOLD;
}
return parsed;
})();
const MAX_SOURCES = 3; // top-N confident matches returned for cross-verification
// When DEMO_MODE is on, retrieval NEVER live-fetches from PubMed/ClinicalTrials.gov —
// it answers only from the pre-ingested cache and returns null/empty on a miss. This keeps
// the live demo deterministic and off the network critical path: a judge pasting an
// off-distribution claim gets an honest "no confident match", never a mid-demo API
// stall or an accidental credit burn.
const DEMO_MODE = process.env.DEMO_MODE === "true";
// Columns selected for every SourceCandidate (kept identical across the queries below so
// phase/enrollment_count are always present, null for PubMed records).
const SOURCE_COLUMNS =
"id, source_type, external_id, title, raw_text, url, phase, enrollment_count, registered_results";
/**
* Return the top confident matching sources for a claim (best first), for
* cross-verification. When the user pinned a cited source, that source leads.
* Otherwise: pgvector similarity search over cached sources, filtered by threshold;
* on a cold miss (and not DEMO_MODE), live-fetch + cache, then re-search.
*/
export async function retrieveSources(
claim: string,
opts?: { preferExternalId?: string }
): Promise<SourceCandidate[]> {
const results: SourceCandidate[] = [];
// Pin-to-source: the specific paper the user cited leads the list (similarity 1).
if (opts?.preferExternalId) {
const pinned = await findByExternalId(opts.preferExternalId);
if (pinned) results.push(pinned);
}
const claimVector = await embed(claim);
let confident = await searchConfidentSources(claimVector);
if (confident.length === 0 && !DEMO_MODE) {
// Cold cache: fetch from PubMed + ClinicalTrials.gov, cache, and re-search.
await ingestLiveResults(claim);
confident = await searchConfidentSources(claimVector);
}
for (const candidate of confident) {
if (results.length >= MAX_SOURCES) break;
if (results.some((r) => r.id === candidate.id)) continue; // dedupe the pinned source
results.push(candidate);
}
return results.slice(0, MAX_SOURCES);
}
/**
* Single best-matching source, or null. Thin wrapper over retrieveSources for
* callers that only need the primary match (batch runs, back-compat).
*/
export async function retrieveSource(
claim: string,
opts?: { preferExternalId?: string }
): Promise<SourceCandidate | null> {
const sources = await retrieveSources(claim, opts);
return sources[0] ?? null;
}
/** Look up a cached source by its external id (PMID / NCT / DOI-derived). Returns a
* fully-confident candidate (similarity 1) so the pinned source is used directly. */
async function findByExternalId(externalId: string): Promise<SourceCandidate | null> {
const pool = getPool();
const { rows } = await pool.query(
`select ${SOURCE_COLUMNS}, 1.0 as similarity
from sources where external_id = $1 limit 1`,
[externalId]
);
return rows.length > 0 ? (rows[0] as SourceCandidate) : null;
}
/** Top-N cached sources by cosine similarity, filtered to the confidence threshold. */
async function searchConfidentSources(claimVector: number[]): Promise<SourceCandidate[]> {
const pool = getPool();
const vectorLiteral = toPgVectorLiteral(claimVector);
const { rows } = await pool.query(
`select ${SOURCE_COLUMNS},
1 - (embedding <=> $1::vector) as similarity
from sources
where embedding is not null
order by embedding <=> $1::vector
limit $2`,
[vectorLiteral, MAX_SOURCES]
);
const confident = (rows as SourceCandidate[]).filter(
(r) => r.similarity >= SIMILARITY_THRESHOLD
);
// Calibration diagnostic: when the best cached source exists but nothing clears the
// threshold, record the top similarity + threshold (no claim text, no source text) so
// the floor can be tuned against real data instead of guessed. Ids/scores only.
if (confident.length === 0 && rows.length > 0) {
const top = rows[0] as SourceCandidate & { similarity: number };
console.error(
`[retrieval] no confident match: top similarity ${Number(
top.similarity
).toFixed(3)} < threshold ${SIMILARITY_THRESHOLD} (source ${top.external_id})`
);
}
return confident;
}
async function ingestLiveResults(claim: string): Promise<void> {
const pool = getPool();
const [pmids, trials] = await Promise.all([
searchPubmed(claim, 3).catch(() => [] as string[]),
searchTrials(claim, 3).catch(() => []),
]);
const pubmedRecords = pmids.length > 0 ? await fetchPubmedRecords(pmids).catch(() => []) : [];
// Fetch each trial's registered results once, at ingestion, so the deterministic
// registry check reads from cache and never blocks the verify request.
const trialResults = await Promise.all(
trials.map((t) => fetchTrialResults(t.nctId).catch(() => []))
);
const toInsert: Array<{
source_type: string;
external_id: string;
title: string;
raw_text: string;
url: string;
phase: string | null;
enrollment_count: number | null;
registered_results: unknown[] | null;
}> = [
...pubmedRecords.map((r) => ({
source_type: "pubmed",
external_id: r.pmid,
title: r.title,
raw_text: r.abstract,
url: r.url,
phase: null,
enrollment_count: null,
registered_results: null,
})),
...trials.map((t, i) => ({
source_type: "clinicaltrials",
external_id: t.nctId,
title: t.title,
raw_text: t.summaryText,
url: t.url,
phase: t.phase,
enrollment_count: t.enrollmentCount,
registered_results: trialResults[i].length > 0 ? trialResults[i] : null,
})),
];
for (const record of toInsert) {
if (!record.raw_text || record.raw_text.trim().length < 20) continue;
try {
const vector = await embed(record.raw_text);
await pool.query(
`insert into sources (source_type, external_id, title, raw_text, url, phase, enrollment_count, registered_results, embedding)
values ($1, $2, $3, $4, $5, $6, $7, $8::jsonb, $9::vector)
on conflict (source_type, external_id) do update
set raw_text = excluded.raw_text, embedding = excluded.embedding,
phase = excluded.phase, enrollment_count = excluded.enrollment_count,
registered_results = excluded.registered_results`,
[
record.source_type,
record.external_id,
record.title,
record.raw_text,
record.url,
record.phase,
record.enrollment_count,
record.registered_results ? JSON.stringify(record.registered_results) : null,
toPgVectorLiteral(vector),
]
);
} catch (err) {
// One bad record shouldn't sink the whole ingestion batch.
console.error(`[retrievalAgent] failed to ingest ${record.source_type}:${record.external_id}`, err);
}
}
}