Joachim M. Vis commited on
Commit
dd6b4e3
·
verified ·
1 Parent(s): 9db7b38

Upload 16 files

Browse files
agent/__pycache__/__init__.cpython-312.pyc CHANGED
Binary files a/agent/__pycache__/__init__.cpython-312.pyc and b/agent/__pycache__/__init__.cpython-312.pyc differ
 
agent/__pycache__/agent.cpython-312.pyc CHANGED
Binary files a/agent/__pycache__/agent.cpython-312.pyc and b/agent/__pycache__/agent.cpython-312.pyc differ
 
agent/__pycache__/assessment_analyzer.cpython-313.pyc ADDED
Binary file (11.8 kB). View file
 
agent/__pycache__/config.cpython-312.pyc CHANGED
Binary files a/agent/__pycache__/config.cpython-312.pyc and b/agent/__pycache__/config.cpython-312.pyc differ
 
agent/__pycache__/config.cpython-313.pyc CHANGED
Binary files a/agent/__pycache__/config.cpython-313.pyc and b/agent/__pycache__/config.cpython-313.pyc differ
 
agent/__pycache__/graph.cpython-312.pyc CHANGED
Binary files a/agent/__pycache__/graph.cpython-312.pyc and b/agent/__pycache__/graph.cpython-312.pyc differ
 
agent/__pycache__/graph.cpython-313.pyc CHANGED
Binary files a/agent/__pycache__/graph.cpython-313.pyc and b/agent/__pycache__/graph.cpython-313.pyc differ
 
agent/__pycache__/tool_registry.cpython-313.pyc CHANGED
Binary files a/agent/__pycache__/tool_registry.cpython-313.pyc and b/agent/__pycache__/tool_registry.cpython-313.pyc differ
 
agent/assessment_analyzer.py ADDED
@@ -0,0 +1,306 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Assessment analyzer for the GAIA agent.
3
+
4
+ This module provides functionality to analyze assessment results,
5
+ categorize questions, calculate success rates, identify error patterns,
6
+ and generate recommendations for further improvements.
7
+ """
8
+
9
+ import logging
10
+ from typing import Dict, Any, List, Optional, Set, Tuple
11
+ import re
12
+ from collections import Counter, defaultdict
13
+
14
+ logger = logging.getLogger("gaia_agent.assessment_analyzer")
15
+
16
+ class AnalysisResult:
17
+ """Class to store assessment analysis results."""
18
+
19
+ def __init__(
20
+ self,
21
+ category: str,
22
+ success_rate: float,
23
+ successful_questions: Dict[str, Dict[str, Any]],
24
+ failed_questions: Dict[str, Dict[str, Any]],
25
+ error_patterns: Dict[str, int],
26
+ recommendations: List[str]
27
+ ):
28
+ """
29
+ Initialize the analysis result.
30
+
31
+ Args:
32
+ category: The category of questions analyzed
33
+ success_rate: The success rate (0.0 to 1.0)
34
+ successful_questions: Dictionary of successfully answered questions
35
+ failed_questions: Dictionary of failed questions
36
+ error_patterns: Dictionary of error patterns and their frequencies
37
+ recommendations: List of recommendations for improvement
38
+ """
39
+ self.category = category
40
+ self.success_rate = success_rate
41
+ self.successful_questions = successful_questions
42
+ self.failed_questions = failed_questions
43
+ self.error_patterns = error_patterns
44
+ self.recommendations = recommendations
45
+
46
+ def __str__(self) -> str:
47
+ """Return a string representation of the analysis result."""
48
+ return (
49
+ f"Analysis Result for {self.category} questions:\n"
50
+ f"Success Rate: {self.success_rate:.2%}\n"
51
+ f"Successful Questions: {len(self.successful_questions)}\n"
52
+ f"Failed Questions: {len(self.failed_questions)}\n"
53
+ f"Top Error Patterns: {dict(sorted(self.error_patterns.items(), key=lambda x: x[1], reverse=True)[:3])}\n"
54
+ f"Recommendations: {self.recommendations}"
55
+ )
56
+
57
+ def categorize_question(question_id: str, question_data: Dict[str, Any]) -> str:
58
+ """
59
+ Categorize a question based on its content and ID.
60
+
61
+ Args:
62
+ question_id: The ID of the question
63
+ question_data: The question data
64
+
65
+ Returns:
66
+ The category of the question
67
+ """
68
+ question_text = question_data.get("question", "").lower()
69
+
70
+ # Web search questions
71
+ web_search_keywords = [
72
+ "mercedes sosa", "wikipedia", "featured article", "malko competition",
73
+ "yankees", "olympics", "universe today", "polish tv actor"
74
+ ]
75
+
76
+ # Multimodal questions
77
+ multimodal_keywords = [
78
+ "video", "youtube", "watch?v=", "audio", "listen", "chess position", "image"
79
+ ]
80
+
81
+ # File processing questions
82
+ file_processing_keywords = [
83
+ "excel", "spreadsheet", "python code", "execute", "run this code"
84
+ ]
85
+
86
+ # Academic paper questions
87
+ academic_paper_keywords = [
88
+ "arxiv", "paper", "scientific", "research", "publication", "journal"
89
+ ]
90
+
91
+ # Mathematical reasoning questions
92
+ math_keywords = [
93
+ "math", "calculate", "commutativity", "table", "operation", "subset"
94
+ ]
95
+
96
+ # Check for web search questions
97
+ for keyword in web_search_keywords:
98
+ if keyword in question_text:
99
+ return "web_search"
100
+
101
+ # Check for multimodal questions
102
+ for keyword in multimodal_keywords:
103
+ if keyword in question_text:
104
+ return "multimodal"
105
+
106
+ # Check for file processing questions
107
+ for keyword in file_processing_keywords:
108
+ if keyword in question_text:
109
+ return "file_processing"
110
+
111
+ # Check for academic paper questions
112
+ for keyword in academic_paper_keywords:
113
+ if keyword in question_text:
114
+ return "academic_paper"
115
+
116
+ # Check for mathematical reasoning questions
117
+ for keyword in math_keywords:
118
+ if keyword in question_text:
119
+ return "mathematical_reasoning"
120
+
121
+ # Default to "other" if no category matches
122
+ return "other"
123
+
124
+ def identify_error_patterns(failed_questions: Dict[str, Dict[str, Any]]) -> Dict[str, int]:
125
+ """
126
+ Identify common error patterns in failed questions.
127
+
128
+ Args:
129
+ failed_questions: Dictionary of failed questions
130
+
131
+ Returns:
132
+ Dictionary of error patterns and their frequencies
133
+ """
134
+ error_patterns = Counter()
135
+
136
+ for question_id, question_data in failed_questions.items():
137
+ error = question_data.get("error", "")
138
+ answer = question_data.get("answer", "")
139
+ expected_answer = question_data.get("expected_answer", "")
140
+
141
+ # Check for specific error types
142
+ if error:
143
+ # Check for timeout-related errors
144
+ if any(term in error.lower() for term in ["timeout", "timed out", "time limit"]):
145
+ error_patterns["timeout"] += 1
146
+ # Check for API-related errors
147
+ elif any(term in error.lower() for term in ["api", "rate limit", "quota"]):
148
+ error_patterns["api_error"] += 1
149
+ # Check for unhashable type errors
150
+ elif "unhashable" in error.lower():
151
+ error_patterns["unhashable_type"] += 1
152
+ else:
153
+ error_patterns["other_error"] += 1
154
+ # Check for incorrect answers
155
+ elif answer and expected_answer and answer != expected_answer:
156
+ # Check for partial matches
157
+ if any(word in answer.lower() for word in expected_answer.lower().split()):
158
+ error_patterns["partial_match"] += 1
159
+ else:
160
+ error_patterns["completely_wrong_answer"] += 1
161
+ else:
162
+ error_patterns["unknown_failure"] += 1
163
+
164
+ return error_patterns
165
+
166
+ def generate_recommendations(
167
+ category: str,
168
+ success_rate: float,
169
+ error_patterns: Dict[str, int],
170
+ failed_questions: Dict[str, Dict[str, Any]]
171
+ ) -> List[str]:
172
+ """
173
+ Generate recommendations for improvement based on analysis.
174
+
175
+ Args:
176
+ category: The category of questions
177
+ success_rate: The success rate
178
+ error_patterns: Dictionary of error patterns and their frequencies
179
+ failed_questions: Dictionary of failed questions
180
+
181
+ Returns:
182
+ List of recommendations
183
+ """
184
+ recommendations = []
185
+
186
+ # General recommendations based on success rate
187
+ if success_rate < 0.3:
188
+ recommendations.append(f"Major improvements needed for {category} questions")
189
+ elif success_rate < 0.7:
190
+ recommendations.append(f"Moderate improvements needed for {category} questions")
191
+ elif success_rate < 1.0:
192
+ recommendations.append(f"Minor improvements needed for {category} questions")
193
+ else:
194
+ recommendations.append(f"All {category} questions are answered correctly")
195
+
196
+ # Specific recommendations based on error patterns
197
+ if error_patterns.get("timeout", 0) > 0:
198
+ recommendations.append("Optimize search performance to prevent timeouts")
199
+
200
+ if error_patterns.get("api_error", 0) > 0:
201
+ recommendations.append("Improve error handling for API failures")
202
+
203
+ if error_patterns.get("unhashable_type", 0) > 0:
204
+ recommendations.append("Fix unhashable type errors in the agent implementation")
205
+
206
+ if error_patterns.get("partial_match", 0) > 0:
207
+ recommendations.append("Improve answer extraction precision")
208
+
209
+ if error_patterns.get("completely_wrong_answer", 0) > 0:
210
+ recommendations.append("Enhance search result relevance filtering")
211
+
212
+ # Category-specific recommendations
213
+ if category == "web_search":
214
+ if success_rate < 1.0:
215
+ recommendations.append("Implement specialized search for specific domains (Wikipedia, news, etc.)")
216
+ recommendations.append("Improve result ranking based on query relevance")
217
+ recommendations.append("Add better error handling for failed searches")
218
+
219
+ elif category == "multimodal":
220
+ if success_rate < 1.0:
221
+ recommendations.append("Enhance video content extraction capabilities")
222
+ recommendations.append("Improve audio transcription accuracy")
223
+ recommendations.append("Add specialized image analysis for specific domains")
224
+
225
+ elif category == "file_processing":
226
+ if success_rate < 1.0:
227
+ recommendations.append("Improve Excel file parsing and data extraction")
228
+ recommendations.append("Enhance code execution sandbox for better isolation")
229
+
230
+ elif category == "academic_paper":
231
+ if success_rate < 1.0:
232
+ recommendations.append("Improve arXiv paper content extraction")
233
+ recommendations.append("Add support for more academic paper sources")
234
+
235
+ elif category == "mathematical_reasoning":
236
+ if success_rate < 1.0:
237
+ recommendations.append("Enhance mathematical operation analysis")
238
+ recommendations.append("Improve structured data parsing for tables and matrices")
239
+
240
+ return recommendations
241
+
242
+ def analyze_assessment_results(
243
+ assessment_results: Dict[str, Dict[str, Any]],
244
+ category: Optional[str] = None
245
+ ) -> AnalysisResult:
246
+ """
247
+ Analyze assessment results and generate insights.
248
+
249
+ Args:
250
+ assessment_results: Dictionary of assessment results
251
+ category: Optional category to filter questions by
252
+
253
+ Returns:
254
+ AnalysisResult object with analysis results
255
+ """
256
+ # Categorize questions
257
+ categorized_questions = defaultdict(dict)
258
+
259
+ for question_id, question_data in assessment_results.items():
260
+ question_category = categorize_question(question_id, question_data)
261
+ categorized_questions[question_category][question_id] = question_data
262
+
263
+ # If a specific category is requested, filter questions
264
+ if category:
265
+ questions_to_analyze = categorized_questions.get(category, {})
266
+ category_name = category
267
+ else:
268
+ questions_to_analyze = assessment_results
269
+ category_name = "all"
270
+
271
+ # Separate successful and failed questions
272
+ successful_questions = {}
273
+ failed_questions = {}
274
+
275
+ for question_id, question_data in questions_to_analyze.items():
276
+ if question_data.get("correct", False):
277
+ successful_questions[question_id] = question_data
278
+ else:
279
+ failed_questions[question_id] = question_data
280
+
281
+ # Calculate success rate
282
+ total_questions = len(questions_to_analyze)
283
+ successful_count = len(successful_questions)
284
+
285
+ success_rate = successful_count / total_questions if total_questions > 0 else 0.0
286
+
287
+ # Identify error patterns
288
+ error_patterns = identify_error_patterns(failed_questions)
289
+
290
+ # Generate recommendations
291
+ recommendations = generate_recommendations(
292
+ category_name,
293
+ success_rate,
294
+ error_patterns,
295
+ failed_questions
296
+ )
297
+
298
+ # Create and return analysis result
299
+ return AnalysisResult(
300
+ category=category_name,
301
+ success_rate=success_rate,
302
+ successful_questions=successful_questions,
303
+ failed_questions=failed_questions,
304
+ error_patterns=error_patterns,
305
+ recommendations=recommendations
306
+ )
agent/graph.py CHANGED
@@ -410,7 +410,7 @@ def execute_tool(state: AgentState) -> AgentState:
410
 
411
  try:
412
  # Import the tool registry here to avoid circular imports
413
- from agent.tool_registry import create_default_registry, robust_search, merged_search
414
 
415
  # Create the tool registry
416
  registry = create_default_registry()
@@ -422,17 +422,25 @@ def execute_tool(state: AgentState) -> AgentState:
422
  raise ValueError("Query is required for web search")
423
 
424
  try:
425
- # Try robust search first (falls back to other providers if one fails)
426
- result = robust_search(registry, query)
427
- logger.info(f"Web search successful using {result.get('provider', 'unknown')}")
 
 
 
 
 
 
 
 
428
  except Exception as search_error:
429
- logger.warning(f"Robust search failed: {str(search_error)}")
430
- # If robust search fails, try merged search as a fallback
431
  try:
432
- result = merged_search(registry, query)
433
- logger.info("Web search successful using merged results")
434
- except Exception as merged_error:
435
- logger.error(f"Merged search failed: {str(merged_error)}")
436
  raise ValueError(f"All search methods failed: {str(search_error)}")
437
 
438
  elif tool_name in ["duckduckgo_search", "serper_search", "perplexity_search", "arxiv_search"]:
 
410
 
411
  try:
412
  # Import the tool registry here to avoid circular imports
413
+ from agent.tool_registry import create_default_registry, search, robust_search, merged_search
414
 
415
  # Create the tool registry
416
  registry = create_default_registry()
 
422
  raise ValueError("Query is required for web search")
423
 
424
  try:
425
+ # Get working memory if available
426
+ working_memory = None
427
+ if hasattr(state, "get") and callable(state.get):
428
+ from memory import WorkingMemory
429
+ working_memory_instance = state.get("working_memory")
430
+ if isinstance(working_memory_instance, WorkingMemory):
431
+ working_memory = working_memory_instance
432
+
433
+ # Use the unified search wrapper with working memory
434
+ result = search(registry, query, format_type="unified", working_memory=working_memory)
435
+ logger.info(f"Web search successful using unified search")
436
  except Exception as search_error:
437
+ logger.error(f"Unified search failed: {str(search_error)}")
438
+ # If unified search fails, try robust search as a fallback
439
  try:
440
+ result = robust_search(registry, query)
441
+ logger.info(f"Web search successful using fallback robust search")
442
+ except Exception as robust_error:
443
+ logger.error(f"Robust search failed: {str(robust_error)}")
444
  raise ValueError(f"All search methods failed: {str(search_error)}")
445
 
446
  elif tool_name in ["duckduckgo_search", "serper_search", "perplexity_search", "arxiv_search"]:
agent/tool_registry.py CHANGED
@@ -11,19 +11,25 @@ The registry is designed to be used with the LangGraph workflow in agent/graph.p
11
  import logging
12
  import os
13
  import re
 
14
  from typing import Dict, Any, Optional, List, Callable
15
 
16
  from tools.web_tools import (
17
  DuckDuckGoSearchTool,
18
  SerperSearchTool,
19
- WikipediaSearchTool,
 
 
20
  create_duckduckgo_search,
21
  create_serper_search,
22
- create_wikipedia_search
 
 
 
23
  )
24
  from tools.perplexity_tool import PerplexityTool, create_perplexity_tool
25
  from tools.arxiv_tool import ArxivSearchTool, create_arxiv_search
26
- from tools.multimodal_tools import YouTubeVideoTool, create_youtube_video_tool
27
 
28
  logger = logging.getLogger("gaia_agent.tool_registry")
29
 
@@ -88,12 +94,20 @@ class ToolRegistry:
88
 
89
 
90
  try:
91
- if name in ["duckduckgo_search", "serper_search", "wikipedia_search"]:
 
92
  query = kwargs.get("query")
93
  if not query:
94
  raise ValueError("Query is required for search tools")
95
  return tool.search(query)
96
 
 
 
 
 
 
 
 
97
  elif name == "perplexity_search":
98
  query = kwargs.get("query")
99
  if not query:
@@ -153,6 +167,14 @@ def create_default_registry() -> ToolRegistry:
153
  """
154
  registry = ToolRegistry()
155
 
 
 
 
 
 
 
 
 
156
  # Register DuckDuckGo search tool (no API key required)
157
  try:
158
  duckduckgo_tool = create_duckduckgo_search()
@@ -204,282 +226,488 @@ def create_default_registry() -> ToolRegistry:
204
  except Exception as e:
205
  logger.warning(f"Failed to create YouTube video tool: {str(e)}")
206
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
207
  logger.info(f"Created default tool registry with {len(registry.list_tools())} tools")
208
  return registry
209
 
210
- def robust_search(registry: ToolRegistry, query: str) -> Dict[str, Any]:
211
  """
212
- Perform a robust search using multiple providers.
213
 
214
- This function tries multiple search providers in order, falling back to
215
- the next provider if one fails. It also filters and ranks results by relevance.
 
 
216
 
217
  Args:
218
- registry: The tool registry
219
  query: The search query
220
 
221
  Returns:
222
- Dict with provider name and search results
223
-
224
- Raises:
225
- Exception: If all search providers fail
226
  """
227
- all_results = []
228
- providers_tried = []
229
-
230
- # Check if the query is specifically about Wikipedia
231
- is_wikipedia_query = "wikipedia" in query.lower() or "featured article" in query.lower()
232
-
233
- # Try Wikipedia search first for Wikipedia-specific queries
234
- if is_wikipedia_query and registry.get_tool("wikipedia_search"):
235
- providers_tried.append("wikipedia")
236
- try:
237
- results = registry.execute_tool("wikipedia_search", query=query)
238
- if results:
239
- all_results.extend(results)
240
- if len(results) >= 3: # If we have enough results, return them
241
- return {"provider": "wikipedia", "results": results}
242
- except Exception as e:
243
- logger.warning(f"Wikipedia search failed: {str(e)}")
244
-
245
- # Try DuckDuckGo next (no API key required)
246
- if registry.get_tool("duckduckgo_search"):
247
- providers_tried.append("duckduckgo")
248
- try:
249
- results = registry.execute_tool("duckduckgo_search", query=query)
250
- if results:
251
- all_results.extend(results)
252
- if len(results) >= 3: # If we have enough results, return them
253
- return {"provider": "duckduckgo", "results": results}
254
- except Exception as e:
255
- logger.warning(f"DuckDuckGo search failed: {str(e)}")
256
-
257
- # Try Serper as fallback
258
- if registry.get_tool("serper_search"):
259
- providers_tried.append("serper")
260
- try:
261
- results = registry.execute_tool("serper_search", query=query)
262
- if results:
263
- all_results.extend(results)
264
- if len(results) >= 3: # If we have enough results, return them
265
- return {"provider": "serper", "results": results}
266
- except Exception as e:
267
- logger.warning(f"Serper search failed: {str(e)}")
268
-
269
- # Try Perplexity as another option
270
- if registry.get_tool("perplexity_search"):
271
- providers_tried.append("perplexity")
272
- try:
273
- results = registry.execute_tool("perplexity_search", query=query)
274
- if results:
275
- # Format perplexity results to match other providers
276
- formatted_results = []
277
- if isinstance(results, dict) and "content" in results:
278
- # Create a single result from perplexity content
279
- formatted_results = [{
280
- "title": "Perplexity Search Result",
281
- "link": "https://perplexity.ai/",
282
- "snippet": results["content"][:500] + "..." if len(results["content"]) > 500 else results["content"],
283
- "relevance_score": 5 # Give perplexity a high base score
284
- }]
285
- all_results.extend(formatted_results)
286
- if formatted_results:
287
- return {"provider": "perplexity", "results": formatted_results}
288
- except Exception as e:
289
- logger.warning(f"Perplexity search failed: {str(e)}")
290
 
291
- # Try arXiv as last resort
292
- if registry.get_tool("arxiv_search"):
293
- providers_tried.append("arxiv")
294
- try:
295
- results = registry.execute_tool("arxiv_search", query=query)
296
- if results:
297
- # Format arXiv results to match other providers
298
- formatted_results = []
299
- for result in results:
300
- formatted_result = {
301
- "title": result.get("title", ""),
302
- "link": result.get("url", ""),
303
- "snippet": result.get("summary", "")[:200] + "..." if result.get("summary") else "",
304
- "relevance_score": 3 # Give arXiv a medium base score
305
- }
306
- formatted_results.append(formatted_result)
307
- all_results.extend(formatted_results)
308
- if formatted_results:
309
- return {"provider": "arxiv", "results": formatted_results}
310
- except Exception as e:
311
- logger.warning(f"arXiv search failed: {str(e)}")
312
-
313
- # If we have any results from any provider, return them
314
- if all_results:
315
- # Sort all results by relevance score
316
- all_results.sort(key=lambda x: x.get("relevance_score", 0), reverse=True)
317
- return {"provider": ",".join(providers_tried), "results": all_results}
318
-
319
- # If all providers failed and we have no results, return an empty result
320
- # instead of raising an exception
321
- logger.error("All search providers failed, returning empty results")
322
- return {"provider": "none", "results": [], "error": "All search providers failed"}
 
 
 
 
 
 
323
 
324
- def merged_search(registry: ToolRegistry, query: str) -> Dict[str, Any]:
325
  """
326
- Perform a merged search using multiple providers.
327
 
328
- This function collects results from all available search providers,
329
- filters them for relevance, ranks them, and merges them into a single
330
- result set, removing duplicates.
 
 
 
331
 
332
  Args:
333
  registry: The tool registry
334
  query: The search query
 
335
 
336
  Returns:
337
- Dict with merged results and Perplexity context
338
  """
339
  from tools.web_tools import calculate_query_relevance
 
340
 
341
- results = {
342
- "wikipedia": [],
343
- "duckduckgo": [],
344
- "serper": [],
345
- "perplexity": None,
346
- "arxiv": []
347
- }
348
-
349
- # Extract keywords from the query for relevance calculation
350
- query_keywords = set(re.findall(r'\b\w+\b', query.lower()))
351
- common_words = {'the', 'a', 'an', 'and', 'or', 'but', 'in', 'on', 'at', 'to', 'for', 'with', 'by', 'about'}
352
- query_keywords = query_keywords - common_words
353
-
354
- # Collect results from all providers
355
- # Check if query is Wikipedia-specific
356
- is_wikipedia_query = "wikipedia" in query.lower() or "featured article" in query.lower()
357
 
358
- # Try Wikipedia search for Wikipedia-specific queries
359
- if is_wikipedia_query and registry.get_tool("wikipedia_search"):
360
- try:
361
- results["wikipedia"] = registry.execute_tool("wikipedia_search", query=query)
362
- except Exception as e:
363
- logger.warning(f"Wikipedia search failed: {str(e)}")
364
-
365
- if registry.get_tool("duckduckgo_search"):
366
- try:
367
- results["duckduckgo"] = registry.execute_tool("duckduckgo_search", query=query)
368
- except Exception as e:
369
- logger.warning(f"DuckDuckGo search failed: {str(e)}")
370
-
371
- if registry.get_tool("serper_search"):
372
- try:
373
- results["serper"] = registry.execute_tool("serper_search", query=query)
374
- except Exception as e:
375
- logger.warning(f"Serper search failed: {str(e)}")
376
 
377
- if registry.get_tool("perplexity_search"):
378
- try:
379
- results["perplexity"] = registry.execute_tool("perplexity_search", query=query)
380
- except Exception as e:
381
- logger.warning(f"Perplexity search failed: {str(e)}")
 
 
382
 
383
- if registry.get_tool("arxiv_search"):
 
 
 
 
384
  try:
385
- results["arxiv"] = registry.execute_tool("arxiv_search", query=query, max_results=5)
386
- except Exception as e:
387
- logger.warning(f"arXiv search failed: {str(e)}")
388
-
389
- merged = []
390
- seen_urls = set()
391
-
392
- # Process and add Wikipedia results first (if query is Wikipedia-specific)
393
- for result in results["wikipedia"]:
394
- if result["link"] not in seen_urls:
395
- # If result doesn't already have a relevance score, calculate it
396
- if "relevance_score" not in result:
397
- title_relevance = calculate_query_relevance(result.get("title", ""), query)
398
- snippet_relevance = calculate_query_relevance(result.get("snippet", ""), query)
399
- # Give Wikipedia results a boost for Wikipedia-specific queries
400
- boost = 1.5 if is_wikipedia_query else 1.0
401
- result["relevance_score"] = ((title_relevance * 2 + snippet_relevance) / 3) * boost
402
 
403
- merged.append(result)
404
- seen_urls.add(result["link"])
405
-
406
- # Process and add DuckDuckGo results
407
- for result in results["duckduckgo"]:
408
- if result["link"] not in seen_urls:
409
- # If result doesn't already have a relevance score, calculate it
410
- if "relevance_score" not in result:
411
- title_relevance = calculate_query_relevance(result.get("title", ""), query)
412
- snippet_relevance = calculate_query_relevance(result.get("snippet", ""), query)
413
- # Title relevance is weighted more heavily
414
- result["relevance_score"] = (title_relevance * 2 + snippet_relevance) / 3
415
 
416
- merged.append(result)
417
- seen_urls.add(result["link"])
418
-
419
- # Process and add Serper results
420
- for result in results["serper"]:
421
- if result["link"] not in seen_urls:
422
- # If result doesn't already have a relevance score, calculate it
423
- if "relevance_score" not in result:
424
- title_relevance = calculate_query_relevance(result.get("title", ""), query)
425
- snippet_relevance = calculate_query_relevance(result.get("snippet", ""), query)
426
- # Title relevance is weighted more heavily
427
- result["relevance_score"] = (title_relevance * 2 + snippet_relevance) / 3
428
-
429
- merged.append(result)
430
- seen_urls.add(result["link"])
431
-
432
- # Process and add arXiv results
433
- arxiv_results = []
434
- for result in results["arxiv"]:
435
- if "url" in result and result["url"] not in seen_urls:
436
- title = result.get("title", "")
437
- summary = result.get("summary", "")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
438
 
439
- # Calculate relevance
440
- title_relevance = calculate_query_relevance(title, query)
441
- summary_relevance = calculate_query_relevance(summary, query)
442
- relevance_score = (title_relevance * 2 + summary_relevance) / 3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
443
 
444
- formatted_result = {
445
- "title": title,
446
- "link": result.get("url", ""),
447
- "snippet": summary[:200] + "..." if summary else "",
448
- "relevance_score": relevance_score
449
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
450
 
451
- merged.append(formatted_result)
452
- seen_urls.add(result["url"])
453
- arxiv_results.append(result) # Keep original format for context
454
-
455
- # Add Perplexity content as context and as a result if relevant
456
- perplexity_content = None
457
- if results["perplexity"] and isinstance(results["perplexity"], dict) and "content" in results["perplexity"]:
458
- perplexity_content = results["perplexity"]["content"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
459
 
460
- # Add perplexity as a result if it's not empty
461
- if perplexity_content and perplexity_content.strip():
462
- relevance_score = calculate_query_relevance(perplexity_content, query)
463
 
464
- # Only add if it's somewhat relevant
465
- if relevance_score > 0.2:
466
- perplexity_result = {
 
 
 
 
 
 
467
  "title": "Perplexity AI Search Result",
468
  "link": "https://perplexity.ai/",
469
  "snippet": perplexity_content[:200] + "..." if len(perplexity_content) > 200 else perplexity_content,
470
- "relevance_score": relevance_score * 1.5 # Boost perplexity slightly
 
471
  }
472
- merged.append(perplexity_result)
473
-
474
- # Sort merged results by relevance score
475
- merged.sort(key=lambda x: x.get("relevance_score", 0), reverse=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
476
 
477
  return {
478
- "merged_results": merged,
479
- "perplexity_context": perplexity_content,
480
- "arxiv_context": arxiv_results
481
  }
482
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
483
  if __name__ == "__main__":
484
  logging.basicConfig(level=logging.INFO)
485
 
 
11
  import logging
12
  import os
13
  import re
14
+ import time
15
  from typing import Dict, Any, Optional, List, Callable
16
 
17
  from tools.web_tools import (
18
  DuckDuckGoSearchTool,
19
  SerperSearchTool,
20
+ EnhancedWebSearchTool,
21
+ LibrarySearchTool,
22
+ ApiSearchTool,
23
  create_duckduckgo_search,
24
  create_serper_search,
25
+ create_wikipedia_search,
26
+ create_enhanced_web_search,
27
+ create_library_search,
28
+ create_api_search
29
  )
30
  from tools.perplexity_tool import PerplexityTool, create_perplexity_tool
31
  from tools.arxiv_tool import ArxivSearchTool, create_arxiv_search
32
+ from tools.multimodal_tools import YouTubeVideoTool, create_youtube_video_tool, BrowserSearchTool, create_browser_search_tool
33
 
34
  logger = logging.getLogger("gaia_agent.tool_registry")
35
 
 
94
 
95
 
96
  try:
97
+ if name in ["duckduckgo_search", "serper_search", "wikipedia_search", "enhanced_web_search",
98
+ "library_search", "api_search"]:
99
  query = kwargs.get("query")
100
  if not query:
101
  raise ValueError("Query is required for search tools")
102
  return tool.search(query)
103
 
104
+ elif name == "browser_search":
105
+ query = kwargs.get("query")
106
+ source = kwargs.get("source")
107
+ if not query:
108
+ raise ValueError("Query is required for browser search")
109
+ return tool.search(query, source)
110
+
111
  elif name == "perplexity_search":
112
  query = kwargs.get("query")
113
  if not query:
 
167
  """
168
  registry = ToolRegistry()
169
 
170
+ # Register Enhanced Web Search tool (handles GAIA assessment questions)
171
+ try:
172
+ enhanced_web_tool = create_enhanced_web_search()
173
+ registry.register_tool("enhanced_web_search", enhanced_web_tool)
174
+ logger.info("Registered Enhanced Web Search tool")
175
+ except Exception as e:
176
+ logger.warning(f"Failed to create Enhanced Web Search tool: {str(e)}")
177
+
178
  # Register DuckDuckGo search tool (no API key required)
179
  try:
180
  duckduckgo_tool = create_duckduckgo_search()
 
226
  except Exception as e:
227
  logger.warning(f"Failed to create YouTube video tool: {str(e)}")
228
 
229
+ # Register Browser Search tool
230
+ try:
231
+ browser_search_tool = create_browser_search_tool()
232
+ registry.register_tool("browser_search", browser_search_tool)
233
+ logger.info("Registered Browser Search tool")
234
+ except Exception as e:
235
+ logger.warning(f"Failed to create Browser Search tool: {str(e)}")
236
+
237
+ # Register Library Search tool
238
+ try:
239
+ library_search_tool = create_library_search()
240
+ registry.register_tool("library_search", library_search_tool)
241
+ logger.info("Registered Library Search tool")
242
+ except Exception as e:
243
+ logger.warning(f"Failed to create Library Search tool: {str(e)}")
244
+
245
+ # Register API Search tool if API keys are available
246
+ if os.environ.get("PERPLEXITY_API_KEY") or os.environ.get("SERPER_API_KEY"):
247
+ try:
248
+ api_search_tool = create_api_search()
249
+ registry.register_tool("api_search", api_search_tool)
250
+ logger.info("Registered API Search tool")
251
+ except Exception as e:
252
+ logger.warning(f"Failed to create API Search tool: {str(e)}")
253
+ else:
254
+ logger.warning("Neither Perplexity nor Serper API keys available, skipping API Search tool")
255
+
256
  logger.info(f"Created default tool registry with {len(registry.list_tools())} tools")
257
  return registry
258
 
259
+ def analyze_query(query: str) -> Dict[str, Any]:
260
  """
261
+ Analyze a query to determine the best search strategy.
262
 
263
+ This function examines the query to identify:
264
+ - Source-specific keywords (Wikipedia, YouTube, arXiv)
265
+ - Question type (factual, research, multimedia)
266
+ - Information depth needed
267
 
268
  Args:
 
269
  query: The search query
270
 
271
  Returns:
272
+ Dict with analysis results
 
 
 
273
  """
274
+ analysis = {
275
+ "source_specific": False,
276
+ "preferred_sources": [],
277
+ "question_type": "factual", # Default to factual
278
+ "depth_needed": "medium", # Default to medium depth
279
+ "is_multimedia": False
280
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
281
 
282
+ # Check for source-specific keywords
283
+ query_lower = query.lower()
284
+
285
+ # Wikipedia specific
286
+ if "wikipedia" in query_lower or "featured article" in query_lower:
287
+ analysis["source_specific"] = True
288
+ analysis["preferred_sources"].append("wikipedia")
289
+
290
+ # YouTube specific
291
+ if "youtube" in query_lower or "video" in query_lower:
292
+ analysis["source_specific"] = True
293
+ analysis["preferred_sources"].append("youtube")
294
+ analysis["is_multimedia"] = True
295
+
296
+ # arXiv specific
297
+ if "arxiv" in query_lower or "paper" in query_lower or "research paper" in query_lower:
298
+ analysis["source_specific"] = True
299
+ analysis["preferred_sources"].append("arxiv")
300
+ analysis["question_type"] = "research"
301
+ analysis["depth_needed"] = "high"
302
+
303
+ # Determine question type if not already set
304
+ if "how" in query_lower or "why" in query_lower or "explain" in query_lower:
305
+ analysis["question_type"] = "explanatory"
306
+ analysis["depth_needed"] = "high"
307
+ elif "when" in query_lower or "where" in query_lower or "who" in query_lower:
308
+ analysis["question_type"] = "factual"
309
+ elif "compare" in query_lower or "difference" in query_lower:
310
+ analysis["question_type"] = "comparative"
311
+ analysis["depth_needed"] = "high"
312
+
313
+ # Check for indicators of needed depth
314
+ if "detailed" in query_lower or "comprehensive" in query_lower or "in depth" in query_lower:
315
+ analysis["depth_needed"] = "high"
316
+ elif "brief" in query_lower or "summary" in query_lower or "overview" in query_lower:
317
+ analysis["depth_needed"] = "low"
318
+
319
+ return analysis
320
 
321
+ def unified_search(registry: ToolRegistry, query: str, working_memory=None) -> Dict[str, Any]:
322
  """
323
+ Perform a unified search using an intelligent routing approach.
324
 
325
+ This function:
326
+ 1. Analyzes the query to determine the best search strategy
327
+ 2. Routes to appropriate tools based on the analysis
328
+ 3. Executes tools in parallel when appropriate
329
+ 4. Stores intermediate results in working_memory
330
+ 5. Combines and ranks results
331
 
332
  Args:
333
  registry: The tool registry
334
  query: The search query
335
+ working_memory: Optional working memory instance for storing results
336
 
337
  Returns:
338
+ Dict with search results and metadata
339
  """
340
  from tools.web_tools import calculate_query_relevance
341
+ import concurrent.futures
342
 
343
+ # Analyze the query
344
+ analysis = analyze_query(query)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
345
 
346
+ # Store the analysis in working memory if available
347
+ if working_memory:
348
+ working_memory.store_intermediate_result("query_analysis", analysis)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
349
 
350
+ # Initialize results container
351
+ all_results = []
352
+ metadata = {
353
+ "providers_used": [],
354
+ "analysis": analysis,
355
+ "execution_times": {}
356
+ }
357
 
358
+ # Check if we should use the enhanced web search tool
359
+ # This tool is especially useful for GAIA assessment questions
360
+ if registry.get_tool("enhanced_web_search"):
361
+ # First try the enhanced web search tool directly (not in parallel)
362
+ # This is more efficient for GAIA assessment questions
363
  try:
364
+ logger.info(f"Using enhanced web search for query: {query}")
365
+ start_time = time.time()
366
+ enhanced_results = registry.execute_tool("enhanced_web_search", query=query)
367
+ end_time = time.time()
 
 
 
 
 
 
 
 
 
 
 
 
 
368
 
369
+ metadata["execution_times"]["enhanced_web_search"] = end_time - start_time
370
+ metadata["providers_used"].append("enhanced_web_search")
 
 
 
 
 
 
 
 
 
 
371
 
372
+ # If we got good results from the enhanced tool, return them directly
373
+ if enhanced_results and len(enhanced_results) > 0:
374
+ # Check if we have a high-quality result (like from Perplexity)
375
+ has_high_quality = False
376
+ for result in enhanced_results:
377
+ if result.get("source") == "perplexity" or result.get("relevance_score", 0) > 8.0:
378
+ has_high_quality = True
379
+ break
380
+
381
+ if has_high_quality:
382
+ logger.info("Enhanced web search returned high-quality results, skipping other tools")
383
+
384
+ # Store results in working memory
385
+ if working_memory:
386
+ working_memory.store_intermediate_result("enhanced_search_results", enhanced_results)
387
+
388
+ return {
389
+ "results": enhanced_results,
390
+ "metadata": metadata
391
+ }
392
+ except Exception as e:
393
+ logger.warning(f"Enhanced web search failed: {str(e)}")
394
+
395
+ # Determine which tools to use based on analysis
396
+ tools_to_use = []
397
+
398
+ # If source-specific, prioritize those sources
399
+ if analysis["source_specific"]:
400
+ for source in analysis["preferred_sources"]:
401
+ if source == "wikipedia" and registry.get_tool("wikipedia_search"):
402
+ tools_to_use.append("wikipedia_search")
403
+ elif source == "youtube" and registry.get_tool("youtube_video"):
404
+ tools_to_use.append("youtube_video")
405
+ elif source == "arxiv" and registry.get_tool("arxiv_search"):
406
+ tools_to_use.append("arxiv_search")
407
+
408
+ # For high depth questions, always include Perplexity if available
409
+ if analysis["depth_needed"] == "high" and registry.get_tool("perplexity_search"):
410
+ if "perplexity_search" not in tools_to_use:
411
+ tools_to_use.append("perplexity_search")
412
+
413
+ # Check for API search tool (combines Perplexity and Serper)
414
+ if registry.get_tool("api_search") and "api_search" not in tools_to_use:
415
+ tools_to_use.append("api_search")
416
+
417
+ # Check for library search tool (combines DuckDuckGo and arXiv)
418
+ if registry.get_tool("library_search") and "library_search" not in tools_to_use:
419
+ tools_to_use.append("library_search")
420
+
421
+ # Always include general search tools as fallbacks
422
+ if registry.get_tool("duckduckgo_search") and "duckduckgo_search" not in tools_to_use:
423
+ tools_to_use.append("duckduckgo_search")
424
+
425
+ if registry.get_tool("serper_search") and "serper_search" not in tools_to_use:
426
+ tools_to_use.append("serper_search")
427
+
428
+ # For visual or interactive queries, include browser search tool
429
+ if analysis["is_multimedia"] and registry.get_tool("browser_search") and "browser_search" not in tools_to_use:
430
+ tools_to_use.append("browser_search")
431
+
432
+ # Execute tools in parallel
433
+ results_dict = {}
434
+ with concurrent.futures.ThreadPoolExecutor() as executor:
435
+ future_to_tool = {}
436
+
437
+ for tool_name in tools_to_use:
438
+ if tool_name == "youtube_video":
439
+ # YouTube tool requires different handling
440
+ continue
441
 
442
+ future = executor.submit(registry.execute_tool, tool_name, query=query)
443
+ future_to_tool[future] = tool_name
444
+
445
+ for future in concurrent.futures.as_completed(future_to_tool):
446
+ tool_name = future_to_tool[future]
447
+ try:
448
+ start_time = time.time()
449
+ result = future.result()
450
+ end_time = time.time()
451
+
452
+ metadata["execution_times"][tool_name] = end_time - start_time
453
+ metadata["providers_used"].append(tool_name)
454
+
455
+ results_dict[tool_name] = result
456
+
457
+ # Store intermediate results in working memory
458
+ if working_memory:
459
+ working_memory.store_intermediate_result(f"search_result_{tool_name}", result)
460
+
461
+ except Exception as e:
462
+ logger.warning(f"{tool_name} search failed: {str(e)}")
463
+ metadata["execution_times"][tool_name] = -1 # Indicate failure
464
+
465
+ # Process and merge results
466
+ seen_urls = set()
467
+
468
+ # Process source-specific results first
469
+ for source in analysis["preferred_sources"]:
470
+ tool_name = None
471
+ if source == "wikipedia":
472
+ tool_name = "wikipedia_search"
473
+ elif source == "arxiv":
474
+ tool_name = "arxiv_search"
475
+
476
+ if tool_name and tool_name in results_dict:
477
+ results = results_dict[tool_name]
478
 
479
+ # Format results if needed
480
+ formatted_results = []
481
+ if tool_name == "arxiv_search":
482
+ for result in results:
483
+ if "url" in result and result["url"] not in seen_urls:
484
+ title = result.get("title", "")
485
+ summary = result.get("summary", "")
486
+
487
+ # Calculate relevance
488
+ title_relevance = calculate_query_relevance(title, query)
489
+ summary_relevance = calculate_query_relevance(summary, query)
490
+ relevance_score = (title_relevance * 2 + summary_relevance) / 3
491
+
492
+ formatted_result = {
493
+ "title": title,
494
+ "link": result.get("url", ""),
495
+ "snippet": summary[:200] + "..." if summary else "",
496
+ "relevance_score": relevance_score * 1.2, # Boost source-specific results
497
+ "source": "arxiv"
498
+ }
499
+
500
+ formatted_results.append(formatted_result)
501
+ seen_urls.add(result["url"])
502
+ else:
503
+ # For other tools, just add source and boost relevance
504
+ for result in results:
505
+ if result["link"] not in seen_urls:
506
+ if "relevance_score" not in result:
507
+ title_relevance = calculate_query_relevance(result.get("title", ""), query)
508
+ snippet_relevance = calculate_query_relevance(result.get("snippet", ""), query)
509
+ result["relevance_score"] = (title_relevance * 2 + snippet_relevance) / 3
510
+
511
+ # Boost source-specific results
512
+ result["relevance_score"] = result["relevance_score"] * 1.2
513
+ result["source"] = source
514
+
515
+ formatted_results.append(result)
516
+ seen_urls.add(result["link"])
517
 
518
+ all_results.extend(formatted_results)
519
+
520
+ # Process general search results
521
+ for tool_name in ["duckduckgo_search", "serper_search", "library_search", "api_search"]:
522
+ if tool_name in results_dict:
523
+ for result in results_dict[tool_name]:
524
+ if "link" in result and result["link"] not in seen_urls:
525
+ if "relevance_score" not in result:
526
+ title_relevance = calculate_query_relevance(result.get("title", ""), query)
527
+ snippet_relevance = calculate_query_relevance(result.get("snippet", ""), query)
528
+ result["relevance_score"] = (title_relevance * 2 + snippet_relevance) / 3
529
+
530
+ # If source is not already set, derive it from the tool name
531
+ if "source" not in result:
532
+ result["source"] = tool_name.replace("_search", "")
533
+
534
+ all_results.append(result)
535
+ seen_urls.add(result["link"])
536
+
537
+ # Process Perplexity results
538
+ if "perplexity_search" in results_dict:
539
+ perplexity_result = results_dict["perplexity_search"]
540
+ perplexity_content = None
541
 
542
+ if isinstance(perplexity_result, dict) and "content" in perplexity_result:
543
+ perplexity_content = perplexity_result["content"]
 
544
 
545
+ # Add perplexity as a result if it's not empty
546
+ if perplexity_content and perplexity_content.strip():
547
+ relevance_score = calculate_query_relevance(perplexity_content, query)
548
+
549
+ # For high depth questions, boost Perplexity even more
550
+ if analysis["depth_needed"] == "high":
551
+ relevance_score = relevance_score * 1.5
552
+
553
+ formatted_result = {
554
  "title": "Perplexity AI Search Result",
555
  "link": "https://perplexity.ai/",
556
  "snippet": perplexity_content[:200] + "..." if len(perplexity_content) > 200 else perplexity_content,
557
+ "relevance_score": relevance_score,
558
+ "source": "perplexity"
559
  }
560
+ all_results.append(formatted_result)
561
+
562
+ # Store the full perplexity content in metadata
563
+ metadata["perplexity_content"] = perplexity_content
564
+
565
+ # Process Browser Search results
566
+ if "browser_search" in results_dict:
567
+ browser_results = results_dict["browser_search"]
568
+ if browser_results and isinstance(browser_results, list):
569
+ for result in browser_results:
570
+ if "link" in result and result["link"] not in seen_urls:
571
+ # Browser search results already have high relevance scores
572
+ if "relevance_score" not in result:
573
+ result["relevance_score"] = 9.0 # High default score for browser results
574
+
575
+ all_results.append(result)
576
+ seen_urls.add(result["link"])
577
+
578
+ # Sort all results by relevance score
579
+ all_results.sort(key=lambda x: x.get("relevance_score", 0), reverse=True)
580
+
581
+ # Store final merged results in working memory
582
+ if working_memory:
583
+ working_memory.store_intermediate_result("merged_search_results", all_results)
584
+ working_memory.store_intermediate_result("search_metadata", metadata)
585
 
586
  return {
587
+ "results": all_results[:10], # Return top 10 results
588
+ "metadata": metadata
 
589
  }
590
 
591
+ def search(registry: ToolRegistry, query: str, format_type: str = "unified", working_memory=None) -> Dict[str, Any]:
592
+ """
593
+ Unified wrapper function for all search types.
594
+
595
+ This function serves as a single entry point for all search operations,
596
+ eliminating redundancy while maintaining backward compatibility with
597
+ different output formats.
598
+
599
+ Args:
600
+ registry: The tool registry
601
+ query: The search query
602
+ format_type: The desired output format ("unified", "robust", or "merged")
603
+ working_memory: Optional working memory instance for storing results
604
+
605
+ Returns:
606
+ Dict with search results formatted according to format_type
607
+ """
608
+ # Execute the unified search
609
+ search_result = unified_search(registry, query, working_memory)
610
+
611
+ # Return results in the requested format
612
+ if format_type == "robust":
613
+ # Format as robust_search result
614
+ providers = [result.get("source", "unknown") for result in search_result["results"]]
615
+ unique_providers = list(set(providers))
616
+
617
+ return {
618
+ "provider": ",".join(unique_providers),
619
+ "results": search_result["results"]
620
+ }
621
+
622
+ elif format_type == "merged":
623
+ # Format as merged_search result
624
+ perplexity_content = search_result["metadata"].get("perplexity_content")
625
+
626
+ # Extract arxiv results if available
627
+ arxiv_results = []
628
+ browser_results = []
629
+ library_results = []
630
+ api_results = []
631
+
632
+ for result in search_result["results"]:
633
+ source = result.get("source", "")
634
+
635
+ if source == "arxiv":
636
+ # Try to reconstruct original arxiv result format
637
+ arxiv_result = {
638
+ "title": result.get("title", ""),
639
+ "url": result.get("link", ""),
640
+ "summary": result.get("snippet", "")
641
+ }
642
+ arxiv_results.append(arxiv_result)
643
+
644
+ elif source == "browser":
645
+ browser_results.append({
646
+ "title": result.get("title", ""),
647
+ "url": result.get("link", ""),
648
+ "snippet": result.get("snippet", "")
649
+ })
650
+
651
+ elif source == "library":
652
+ library_results.append({
653
+ "title": result.get("title", ""),
654
+ "url": result.get("link", ""),
655
+ "snippet": result.get("snippet", "")
656
+ })
657
+
658
+ elif source == "api":
659
+ api_results.append({
660
+ "title": result.get("title", ""),
661
+ "url": result.get("link", ""),
662
+ "snippet": result.get("snippet", "")
663
+ })
664
+
665
+ return {
666
+ "merged_results": search_result["results"],
667
+ "perplexity_context": perplexity_content,
668
+ "arxiv_context": arxiv_results,
669
+ "browser_context": browser_results,
670
+ "library_context": library_results,
671
+ "api_context": api_results
672
+ }
673
+
674
+ else: # "unified" or any other value
675
+ # Return the unified search result directly
676
+ return search_result
677
+
678
+ def robust_search(registry: ToolRegistry, query: str) -> Dict[str, Any]:
679
+ """
680
+ Legacy robust search function - now uses the unified search wrapper.
681
+
682
+ This function is maintained for backward compatibility.
683
+ New code should use the 'search' function with format_type="robust".
684
+
685
+ Args:
686
+ registry: The tool registry
687
+ query: The search query
688
+
689
+ Returns:
690
+ Dict with provider name and search results
691
+ """
692
+ return search(registry, query, format_type="robust")
693
+
694
+ def merged_search(registry: ToolRegistry, query: str, working_memory=None) -> Dict[str, Any]:
695
+ """
696
+ Legacy merged search function - now uses the unified search wrapper.
697
+
698
+ This function is maintained for backward compatibility.
699
+ New code should use the 'search' function with format_type="merged".
700
+
701
+ Args:
702
+ registry: The tool registry
703
+ query: The search query
704
+ working_memory: Optional working memory instance
705
+
706
+ Returns:
707
+ Dict with merged results and context
708
+ """
709
+ return search(registry, query, format_type="merged", working_memory=working_memory)
710
+
711
  if __name__ == "__main__":
712
  logging.basicConfig(level=logging.INFO)
713