papertrail/lib/embeddings.ts
caelum0x 695a365ae4 feat: PaperTrail — enterprise evidence-verification platform
Multi-tenant platform (182 pages, 228 API routes, ~30 modules) built on a
deterministic verification core: clinical-trial claims checked against
ClinicalTrials.gov registered results and raw event counts (ARR/NNT/RR+CI
recomputed in code, oracle-tested), code-enforced exact-span grounding, and
multi-source cross-verification. Auth/RBAC/multi-tenancy, document processing
at scale (unpdf + Docling), agentic workflows, Claude Science connector,
compliance/audit, billing, public API, and more.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-08 07:21:22 +03:00

49 lines
1.5 KiB
TypeScript

// Embeddings via Voyage AI (Anthropic's recommended embeddings partner).
// Swap the fetch call here if you switch providers - nothing else in the
// codebase depends on which provider is used.
const VOYAGE_URL = "https://api.voyageai.com/v1/embeddings";
export async function embed(text: string): Promise<number[]> {
const apiKey = process.env.VOYAGE_API_KEY;
if (!apiKey) {
throw new Error("VOYAGE_API_KEY is not set");
}
const model = process.env.VOYAGE_MODEL || "voyage-3";
const res = await fetch(VOYAGE_URL, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${apiKey}`,
},
body: JSON.stringify({
input: [truncateForEmbedding(text)],
model,
input_type: "document",
}),
});
if (!res.ok) {
const body = await res.text();
throw new Error(`Voyage embeddings request failed (${res.status}): ${body}`);
}
const data = await res.json();
const vector = data?.data?.[0]?.embedding;
if (!Array.isArray(vector)) {
throw new Error("Voyage embeddings response missing embedding vector");
}
return vector;
}
// Voyage-3 has an 8k-ish practical input ceiling for a single doc; PubMed
// abstracts are well under this, but be defensive with any full-text pulls.
function truncateForEmbedding(text: string, maxChars = 20000): string {
return text.length > maxChars ? text.slice(0, maxChars) : text;
}
export function toPgVectorLiteral(vector: number[]): string {
return `[${vector.join(",")}]`;
}