feat: add aggregated news retrieval methods for top headlines and latest news

This commit is contained in:
2025-10-02 00:32:49 +02:00
parent 8b81cd5a71
commit 27ad095713
3 changed files with 25 additions and 7 deletions

View File

@@ -45,13 +45,32 @@ class NewsAPIsTool(NewsWrapper, Toolkit):
], ],
) )
# TODO Pensare se ha senso restituire gli articoli da TUTTI i wrapper o solo dal primo che funziona
# la modifica è banale, basta usare try_call_all invece di try_call
def get_top_headlines(self, limit: int = 100) -> list[Article]: def get_top_headlines(self, limit: int = 100) -> list[Article]:
return self.wrapper_handler.try_call(lambda w: w.get_top_headlines(limit)) return self.wrapper_handler.try_call(lambda w: w.get_top_headlines(limit))
def get_latest_news(self, query: str, limit: int = 100) -> list[Article]: def get_latest_news(self, query: str, limit: int = 100) -> list[Article]:
return self.wrapper_handler.try_call(lambda w: w.get_latest_news(query, limit)) return self.wrapper_handler.try_call(lambda w: w.get_latest_news(query, limit))
def get_top_headlines_aggregated(self, limit: int = 100) -> dict[str, list[Article]]:
"""
Calls get_top_headlines on all wrappers/providers and returns a dictionary mapping their names to their articles.
Args:
limit (int): Maximum number of articles to retrieve from each provider.
Returns:
dict[str, list[Article]]: A dictionary mapping providers names to their list of Articles
"""
return self.wrapper_handler.try_call_all(lambda w: w.get_top_headlines(limit))
def get_latest_news_aggregated(self, query: str, limit: int = 100) -> dict[str, list[Article]]:
"""
Calls get_latest_news on all wrappers/providers and returns a dictionary mapping their names to their articles.
Args:
query (str): The search query to find relevant news articles.
limit (int): Maximum number of articles to retrieve from each provider.
Returns:
dict[str, list[Article]]: A dictionary mapping providers names to their list of Articles
"""
return self.wrapper_handler.try_call_all(lambda w: w.get_latest_news(query, limit))
NEWS_INSTRUCTIONS = """ NEWS_INSTRUCTIONS = """
**TASK:** You are a specialized **Crypto News Analyst**. Your goal is to fetch the latest news or top headlines related to cryptocurrencies, and then **analyze the sentiment** of the content to provide a concise report to the team leader. Prioritize 'crypto' or specific cryptocurrency names (e.g., 'Bitcoin', 'Ethereum') in your searches. **TASK:** You are a specialized **Crypto News Analyst**. Your goal is to fetch the latest news or top headlines related to cryptocurrencies, and then **analyze the sentiment** of the content to provide a concise report to the team leader. Prioritize 'crypto' or specific cryptocurrency names (e.g., 'Bitcoin', 'Ethereum') in your searches.
@@ -59,6 +78,8 @@ NEWS_INSTRUCTIONS = """
**AVAILABLE TOOLS:** **AVAILABLE TOOLS:**
1. `get_latest_news(query: str, limit: int)`: Get the 'limit' most recent news articles for a specific 'query'. 1. `get_latest_news(query: str, limit: int)`: Get the 'limit' most recent news articles for a specific 'query'.
2. `get_top_headlines(limit: int)`: Get the 'limit' top global news headlines. 2. `get_top_headlines(limit: int)`: Get the 'limit' top global news headlines.
3. `get_latest_news_aggregated(query: str, limit: int)`: Get aggregated latest news articles for a specific 'query'.
4. `get_top_headlines_aggregated(limit: int)`: Get aggregated top global news headlines.
**USAGE GUIDELINE:** **USAGE GUIDELINE:**
* Always use `get_latest_news` with a relevant crypto-related query first. * Always use `get_latest_news` with a relevant crypto-related query first.

View File

@@ -4,8 +4,8 @@ from praw.models import Submission, MoreComments
from .base import SocialWrapper, SocialPost, SocialComment from .base import SocialWrapper, SocialPost, SocialComment
MAX_COMMENTS = 5 MAX_COMMENTS = 5
# TODO mettere piu' subreddit? # metterne altri se necessario.
# scelti da https://lkiconsulting.io/marketing/best-crypto-subreddits/ # fonti: https://lkiconsulting.io/marketing/best-crypto-subreddits/
SUBREDDITS = [ SUBREDDITS = [
"CryptoCurrency", "CryptoCurrency",
"Bitcoin", "Bitcoin",

View File

@@ -63,9 +63,6 @@ def aggregate_product_info(products: dict[str, list[ProductInfo]]) -> list[Produ
product.price = (prices / volume_sum) if volume_sum > 0 else 0.0 product.price = (prices / volume_sum) if volume_sum > 0 else 0.0
aggregated_products.append(product) aggregated_products.append(product)
confidence = _calculate_confidence(product_list, sources) # TODO necessary?
return aggregated_products return aggregated_products
def _calculate_confidence(products: list[ProductInfo], sources: list[str]) -> float: def _calculate_confidence(products: list[ProductInfo], sources: list[str]) -> float: