from typing import Any, Optional from smolagents.tools import Tool class GetNewsTool(Tool): name = "Get_News" description = "Fetches the latest NYTimes RSS feed and returns the headlines and URLs of the most recent 5 stories." inputs = {} # This tool does not require any inputs. output_type = "string" def __init__(self, feed_url: str = "https://rss.nytimes.com/services/xml/rss/nyt/HomePage.xml", max_results: int = 5, **kwargs): super().__init__() self.feed_url = feed_url self.max_results = max_results try: import feedparser except ImportError as e: raise ImportError( "You must install package `feedparser` to run this tool: for instance run `pip install feedparser`." ) from e self.feedparser = feedparser def forward(self) -> str: feed = self.feedparser.parse(self.feed_url) if not feed.entries: raise Exception("No entries found in the feed.") latest_entries = feed.entries[:self.max_results] # Format each entry with its headline as a clickable link pointing to the URL. result = "## NYTimes Latest Headlines and URLs\n\n" + "\n".join( f"- [{entry.title}]({entry.link})" for entry in latest_entries ) return result