from smolagents.tools import Tool from duckduckgo_search import DDGS class ExpertSearchTool(Tool): name = "expert_search" description = "Searches for expert answers from StackOverflow or Quora related to your query." inputs = {'query': {'type': 'string', 'description': 'The expert search query to perform.'}} output_type = "string" def __init__(self, max_results=10, **kwargs): super().__init__() self.max_results = max_results self.ddgs = DDGS(**kwargs) def forward(self, query: str) -> str: expert_query = f"{query} site:stackoverflow.com OR site:quora.com" results = self.ddgs.text(expert_query, max_results=self.max_results) if len(results) == 0: return "No expert answers found!" postprocessed_results = [f"[{result['title']}]({result['href']})\n{result['body']}" for result in results] return "## Expert Search Results\n\n" + "\n\n".join(postprocessed_results)