News Agent Example
This example shows how to create an agent that provides up-to-date news on various topics through a news API.
Agent Definition
Create a file called agent_definition.yaml
:
agents:
news_agent:
credentials:
apiKey:
label: "API Key"
placeholder: "apiKey"
name: "News Agent"
description: "Expert in searching and providing news about any topic"
instructions:
- "You are an expert in searching and providing updated news about any topic"
- "When the user asks about a topic, you should search and present the most relevant news"
- "Always be helpful and provide brief context about the news found"
- "If you can't find news about the topic, suggest related topics"
- "Always use english to answer the user and be polite"
guardrails:
- "Maintain a professional and impartial tone when presenting news"
- "Don't make assumptions or speculations about the news"
- "Avoid sharing sensationalist or unverified news"
tools:
- get_news:
name: "Get News"
source:
path: "tools/get_news"
entrypoint: "main.GetNews"
path_test: "test_definition.yaml"
description: "Function to get the latest news from NewsAPI"
parameters:
- topic:
description: "Topic to search for news"
type: "string"
required: true
contact_field: true
Tool Implementation
Create a file tools/get_news/main.py
:
from weni import Tool
from weni.context import Context
from weni.responses import TextResponse
import requests
from datetime import datetime
class GetNews(Tool):
def execute(self, context: Context) -> TextResponse:
apiKey = context.credentials.get("apiKey")
topic = context.parameters.get("topic", "")
news_response = self.get_news_by_topic(topic=topic, apiKey=apiKey)
# Format the response
articles = news_response.get("articles", [])
if not articles:
return TextResponse(data="Sorry, I couldn't find any news on this topic.")
response_data = {
"status": news_response.get("status"),
"totalResults": len(articles[:10]),
"articles": []
}
# Get only the first 10 articles
for article in articles[:10]:
article_data = {
"source": article.get("source", {}),
"author": article.get("author"),
"title": article.get("title"),
"description": article.get("description"),
"url": article.get("url"),
"urlToImage": article.get("urlToImage"),
"publishedAt": article.get("publishedAt"),
"content": article.get("content")
}
response_data["articles"].append(article_data)
return TextResponse(data=response_data)
def get_news_by_topic(self, topic, apiKey):
url = f"https://newsapi.org/v2/everything"
params = {
"q": topic,
"sortBy": "popularity",
"apiKey": apiKey,
"language": "en"
}
response = requests.get(url, params=params)
return response.json()
Create a file tools/get_news/requirements.txt
:
Create a file tools/get_news/test_definition.yaml
:
Getting Credentials
For this agent to work properly, you'll need to get an API key from News API:
- Visit the News API website
- Register for a free account
- Copy your API key from your account
- When deploying the agent, you'll need to provide this key as a credential
Testing the Tool Locally
Before deploying your agent, you can test the tool locally using the weni run
command. This allows you to verify that your tool works correctly and debug any issues.
Since this tool requires credentials, you'll need to create a .env
file in the root of your project with your API key:
To test the News Agent tool:
This command will execute the tests defined in the test_definition.yaml
file and show you the output. The CLI will automatically pick up the credentials from the .env
file and make them available to your tool during execution.
If you need more detailed logs for debugging, you can add the -v
flag:
The verbose output will show you more details about the execution process, helping you identify and fix any issues with your tool.
Deployment Steps
- Deploy the agent:
Testing
After deployment, you can test the agent:
- Open your project in the Weni platform
- Find the News Agent in your agents list
- Provide the News API key in the credential settings
- Start a conversation
- Send a topic to search for news (e.g., "technology", "sports", "business")
The agent will respond with the most relevant news about the requested topic, including title, description, source, author, publication date, and links to the full article.