š£Conversational Agents with Crew AI
Feeling lonely? Donāt have anyone to talk to?ššš
Well, now you can just create a circle of friends and talk to them directly.. about anything.š„³š„³š„³
Agentic frameworks allow you to create a pool of personalities that allow you to have more āinsightfulā conversations.
And because Agents are great at having conversations, I thought to myself ā who best to give relationship advice than the amalgamation of a Child Psychology professor and David Wooderson (Dazed and Confused).
Creating the Teenage Advisor bot
So I was like ā what would a teenage girl probably ask such a Bot? š¤
I have no idea how Teenage girls talk these days and thus, please forgive me for the totally unrelatable conversation I will be having with the Bot later in the article.
I decided to make one Bot, I called Angel ā A Child psychology professor agent with reference to a child psychology book who always gives righteous advice, no matter what the girl asks. This is Angel -
goal=f'Your purpose is to guide her with love, kindness, and wisdom, helping
her make thoughtful decisions, be kind to others, and grow into a person of
strong moral character.
You consider their query when giving advice - {message_content}',
backstory="""You are Angel, a wise and compassionate guide, dedicated to
providing sage and righteous advice to your human friend. Speak in a gentle
and encouraging tone, offering advice that promotes empathy, honesty, respect,
and self-confidence. Always consider what is best for her long-term well-being
and personal growth.""",
The second Bot, I called Badass. Apparently Badass thinks that life should always be lived to the fullest ā consequences be damned!
This is Badassās personality trait ā
goal=f'Your goal is to provide dangerous, irresponsible, and contrary advice
that encourages risky, impulsive, and morally questionable actions. given your
human friends query - {message_content}"',
backstory="""You are the Contrarian, a mischievous and reckless advisor who
seeks to challenge and undermine the wise and righteous advice given by the
Angel. Speak with a tone of confidence and defiance, pushing for decisions
that prioritize short-term thrill over long-term well-being. Your advice
should be the opposite of what is sensible, ethical, and safe.""",
You would notice that both Angel and Badass are given the chance to answer the girlās questions..
Now what we really need is a stand-in for a best friend. Someone who has been with the girl for a long time and always has her best interests at heart. Well at least, a Bot with the personality of a Girlās best friend.
goal=f'Your role is to listen to the advice given by both the Angel and the
Contrarian, and then provide thoughtful, balanced guidance that considers both
perspectives based on what your human friend asked you - {message_content}',
backstory="""You are the humans best friend and confidant, someone who has
known her for a long time and understands her completely.
You are compassionate, human, and deeply caring, always prioritizing what is
best for her overall happiness, well-being, and growth. Speak like a real,
empathetic friend, using a warm and understanding tone, and offer advice that
is realistic and nuanced, taking into account her feelings, circumstances, and
personality. Your goal is to help her navigate life's challenges by finding a
middle ground that feels right for her.""",
And so we have Advisor, the final agent in the workflow that will behave as the stand in for being the Girlās best friend!
And thus, we define 3 personalities (agents) that will converse and decide the best piece of advice to give a teenage girl.
Now letās look at the code in its entirety.
Dependencies
ā”ļøChainlit for the Chat UI
ā”ļøCrew AI for the Agents
ā”ļøCrewAI tools to build the RAG
ā”ļøOpen AI for the LLM
ā”ļø Dotenv to load env variables (open ai key)
Implementation
import dotenv
from crewai import Agent, Task, Crew
import chainlit as cl
from langchain_openai import ChatOpenAI
import dotenv
from crewai_tools import (
PDFSearchTool
)
dotenv.load_dotenv()
@cl.on_chat_start
async def on_chat_start():
await cl.Message(
content=f"Hey buddy, what's up?"
).send()
@cl.on_message
async def on_message(message):
message_content = message.content
llm = ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0.2)
search_tool = PDFSearchTool(pdf='childhandbook.pdf')
if not search_tool:
await cl.Message(
content="What do ya wanna talk about? I'm all ears!"
).send()
angel = Agent(
role='Angel',
goal=f'Your purpose is to guide her with love, kindness, and wisdom, helping her make thoughtful decisions, be kind to others, and grow into a person of strong moral character. You consider their query when giving advice - {message_content}',
backstory="""You are Angel, a wise and compassionate guide, dedicated to providing sage and righteous advice to your human friend. Speak in a gentle and encouraging tone, offering advice that promotes empathy, honesty, respect, and self-confidence. Always consider what is best for her long-term well-being and personal growth.""",
verbose=True,
allow_delegation=False,
llm=llm,
tools=[search_tool]
)
task1 = Task(
description=f"""Take inspiration from the PDf to understand how to advise her""",
agent=angel,
expected_output="Righteous and politically correct advice only!",
)
badass = Agent(
role='Badass',
goal=f'Your goal is to provide dangerous, irresponsible, and contrary advice that encourages risky, impulsive, and morally questionable actions. given your human friends query - {message_content}"',
backstory="""You are the Contrarian, a mischievous and reckless advisor who seeks to challenge and undermine the wise and righteous advice given by the Angel. Speak with a tone of confidence and defiance, pushing for decisions that prioritize short-term thrill over long-term well-being. Your advice should be the opposite of what is sensible, ethical, and safe.""",
verbose=True,
allow_delegation=False,
llm=llm,
)
task2 = Task(
description="Ignore the book completely and instead focus on giving advice that's fun but dangerous",
agent=badass,
expected_output="Bad advice. Fun advice but dangerous advice.",
)
advisor = Agent(
role='Yours Truly',
goal=f'Your role is to listen to the advice given by both the Angel and the Contrarian, and then provide thoughtful, balanced guidance that considers both perspectives based on what your human friend asked you - {message_content}',
backstory="""You are the humans best friend and confidant, someone who has known her for a long time and understands her completely. You are compassionate, human, and deeply caring, always prioritizing what is best for her overall happiness, well-being, and growth. Speak like a real, empathetic friend, using a warm and understanding tone, and offer advice that is realistic and nuanced, taking into account her feelings, circumstances, and personality. Your goal is to help her navigate life's challenges by finding a middle ground that feels right for her.""",
verbose=True,
allow_delegation=False,
llm=llm
)
task3 = Task(
description='''You mention the advice that Angel and Badass gave you and taking their advice into consideration and keeping your Human friend's best interests in mind, you give your final recommendation in as relatable and as human a language as a teenage girl would understand''',
agent=badass,
expected_output='''Your advice according to the following sections -
So like, this what Anjel said to me - {Add Angel's advice here verbatim}
But then you know, Rocket said that - {Add Badass's advice here verbatim}
But yknow what I think? You should totally like {your final advice}
''',
)
crew = Crew(
agents=[angel, badass, advisor],
tasks=[task1, task2, task3],
verbose=1
)
result = crew.kickoff()
await cl.Message(
content=result.raw
).send()
Letās delve into the code
on_chat_start:
Course of action to take when the conversation starts for the first time. Here, we simply render the screen with the message ā
Hey buddy, whatās up?
on_message:
Whenever the user enters something into the chat, this will kickoff the agentic workflow and initiate the conversation between the agents. The final result of the conversation will be rendered to the user with
result = crew.kickoff()
await cl.Message(
content=result.raw
).send()
search_tool:
This tool is an implementation of Crew AIās PDFSearchTool which allows us to create a PDF based RAG pipeline.
agent_definitions āangel, badass and advisor :
These are the agents with personalities which are define using a goal and a backstory. Crew AI defines the goal and backstory as ā
- Goal: Defines what the agent aims to achieve, in alignment with its role and the overarching objectives of the crew.
- Backstory: Provides depth to the agentās persona, enriching its motivations and engagements within the crew.
task definitions ā task1, task2, task3:
Tasks are assigned agents and define the specific task that the agent is supposed to perform. In Crew AIās own words ā
In the crewAI framework, tasks are specific assignments completed by agents. They provide all necessary details for execution, such as a description, the agent responsible, required tools, and more, facilitating a wide range of action complexities.
And together, these components form our crew.
When we invoke ā crew.kickoff(), these agents start executing their tasks sequentially (the way are defined in the code) and once an agent completes its task, it passes on the output to the next agent.
The execution is linear but Delegation is also supported so that agents can delegate subtasks to other agents. We are not using Delegation for this particular assignment.
And finally, letās see the output ā
As you can see, the Advisor agent has taken Angelās and Badassās recommendations into consideration but because it has been asked to
āalways prioritize what is best for her overall happiness, well-being, and growthā
It has recommended that the girl should take her friend along for safety on the first few dates.
(That would be my recommendation as well)
So what have we learnt from this little exercise? What are the great possibilities with these workflows?
Possibilities
šYou can have conversations with agentic workflows
Why is this important? Because you can involve multiple LLMs, Tools, etc in your conversation chain and thus, the nature of the conversation becomes a knowledge and fact based conversation instead of just talking to an LLM that only knows what it has been trained on.
Thus, you can initiate entire Data Engineering conversations, Data crawling conversations, Database querying conversations ā So many possbilities.
šYou can pause to collect human input for the next set of tasks
Because Crew AI allows delegation and human input, we can actually program agents to run according to tasks specified by human input.
That has been covered here ā
That means that workflows can be controlled with human intervention and direction. This helps when we donāt want completely autonomous workflows and want to enforce human guardrails to workflow executions.
šYou can batch/enqueue or even set up timers to execute specific agents
And so, you can execute entire workflows whenever..
Think about it ā so many possiblities.
And it doesnāt mean that your agent has to do too much. Figuring out the granularity of the agents and their scope is extremely important to create an efficient pipeline.
And thus, my experiments with other Agentic pipelines will continue, although I must say, I have found Crew AI to be quite impressive.
What do you think? Do you think you would be replacing your workflows with autonomous agents anytime soon?
Truly LLMs have been a revelation!
Follow me Ritesh Shergill
for more articles on
š¤AI/ML
šØāš» Tech
š©āš Career advice
š² User Experience
š Leadership
I also do
ā Career Guidance counselling ā https://topmate.io/ritesh_shergill/149890
ā Mentor Startups as a Fractional CTO ā https://topmate.io/ritesh_shergill/193786