DEV Community

Cover image for Build a Real-Time News AI Agent Using LangChain — In Just a Few Steps!
Pavan Belagatti
Pavan Belagatti

Posted on

Build a Real-Time News AI Agent Using LangChain — In Just a Few Steps!

In the rapidly evolving landscape of artificial intelligence, AI agents have emerged as one of the most practical and powerful applications of large language models. These intelligent systems can understand natural language, reason about complex tasks, and autonomously use specialized tools to accomplish goals—much like having a digital assistant that can think, plan, and execute actions on your behalf. Today, we'll explore how to build a sophisticated real-time news AI agent that can fetch current events, perform web searches, and engage in meaningful conversations, all while running locally on your machine.

Understanding AI Agents

AI Agents image

AI agents represent a significant leap forward from traditional chatbots or simple question-answering systems. At their core, AI agents are autonomous software entities that can perceive their environment, make decisions, and take actions to achieve specific objectives. Unlike static AI models that simply respond to prompts, agents possess the ability to reason about problems, plan multi-step solutions, and dynamically select from a toolkit of specialized functions.

Architectural Deep Dive: Building Blocks of Our News Agent

AI Agent architecture

Our real-time news AI agent exemplifies modern agent architecture through its sophisticated yet approachable design. Built on LangChain—an open-source framework specifically designed for LLM-powered applications—the system demonstrates how to effectively combine reasoning capabilities with practical functionality.

The foundation of our agent rests on OpenAI's GPT models, specifically leveraging the function calling capabilities that allow the model to determine when and how to use external tools. This isn't merely about generating text; it's about intelligent decision-making. When a user asks for "the latest news about climate change," the agent must understand the intent, decide which news tool to use, format the appropriate API call, process the results, and synthesize a coherent response.

AI Agents workflow

The agent's toolkit showcases the versatility of modern AI systems. The web search functionality uses DuckDuckGo to provide general information beyond the model's training data, ensuring the agent can access current information on virtually any topic.

Two specialized news tools work in tandem: LatestNews fetches category-based or topic-specific stories, while LocationNews is optimized for geography-specific news retrieval. A calculator tool handles mathematical operations safely and efficiently, while the time tool provides temporal grounding, helping the agent understand "current" in the context of real-time requests.

AI Agent: Implementation

Create a Python virtual environment first and then install the dependencies required.

pip install --upgrade "langchain>=0.2.0" "langchain-openai>=0.0.5" "langchain-community>=0.0.15"
Enter fullscreen mode Exit fullscreen mode

Mention your api keys for AI agent's tool calling purpose

OPENAI_API_KEY=Add your openai api key
TAVILY_API_KEY=Add your Tavily api key
NEWSAPI_API_KEY=Add your newsapi pai key
Enter fullscreen mode Exit fullscreen mode

Below is our agent implementation code workflow.

import os
from dotenv import load_dotenv
import gradio as gr
from langchain_openai import ChatOpenAI
from langchain.agents import create_openai_tools_agent, AgentExecutor
from langchain_core.tools import Tool
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain_core.messages import AIMessage, HumanMessage
from langchain_community.tools.ddg_search import DuckDuckGoSearchRun
from datetime import datetime
import requests
import json

# Load environment variables
load_dotenv()

# Initialize the language model
llm = ChatOpenAI(
    model="gpt-4",
    temperature=0
)

# Define custom tools
def get_current_time() -> str:
    """Get the current date and time."""
    return datetime.now().strftime("%Y-%m-%d %H:%M:%S")

def calculator(expression: str) -> str:
    """Evaluate a mathematical expression."""
    try:
        return str(eval(expression))
    except Exception as e:
        return f"Error calculating: {str(e)}"

def get_latest_news(query: str = "", category: str = "") -> str:
    """
    Get the latest news headlines.
    Parameters:
    - query: Search term for specific news (optional)
    - category: News category like business, entertainment, health, science, sports, technology (optional)
    """
    api_key = os.getenv("NEWSAPI_API_KEY")
    if not api_key:
        return "News API key not found. Please set NEWSAPI_API_KEY in your .env file."

    # Construct the API request
    url = "https://newsapi.org/v2/top-headlines"
    params = {
        "apiKey": api_key,
        "language": "en",
        "pageSize": 5  # Limit to 5 articles for readability
    }

    # Add optional parameters if provided
    if query:
        params["q"] = query
    if category and category.lower() in ["business", "entertainment", "general", "health", "science", "sports", "technology"]:
        params["category"] = category.lower()
    elif not query:  # Default to general news if no query or category
        params["category"] = "general"

    try:
        response = requests.get(url, params=params)
        if response.status_code == 200:
            news_data = response.json()
            if news_data["totalResults"] == 0:
                # Try an alternative approach with everything endpoint for location-based searches
                return get_location_news(query)

            # Format the results
            result = f"Latest News {f'on {query}' if query else ''} {f'in {category}' if category else ''}:\n\n"
            for i, article in enumerate(news_data["articles"], 1):
                result += f"{i}. {article['title']}\n"
                result += f"   Source: {article['source']['name']}\n"
                result += f"   Published: {article['publishedAt']}\n"
                result += f"   Summary: {article['description'] if article['description'] else 'No description available'}\n"
                result += f"   URL: {article['url']}\n\n"

            return result
        else:
            return f"Error fetching news: {response.status_code}"
    except Exception as e:
        return f"Error processing news request: {str(e)}"

def get_location_news(location: str) -> str:
    """
    Get news for a specific location using the everything endpoint.
    This is better for location-based searches.
    """
    api_key = os.getenv("NEWSAPI_API_KEY")
    if not api_key:
        return "News API key not found. Please set NEWSAPI_API_KEY in your .env file."

    # Use the everything endpoint which is better for location searches
    url = "https://newsapi.org/v2/everything"
    params = {
        "apiKey": api_key,
        "q": location,  # Search for the location name
        "sortBy": "publishedAt",  # Sort by most recent
        "language": "en",
        "pageSize": 5
    }

    try:
        response = requests.get(url, params=params)
        if response.status_code == 200:
            news_data = response.json()

            if news_data["totalResults"] == 0:
                return f"No news found for location: {location}. Try a different search term or check back later."

            # Format the results
            result = f"Latest News related to {location}:\n\n"
            for i, article in enumerate(news_data["articles"], 1):
                result += f"{i}. {article['title']}\n"
                result += f"   Source: {article['source']['name']}\n"
                result += f"   Published: {article['publishedAt']}\n"
                result += f"   Summary: {article['description'] if article['description'] else 'No description available'}\n"
                result += f"   URL: {article['url']}\n\n"

            return result
        else:
            return f"Error fetching location news: {response.status_code}"
    except Exception as e:
        return f"Error processing location news request: {str(e)}"

# Create search tool
duckduckgo_search = DuckDuckGoSearchRun()

# Define the tools
tools = [
    Tool(
        name="Search",
        func=duckduckgo_search.run,
        description="Useful for searching the web for current information."
    ),
    Tool(
        name="Calculator",
        func=calculator,
        description="Useful for performing mathematical calculations. Input should be a mathematical expression."
    ),
    Tool(
        name="CurrentTime",
        func=get_current_time,
        description="Get the current date and time. No input is needed."
    ),
    Tool(
        name="LatestNews",
        func=get_latest_news,
        description="Get the latest news headlines. You can specify a search query and/or category (business, entertainment, health, science, sports, technology)."
    ),
    Tool(
        name="LocationNews",
        func=get_location_news,
        description="Get news for a specific location or city. Input should be the name of the location (e.g., 'Mumbai', 'New York')."
    )
]

# Create the agent prompt
prompt = ChatPromptTemplate.from_messages([
    ("system", """You are an intelligent assistant that helps users with their questions.
    You have access to tools that can search the web, get the latest news, perform calculations, and get the current time.
    Use these tools to provide helpful and accurate responses.

    When asked about general news or news categories, use the LatestNews tool.
    When asked about news in a specific location or city, use the LocationNews tool.

    Always think step by step and explain your reasoning clearly.
    """),
    MessagesPlaceholder(variable_name="chat_history"),
    ("human", "{input}"),
    MessagesPlaceholder(variable_name="agent_scratchpad")
])

# Create the agent
agent = create_openai_tools_agent(
    llm=llm,
    tools=tools,
    prompt=prompt
)

# Create the agent executor
agent_executor = AgentExecutor.from_agent_and_tools(
    agent=agent,
    tools=tools,
    verbose=True,
    handle_parsing_errors=True,
    max_iterations=5
)

# Initialize chat history
chat_history = []

# Function to process user input
def process_input(message):
    global chat_history
    # Run the agent
    response = agent_executor.invoke({
        "input": message,
        "chat_history": chat_history
    })
    # Update chat history
    chat_history.append(HumanMessage(content=message))
    chat_history.append(AIMessage(content=response["output"]))
    return response["output"]

# Create the Gradio interface
with gr.Blocks(title="AI Agent Dashboard") as demo:
    gr.Markdown("# 🤖 AI Agent Dashboard")
    gr.Markdown("Ask me anything! I can search the web, get the latest news, perform calculations, and more.")

    chatbot = gr.Chatbot(height=500)
    msg = gr.Textbox(label="Your question", placeholder="Ask me about the latest news, search the web, or do calculations...")
    clear = gr.Button("Clear conversation")

    def respond(message, chat_history):
        bot_message = process_input(message)
        chat_history.append((message, bot_message))
        return "", chat_history

    def clear_chat():
        global chat_history
        chat_history = []
        return None

    msg.submit(respond, [msg, chatbot], [msg, chatbot])
    clear.click(clear_chat, None, chatbot, queue=False)

if __name__ == "__main__":
    demo.launch(share=True)
Enter fullscreen mode Exit fullscreen mode

Run the application to see the AI Agent dashboard with real-time magic.

python3 agent.py  
Enter fullscreen mode Exit fullscreen mode

You should be able to open your localhost and see this.

local news

news info from sf

The complete code is present here below.

Overview of the AI Agent Architecture

This agent is built using a modern AI architecture that combines large language models (LLMs) with specialized tools. The fundamental design pattern follows what's known as a "tool-using agent" architecture, where an LLM acts as the brain that can reason about problems and decide which specialized tools to use to accomplish tasks Core Components and Technologies

Framework: LangChain

LangChain is an open-source framework designed specifically for building LLM-powered applications. It provides the scaffolding for connecting language models to external tools and data sources.

Language Model: OpenAI's GPT model

We used gpt-4, which supports function calling. This allows the model to determine when to use which tools in a structured way.

Agent Type: OpenAI Tools Agent

We implemented the agent using LangChain's create_openai_tools_agent pattern. This pattern leverages OpenAI's function calling capabilities for reliable tool selection.

User Interface: Gradio

Gradio provides a simple way to create…

SingleStore as a Vector Database & for Real-Time Analytics

For enhanced data persistence and real-time analytics, SingleStore provides an excellent foundation for scaling your news AI agent. As a distributed SQL database optimized for both transactions and analytics, SingleStore can store conversation history, user preferences, and cached news data while enabling lightning-fast queries across large datasets. Its ability to handle real-time data ingestion makes it perfect for continuously updating news feeds, while its SQL compatibility ensures easy integration with your existing Python codebase. This combination allows your agent to maintain context across sessions and perform sophisticated analytics on news trends and user interactions.

SingleStore has excellent LangChain integration that can significantly improve your agent's memory and news data management.

Key Benefits of Adding SingleStore:

  • Persistent Memory: Store conversation history across sessions.
  • Semantic News Search: Find related articles using AI-powered similarity search.
  • Real-time Analytics: Analyze trending topics and news patterns.
  • Scalable Caching: Efficiently store and retrieve large amounts of news data.

Try SingleStore for free!

Once you signup to SingleStore, create a workspace and a database.

workspace and db

Then go to the 'Data Studio'.

data studio

Create a new notebook.

create notebook

Start adding the step-by-step code.

!pip install --upgrade "langchain>=0.2.0" "langchain-openai>=0.0.5" "langchain-community>=0.0.15"
!pip install --upgrade "singlestoredb>=1.0.0" "langchain-singlestoredb>=0.1.0"
!pip install --upgrade gradio requests python-dotenv
!pip install --upgrade sentence-transformers tiktoken
Enter fullscreen mode Exit fullscreen mode
import os
import gradio as gr
from langchain_openai import ChatOpenAI, OpenAIEmbeddings
from langchain.agents import create_openai_tools_agent, AgentExecutor
from langchain_core.tools import Tool
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain_core.messages import AIMessage, HumanMessage
from langchain_community.tools.ddg_search import DuckDuckGoSearchRun
from langchain_community.vectorstores import SingleStoreDB
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_core.documents import Document
from datetime import datetime
import requests
import json
import singlestoredb as s2
Enter fullscreen mode Exit fullscreen mode
SINGLESTORE_HOST = "add your SingleStore "
SINGLESTORE_PORT = 3306
SINGLESTORE_USER = "add your username"
SINGLESTORE_PASSWORD = "add password"
SINGLESTORE_DATABASE = "add db name"
SINGLESTORE_TABLE = "ai_agent_knowledge"
OPENAI_API_KEY = "add your OpenAI API key"
NEWSAPI_API_KEY = "add your newsapi key"
Enter fullscreen mode Exit fullscreen mode

Below is the complete repository that will guide you on integrating SingleStore database functionality. This will allow your agent to store and retrieve information from a vector database, making it more powerful for knowledge management and retrieval.

AI Agents with SingleStore

Enhance your AI Agents by integrating SingleStore database functionality. This will allow your agent to store and retrieve information from a vector database, making it more powerful for knowledge management and retrieval.

Try SingleStore for free!




Conclusion

Building a real-time news AI agent demonstrates the practical power of combining large language models with specialized tools and thoughtful architecture. Through LangChain's framework, we've created a system that can understand natural language, make intelligent decisions about tool usage, and provide valuable real-time information while maintaining user privacy and control.

Top comments (1)

Collapse
 
youngfra profile image
Fraser Young

Love how you broke down using LangChain for a real-time news agent and included step-by-step code. Super useful!