Spaces:
Sleeping
Sleeping
File size: 3,991 Bytes
b3515de | 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 | """
scripts/demo.py
---------------
Interactive demonstration for the board game RAG system.
Run (from project root):
.venv/bin/python scripts/demo.py catan
.venv/bin/python scripts/demo.py monopoly
Press Ctrl-C to exit interactive mode.
"""
import os
import sys
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from model import query_rag
DIVIDER = "=" * 70
DEMO_QUESTIONS: dict[str, list[dict]] = {
"catan": [
{
"label": "β
Easy win β direct factual lookup",
"question": "What happens when you roll a 7?",
},
{
"label": "β
Multi-section synthesis β retrieves correctly across chunks",
"question": (
"My road is 6 segments long and holds the Longest Road card. "
"An opponent builds a settlement in the middle, breaking it into "
"branches of 4 and 2. Do I lose the Longest Road card?"
),
},
{
"label": "β οΈ Generation failure β answer IS in context, model still abstains",
"question": "Can players trade resource cards with each other when it is not their turn?",
},
{
"label": "β
Hallucination resistance β correctly says rules don't specify",
"question": "Can a player make a binding promise to give resources on a future turn?",
},
],
"monopoly": [
{
"label": "β
Easy win β direct factual lookup",
"question": "How much money does each player start with?",
},
{
"label": "β
Multi-section synthesis β jail + property rules",
"question": "I am in jail. Can I still buy houses and collect rent on my properties?",
},
{
"label": "β οΈ Common misconception β Free Parking payout",
"question": "What do you receive when you land on Free Parking?",
},
{
"label": "β
Hallucination resistance β rules are silent on tie-breaking",
"question": "What happens if two players tie on the initial roll to see who goes first?",
},
],
}
def show_question(label: str, question: str, game: str):
print(f"\n{DIVIDER}")
print(f" {label}")
print(DIVIDER)
print(f"\nQ: {question}\n")
result = query_rag(question, game=game, k=3)
print("Retrieved chunks:")
for c in result["retrieved"]:
print(f" [{c['score']:.3f}] {c['title']}")
print(f"\nAnswer:\n{result['answer']}\n")
def main():
import argparse
parser = argparse.ArgumentParser(description="Interactive RAG demo for a board game.")
parser.add_argument("game", choices=list(DEMO_QUESTIONS.keys()), help="Game to demo")
args = parser.parse_args()
game = args.game
demos = DEMO_QUESTIONS[game]
print(DIVIDER)
print(f" {game.upper()} RAG SYSTEM β Demo")
print(DIVIDER)
print("Loading model and index (one-time)...")
show_question(demos[0]["label"], demos[0]["question"], game)
input("\n[Press Enter to continue to next demo question...]")
for demo in demos[1:]:
show_question(demo["label"], demo["question"], game)
input("[Press Enter to continue...]")
print(DIVIDER)
print(f" INTERACTIVE MODE β ask your own {game.title()} question")
print(" (type 'quit' to exit)")
print(DIVIDER)
while True:
try:
q = input("\nYour question: ").strip()
except (KeyboardInterrupt, EOFError):
print("\nDone.")
break
if not q or q.lower() in {"quit", "exit", "q"}:
print("Done.")
break
result = query_rag(q, game=game, k=3)
print("\nRetrieved chunks:")
for c in result["retrieved"]:
print(f" [{c['score']:.3f}] {c['title']}")
print(f"\nAnswer:\n{result['answer']}")
if __name__ == "__main__":
main()
|