Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
kyegomez authored Oct 8, 2024
1 parent 19f37f0 commit cae4616
Showing 1 changed file with 167 additions and 0 deletions.
167 changes: 167 additions & 0 deletions cookbook/enterprise/marketing/news/Swarms_Cookbook_NewsAgent.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "1e7d476c",
"metadata": {},
"source": [
"# Swarms Cookbook: NewsAgent - Real-Time News Aggregation and Summarization\n",
"\n",
"Welcome to the Swarms Cookbook! In this guide, we will walk through the steps of using **NewsAgent**, an enterprise-grade solution for real-time news aggregation, querying, and summarization.\n",
"\n",
"NewsAgent helps businesses stay informed by automating news monitoring from multiple sources, summarizing relevant content, and providing timely updates.\n",
"\n",
"### Key Features:\n",
"- **News Aggregation**: Fetch news from various sources via NewsAPI.\n",
"- **Advanced Summarization**: Leverages AI to generate concise summaries of complex news stories.\n",
"- **Enterprise-Grade**: Built with robust error handling and scalability for enterprise deployments.\n",
"\n",
"Follow this step-by-step guide to set up **NewsAgent** and integrate it into your news monitoring workflows.\n",
"\n",
"Be sure to visit the **[Swarms GitHub](https://github.com/kyegomez/swarms)**, **[Discord](https://discord.com/servers/agora-999382051935506503)**, and **[Swarms Marketplace](https://swarms.xyz)** for more resources and to connect with the community.\n"
]
},
{
"cell_type": "markdown",
"id": "8e2c95c9",
"metadata": {},
"source": [
"## Installation\n",
"\n",
"First, install the **NewsAgent** package by running the following command:\n",
"\n",
"```bash\n",
"pip3 install -U news-swarm\n",
"```\n",
"\n",
"Ensure that you have Python 3.10+ installed.\n",
"\n",
"You will also need to create a `.env` file with the following environment variables for API access:\n",
"\n",
"```bash\n",
"OPENAI_API_KEY=\"your-openai-api-key\"\n",
"NEWSAPI_API_KEY=\"your-newsapi-api-key\"\n",
"WORKSPACE_DIR=\"agent_workspace\"\n",
"```\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a9480d48",
"metadata": {},
"outputs": [],
"source": [
"# Import necessary libraries\n",
"import os\n",
"from dotenv import load_dotenv\n",
"from swarm_models import OpenAIChat\n",
"from swarms import Agent\n",
"from news_swarm.main import NewsAgent\n",
"\n",
"# Load environment variables from the .env file\n",
"load_dotenv()\n",
"\n",
"# Ensure API keys are loaded\n",
"openai_api_key = os.getenv(\"OPENAI_API_KEY\")\n",
"newsapi_api_key = os.getenv(\"NEWSAPI_API_KEY\")\n"
]
},
{
"cell_type": "markdown",
"id": "948f87de",
"metadata": {},
"source": [
"## Setting Up NewsAgent\n",
"\n",
"Now, let's initialize **NewsAgent** for real-time news aggregation and summarization. We will create an **OpenAIChat** instance for language model integration and set up the news agent to query and summarize the latest news from multiple sources.\n",
"\n",
"In this example, we'll fetch news related to **multi-agent collaboration** and display the results.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7da9002b",
"metadata": {},
"outputs": [],
"source": [
"# Create an instance of OpenAIChat class for LLM integration\n",
"model = OpenAIChat(\n",
" openai_api_key=openai_api_key,\n",
" model_name=\"gpt-4o-mini\",\n",
" temperature=0.1\n",
")\n",
"\n",
"# Initialize the base agent\n",
"base_agent = Agent(\n",
" agent_name=\"News-Agent-V1\",\n",
" llm=model,\n",
" max_loops=1,\n",
" autosave=True,\n",
" dashboard=False,\n",
" verbose=True,\n",
" dynamic_temperature_enabled=True,\n",
" saved_state_path=\"news_agent.json\",\n",
" user_name=\"swarms_corp\",\n",
" retry_attempts=1,\n",
" context_length=200000,\n",
")\n",
"\n",
"# Create the NewsAgent instance\n",
"news_agent = NewsAgent(\n",
" agent_name=\"news-agent-v1\",\n",
" agent=base_agent,\n",
" newsapi_api_key=newsapi_api_key,\n",
" system_prompt=None,\n",
" return_json=True,\n",
")\n",
"\n",
"# Run the agent to fetch news related to \"multi-agent collaboration\"\n",
"print(news_agent.run(\"multi-agent collaboration\"))\n",
"\n",
"# Fetch news for multiple topics concurrently\n",
"print(news_agent.run_concurrently([\"OpenAI\", \"Anthropic\"]))\n"
]
},
{
"cell_type": "markdown",
"id": "ebaadd82",
"metadata": {},
"source": [
"## System Architecture\n",
"\n",
"**NewsAgent** operates by combining real-time data fetching from **NewsAPI** with advanced AI-based summarization. Here’s how it works:\n",
"\n",
"- **News Fetching**: NewsAgent queries **NewsAPI** to fetch the latest news articles based on the provided keywords or topics.\n",
"- **OpenAI Integration**: Uses **GPT-4** to summarize complex news stories into concise reports that are easy to understand and act upon.\n",
"- **Agent Framework**: Powered by the **Swarms** framework, NewsAgent is scalable, flexible, and reliable for enterprise use.\n"
]
},
{
"cell_type": "markdown",
"id": "5f8c3abb",
"metadata": {},
"source": [
"## Next Steps and Resources\n",
"\n",
"You've successfully set up and run a real-time news aggregation and summarization using **NewsAgent**!\n",
"\n",
"To explore more:\n",
"- Try different queries for a wide range of topics.\n",
"- Adjust the agent’s configuration for more tailored results.\n",
"- Integrate **NewsAgent** into your existing platforms for automated news monitoring.\n",
"\n",
"For more resources and to contribute, visit:\n",
"- **[Swarms GitHub](https://github.com/kyegomez/swarms)**\n",
"- **[Swarms Discord](https://discord.com/servers/agora-999382051935506503)**\n",
"- **[Swarms Marketplace](https://swarms.xyz)**\n",
"\n",
"Stay connected with the Swarms community and keep building advanced agents!\n"
]
}
],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 5
}

0 comments on commit cae4616

Please sign in to comment.