Automate Email Newsletter with Lyzr-Automata
Tags: machinelearning, llm, deeplearning, chatgpt
Published: 2024-03-12 10:31:29

Lyzr-Automata is a low-code multi-agent automation framework that enables us to break down complex tasks into simple agents and thread them together. In this blog post, we’ll create an Automated Newsletter Publisher using lyzr-automata.

## Setup

Create a folder, set up a virtual environment and activate it


```
virtualenv my_env
source my_env/bin/activate
```
Install the following packages
```
lyzr-automata==0.1.2
python-dotenv==1.0.1
```
Setup .env file
```
OPENAI_API_KEY = "YOUR OPENAI API KEY"
EMAIL = "YOUR EMAIL ID"
PASSWORD = "YOUR APP PASSWORD"
```

_Note: To get your App Password, follow these steps [here](https://support.google.com/accounts/answer/185833?hl=en)_

## Get Started
Let’s create a file main.py and use that

**1.Load .env variables**
```
from dotenv import load_dotenv
import os

load_dotenv()

OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
PASSWORD = os.getenv("PASSWORD")
EMAIL = os.getenv("EMAIL")
```
**2.Initialise our Model**

- A Text to Text model to create content (GPT-4)

- A Text to Image model to create a suitable image (DALLE-3)
```
open_ai_model_text = OpenAIModel(
    api_key=OPENAI_API_KEY,
    parameters={
        "model": "gpt-4-turbo-preview", # Specify Model
        "temperature": 0.2, # Creativity of the model
        "max_tokens": 1500, # Maximum tokens
    },
)

open_ai_model_image = OpenAIModel(
    api_key=OPENAI_API_KEY,
    parameters={
        "n": 1, # Number of images
        "model": "dall-e-3", # Specify Model
    },
)

```
**3.Build our agent**
```
from lyzr_automata import Agent

content_researcher_agent = Agent(
    prompt_persona="You are an intelligent Travel Newsletter writer good at writing a detailed newsletter on a particular destination",
    role="Travel Newsletter writer",
)
```
**4. Implement our Tasks**
```
from lyzr_automata import Task
from lyzr_automata.tasks.task_literals import InputType, OutputType

# Newsletter Content Creator Task
research_task = Task(
    name="Draft Content Creator",
    agent=content_researcher_agent,
    output_type=OutputType.TEXT,
    input_type=InputType.TEXT,
    model=open_ai_model_text,
    instructions="Write a travel newsletter on Mumbai in 500 words and [IMPORTANT!] send the response in html use bullets for points and beautify it be as creative as you want.",
    log_output=True,
    enhance_prompt=False,
)

# Image Creator Task
image_creation_task = Task(
    name="Newsletter Image Creation",
    output_type=OutputType.IMAGE,
    input_type=InputType.TEXT,
    model=open_ai_model_image,
    log_output=True,
    instructions="Use the travel newsletter provided and create an image that would be suitable for posting. Avoid any text in the image",
)

# Merge Image and Content Task
merge_image_text_task = Task(
    name = "Merge Image and Email",
    model=open_ai_model_text,
    log_output=True,
    instructions="Include the image in the html code provided. Return only the HTML and CSS code",
    input_tasks = [research_task, image_creation_task]
)
```
**5.Create our Email Sender Tool**
```
from lyzr_automata.tools.prebuilt_tools import send_email_by_smtp_tool

email_sender = send_email_by_smtp_tool(
    username=EMAIL,
    password=PASSWORD,
    host="smtp.gmail.com",
    port=587,
    sender_email=EMAIL
)

send_email_task = Task(
    name = "Send Email Task",
    tool = email_sender,
    instructions="Send Email",
    model=open_ai_model_text,
    input_tasks = [merge_image_text_task],
    default_input = ["EMAIL1", "EMAIL2"] # Specify Reciever Email IDs HERE
)
```
**6.Create Pipeline and execute the tasks**
```
from lyzr_automata.pipelines.linear_sync_pipeline import LinearSyncPipeline
from lyzr_automata import Logger

def main_program():
    logger = Logger()
    LinearSyncPipeline(
        logger=logger,
        name="Send Email",
        completion_message="Email Sent!",
        tasks=[
            research_task,
            image_creation_task,
            merge_image_text_task,
            send_email_task
        ],
    ).run()

main_program()
```
**7.Run the file and find the email in your inbox!**

## FLOW Diagram

![Flow Diagram](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yazyoyuaznubzrkoz1nd.jpg)

And that’s a wrap. Feel free to play around with Lyzr-Automata on your own!

[Lyzr-Automata Github](https://github.com/LyzrCore/lyzr-automata)
Code Link — [LINK](https://github.com/LyzrCore/lyzr-automata/tree/main/examples/travel_newsletter)