-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLLM.py
More file actions
70 lines (58 loc) · 1.89 KB
/
LLM.py
File metadata and controls
70 lines (58 loc) · 1.89 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
"""CLI entry point for RAG PDF Q&A (direct, no API server)."""
import logging
from textbookapi.engine import RAGEngine, RAGEngineError
from textbookapi.config import RAGConfig, BOOKS
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s [%(name)s] %(message)s",
datefmt="%H:%M:%S",
)
def main():
print("=" * 60)
print(" RAG PDF Q&A System (Direct Mode)")
print("=" * 60)
print()
print("Choose a book:")
for i, (book_id, info) in enumerate(BOOKS.items(), 1):
print(f" {i}. {info['title']}")
print()
choice = input("Enter number: ").strip()
book_ids = list(BOOKS.keys())
idx = int(choice) - 1 if choice.isdigit() else 0
idx = max(0, min(idx, len(book_ids) - 1))
book_id = book_ids[idx]
config = RAGConfig.for_book(book_id)
print(f"\n Model: Ollama + {config.model_name}")
print(f" Book: {config.book_title}")
print("=" * 60)
try:
engine = RAGEngine(config)
engine.initialize()
except (RAGEngineError, FileNotFoundError) as e:
print(f"\nError: {e}")
return
print("\n" + "=" * 60)
print(" Ready! Ask questions about the book.")
print(" Type 'quit' or 'exit' to stop.")
print("=" * 60)
while True:
print()
question = input("You: ").strip()
if not question:
continue
if question.lower() in ("quit", "exit", "q"):
print("Goodbye!")
break
try:
token_gen, results = engine.query(question, stream=True)
print(f" [Retrieved {len(results)} passages, "
f"score: {results[0][1]:.3f}]")
print()
print("Assistant: ", end="")
for token in token_gen:
print(token, end="", flush=True)
print()
except RAGEngineError as e:
print(f"\nError: {e}")
if __name__ == "__main__":
main()