Build a News Agent with SwarmZero.ai using the GDELT API
Last updated
Last updated
© 2024 SwarmZero Technology Solutions Inc. All rights reserved.
Imagine having a powerful agent that fetches the latest news, processes it, and delivers personalized responses to your queries. In this tutorial, you will learn how to create a News Agent that uses the GDELT Project's API to fetch and process news from various sources.
The GDELT Project monitors the world's news media, providing real-time translation, indexing, and analysis of global news coverage. Whether seeking the latest headlines, in-depth analyses, or specific topic updates, this agent ensures you receive accurate and timely information.
Whether you need the latest headlines, in-depth analyses, or updates on specific topics, this News Agent, built with SwarmzeroSDK, ensures you receive accurate and timely information.
Before starting, ensure you have the following:
Python 3.11 or higher installed
Git installed
An IDE or text editor (VS Code, PyCharm, etc.)
Basic understanding of Python programming
API keys from OpenAI, Anthropic, and Mistral if you plan to use these services
Here is a detailed guide to building the News Agent:
To begin, we need to clone the news_agent
repository to our local machine.
Step 1: Open your terminal or command prompt. Ensure you have Git installed on your system.
Step 2: Run the following commands:
These commands will download the repository and navigate into its directory, setting up the base environment to start working on the agent.
Now that you have the repository cloned, the next step is to set up the environment to run the agent. This involves creating a virtual environment, installing dependencies, and configuring environment variables.
Step 1: Creating a Virtual Environment
Step 2: Activate the virtual environment
Command Prompt:
PowerShell:
On Unix/Mac:
With the virtual environment activated, install the required dependencies:
Step 1: Create a new file called .env in the root directory:
Step 2: Copy the contents of .env.example into your new .env file.
Step 3: Add your API keys to the .env file:
These steps will ensure your environment is properly configured to run the Dune Agent.
Before running and customizing the agent, it's essential to understand the structure of the news_agent
repository. This will help you navigate through the code and make necessary modifications efficiently.
Overview of Key Files and Directories
.env.example
: This file provides an example of the environment variables required for the project. You should copy this file to create your .env
file and add the necessary API keys.
swarmzero_config.toml
: Contains configuration settings for the agent, such as API keys, query parameters, and other adjustable settings. This file is essential for configuring the agent's behavior.
news_agent.py
: This is the main script that runs the agent. It contains the core logic for fetching and processing news data.
requirements.txt
: Lists all the dependencies required to run the agent. These are the libraries and tools the agent relies on.
requirements-dev.txt
: Lists the development dependencies needed for testing and development purposes. This file is useful for setting up a development environment.
In this section, you will understand the logic and process of building a News agent with SwarmZero.ai.
Step 1: Import Necessary Libraries
Start by importing all the required libraries. These include libraries for handling environment variables, making HTTP requests, and interacting with the Agent.
Step 2: Load Environment Variables
Load your environment variables from a .env
file. This file should contain any necessary configuration, such as API keys, used by your application.
Step 3: Define the Function to Fetch the Latest News
Create a function that fetches the latest news articles based on a user query. This function will make an HTTP request to the GDELT Project API and return a summary of the latest news articles.
Step 4: Create and Run the Agent
Finally, create an instance of Agent
, passing in the functions and configuration path. Then, run the agent.
With the environment set up and a basic understanding of the code structure, you are now ready to run the agent and see it in action.
Step 1: Run the API Server
Ensure your virtual environment is activated. You can test your agent by running the following command:
Step 2: Open the Swagger Docs
Access the Swagger UI at http://localhost:8000/docs to interact with the API.
Step 3: Test the POST /api/v1/chat
Endpoint
You can test the agent with a curl command as follows:
This will send a request to the API and provide you with a response, allowing you to verify the agent’s functionality.
You can also interact with the News Agent using SwarmZero’s UI, following the instructions provided in this repository.
As the next steps, you can proceed with further testing and customization if required.
Congratulations! You've successfully set up, configured, and customized your News Agent using the news_agent repository from SwarmZero.ai.