diff --git a/news-summarizer-agent/.env.example b/news-summarizer-agent/.env.example new file mode 100644 index 0000000..d539bd4 --- /dev/null +++ b/news-summarizer-agent/.env.example @@ -0,0 +1,5 @@ +# ASI One API key +ASI1_API_KEY=your_asi1_api_key_here + +# NewsAPI key (free tier at newsapi.org/register) +NEWS_API_KEY=your_news_api_key_here diff --git a/news-summarizer-agent/README.md b/news-summarizer-agent/README.md new file mode 100644 index 0000000..d0af52e --- /dev/null +++ b/news-summarizer-agent/README.md @@ -0,0 +1,67 @@ +# News Summarizer Agent + +A beginner-friendly Fetch.ai agent that fetches top news headlines for any topic using [NewsAPI](https://newsapi.org/) and summarizes them using the [ASI:One](https://asi1.ai/) LLM. + +## What It Does + +1. You provide a topic (e.g. "AI", "climate", "sports") +2. The agent fetches the top 5 latest headlines from NewsAPI +3. The headlines are sent to ASI:One LLM for summarization +4. A short, readable summary is printed to the terminal + +## Tech Stack + +- Python 3.10+ +- NewsAPI - free tier, no credit card required +- ASI:One - Fetch.ai's LLM API +- requests - HTTP calls +- python-dotenv - environment variable management + +## Setup + +### 1. Clone the repo + +git clone https://github.com/fetchai/innovation-lab-examples.git +cd innovation-lab-examples/news-summarizer-agent + +### 2. Install dependencies + +pip install -r requirements.txt + +### 3. Set up environment variables + +cp .env.example .env + +Edit .env and fill in your API keys: + +ASI1_API_KEY=your_asi1_api_key_here +NEWS_API_KEY=your_news_api_key_here + +- Get a free NewsAPI key at: https://newsapi.org/register +- Get your ASI:One API key at: https://asi1.ai/ + +### 4. Run the agent + +python agent.py + +When prompted, enter a topic like AI, climate, or sports. + +## Demo + +![Demo output](demo.png) + +## Environment Variables + +| Variable | Description | +|---|---| +| ASI1_API_KEY | Your ASI:One API key from asi1.ai | +| NEWS_API_KEY | Your NewsAPI key from newsapi.org | + +## Project Structure + +news-summarizer-agent/ +├── agent.py # Main agent logic +├── requirements.txt # Python dependencies +├── .env.example # Environment variable template +├── demo.png # Demo screenshot +└── README.md # This file diff --git a/news-summarizer-agent/agent.py b/news-summarizer-agent/agent.py new file mode 100644 index 0000000..6013e47 --- /dev/null +++ b/news-summarizer-agent/agent.py @@ -0,0 +1,83 @@ +import os +import requests +from dotenv import load_dotenv + +load_dotenv() + +load_dotenv() + +ASI1_API_KEY = os.getenv("ASI1_API_KEY") +NEWS_API_KEY = os.getenv("NEWS_API_KEY") + +NEWS_API_URL = "https://newsapi.org/v2/everything" +ASI1_API_URL = "https://api.asi1.ai/v1/chat/completions" + + +def fetch_headlines(topic: str) -> list[str]: + """Fetch top 5 news headlines for a given topic using NewsAPI.""" + params = { + "q": topic, + "pageSize": 5, + "sortBy": "publishedAt", + "language": "en", + "apiKey": NEWS_API_KEY, + } + response = requests.get(NEWS_API_URL, params=params) + response.raise_for_status() + articles = response.json().get("articles", []) + headlines = [a["title"] for a in articles if a.get("title")] + return headlines + + +def summarize_with_asi1(topic: str, headlines: list[str]) -> str: + """Send headlines to ASI:One LLM and get a readable summary.""" + headlines_text = "\n".join(f"- {h}" for h in headlines) + prompt = ( + f"Here are the top 5 recent news headlines about '{topic}':\n\n" + f"{headlines_text}\n\n" + f"Please write a short, clear, 3-4 sentence summary of what is " + f"currently happening with '{topic}' based on these headlines." + ) + + headers = { + "Authorization": f"Bearer {ASI1_API_KEY}", + "Content-Type": "application/json", + } + payload = { + "model": "asi1-mini", + "messages": [{"role": "user", "content": prompt}], + "temperature": 0.7, + "max_tokens": 300, + "stream": False, + } + + response = requests.post(ASI1_API_URL, headers=headers, json=payload) + response.raise_for_status() + data = response.json() + return data["choices"][0]["message"]["content"] + + +def run_agent(topic: str) -> None: + """Main agent flow: fetch headlines then summarize.""" + print(f"\nFetching top headlines for topic: '{topic}'...") + headlines = fetch_headlines(topic) + + if not headlines: + print("No headlines found for this topic. Try a different one.") + return + + print(f"\nFound {len(headlines)} headlines:") + for h in headlines: + print(f" - {h}") + + print("\nSummarizing with ASI:One LLM...") + summary = summarize_with_asi1(topic, headlines) + + print("\n--- Summary ---") + print(summary) + print("---------------\n") + + +if __name__ == "__main__": + topic = input("Enter a topic to summarize news for (e.g. AI, climate, sports): ") + run_agent(topic.strip()) diff --git a/news-summarizer-agent/demo.png b/news-summarizer-agent/demo.png new file mode 100644 index 0000000..ffeffa6 Binary files /dev/null and b/news-summarizer-agent/demo.png differ diff --git a/news-summarizer-agent/requirements.txt b/news-summarizer-agent/requirements.txt new file mode 100644 index 0000000..9ffc86f --- /dev/null +++ b/news-summarizer-agent/requirements.txt @@ -0,0 +1,2 @@ +requests>=2.28.0 +python-dotenv>=1.0.0