-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsearch_engine.py
57 lines (41 loc) · 1.84 KB
/
search_engine.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
from googlesearch import search
class AISearch:
def search_engine(self, ticker):
#print(f" Debug: searching {query} on the web.")
query = f"latest news about {ticker}."
results = search(query, num_results=4, advanced=True)
formatted_results = []
if results is not None:
for result in results:
formatted_result = {
'title': result.title,
'description': result.description,
'url': result.url
}
formatted_results.append(formatted_result)
else:
formatted_results = None
return formatted_results
def serch_prompt_generate(self,user_input,search_mode):
if search_mode == True:
searched = self.search_engine(user_input)
search_prompt = f"Additional search results :{searched}.\nGenerate in markdown format if possible."
else:
search_prompt =""
return search_prompt
def search_patterns(self,query):
#print(f" Debug: screening search patterns")
import re
query_patterns = [
r"\b(internet|web|online|search|google|bing|yahoo|duckduckgo)\b",
r"\b(latest|news|now|update|today|breaking|current)\b",
r"\b(according to|source of|quoted by|cited by|do you know)\b",
r"\b(research|study|article|report|statistics|evidence|tutorial|guide)\b"
]
if any(re.search(pattern, query, re.IGNORECASE) for pattern in query_patterns):
print("System: This query likely requires an internet search.")
search_mode = True
else:
print("System: This query might be answered with existing knowledge.")
search_mode = False
return search_mode