Raiff1982 commited on
Commit
50d7bde
·
verified ·
1 Parent(s): 6991e15

Update src/utils/search_utility.py

Browse files
Files changed (1) hide show
  1. src/utils/search_utility.py +66 -66
src/utils/search_utility.py CHANGED
@@ -1,67 +1,67 @@
1
- from typing import List, Dict, Any
2
- import json
3
- import os
4
- from components.search_engine import SearchEngine
5
-
6
- class SearchUtility:
7
- """Utility class for performing web searches"""
8
-
9
- def __init__(self, config_path: str = "config/google_config.json"):
10
- self.search_engine = None
11
- self._initialize_search_engine(config_path)
12
-
13
- def _initialize_search_engine(self, config_path: str):
14
- """Initialize the search engine with configuration"""
15
- try:
16
- # Load config if exists
17
- if os.path.exists(config_path):
18
- with open(config_path, 'r') as f:
19
- config = json.load(f)
20
-
21
- # Set environment variables
22
- os.environ['GOOGLE_API_KEY'] = config['google_search']['api_key']
23
- os.environ['GOOGLE_CUSTOM_SEARCH_ID'] = config['google_search']['custom_search_id']
24
-
25
- self.search_engine = SearchEngine()
26
-
27
- except Exception as e:
28
- print(f"Failed to initialize search engine: {str(e)}")
29
- self.search_engine = None
30
-
31
- async def search(self, query: str, max_results: int = 3) -> str:
32
- """
33
- Perform a web search and return formatted results
34
-
35
- Args:
36
- query (str): The search query
37
- max_results (int): Maximum number of results to return
38
-
39
- Returns:
40
- str: Formatted search results with sources
41
- """
42
- if not self.search_engine:
43
- return "Search functionality is not available. Please check configuration."
44
-
45
- try:
46
- return await self.search_engine.get_knowledge(query, max_results)
47
- except Exception as e:
48
- return f"Search failed: {str(e)}"
49
-
50
- async def get_raw_results(self, query: str, num_results: int = 5) -> List[Dict[str, Any]]:
51
- """
52
- Get raw search results for further processing
53
-
54
- Args:
55
- query (str): The search query
56
- num_results (int): Number of results to return
57
-
58
- Returns:
59
- List[Dict]: List of search results with title, link, and snippet
60
- """
61
- if not self.search_engine:
62
- return []
63
-
64
- try:
65
- return await self.search_engine.search(query, num_results)
66
- except Exception:
67
  return []
 
1
+ from typing import List, Dict, Any
2
+ import json
3
+ import os
4
+ from ..components.search_engine import SearchEngine
5
+
6
+ class SearchUtility:
7
+ """Utility class for performing web searches"""
8
+
9
+ def __init__(self, config_path: str = "config/google_config.json"):
10
+ self.search_engine = None
11
+ self._initialize_search_engine(config_path)
12
+
13
+ def _initialize_search_engine(self, config_path: str):
14
+ """Initialize the search engine with configuration"""
15
+ try:
16
+ # Load config if exists
17
+ if os.path.exists(config_path):
18
+ with open(config_path, 'r') as f:
19
+ config = json.load(f)
20
+
21
+ # Set environment variables
22
+ os.environ['GOOGLE_API_KEY'] = config['google_search']['api_key']
23
+ os.environ['GOOGLE_CUSTOM_SEARCH_ID'] = config['google_search']['custom_search_id']
24
+
25
+ self.search_engine = SearchEngine()
26
+
27
+ except Exception as e:
28
+ print(f"Failed to initialize search engine: {str(e)}")
29
+ self.search_engine = None
30
+
31
+ async def search(self, query: str, max_results: int = 3) -> str:
32
+ """
33
+ Perform a web search and return formatted results
34
+
35
+ Args:
36
+ query (str): The search query
37
+ max_results (int): Maximum number of results to return
38
+
39
+ Returns:
40
+ str: Formatted search results with sources
41
+ """
42
+ if not self.search_engine:
43
+ return "Search functionality is not available. Please check configuration."
44
+
45
+ try:
46
+ return await self.search_engine.get_knowledge(query, max_results)
47
+ except Exception as e:
48
+ return f"Search failed: {str(e)}"
49
+
50
+ async def get_raw_results(self, query: str, num_results: int = 5) -> List[Dict[str, Any]]:
51
+ """
52
+ Get raw search results for further processing
53
+
54
+ Args:
55
+ query (str): The search query
56
+ num_results (int): Number of results to return
57
+
58
+ Returns:
59
+ List[Dict]: List of search results with title, link, and snippet
60
+ """
61
+ if not self.search_engine:
62
+ return []
63
+
64
+ try:
65
+ return await self.search_engine.search(query, num_results)
66
+ except Exception:
67
  return []