File size: 2,088 Bytes
e183645
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Minimal usage example for the Hub snapshot.

    python examples/quickstart.py                  # local snapshot (repo root)
    python examples/quickstart.py <repo-id>        # straight from the Hub
"""

from __future__ import annotations

import sys
from pathlib import Path

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

from modeling_megaram import load  # noqa: E402

DOCS = [
    {
        "ext_id": "bm25",
        "title": "BM25",
        "text": (
            "BM25 is a bag-of-words retrieval function that ranks documents by query "
            "term frequency with saturation k1 and document length normalisation b. "
            "Typical values are k1 equal to 1.2 and b equal to 0.75."
        ),
    },
    {
        "ext_id": "beir",
        "title": "BEIR",
        "text": (
            "BEIR is a heterogeneous benchmark of 18 information retrieval datasets, "
            "introduced by Thakur, Reimers and Gurevych in 2021. It measures zero-shot "
            "retrieval with nDCG at 10."
        ),
    },
    {
        "ext_id": "rgb",
        "title": "RGB",
        "text": (
            "RGB tests noise robustness, negative rejection, information integration "
            "and counterfactual robustness in retrieval augmented generation."
        ),
    },
]


def main() -> int:
    target = sys.argv[1] if len(sys.argv) > 1 else str(ROOT)
    rag = load(target)
    print("loaded:", rag.info())

    rag.index_documents(DOCS)
    for q in [
        "What are the typical values of k1 and b in BM25?",
        "How many datasets does BEIR contain?",
        "Which benchmark tests negative rejection?",
        "What is the capital of France?",          # answerable from no document
    ]:
        ans = rag.answer(q)
        print(f"\nQ: {q}")
        print(f"   intent={ans.intent} conf={ans.confidence:.3f} abstained={ans.abstained}")
        print(f"   A: {ans.text}")
        for c in ans.citations:
            print(f"      [{c.doc_id}] {c.quote}")
    return 0


if __name__ == "__main__":
    raise SystemExit(main())