AI-Powered Romance: Personalized Love Poems with CrewAI Agents

Expressing Your Love Creatively with AI Collaboration using Crew AI, LangChain, and OpenAI’s API

Ingrid Stevens
The Deep Hub
6 min readFeb 13, 2024

--

Are you looking for a creative way to express your feelings to your loved one this Valentine’s Day while learning a new AI tool? Look no further!

In this tutorial, we’ll explore how to leverage CrewAI, a framework for orchestrating role-playing autonomous AI agents, to create custom love poems that are sure to melt hearts.

CREW AI is a collaborative working system that enables various artificial intelligence agents to work together as a team, efficiently accomplishing complex tasks. Each agent has a specific role, resembling a team composed of researchers, writers, and planners.

In this tutorial, we’ll create a hierarchical team consisting of a manager, a researcher, and a writer, who will collaborate as a team to craft custom love poems for clients to give to their partners!

Full code is available on GitHub:

Let’s get started!

Requirements

  • Familiarity with the command line & python
  • OpenAI API Key
  • VS Code set up with Conda + Python 3.11

Step 1: Set Up Your Environment

I did this project using a Jupyter Notebook running in VS Code. I also had an OpenAI API key in my environment variables. You can do this on a mac in terminal with:

export OPENAI_API_KEY=your_actual_api_key_here

Next, create your environment:

conda create -n crew python=3.11

conda activate crew

And install dependencies

! pip install crewai duckduckgo-search

And load the libraries

from crewai import Agent, Task, Crew, Process
from langchain_community.tools import DuckDuckGoSearchRun
from langchain.agents import load_tools
from langchain.tools import tool
from crewai.tasks.task_output import TaskOutput
from langchain_openai import ChatOpenAI

Step 2: Set Up Your Crew

Define the search tool to use:

# Define the search tool 

search_tool = DuckDuckGoSearchRun()

Set up the topic of the poem (this should be specific to your loved one’s preferences:

# Define the topic 
# In this case, since we want a poem for our loved one, the topic should be things that they like and that you want to include in the poem

topic = "Taylor Swift and long walks on the beach at sunset"

Define what should happen after the task is completed (currently, this is just a print function, but it could be modified to do more):

# Function that is run after the task is completed

def callback_function(output: TaskOutput):
## What to do after the task is completed (i.e. send an sms with a love poem)
print(f"""
Task completed with the following output:
Task: {output.description}
Output: {output.result}
""")

Define a tool to read webpage content. Thanks to Mervin Praison!

# Custom tool 

class ContentTools:
@tool("Read webpage content")
def read_content(url: str) -> str:
"""Read the content of a webpage"""
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
text_content = soup.get_text()
return text_content[:5000]

Create the Agents

The agents created below are the “workers” who will collaborate to complete the task.

# First agent: the love poem manager
manager = Agent(
role = "Love Poem Manager",
goal = "Manage the process of creating a love poem for the partner of the client",
verbose = True,
backstory = "With a mind for romance, I want to deliver the best custom love poem to my client to give to their partner for Valentines Day",
allow_delegation = True,
max_iter = 10,
max_rpm = 20,
)
# Second agent: the love poem searcher
researcher = Agent(
role = "Love Poem Searcher",
goal = f"Find a love poem for my partner, who really likes {topic}",
verbose = True,
backstory = """You are a curious love poem searcher, and you want to
find the best and most custom love poem for your client to give to their partner,
therefore you need some details about the things that the partner really likes! You will search for these details.""",
)
# Third agent: the love poem writer
writer = Agent(
role = "Love Poem Writer",
goal = f"Write a love poem for my partner, who really likes {topic}",
verbose = True,
backstory = """You are a creative love poem writer, and you want to
write the best and most custom love poem for your client to give to their partner.
With a mind for romance and a deep love for poetry, you find the perfect poetry style
for your client and create a wonderful poem that conveys love, respect, kindness, and admiration""",
)

Define the Tasks

These are the research tasks that are needed to complete the project.

# Task 1: List ideas for a love poem

list_ideas = Task(
description = "List of 5 cute ideas for a love poem, including possible poetry styles",
expected_output = "Bullet point of 5 cute ideas for a love poem",
tools=[search_tool, ContentTools().read_content],
agent = researcher,
async_execution=True,
)
# Task 2: List important facts related to topic

list_facts = Task(
description = f"List of 5 important facts specifically for someone that likes {topic}",
expected_output = "Bullet point of 5 important facts related to the given topic for a love poem",
tools=[search_tool, ContentTools().read_content],
agent = researcher,
async_execution=True,
)
# Task 3: Write the love poem

write_poem = Task(
description = f"Write a poem that would make Romeo and Juliet blush. The poem should be custom and tailored to the client's partner, who really likes {topic}",
expected_output = "A lovely poem in a style that the client would like",
tools=[search_tool],
agent = writer,
context=[list_ideas, list_facts],
callback=callback_function,
)
# Task 4: Coordinate the love poem

manager_task = Task(
description=f"""Oversee the creation of a great love poem for the client,
a lovebird who's looking for a custom poem for their partner, who really likes {topic}.
Ensure that the poem is custom and tailored to the client's partner and in a style that
is appropriate and conveys love, respect, kindness, and admiration""",
expected_output=f"A lovely poem in a style that the client would like that incoporates the ideas and facts related to the {topic}",
agent=manager,
)

Form the Crew

You may want to change this to use a cheaper model, or use Ollama or LM Studio if you’re concerned about costs.

This code combines the agents (the workers doing the tasks) with the defined tasks that compose the project.

crew = Crew(
agents = [manager, researcher, writer],
tasks = [list_ideas, list_facts, write_poem, manager_task],
process = Process.hierarchical,
manager_llm=ChatOpenAI(temperature=0, model="gpt-4"),
)

Step 3: Run the Crew

Now we can run the Crew and create our love poem!

result = crew.kickoff()

print(result)

Enjoy the Poem!

This is the result of the Crew:

Here is the poem titled "Swift Sunset Love":

In the golden twilight, where the sea meets the sky,
Hand in hand, we walk, as the sun begins to die.
Your laughter, a melody, echoing Taylor's sweet refrain,
Each sunset with you, a salve for life's mundane pain.

Through the rhythmic waves, our love story unfolds,
As enchanting and timeless, as the classics of old.
Under the canvas of the dusk, painted in hues so bright,
Our shadows dance, in the soft and fading light.

In the silence, your gaze speaks volumes to me,
Like a love-struck Romeo, beneath Juliet's balcony.
In your eyes, I see a love, strong and unwavering,
Like a lighthouse, guiding me, forever anchoring.

Our footprints in the sand, a testament to our journey,
A tale of enduring love, of you and me.
As the sun dips low, surrendering to the night,
Our hearts echo Taylor's words, "you are in love, right?"

With each wave that kisses the shore, with the moon up above,
We are reminded of Swift's tales, of longing and love.
In the quiet of the night, under the vast starry dome,
We find our 'Love Story', in the lyrics of her songs.

In the twilight, by the sea, we found our rhythm,
In the serenity of the beach, our love was given.
And as the day surrenders to the night's gentle cover,
We hold on to each other, knowing 'You belong with me, Lover'.

This poem, a testament to our love, so rare and swift,
A melody, a sunset, a precious gift.
In this moment, and in a thousand sunsets to come,
We'll celebrate our love, under the setting sun.

I hope you enjoyed this brief introduction to Crew AI and the myriad possibilities it offers in orchestrating role-playing autonomous AI agents to write personalized love poems or perform a multitude of other tasks!

--

--