DeathBlade020 commited on
Commit
5a18a8e
Β·
verified Β·
1 Parent(s): 0466455

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +352 -356
app.py CHANGED
@@ -1,357 +1,353 @@
1
- import gradio as gr
2
- import os
3
- import hashlib
4
- import json
5
- import pickle
6
- from datetime import datetime, timedelta
7
- from pathlib import Path
8
- from dotenv import load_dotenv
9
-
10
- # Import your original RAG technique modules
11
- from Hyde import get_answer_using_hyde
12
- from QueryDecomposition import get_answer_using_query_decomposition
13
- from QueryExpansion import get_answer_using_query_expansion
14
- from RagFusion import get_answer_using_rag_fusion
15
- from StepBackQuery import get_answer
16
-
17
- # Import new advanced retrieval techniques
18
- from AdvancedRag import (
19
- get_answer_using_multi_query,
20
- get_answer_using_parent_child,
21
- get_answer_using_contextual_compression,
22
- get_answer_using_cross_encoder,
23
- get_answer_using_semantic_routing
24
- )
25
-
26
- load_dotenv()
27
-
28
- # Cache configuration
29
- CACHE_DIR = Path("rag_cache")
30
- CACHE_DIR.mkdir(exist_ok=True)
31
- CACHE_EXPIRY_HOURS = 24 # Cache expires after 24 hours
32
-
33
- # Extended dictionary mapping technique names to their corresponding functions
34
- RAG_TECHNIQUES = {
35
- # Original Techniques
36
- "HyDE (Hypothetical Document Embeddings)": get_answer_using_hyde,
37
- "Query Decomposition": get_answer_using_query_decomposition,
38
- "Query Expansion": get_answer_using_query_expansion,
39
- "RAG Fusion": get_answer_using_rag_fusion,
40
- "Step Back Query": get_answer,
41
-
42
- # Advanced Retrieval Techniques
43
- "Multi-Query Retrieval": get_answer_using_multi_query,
44
- "Parent-Child Retrieval": get_answer_using_parent_child,
45
- "Contextual Compression": get_answer_using_contextual_compression,
46
- "Cross-Encoder Reranking": get_answer_using_cross_encoder,
47
- "Semantic Routing": get_answer_using_semantic_routing,
48
- }
49
-
50
- def generate_cache_key(link, technique):
51
- """
52
- Generate a unique cache key based on link and technique
53
- """
54
- cache_string = f"{link}_{technique}"
55
- return hashlib.md5(cache_string.encode()).hexdigest()
56
-
57
- def get_cache_file_path(cache_key):
58
- """
59
- Get the full path for a cache file
60
- """
61
- return CACHE_DIR / f"{cache_key}.pkl"
62
-
63
- def is_cache_valid(cache_file_path):
64
- """
65
- Check if cache file exists and is not expired
66
- """
67
- if not cache_file_path.exists():
68
- return False
69
-
70
- # Check if cache is expired
71
- file_time = datetime.fromtimestamp(cache_file_path.stat().st_mtime)
72
- expiry_time = datetime.now() - timedelta(hours=CACHE_EXPIRY_HOURS)
73
-
74
- return file_time > expiry_time
75
-
76
- def save_to_cache(cache_key, data):
77
- """
78
- Save data to cache file
79
- """
80
- try:
81
- cache_file_path = get_cache_file_path(cache_key)
82
- cache_data = {
83
- 'data': data,
84
- 'timestamp': datetime.now().isoformat(),
85
- 'cache_key': cache_key
86
- }
87
-
88
- with open(cache_file_path, 'wb') as f:
89
- pickle.dump(cache_data, f)
90
-
91
- print(f"βœ… Cached result for key: {cache_key}")
92
- return True
93
- except Exception as e:
94
- print(f"❌ Failed to save cache: {e}")
95
- return False
96
-
97
- def load_from_cache(cache_key):
98
- """
99
- Load data from cache file
100
- """
101
- try:
102
- cache_file_path = get_cache_file_path(cache_key)
103
-
104
- if not is_cache_valid(cache_file_path):
105
- return None
106
-
107
- with open(cache_file_path, 'rb') as f:
108
- cache_data = pickle.load(f)
109
-
110
- print(f"🎯 Cache hit for key: {cache_key}")
111
- return cache_data['data']
112
- except Exception as e:
113
- print(f"❌ Failed to load cache: {e}")
114
- return None
115
-
116
- def clear_expired_cache():
117
- """
118
- Automatically clear expired cache files
119
- """
120
- try:
121
- cache_files = list(CACHE_DIR.glob("*.pkl"))
122
- expired_count = 0
123
-
124
- for cache_file in cache_files:
125
- if not is_cache_valid(cache_file):
126
- cache_file.unlink()
127
- expired_count += 1
128
-
129
- if expired_count > 0:
130
- print(f"🧹 Auto-cleared {expired_count} expired cache files")
131
- except Exception as e:
132
- print(f"❌ Failed to auto-clear expired cache: {e}")
133
-
134
- def process_rag_query(link, question, technique):
135
- """
136
- Process the RAG query using the selected technique with caching
137
- """
138
- try:
139
- if not link or not question:
140
- return "Please provide both a link and a question."
141
-
142
- if not link.startswith(('http://', 'https://')):
143
- return "Please provide a valid URL starting with http:// or https://"
144
-
145
- # Auto-clear expired cache files
146
- clear_expired_cache()
147
-
148
- # Generate cache key based on link and technique
149
- cache_key = generate_cache_key(link, technique)
150
-
151
- # Try to load from cache first
152
- cached_result = load_from_cache(cache_key)
153
- if cached_result is not None:
154
- # Check if we have this specific question cached
155
- if isinstance(cached_result, dict) and question in cached_result:
156
- return cached_result[question]
157
-
158
- # Get the corresponding function for the selected technique
159
- rag_function = RAG_TECHNIQUES.get(technique)
160
-
161
- if not rag_function:
162
- return "Invalid technique selected."
163
-
164
- print(f"πŸ”„ Processing new query: {technique} for {link}")
165
-
166
- # Call the appropriate RAG function
167
- answer = rag_function(link, question)
168
-
169
- # Save to cache
170
- if cached_result is None:
171
- cached_result = {}
172
- elif not isinstance(cached_result, dict):
173
- cached_result = {}
174
-
175
- cached_result[question] = answer
176
- save_to_cache(cache_key, cached_result)
177
-
178
- return answer
179
-
180
- except Exception as e:
181
- return f"Error processing query: {str(e)}\n\nNote: Advanced techniques require additional dependencies. Make sure you have installed: sentence-transformers, scikit-learn"
182
-
183
- def create_webpage_preview(link):
184
- """
185
- Create an HTML iframe to preview the webpage
186
- """
187
- if not link:
188
- return ""
189
-
190
- if not link.startswith(('http://', 'https://')):
191
- return "<p style='color: red;'>Please provide a valid URL starting with http:// or https://</p>"
192
-
193
- # Create an iframe to display the webpage
194
- iframe_html = f"""
195
- <div style="width: 100%; height: 500px; border: 1px solid #ccc; border-radius: 5px;">
196
- <iframe src="{link}" width="100%" height="100%" frameborder="0"
197
- style="border-radius: 5px;">
198
- <p>Your browser does not support iframes.
199
- <a href="{link}" target="_blank">Click here to open the link</a></p>
200
- </iframe>
201
- </div>
202
- """
203
- return iframe_html
204
-
205
- # Create the Gradio interface
206
- def create_interface():
207
- with gr.Blocks(title="Advanced RAG Techniques", theme=gr.themes.Soft()) as demo: # type: ignore
208
-
209
- gr.Markdown("""
210
- # πŸš€ Advanced RAG Techniques Comparison Tool
211
- """)
212
- # This tool now includes **5 advanced retrieval techniques** alongside the original methods:
213
-
214
- # **πŸ”₯ New Advanced Techniques:**
215
- # - **Multi-Query Retrieval** - Generate diverse queries for comprehensive results
216
- # - **Parent-Child Retrieval** - Search with small chunks, return large context
217
- # - **Contextual Compression** - AI-powered relevance filtering
218
- # - **Cross-Encoder Reranking** - Superior relevance scoring
219
- # - **Semantic Routing** - Smart query classification and routing
220
-
221
- # **Instructions:**
222
- # 1. Enter a valid URL in the link box
223
- # 2. Preview the webpage content
224
- # 3. Enter your question about the content
225
- # 4. Select a RAG technique from the dropdown (try the new advanced ones!)
226
- # 5. Click Submit to get your answer
227
- # """)
228
-
229
- with gr.Row():
230
- with gr.Column(scale=1):
231
- # Input section
232
- gr.Markdown("## πŸ“ Input Section")
233
-
234
- link_input = gr.Textbox(
235
- label="Website URL",
236
- placeholder="https://example.com/article",
237
- info="Enter the URL of the webpage you want to analyze"
238
- )
239
-
240
- question_input = gr.Textbox(
241
- label="Your Question",
242
- placeholder="What is the main topic discussed in this article?",
243
- info="Ask any question about the content of the webpage"
244
- )
245
-
246
- technique_dropdown = gr.Dropdown(
247
- choices=list(RAG_TECHNIQUES.keys()),
248
- label="RAG Technique",
249
- value="Multi-Query Retrieval",
250
- info="Choose the RAG technique - try the new advanced techniques!"
251
- )
252
-
253
- submit_btn = gr.Button("πŸš€ Submit Query", variant="primary", size="lg")
254
-
255
- # Output section
256
- gr.Markdown("## πŸ’‘ Answer")
257
- answer_output = gr.Textbox(
258
- label="Generated Answer",
259
- lines=10,
260
- interactive=False,
261
- placeholder="Your answer will appear here..."
262
- )
263
-
264
- with gr.Column(scale=1):
265
- # Webpage preview section
266
- gr.Markdown("## 🌐 Webpage Preview")
267
- webpage_preview = gr.HTML(
268
- label="Webpage Content",
269
- value="<p style='text-align: center; color: #666; padding: 50px;'>Enter a URL to preview the webpage</p>"
270
- )
271
-
272
- # Event handlers
273
- link_input.change(
274
- fn=create_webpage_preview,
275
- inputs=[link_input],
276
- outputs=[webpage_preview]
277
- )
278
-
279
- submit_btn.click(
280
- fn=process_rag_query,
281
- inputs=[link_input, question_input, technique_dropdown],
282
- outputs=[answer_output]
283
- )
284
-
285
- # Add some example links and questions
286
- # gr.Markdown("""
287
- # ## πŸ“š Example Usage & Technique Comparison
288
-
289
- # **Sample URLs to try:**
290
- # - `https://lilianweng.github.io/posts/2023-06-23-agent/` (AI Agents blog post)
291
- # - `https://docs.python.org/3/tutorial/` (Python Tutorial)
292
- # - `https://en.wikipedia.org/wiki/Machine_learning` (Machine Learning Wikipedia)
293
-
294
- # **Sample Questions:**
295
- # - "What is task decomposition for LLM agents?"
296
- # - "What are the main components of an AI agent?"
297
- # - "How does retrieval-augmented generation work?"
298
-
299
- # **πŸ’‘ Pro Tip:** Try the same question with different techniques to see how results vary!
300
- # """)
301
-
302
- # # Add advanced technique descriptions
303
- # with gr.Accordion("πŸ”§ Advanced RAG Techniques Explained", open=False):
304
- # gr.Markdown("""
305
- # ## Original Techniques:
306
- # **HyDE:** Generates a hypothetical answer first, then uses it to retrieve relevant documents.
307
-
308
- # **Query Decomposition:** Breaks down complex questions into simpler sub-questions that are answered sequentially.
309
-
310
- # **Query Expansion:** Generates multiple variations of the original query to improve retrieval coverage.
311
-
312
- # **RAG Fusion:** Creates multiple related queries and uses reciprocal rank fusion to combine results.
313
-
314
- # **Step Back Query:** Transforms specific questions into more general ones to retrieve broader context.
315
-
316
- # ## πŸš€ Advanced Techniques:
317
- # **Multi-Query Retrieval:** Generates 4+ diverse query perspectives and merges results for comprehensive coverage.
318
-
319
- # **Parent-Child Retrieval:** Uses small chunks for precise matching but returns larger parent chunks for better context.
320
-
321
- # **Contextual Compression:** Uses LLM to compress retrieved chunks, keeping only information relevant to your question.
322
-
323
- # **Cross-Encoder Reranking:** Uses specialized neural models to score and rerank documents for superior relevance.
324
-
325
- # **Semantic Routing:** Automatically classifies your query type (factual, conceptual, comparative, analytical) and routes to the best retrieval strategy.
326
- # """)
327
-
328
- # # Installation requirements
329
- # with gr.Accordion("πŸ“¦ Additional Dependencies for Advanced Techniques", open=False):
330
- # gr.Markdown("""
331
- # To use the advanced retrieval techniques, install these additional packages:
332
-
333
- # ```bash
334
- # pip install sentence-transformers scikit-learn
335
- # ```
336
-
337
- # If you encounter errors with advanced techniques, make sure these dependencies are installed.
338
- # """)
339
-
340
- return demo
341
-
342
- # Launch the application
343
- if __name__ == "__main__":
344
- # Check if required environment variables are set
345
- if not os.getenv("OPENAI_API_KEY"):
346
- print("Warning: OPENAI_API_KEY not found in environment variables.")
347
- print("Please make sure to set your OpenAI API key in your .env file.")
348
-
349
- # Create and launch the interface
350
- demo = create_interface()
351
- demo.launch(
352
- server_name="127.0.0.1", # Local access
353
- server_port=7860, # Default Gradio port
354
- share=False, # Set to True if you want a public link
355
- debug=True, # Enable debug mode for development
356
- inbrowser=True # Automatically open in browser
357
  )
 
1
+ import gradio as gr
2
+ import os
3
+ import hashlib
4
+ import json
5
+ import pickle
6
+ from datetime import datetime, timedelta
7
+ from pathlib import Path
8
+ from dotenv import load_dotenv
9
+
10
+ # Import your original RAG technique modules
11
+ from Hyde import get_answer_using_hyde
12
+ from QueryDecomposition import get_answer_using_query_decomposition
13
+ from QueryExpansion import get_answer_using_query_expansion
14
+ from RagFusion import get_answer_using_rag_fusion
15
+ from StepBackQuery import get_answer
16
+
17
+ # Import new advanced retrieval techniques
18
+ from AdvancedRag import (
19
+ get_answer_using_multi_query,
20
+ get_answer_using_parent_child,
21
+ get_answer_using_contextual_compression,
22
+ get_answer_using_cross_encoder,
23
+ get_answer_using_semantic_routing
24
+ )
25
+
26
+ load_dotenv()
27
+
28
+ # Cache configuration
29
+ CACHE_DIR = Path("rag_cache")
30
+ CACHE_DIR.mkdir(exist_ok=True)
31
+ CACHE_EXPIRY_HOURS = 24 # Cache expires after 24 hours
32
+
33
+ # Extended dictionary mapping technique names to their corresponding functions
34
+ RAG_TECHNIQUES = {
35
+ # Original Techniques
36
+ "HyDE (Hypothetical Document Embeddings)": get_answer_using_hyde,
37
+ "Query Decomposition": get_answer_using_query_decomposition,
38
+ "Query Expansion": get_answer_using_query_expansion,
39
+ "RAG Fusion": get_answer_using_rag_fusion,
40
+ "Step Back Query": get_answer,
41
+
42
+ # Advanced Retrieval Techniques
43
+ "Multi-Query Retrieval": get_answer_using_multi_query,
44
+ "Parent-Child Retrieval": get_answer_using_parent_child,
45
+ "Contextual Compression": get_answer_using_contextual_compression,
46
+ "Cross-Encoder Reranking": get_answer_using_cross_encoder,
47
+ "Semantic Routing": get_answer_using_semantic_routing,
48
+ }
49
+
50
+ def generate_cache_key(link, technique):
51
+ """
52
+ Generate a unique cache key based on link and technique
53
+ """
54
+ cache_string = f"{link}_{technique}"
55
+ return hashlib.md5(cache_string.encode()).hexdigest()
56
+
57
+ def get_cache_file_path(cache_key):
58
+ """
59
+ Get the full path for a cache file
60
+ """
61
+ return CACHE_DIR / f"{cache_key}.pkl"
62
+
63
+ def is_cache_valid(cache_file_path):
64
+ """
65
+ Check if cache file exists and is not expired
66
+ """
67
+ if not cache_file_path.exists():
68
+ return False
69
+
70
+ # Check if cache is expired
71
+ file_time = datetime.fromtimestamp(cache_file_path.stat().st_mtime)
72
+ expiry_time = datetime.now() - timedelta(hours=CACHE_EXPIRY_HOURS)
73
+
74
+ return file_time > expiry_time
75
+
76
+ def save_to_cache(cache_key, data):
77
+ """
78
+ Save data to cache file
79
+ """
80
+ try:
81
+ cache_file_path = get_cache_file_path(cache_key)
82
+ cache_data = {
83
+ 'data': data,
84
+ 'timestamp': datetime.now().isoformat(),
85
+ 'cache_key': cache_key
86
+ }
87
+
88
+ with open(cache_file_path, 'wb') as f:
89
+ pickle.dump(cache_data, f)
90
+
91
+ print(f"βœ… Cached result for key: {cache_key}")
92
+ return True
93
+ except Exception as e:
94
+ print(f"❌ Failed to save cache: {e}")
95
+ return False
96
+
97
+ def load_from_cache(cache_key):
98
+ """
99
+ Load data from cache file
100
+ """
101
+ try:
102
+ cache_file_path = get_cache_file_path(cache_key)
103
+
104
+ if not is_cache_valid(cache_file_path):
105
+ return None
106
+
107
+ with open(cache_file_path, 'rb') as f:
108
+ cache_data = pickle.load(f)
109
+
110
+ print(f"🎯 Cache hit for key: {cache_key}")
111
+ return cache_data['data']
112
+ except Exception as e:
113
+ print(f"❌ Failed to load cache: {e}")
114
+ return None
115
+
116
+ def clear_expired_cache():
117
+ """
118
+ Automatically clear expired cache files
119
+ """
120
+ try:
121
+ cache_files = list(CACHE_DIR.glob("*.pkl"))
122
+ expired_count = 0
123
+
124
+ for cache_file in cache_files:
125
+ if not is_cache_valid(cache_file):
126
+ cache_file.unlink()
127
+ expired_count += 1
128
+
129
+ if expired_count > 0:
130
+ print(f"🧹 Auto-cleared {expired_count} expired cache files")
131
+ except Exception as e:
132
+ print(f"❌ Failed to auto-clear expired cache: {e}")
133
+
134
+ def process_rag_query(link, question, technique):
135
+ """
136
+ Process the RAG query using the selected technique with caching
137
+ """
138
+ try:
139
+ if not link or not question:
140
+ return "Please provide both a link and a question."
141
+
142
+ if not link.startswith(('http://', 'https://')):
143
+ return "Please provide a valid URL starting with http:// or https://"
144
+
145
+ # Auto-clear expired cache files
146
+ clear_expired_cache()
147
+
148
+ # Generate cache key based on link and technique
149
+ cache_key = generate_cache_key(link, technique)
150
+
151
+ # Try to load from cache first
152
+ cached_result = load_from_cache(cache_key)
153
+ if cached_result is not None:
154
+ # Check if we have this specific question cached
155
+ if isinstance(cached_result, dict) and question in cached_result:
156
+ return cached_result[question]
157
+
158
+ # Get the corresponding function for the selected technique
159
+ rag_function = RAG_TECHNIQUES.get(technique)
160
+
161
+ if not rag_function:
162
+ return "Invalid technique selected."
163
+
164
+ print(f"πŸ”„ Processing new query: {technique} for {link}")
165
+
166
+ # Call the appropriate RAG function
167
+ answer = rag_function(link, question)
168
+
169
+ # Save to cache
170
+ if cached_result is None:
171
+ cached_result = {}
172
+ elif not isinstance(cached_result, dict):
173
+ cached_result = {}
174
+
175
+ cached_result[question] = answer
176
+ save_to_cache(cache_key, cached_result)
177
+
178
+ return answer
179
+
180
+ except Exception as e:
181
+ return f"Error processing query: {str(e)}\n\nNote: Advanced techniques require additional dependencies. Make sure you have installed: sentence-transformers, scikit-learn"
182
+
183
+ def create_webpage_preview(link):
184
+ """
185
+ Create an HTML iframe to preview the webpage
186
+ """
187
+ if not link:
188
+ return ""
189
+
190
+ if not link.startswith(('http://', 'https://')):
191
+ return "<p style='color: red;'>Please provide a valid URL starting with http:// or https://</p>"
192
+
193
+ # Create an iframe to display the webpage
194
+ iframe_html = f"""
195
+ <div style="width: 100%; height: 500px; border: 1px solid #ccc; border-radius: 5px;">
196
+ <iframe src="{link}" width="100%" height="100%" frameborder="0"
197
+ style="border-radius: 5px;">
198
+ <p>Your browser does not support iframes.
199
+ <a href="{link}" target="_blank">Click here to open the link</a></p>
200
+ </iframe>
201
+ </div>
202
+ """
203
+ return iframe_html
204
+
205
+ # Create the Gradio interface
206
+ def create_interface():
207
+ with gr.Blocks(title="Advanced RAG Techniques", theme=gr.themes.Soft()) as demo: # type: ignore
208
+
209
+ gr.Markdown("""
210
+ # πŸš€ Advanced RAG Techniques Comparison Tool
211
+ """)
212
+ # This tool now includes **5 advanced retrieval techniques** alongside the original methods:
213
+
214
+ # **πŸ”₯ New Advanced Techniques:**
215
+ # - **Multi-Query Retrieval** - Generate diverse queries for comprehensive results
216
+ # - **Parent-Child Retrieval** - Search with small chunks, return large context
217
+ # - **Contextual Compression** - AI-powered relevance filtering
218
+ # - **Cross-Encoder Reranking** - Superior relevance scoring
219
+ # - **Semantic Routing** - Smart query classification and routing
220
+
221
+ # **Instructions:**
222
+ # 1. Enter a valid URL in the link box
223
+ # 2. Preview the webpage content
224
+ # 3. Enter your question about the content
225
+ # 4. Select a RAG technique from the dropdown (try the new advanced ones!)
226
+ # 5. Click Submit to get your answer
227
+ # """)
228
+
229
+ with gr.Row():
230
+ with gr.Column(scale=1):
231
+ # Input section
232
+ gr.Markdown("## πŸ“ Input Section")
233
+
234
+ link_input = gr.Textbox(
235
+ label="Website URL",
236
+ placeholder="https://example.com/article",
237
+ info="Enter the URL of the webpage you want to analyze"
238
+ )
239
+
240
+ question_input = gr.Textbox(
241
+ label="Your Question",
242
+ placeholder="What is the main topic discussed in this article?",
243
+ info="Ask any question about the content of the webpage"
244
+ )
245
+
246
+ technique_dropdown = gr.Dropdown(
247
+ choices=list(RAG_TECHNIQUES.keys()),
248
+ label="RAG Technique",
249
+ value="Multi-Query Retrieval",
250
+ info="Choose the RAG technique - try the new advanced techniques!"
251
+ )
252
+
253
+ submit_btn = gr.Button("πŸš€ Submit Query", variant="primary", size="lg")
254
+
255
+ # Output section
256
+ gr.Markdown("## πŸ’‘ Answer")
257
+ answer_output = gr.Textbox(
258
+ label="Generated Answer",
259
+ lines=10,
260
+ interactive=False,
261
+ placeholder="Your answer will appear here..."
262
+ )
263
+
264
+ with gr.Column(scale=1):
265
+ # Webpage preview section
266
+ gr.Markdown("## 🌐 Webpage Preview")
267
+ webpage_preview = gr.HTML(
268
+ label="Webpage Content",
269
+ value="<p style='text-align: center; color: #666; padding: 50px;'>Enter a URL to preview the webpage</p>"
270
+ )
271
+
272
+ # Event handlers
273
+ link_input.change(
274
+ fn=create_webpage_preview,
275
+ inputs=[link_input],
276
+ outputs=[webpage_preview]
277
+ )
278
+
279
+ submit_btn.click(
280
+ fn=process_rag_query,
281
+ inputs=[link_input, question_input, technique_dropdown],
282
+ outputs=[answer_output]
283
+ )
284
+
285
+ # Add some example links and questions
286
+ # gr.Markdown("""
287
+ # ## πŸ“š Example Usage & Technique Comparison
288
+
289
+ # **Sample URLs to try:**
290
+ # - `https://lilianweng.github.io/posts/2023-06-23-agent/` (AI Agents blog post)
291
+ # - `https://docs.python.org/3/tutorial/` (Python Tutorial)
292
+ # - `https://en.wikipedia.org/wiki/Machine_learning` (Machine Learning Wikipedia)
293
+
294
+ # **Sample Questions:**
295
+ # - "What is task decomposition for LLM agents?"
296
+ # - "What are the main components of an AI agent?"
297
+ # - "How does retrieval-augmented generation work?"
298
+
299
+ # **πŸ’‘ Pro Tip:** Try the same question with different techniques to see how results vary!
300
+ # """)
301
+
302
+ # # Add advanced technique descriptions
303
+ # with gr.Accordion("πŸ”§ Advanced RAG Techniques Explained", open=False):
304
+ # gr.Markdown("""
305
+ # ## Original Techniques:
306
+ # **HyDE:** Generates a hypothetical answer first, then uses it to retrieve relevant documents.
307
+
308
+ # **Query Decomposition:** Breaks down complex questions into simpler sub-questions that are answered sequentially.
309
+
310
+ # **Query Expansion:** Generates multiple variations of the original query to improve retrieval coverage.
311
+
312
+ # **RAG Fusion:** Creates multiple related queries and uses reciprocal rank fusion to combine results.
313
+
314
+ # **Step Back Query:** Transforms specific questions into more general ones to retrieve broader context.
315
+
316
+ # ## πŸš€ Advanced Techniques:
317
+ # **Multi-Query Retrieval:** Generates 4+ diverse query perspectives and merges results for comprehensive coverage.
318
+
319
+ # **Parent-Child Retrieval:** Uses small chunks for precise matching but returns larger parent chunks for better context.
320
+
321
+ # **Contextual Compression:** Uses LLM to compress retrieved chunks, keeping only information relevant to your question.
322
+
323
+ # **Cross-Encoder Reranking:** Uses specialized neural models to score and rerank documents for superior relevance.
324
+
325
+ # **Semantic Routing:** Automatically classifies your query type (factual, conceptual, comparative, analytical) and routes to the best retrieval strategy.
326
+ # """)
327
+
328
+ # # Installation requirements
329
+ # with gr.Accordion("πŸ“¦ Additional Dependencies for Advanced Techniques", open=False):
330
+ # gr.Markdown("""
331
+ # To use the advanced retrieval techniques, install these additional packages:
332
+
333
+ # ```bash
334
+ # pip install sentence-transformers scikit-learn
335
+ # ```
336
+
337
+ # If you encounter errors with advanced techniques, make sure these dependencies are installed.
338
+ # """)
339
+
340
+ return demo
341
+
342
+ # Launch the application
343
+ if __name__ == "__main__":
344
+ # Check if required environment variables are set
345
+ if not os.getenv("OPENAI_API_KEY"):
346
+ print("Warning: OPENAI_API_KEY not found in environment variables.")
347
+ print("Please make sure to set your OpenAI API key in your .env file.")
348
+
349
+ # Create and launch the interface
350
+ demo = create_interface()
351
+ demo.launch(
352
+ share=True, # Set to True if you want a public link
 
 
 
 
353
  )