File size: 2,157 Bytes
3998131
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Verification Script for Module C (RAG Architecture)
Tests the RAG-based Letter Generation functionality.
"""

import sys
import os
from pathlib import Path

# Add project root to path
sys.path.append(str(Path(__file__).parent.parent))

from module_c.interface import LetterGenerationAPI
from module_c.retriever import TemplateRetriever

def test_module_c_rag():
    print("=== Testing Module C: RAG Letter Generation ===")
    
    # 1. Test Retrieval Directly
    print("\n[1] Testing Retrieval Logic")
    retriever = TemplateRetriever()
    
    queries = [
        ("I need a citizenship certificate for my child", "CitizenshipbyDescentApplication.txt"),
        ("I want to complain about noise in my ward", "General Application to DAO.txt"),
        ("I need a recommendation letter from ward office", "WardOfficeRecommendationRequest.txt")
    ]
    
    for query, expected in queries:
        print(f"\nQuery: '{query}'")
        results = retriever.retrieve_templates(query, k=1)
        if results:
            top_match = results[0]['filename']
            print(f" -> Retrieved: {top_match}")
            if top_match == expected:
                print(" -> [PASS] Correct template found.")
            else:
                print(f" -> [WARN] Expected {expected}, got {top_match}")
        else:
            print(" -> [FAIL] No templates retrieved.")

    # 2. Test End-to-End Generation
    print("\n[2] Testing End-to-End RAG Generation")
    api = LetterGenerationAPI()
    
    description = "I need a citizenship certificate for my daughter Sita. I am Ram Sharma from Kathmandu Ward 10. Date is 2081-01-01."
    
    if not os.getenv("MISTRAL_API_KEY"):
        print("WARNING: MISTRAL_API_KEY not set. Generation might fail.")
        
    result = api.generate_smart_letter(description)
    
    if result['success']:
        print("SUCCESS: Letter Generated")
        print(f"Template Used: {result.get('template_used')}")
        print("--- Preview (First 200 chars) ---")
        print(result['letter'][:200] + "...")
    else:
        print(f"FAILED: {result.get('error')}")

if __name__ == "__main__":
    test_module_c_rag()