File size: 2,594 Bytes
7640b67
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a6df84a
 
 
 
 
 
 
 
7640b67
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a6df84a
7640b67
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Quick Demo Script for RAG Pipeline

A simple script to demonstrate the RAG pipeline in action.
Run this after setting up your GOOGLE_API_KEY in .env
"""

from src.rag import (
    load_documents,
    chunk_documents,
    get_embeddings,
    create_vector_store,
    create_rag_pipeline,
)

import sys
from pathlib import Path

# Add project root to path
project_root = Path(__file__).parent.parent
sys.path.insert(0, str(project_root))



def main():
    print("๐Ÿš€ DocuMind AI - RAG Demo")
    print("=" * 50)
    
    # Step 1: Load and process documents
    print("\n๐Ÿ“„ Loading documents...")
    docs = load_documents("data/sample_docs")
    print(f"   Loaded {len(docs)} document(s)")
    
    # Step 2: Chunk documents
    print("\nโœ‚๏ธ  Chunking documents...")
    chunks = chunk_documents(docs)
    print(f"   Created {len(chunks)} chunks")
    
    # Step 3: Create vector store and add documents
    print("\n๐Ÿ—„๏ธ  Setting up vector store...")
    embeddings = get_embeddings()
    store = create_vector_store(embeddings, collection_name="demo_docs")
    store.add_documents(chunks)
    print(f"   Indexed {len(chunks)} chunks")
    
    # Step 4: Create RAG pipeline
    print("\n๐Ÿ”— Creating RAG pipeline...")
    pipeline = create_rag_pipeline(collection_name="demo_docs")
    print("   Pipeline ready!")
    
    # Step 5: Interactive Q&A
    print("\n" + "=" * 50)
    print("๐Ÿ’ฌ Ask questions about your documents")
    print("   Type 'quit' to exit")
    print("=" * 50)
    
    while True:
        try:
            question = input("\nโ“ Your question: ").strip()
            
            if question.lower() in ['quit', 'exit', 'q']:
                print("\n๐Ÿ‘‹ Goodbye!")
                break
            
            if not question:
                continue
            
            print("\n๐Ÿ” Searching and generating answer...")
            result = pipeline.query(question, include_sources=True)
            
            print(f"\n๐Ÿ’ฌ Answer:\n{result['answer']}")
            
            if result.get('sources'):
                print("\n๐Ÿ“š Sources:")
                for i, source in enumerate(result['sources'][:3], 1):
                    fname = source['metadata'].get('file_name', 'unknown')
                    score = source.get('score', 0)
                    print(f"   {i}. {fname} (relevance: {score:.2f})")
                    
        except KeyboardInterrupt:
            print("\n\n๐Ÿ‘‹ Goodbye!")
            break
        except Exception as e:
            print(f"\nโŒ Error: {e}")


if __name__ == "__main__":
    main()