-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
68 lines (60 loc) · 2.46 KB
/
Copy pathmain.py
File metadata and controls
68 lines (60 loc) · 2.46 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
import sys
import argparse
from rag_pipeline import RAGPipeline
from config import Config
def main():
# Validate configuration
try:
Config.validate()
except ValueError as e:
print(f"Configuration Error: {e}")
print("\nPlease create a .env file with your OPENAI_API_KEY")
print("Example: OPENAI_API_KEY=sk-your-key-here")
return
parser = argparse.ArgumentParser(description='RAG System - Query PDFs with OpenAI')
parser.add_argument('--ingest', type=str, help='Path to PDF file to ingest')
parser.add_argument('--query', type=str, help='Query to ask')
parser.add_argument('--interactive', action='store_true', help='Start interactive mode')
parser.add_argument('--no-vision', action='store_true', help='Skip vision analysis (faster)')
parser.add_argument('--top-k', type=int, default=5, help='Number of chunks to retrieve')
args = parser.parse_args()
# Initialize pipeline
rag = RAGPipeline()
# Ingest PDF if provided
if args.ingest:
print(f"Ingesting PDF: {args.ingest}")
use_vision = not args.no_vision
rag.ingest_pdf(args.ingest, use_vision=use_vision)
else:
# Try to load existing index
if not rag.load_existing_index():
print("\nNo existing index found and no PDF provided.")
print("Usage: python main.py --ingest <pdf_path>")
return
# Handle query modes
if args.interactive:
rag.interactive_query()
elif args.query:
result = rag.query(args.query, top_k=args.top_k)
if 'error' in result:
print(f"Error: {result['error']}")
else:
print("\n" + "=" * 50)
print("QUESTION:")
print(result['question'])
print("\n" + "-" * 50)
print("ANSWER:")
print(result['answer'])
print("\n" + "-" * 50)
print("SOURCES:")
for i, source in enumerate(result['sources'], 1):
print(f"{i}. Page {source['page']} ({source['type']}) - Score: {source['similarity_score']:.4f}")
print("=" * 50)
else:
print("\nIndex loaded successfully!")
print("\nUsage examples:")
print(" Interactive mode: python main.py --interactive")
print(" Single query: python main.py --query 'What is the main topic?'")
print(" Ingest new PDF: python main.py --ingest document.pdf")
if __name__ == "__main__":
main()