Skip to content

Commit c07330a

Browse files
committed
Create search_engine.py
1 parent 7a4946e commit c07330a

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

python/tools/search_engine.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import os
2+
import asyncio
3+
from python.helpers import dotenv, memory, perplexity_search, duckduckgo_search
4+
from python.helpers.tool import Tool, Response
5+
from python.helpers.print_style import PrintStyle
6+
from python.helpers.errors import handle_error
7+
from python.helpers.searxng import search as searxng
8+
9+
SEARCH_ENGINE_RESULTS = 10
10+
11+
12+
class SearchEngine(Tool):
13+
async def execute(self, query="", **kwargs):
14+
15+
16+
searxng_result = await self.searxng_search(query)
17+
18+
await self.agent.handle_intervention(
19+
searxng_result
20+
) # wait for intervention and handle it, if paused
21+
22+
return Response(message=searxng_result, break_loop=False)
23+
24+
25+
async def searxng_search(self, question):
26+
results = await searxng(question)
27+
return self.format_result_searxng(results, "Search Engine")
28+
29+
def format_result_searxng(self, result, source):
30+
if isinstance(result, Exception):
31+
handle_error(result)
32+
return f"{source} search failed: {str(result)}"
33+
34+
outputs = []
35+
for item in result["results"]:
36+
outputs.append(f"{item['title']}\n{item['url']}\n{item['content']}")
37+
38+
return "\n\n".join(outputs[:SEARCH_ENGINE_RESULTS]).strip()

0 commit comments

Comments
 (0)