DEV Community

Cover image for Building a Personalized News Agent with Lyzr Automata
harshit-lyzr
harshit-lyzr

Posted on

Building a Personalized News Agent with Lyzr Automata

In today's fast-paced world, staying informed about the latest news is more important than ever. However, with the overwhelming amount of information available online, it can be challenging to filter through and find relevant news articles. That's where the Lyzr Personal News Agent comes in. In this guide, we'll walk you through the process of building your own personalized news agent using Lyzr Automata.

Introduction to Lyzr Personal News Agent
The Lyzr Personal News Agent is a versatile tool that automates the process of fetching, summarizing, and presenting news articles tailored to the user's interests. Powered by Lyzr Automata and integrated with the SERPAPI API, this news agent delivers a seamless and personalized news browsing experience.

Lyzr.ai

Check out the Lyzr.ai community on Discord - hang out with 225 other members and enjoy free voice and text chat.

favicon discord.com

Setting Up the Environment
Before we dive into building the news agent, let's make sure we have all the necessary tools and dependencies installed. Here's what you'll need:

  • Python environment with Streamlit installed
  • Lyzr Automata library
  • SERPAPI API key
  • OpenAI API key

Ensure you have these prerequisites in place before proceeding further.

Import Libraries:

import streamlit as st
import os
from dotenv import load_dotenv,find_dotenv
from lyzr_automata.ai_models.openai import OpenAIModel
from lyzr_automata import Agent,Task
from lyzr_automata.pipelines.linear_sync_pipeline import LinearSyncPipeline
import json
from PIL import Image
import requests
from bs4 import BeautifulSoup
import re
import ssl
Enter fullscreen mode Exit fullscreen mode

json: Used for working with JSON data (e.g., API responses).
requests: Used for making HTTP requests to websites.
BeautifulSoup: A library for parsing HTML content.
streamlit: for building the web app interface.
lyzr_automata libraries: for defining AI models, agents, and tasks.
dotenv: for loading environment variables (API key).

search Function:

def search(query):
    url = "https://google.serper.dev/search"

    payload = json.dumps({
        "q": query,
        "gl": "in",
    })

    headers = {
        'X-API-KEY': serpapi,
        'Content-Type': 'application/json'
    }

    response = requests.request("POST", url, headers=headers, data=payload)
    res = response.json()
    # Print the response JSON object to inspect its structure

    mys = []
    for item in res.get('organic', []):
        mys.append(item.get('link'))
    return mys
Enter fullscreen mode Exit fullscreen mode

Takes a search query and an optional country code (gl) as input.
Sends a POST request to a Google Search API endpoint (replace with your preferred API URL).
Parses the JSON response to extract relevant URLs from search results.
Returns a list of URLs associated with the query.

extract_text_from_url Function:

def extract_text_from_url(url):
    try:
        # Fetch HTML content from the URL
        response = requests.get(url)
        response.raise_for_status()

        # Parse HTML using BeautifulSoup
        soup = BeautifulSoup(response.content, 'html.parser')

        # Extract text content and replace consecutive spaces with a maximum of three spaces
        text_content = re.sub(r'\s{4,}', '   ', soup.get_text())

        return text_content

    except requests.exceptions.RequestException as e:
        print(f"Error fetching content from {url}: {e}")
        return None
Enter fullscreen mode Exit fullscreen mode

Takes a URL as input.
Fetches the HTML content from the URL using requests.get.
Parses the HTML using BeautifulSoup.
Extracts the text content, replacing consecutive spaces with a maximum of three spaces for better readability.
Handles potential request exceptions (e.g., network errors) by printing an error message and returning None.

extracteddata Function:

def extracteddata(query):
    result =  search(query)
    my_data = []
    for i in result:
        get_data = extract_text_from_url(i)
        my_data.append(get_data)
    return my_data
Enter fullscreen mode Exit fullscreen mode

Takes a search query as input.
Calls the search function to get a list of URLs.
Iterates through the URLs, extracting text from each using extract_text_from_url.
Filters out URLs that don't return text (e.g., non-textual content).
Returns a list of extracted text snippets from the search results.

Input News Title:

give_news=st.text_input("search")
Enter fullscreen mode Exit fullscreen mode

st.text_input("search") creates a text input box on the Streamlit app where users can enter their search query.
The entered text is stored in the variable give_news.

Lyzr Automata for Personalized News:

if st.button("Get News"):
    data=extracteddata(give_news)

    writer_agent = Agent(
            role="writer",
            prompt_persona=f"""You are an expert News Writer and You have to write news from {data} in 300 words include all hot topics from {data}
            """
        )

    task1  =  Task(
        name="news writing",
        model=open_ai_text_completion_model,
        agent=writer_agent,
        instructions=f'Analyse {data} and write news article from {data} in 250 words',
    )

    output = LinearSyncPipeline(
        name="Game Pipline",
        completion_message="pipeline completed",
        tasks=[
              task1,
        ],
    ).run()

    st.markdown(output[0]['task_output'])
Enter fullscreen mode Exit fullscreen mode
  • give_news is a function or variable that provides some news data, the code calls extracteddata(give_news) to extract this data.
  • An agent is created with a role of "writer" and a prompt_persona. This persona provides a context for the AI to generate text. Then, a task named "news writing" is created, specifying the model, agent, and instructions.
  • A linear pipeline named "Game Pipeline" is created with the specified task. This pipeline will execute the task sequentially.
  • The output of the pipeline is then displayed using Streamlit's st.markdown function.

This code demonstrates how to leverage Lyzr Automata,Streamlit and OpenAI to create a user-friendly application for generating news articles based on search queries. Remember to replace the placeholder extracteddata function with your actual data processing logic and use the appropriate OpenAI text-completion model. With some additional customization, you can build a powerful tool for staying informed on the latest news.
Try it now:https://lyzr-news.streamlit.app/
For more information explore the website: Lyzr


Top comments (0)