-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
73 lines (61 loc) · 2.44 KB
/
Copy pathmain.py
File metadata and controls
73 lines (61 loc) · 2.44 KB
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
import argparse
from pathlib import Path
from src import Pipeline
def main():
parser = argparse.ArgumentParser(description="REFRAG CLI")
sub = parser.add_subparsers(dest="command")
ix = sub.add_parser("index")
ix.add_argument("--file", "-f", type=str)
ix.add_argument("--dir", "-d", type=str)
ix.add_argument("--namespace", "-n", default="")
q = sub.add_parser("query")
q.add_argument("question", type=str)
q.add_argument("--namespace", "-n", default="")
q.add_argument("--stream", "-s", action="store_true")
q.add_argument("--no-rl", action="store_true")
sub.add_parser("stats")
args = parser.parse_args()
if args.command == "index":
pipe = Pipeline()
if args.file:
p = Path(args.file)
if not p.exists():
print(f"Not found: {p}")
return
pipe.index_document(p.read_text(encoding="utf-8"), p.stem, namespace=args.namespace)
elif args.dir:
d = Path(args.dir)
if not d.exists():
print(f"Not found: {d}")
return
docs = [{"id": f.stem, "text": f.read_text(encoding="utf-8")} for f in d.glob("*.txt")]
if docs:
pipe.index_documents(docs, namespace=args.namespace)
else:
print("No .txt files found")
elif args.command == "query":
pipe = Pipeline()
if args.stream:
print("\nAnswer: ", end="", flush=True)
for tok in pipe.query_stream(args.question, args.namespace, use_rl=not args.no_rl):
print(tok, end="", flush=True)
print()
else:
r = pipe.query(args.question, args.namespace, use_rl=not args.no_rl)
print(f"\nQ: {r['query']}")
print(f"A: {r['answer']}")
if "decompression_stats" in r:
s = r["decompression_stats"]
print(f"Decompressed: {s['decompressed_count']}/{s['total_chunks']}")
if "token_savings" in r:
print(f"Savings: {r['token_savings']['percentage_saved']}%")
elif args.command == "stats":
pipe = Pipeline()
s = pipe.get_stats()
print(f"Encoder: {s['encoder']['model']} (dim={s['encoder']['dim']})")
print(f"Decoder: {s['decoder']['model']}")
print(f"Vectors: {s['vector_store']['total_vectors']}")
else:
parser.print_help()
if __name__ == "__main__":
main()