Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions news-summarizer-agent/.env.example
Original file line number Diff line number Diff line change
@@ -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
67 changes: 67 additions & 0 deletions news-summarizer-agent/README.md
Original file line number Diff line number Diff line change
@@ -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
83 changes: 83 additions & 0 deletions news-summarizer-agent/agent.py
Original file line number Diff line number Diff line change
@@ -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())
Binary file added news-summarizer-agent/demo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions news-summarizer-agent/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
requests>=2.28.0
python-dotenv>=1.0.0
Loading