-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdemonstrator.py
149 lines (121 loc) · 4.46 KB
/
demonstrator.py
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
"""
CAG Demonstrator Agent for comparing Cache-Augmented Generation (CAG) and RAG frameworks.
"""
import asyncio
import json
import logging
import time
from datetime import datetime
from pathlib import Path
from cag_demo.config import (
DATA_DIR,
LOGS_DIR,
CAG_CONFIG,
RAG_CONFIG,
LLM_CONFIGS
)
from cag_demo.cag_framework import CAGFramework
from cag_demo.rag_framework import RAGFramework
# Create necessary directories
RESULTS_DIR = Path(__file__).parent / "Results"
for dir_path in [DATA_DIR / "Preloaded_Contexts",
DATA_DIR / "Retrieved_Documents",
LOGS_DIR,
RESULTS_DIR]:
dir_path.mkdir(parents=True, exist_ok=True)
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s [%(levelname)s] %(message)s',
handlers=[
logging.FileHandler(LOGS_DIR / 'demonstrator.log'),
logging.StreamHandler()
]
)
logger = logging.getLogger(__name__)
class CAGDemonstrator:
"""Agent for demonstrating CAG Framework capabilities."""
def __init__(self, llm_name: str = 'gpt4'):
"""
Initialize the demonstrator.
Args:
llm_name: Name of the LLM to use
"""
self.llm_name = llm_name
self.cag = CAGFramework(llm_name, DATA_DIR / "Preloaded_Contexts")
self.rag = RAGFramework(llm_name, DATA_DIR / "Retrieved_Documents")
self.results = []
async def compare_frameworks(self, query: str) -> dict:
"""
Compare CAG and RAG frameworks on a given query.
Args:
query: Question to ask both frameworks
Returns:
dict: Comparison metrics
"""
logger.info(f"\nProcessing query: {query}")
# Test CAG
logger.info("\nCAG Response:")
start_time = time.time()
cag_response, cag_metrics = await self.cag.query(query)
cag_time = time.time() - start_time
logger.info(f"Response: {cag_response}")
logger.info(f"Time taken: {cag_time:.2f} seconds")
logger.info(f"Metrics: {cag_metrics}")
# Test RAG
logger.info("\nRAG Response:")
start_time = time.time()
rag_response, rag_metrics = await self.rag.query(query)
rag_time = time.time() - start_time
logger.info(f"Response: {rag_response}")
logger.info(f"Time taken: {rag_time:.2f} seconds")
logger.info(f"Metrics: {rag_metrics}")
# Compare results
comparison = {
'timestamp': datetime.now().isoformat(),
'query': query,
'cag_response': cag_response,
'rag_response': rag_response,
'cag_time': cag_time,
'rag_time': rag_time,
'time_difference': rag_time - cag_time,
'cag_metrics': cag_metrics,
'rag_metrics': rag_metrics
}
self.results.append(comparison)
logger.info("\nComparison:")
logger.info(f"CAG vs RAG time difference: {comparison['time_difference']:.2f} seconds")
logger.info("=" * 80)
return comparison
def save_results(self):
"""Save results to a JSON file."""
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
results_file = RESULTS_DIR / f"comparison_results_{timestamp}.json"
summary = {
'timestamp': datetime.now().isoformat(),
'llm_name': self.llm_name,
'cag_config': CAG_CONFIG,
'rag_config': RAG_CONFIG,
'results': self.results
}
with open(results_file, 'w') as f:
json.dump(summary, f, indent=2)
logger.info(f"\nResults saved to: {results_file}")
async def main():
"""Run the demonstration."""
demonstrator = CAGDemonstrator()
# Test queries
queries = [
"What are the main advantages of the CAG framework?",
"How does CAG compare to traditional RAG in terms of performance?",
"Explain how CAG eliminates retrieval steps.",
"What are the key differences between CAG and RAG architectures?",
"How does CAG handle context management differently from RAG?"
]
logger.info("Starting CAG Framework Demonstration...")
for query in queries:
await demonstrator.compare_frameworks(query)
# Save results
demonstrator.save_results()
if __name__ == "__main__":
asyncio.run(main())