|
| 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