Spaces:
Sleeping
Sleeping
File size: 4,442 Bytes
c6dfde3 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | 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);
});
|