File size: 3,212 Bytes
e8c5cc2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""ResearchOS-ML - ask the corpus a question from the terminal.



A CLI rather than a server, because the tool-first phase needs a fast feedback

loop more than it needs HTTP.



    python scripts/ask.py --config configs/ml.yaml "explain JEPA"

    python scripts/ask.py "how does JEPA differ from MAE?" --no-generate

    python scripts/ask.py --interactive

"""

from __future__ import annotations

import argparse
import sys
import textwrap
from pathlib import Path
from typing import Any

sys.path.insert(0, str(Path(__file__).resolve().parents[1]))

# Windows consoles default to cp1252. Model output routinely contains en dashes,
# non-breaking hyphens and curly quotes, and printing those raises
# UnicodeEncodeError mid-answer. Force UTF-8 and replace anything unmappable.
if hasattr(sys.stdout, "reconfigure"):
    sys.stdout.reconfigure(encoding="utf-8", errors="replace")

from src.api.ml_pipeline import MLPipeline
from src.config import load_config, seed_everything


def render(result: dict[str, Any], width: int = 96, snippet: int = 320) -> None:
    """Print an answer and its citations."""
    print()
    if result["answer"]:
        for para in result["answer"].split("\n"):
            print(textwrap.fill(para, width=width) if para.strip() else "")
    print()
    print("-" * width)
    flag = "" if result["confident"] else "  [LOW CONFIDENCE]"
    print(
        f"top score {result['top_score']}  |  {result['distinct_papers']} distinct papers  |  "
        f"{result['timing_ms']['total']:.0f} ms{flag}"
    )
    print("-" * width)

    for i, c in enumerate(result["citations"], 1):
        print(f"\n[{i}] {c['score']:+.2f}  {c['title'][:width - 14]}")
        print(f"     {c['arxiv_url']}  ({c['published']}, section: {c['section']})")
        body = c["text"][:snippet] + ("..." if len(c["text"]) > snippet else "")
        print(textwrap.fill(body, width=width - 5, initial_indent="     ", subsequent_indent="     "))
    print()


def main() -> None:
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("question", nargs="*", help="the question to ask")
    parser.add_argument("--config", default="configs/ml.yaml")
    parser.add_argument("--no-generate", action="store_true", help="passages only")
    parser.add_argument("--interactive", "-i", action="store_true")
    args = parser.parse_args()

    cfg = load_config(args.config)
    seed_everything(cfg["seed"])
    pipeline = MLPipeline(cfg)
    generate = not args.no_generate

    if args.interactive:
        print("Ask a question, or an empty line to quit.\n")
        while True:
            try:
                question = input("> ").strip()
            except (EOFError, KeyboardInterrupt):
                print()
                break
            if not question:
                break
            render(pipeline.answer(question, generate=generate))
        return

    question = " ".join(args.question).strip()
    if not question:
        raise SystemExit("Give a question, or use --interactive.")
    render(pipeline.answer(question, generate=generate))


if __name__ == "__main__":
    main()