askneo / rag /src /cli.ts
Faithology
Claude Opus 4.8
Deploy backend + RAG to Hugging Face Space (backend-only tree)
c6dfde3
Raw History Blame Contribute Delete
4.44 kB
import readline from "node:readline/promises";
import { parseArgs } from "node:util";
import { ALL_SOURCES, DEFAULT_SOURCES, type SourceName } from "./dataset.js";
import { runEval } from "./eval.js";
import { ingest } from "./ingest.js";
import { ask } from "./pipeline.js";
import { VectorStore } from "./store.js";
import type { ChatMessage, RagAnswer } from "./types.js";
const USAGE = `askneo-rag — RAG over the data dir
Usage:
tsx src/cli.ts ingest [--sources all|${DEFAULT_SOURCES.join(",")}] [--limit N] [--skip N] [--fresh]
tsx src/cli.ts query "<question>" [--no-rerank] [--json]
tsx src/cli.ts chat [--no-rerank]
tsx src/cli.ts eval [--n 20] [--seed 42] [--no-rerank]
tsx src/cli.ts stats
Sources: ${ALL_SOURCES.join(", ")}
default = every distinct primary source (${DEFAULT_SOURCES.join(", ")}); "all" is an alias for it.
`;
function printSources(res: RagAnswer): void {
console.log("\n\n--- Sources ---");
res.sources.forEach((s, i) => {
const label = [s.title, s.topic, s.source].filter(Boolean).join(" | ");
console.log(`[${i + 1}] ${label} (score ${s.score.toFixed(3)}, via ${s.via.join("+")})`);
});
console.log(`\nAnswered by ${res.provider}/${res.model}; queries: ${res.queries.join(" | ")}`);
}
async function main(): Promise<void> {
const [command, ...rest] = process.argv.slice(2);
const { values, positionals } = parseArgs({
args: rest,
allowPositionals: true,
options: {
sources: { type: "string" },
limit: { type: "string" },
skip: { type: "string" },
fresh: { type: "boolean", default: false },
"no-rerank": { type: "boolean", default: false },
json: { type: "boolean", default: false },
n: { type: "string", default: "20" },
seed: { type: "string", default: "42" },
},
});
const rerank = !values["no-rerank"];
switch (command) {
case "ingest": {
const sources = (values.sources?.trim() === "all"
? [...DEFAULT_SOURCES]
: (values.sources?.split(",").map((s) => s.trim()) ??
[...DEFAULT_SOURCES])) as SourceName[];
for (const s of sources) {
if (!ALL_SOURCES.includes(s)) {
throw new Error(`Unknown source "${s}". Valid: ${ALL_SOURCES.join(", ")}`);
}
}
await ingest({
sources,
limitPerSource: values.limit ? Number(values.limit) : undefined,
skip: values.skip ? Number(values.skip) : undefined,
fresh: values.fresh,
});
break;
}
case "query": {
const question = positionals.join(" ").trim();
if (!question) throw new Error('Provide a question: query "..."');
const index = await VectorStore.connect();
if (values.json) {
const res = await ask(index, question, { rerank });
console.log(JSON.stringify(res, null, 2));
} else {
const res = await ask(index, question, {
rerank,
onDelta: (t) => process.stdout.write(t),
});
printSources(res);
}
break;
}
case "chat": {
const index = await VectorStore.connect();
console.log(`Loaded ${index.size} chunks. Ask Neo anything (Ctrl+C or "exit" to quit).\n`);
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
const history: ChatMessage[] = [];
for (;;) {
const question = (await rl.question("you> ")).trim();
if (!question) continue;
if (question === "exit" || question === "quit") break;
process.stdout.write("neo> ");
const res = await ask(index, question, {
rerank,
history,
onDelta: (t) => process.stdout.write(t),
});
printSources(res);
console.log();
history.push({ role: "user", content: question });
history.push({ role: "assistant", content: res.answer });
}
rl.close();
break;
}
case "eval": {
const index = await VectorStore.connect();
await runEval(index, { n: Number(values.n), seed: Number(values.seed), rerank });
break;
}
case "stats": {
const store = await VectorStore.connect();
console.log(JSON.stringify(store.manifest, null, 2));
break;
}
default:
console.log(USAGE);
process.exitCode = command ? 1 : 0;
}
}
main().catch((err) => {
console.error(`\nError: ${err instanceof Error ? err.message : err}`);
process.exit(1);
});