In today’s Story, I will show you how to create a powerful AI agent with Crewai, Shared memory across agents, and Grop API.
This Story is highly requested because many of you have created awesome Crewai agents.
The issue is that speeding up LLM open source and avoiding Agent memory history deletion can be an extremely painful process.
So I wanted to create this video to walk you through the easiest method that I found, so you can get things set up and improve your AI chatbot performance.
As always, I will be giving away the code and everything you learn in this at the end so that you can take this code and start to use it in your own business
So let me give you a quick demo of an example of a live chatbot that’s using the strategy we are going deploy in this video and you will get a sense of what I mean.
Before we start! 🦸🏻♀
If you like this topic and you want to support me:
- Clap my article 50 times; that will really help me out.👏
- Follow me on Medium and subscribe for Free to get my latest article🫶
- Follow me on my YouTube channel
What is Groq?
Groq is the creator of the world’s first Language Processing Unit (LPU), providing exceptional speed performance for AI workloads running on their LPU Inference Engine.
Why Groq is so much faster Than GPUs For LLm?
The LPU is designed to overcome the two LLM bottlenecks: compute density and memory bandwidth. An LPU has greater compute capacity than a GPU and CPU regarding LLMs. This reduces the amount of time per word calculated, allowing text sequences to be generated much faster.
How to access API?
To use LLMs from Groq, let’s go to their platform and log in once we logged in, we click on the ‘Create API’ button Next, assign a name to the API key and click “Submit” to create a new API Key.
Shared memory
Adding memory to the agents and tools enables them to work better with the ability to use the chat history of the model.
With memory, the agent can efficiently decide which tool to deploy and when. It is preferred to use the “ReadOnlyMemory” for both agents and tools so they won’t be able to modify it.
Before we dive into our application, we will create an ideal environment for the code to work. For this, we need to install the necessary Python libraries needed. Firstly, we will start by installing the libraries that support the model. For this, we will do a pip install of the below libraries.
Else Read : CrewAi + Solar/Hermes + Langchain + Ollama = Super Ai Agent
pip install -r requirements.txt
We initiate the code by importing classes from
crewai: enable AI agents to assume roles, share goals, and operate in a cohesive unit
langchain_groq: providing free-to-use API endpoints to the Large Language Models
langchain_community: making them ready to use in any LangChain application.
from crewai import Agent, Task, Crew, Process
from langchain_groq import ChatGroq
from langchain.agents import Tool
from langchain.chains import LLMChain
from langchain.memory import ConversationBufferMemory, ReadOnlySharedMemory
from langchain.prompts import PromptTemplate
from langchain_community.tools import DuckDuckGoSearchRun
Groq is even compatible with LangChain. To begin using Groq in LangChain, Create a new ChatGroq object named llm
- Set the temperature parameter to 0, indicating that the responses should be more predictable, and paste your API
- Sets the model_name parameter to “mixtral-8x7b-32768“, specifying the language model to use
llm = ChatGroq(temperature=0, groq_api_key="Your-API-Key", model_name="mixtral-8x7b-32768")
Configure the template for the agent to perform tasks as soon as the user provides the input. After that, add the “ConversationBufferMemory()” to store the chat history of the model and initialize the “ReadOnlyMemory” for the agents and their tools.
Once the agent is set to execute the tools, a model with ReadOnlyMemory is the preferred way to build and execute the chains to fetch answers and the process it
template = """This is a conversation between a human and ai agent:
{chat_history}
Write a summary of the conversation for {input}:
"""
prompt = PromptTemplate(input_variables=["input", "chat_history"], template=template)
memory = ConversationBufferMemory(memory_key="chat_history")
readonlymemory = ReadOnlySharedMemory(memory=memory)
summary_chain = LLMChain(
llm=llm,
prompt=prompt,
verbose=True,
memory=readonlymemory, # use the read-only memory to prevent the tool from modifying the memory
)
Now, The “Search” tool is associated with the DuckDuckGoSearchRun class, which is used for performing web searches using the DuckDuckGo search engine.
Defines two lists of “tool” objects: tool_use
and tool_use_1
. These lists contain information about available tools or functions that can be used within a larger program.
- Each tool object has three properties:
name
: A descriptive string representing the tool’s name (e.g., “Search”, “Summary”).func
: A reference to the function or method that executes the tool’s actions.description
: A brief description of the tool’s purpose and usage.
The “Summary” tool is associated with the summary_chain.run function, which is responsible for generating summaries of conversations.
#create searches
search = DuckDuckGoSearchRun()
tool_use = [
Tool(
name="Search",
func=search.run,
description="useful for when you need to answer questions about current events",
),
Tool(
name="Summary",
func=summary_chain.run,
description="useful for when you summarize a conversation. The input to this tool should be a string, representing who will read this summary.",
)
]
tool_use_1 = [
Tool(
name="Summary",
func=summary_chain.run,
description="useful for when you summarize a conversation. The input to this tool should be a string, representing who will read this summary.",
)
]
CrewAI has a concept of the Agent
, a highly customizable entity designed to perform specific roles and tasks. Each agent can be tailored with unique attributes, goals, and tools, making CrewAI adaptable to diverse scenarios and requirements. and Both agents have access to a memory store (store_memory
) for storing information.
All three agents use the same language model and have the verbose
attribute set to True
, which means they will provide more detailed responses.
The tool_use and tool_use_1 lists contain the tools available to each agent. The email_author has access to both the ‘Search’ and ‘Summary’ tools, while the marketing_strategist and content_specialist only have access to the ‘Summary’ tool for sharing the memory
# Define Agents
email_author = Agent(
role='Professional Email Author',
goal='Craft concise and engaging emails',
backstory='Experienced in writing impactful marketing emails.',
verbose=True,
allow_delegation=False,
llm=chat,
tools=tool_use
)
marketing_strategist = Agent(
role='Marketing Strategist',
goal='Lead the team in creating effective cold emails',
backstory='A seasoned Chief Marketing Officer with a keen eye for standout marketing content.',
verbose=True,
allow_delegation=True,
llm=chat,
tools=tool_use_1
)
content_specialist = Agent(
role='Content Specialist',
goal='Critique and refine email content',
backstory='A professional copywriter with a wealth of experience in persuasive writing.',
verbose=True,
allow_delegation=False,
llm=chat,
tools=tool_use_1
)
- defines a task related to creating and refining two versions of a cold email promoting a video editing solution, with specific steps for evaluation, feedback incorporation, and finalization of the emails.
- The
marketing_strategist
object is assigned as the agent responsible for overseeing and managing this task.
# Define Task
email_task = Task(
description='''1. Generate two distinct variations of a cold email promoting a video editing solution.
2. Evaluate the written emails for their effectiveness and engagement.
3. Scrutinize the emails for grammatical correctness and clarity.
4. Adjust the emails to align with best practices for cold outreach. Consider the feedback
provided to the marketing_strategist.
5. Revise the emails based on all feedback, creating two final versions.''',
agent=marketing_strategist # The Marketing Strategist is in charge and can delegate
- defines a task called
email_task
that involves creating, evaluating, and refining two variations of a cold email for promoting a video editing solution. - The task is assigned to an agent email_author, marketing_strategist, and content_specialist for executing the task.
# Create a Single Crew
email_crew = Crew(
agents=[email_author, marketing_strategist, content_specialist],
tasks=[email_task],
verbose=True,
process=Process.sequential
)
# Execution Flow
print("Crew: Working on Email Task")
emails_output = email_crew.kickoff()
The Results
Crew: Working on Email Task
[DEBUG]: Working Agent: Marketing Strategist
[INFO]: Starting Task: 1. Generate two distinct variations of a cold email promoting a video editing solution.
2. Evaluate the written emails for their effectiveness and engagement.
3. Scrutinize the emails for grammatical correctness and clarity.
4. Adjust the emails to align with best practices for cold outreach. Consider the feedback
provided to the marketing_strategist.
5. Revise the emails based on all feedback, creating two final versions.
> Entering new CrewAgentExecutor chain...
Thought: Do I need to use a tool? Yes
Action: Ask question to co-worker
Action Input: Marketing Strategist|What are the key features and benefits of our video editing solution that we should highlight in the cold emails?|Current Task
> Entering new CrewAgentExecutor chain...
Thought: Do I need to use a tool? Yes
Action: Summary
Action Input: This task requires summarizing the key features and benefits of our video editing solution for cold emails.
> Entering new LLMChain chain...
Prompt after formatting:
This is a conversation between a human and ai Agent:
Write a summary of the conversation for This task requires summarizing the key features and benefits of our video editing solution for cold emails.
:
> Finished chain.
The AI Agent and human discussed the key features and benefits of a video editing solution for use in cold emails. The solution includes a user-friendly interface, efficient editing tools, and the ability to handle high-quality video content. It also offers customizable templates, collaboration capabilities, and integrations with popular platforms. The benefits include saving time and resources, improved communication, and the ability to create professional-looking videos without the need for extensive video editing skills. Overall, the solution aims to help businesses enhance their outreach efforts and effectively engage with their audience through compelling video content.Do I need to use a tool? No
Final Answer: The key features of our video editing solution that we should highlight in the cold emails include a user-friendly interface, efficient editing tools, handling of high-quality video content, customizable templates, collaboration capabilities, and integrations with popular platforms. The benefits to emphasize are saving time and resources, improved communication, and the ability to create professional-looking videos without requiring extensive video editing skills. This solution aims to assist businesses in enhancing their outreach efforts and effectively engaging with their audience through compelling video content.
> Finished chain.
The key features of our video editing solution that we should highlight in the cold emails include a user-friendly interface, efficient editing tools, handling of high-quality video content, customizable templates, collaboration capabilities, and integrations with popular platforms. The benefits to emphasize are saving time and resources, improved communication, and the ability to create professional-looking videos without requiring extensive video editing skills. This solution aims to assist businesses in enhancing their outreach efforts and effectively engaging with their audience through compelling video content.Do I need to use a tool? Yes
Action: Summary
Action Input: Marketing Strategist|Current Task
> Entering new LLMChain chain...
Prompt after formatting:
This is a conversation between a human and ai Agent:
Write a summary of the conversation for Marketing Strategist|Current Task:
> Finished chain.
The conversation is between a human and an AI agent, and the task at hand for the human is that of a marketing strategist. However, the specifics of the current task are not provided in the text. The human and the AI agent may be discussing various marketing strategies, analyzing data, or brainstorming ideas to achieve marketing goals. The summary of the conversation would depend on the actual content of the discussion, which is not provided.Do I need to use a tool? Yes
Action: Ask question to co-worker
Action Input: Content Specialist|Can you provide two distinct variations of a cold email promoting our video editing solution, highlighting the key features and benefits discussed earlier?|Current Task
> Entering new CrewAgentExecutor chain...
Thought: Do I need to use a tool? No
Final Answer:
Variation 1:
Subject: Elevate Your Video Content with Our Cutting-Edge Solution
Dear [Recipient's Name],
In today's digital landscape, video content is king. But creating high-quality, engaging videos can be time-consuming and complex. That's where we come in. Our video editing solution simplifies the process, allowing you to produce professional-grade videos in a fraction of the time.
Key Features:
1. User-Friendly Interface: Our platform is designed for users of all skill levels, making video editing a breeze.
2. Customizable Templates: Save time with our library of customizable templates, tailored to your brand.
3. Advanced Editing Tools: Take your videos to the next level with our robust suite of editing tools.
4. Seamless Collaboration: Work with your team in real-time, ensuring consistent branding and messaging.
Variation 2:
Subject: Transform Your Video Content in Minutes
Dear [Recipient's Name],
Are you looking to create stunning video content that captivates your audience and drives results? Our video editing solution is here to help. With our platform, you can produce high-quality videos in just a few clicks.
Key Features:
1. Intuitive Design: Our platform is easy to navigate, so you can start creating immediately.
2. Pre-Built Themes: Choose from a variety of professionally designed themes to match your brand.
3. Powerful Editing Tools: Enhance your videos with our state-of-the-art editing tools.
4. Real-Time Feedback: Collaborate with your team and get instant feedback, ensuring your videos are always on-brand and effective.
This is the summary of your work so far:
The task required creating two distinct variations of a cold email promoting a video editing solution. The first variation, "Elevate Your Video Content with Our Cutting-Edge Solution," focuses on the time-saving and professional aspects of the solution. The second variation, "Transform Your Video Content in Minutes," emphasizes the ease of use and the ability to create stunning videos quickly. Both variations highlight the key features of the platform, including the user-friendly interface, customizable templates, advanced editing tools, and real-time collaboration.
> Finished chain.
Variation 1:
Subject: Elevate Your Video Content with Our Cutting-Edge Solution
Dear [Recipient's Name],
In today's digital landscape, video content is king. But creating high-quality, engaging videos can be time-consuming and complex. That's where we come in. Our video editing solution simplifies the process, allowing you to produce professional-grade videos in a fraction of the time.
Key Features:
1. User-Friendly Interface: Our platform is designed for users of all skill levels, making video editing a breeze.
2. Customizable Templates: Save time with our library of customizable templates, tailored to your brand.
3. Advanced Editing Tools: Take your videos to the next level with our robust suite of editing tools.
4. Seamless Collaboration: Work with your team in real-time, ensuring consistent branding and messaging.
Variation 2:
Subject: Transform Your Video Content in Minutes
Dear [Recipient's Name],
Are you looking to create stunning video content that captivates your audience and drives results? Our video editing solution is here to help. With our platform, you can produce high-quality videos in just a few clicks.
Key Features:
1. Intuitive Design: Our platform is easy to navigate, so you can start creating immediately.
2. Pre-Built Themes: Choose from a variety of professionally designed themes to match your brand.
3. Powerful Editing Tools: Enhance your videos with our state-of-the-art editing tools.
4. Real-Time Feedback: Collaborate with your team and get instant feedback, ensuring your videos are always on-brand and effective.
This is the summary of your work so far:
The task required creating two distinct variations of a cold email promoting a video editing solution. The first variation, "Elevate Your Video Content with Our Cutting-Edge Solution," focuses on the time-saving and professional aspects of the solution. The second variation, "Transform Your Video Content in Minutes," emphasizes the ease of use and the ability to create stunning videos quickly. Both variations highlight the key features of the platform, including the user-friendly interface, customizable templates, advanced editing tools, and real-time collaboration.Do I need to use a tool? No
Final Answer: Here are the two distinct variations of a cold email promoting our video editing solution:
Variation 1:
Subject: Elevate Your Video Content with Our Cutting-Edge Solution
Dear [Recipient's Name],
In today's digital landscape, video content is king. But creating high-quality, engaging videos can be time-consuming and complex. That's where we come in. Our video editing solution simplifies the process, allowing you to produce professional-grade videos in a fraction of the time.
Key Features:
- User-Friendly Interface: Our platform is designed for users of all skill levels, making video editing a breeze.
- Customizable Templates: Save time with our library of customizable templates, tailored to your brand.
- Advanced Editing Tools: Take your videos to the next level with our robust suite of editing tools.
- Seamless Collaboration: Work with your team in real-time, ensuring consistent branding and messaging.
Variation 2:
Subject: Transform Your Video Content in Minutes
Dear [Recipient's Name],
Are you looking to create stunning video content that captivates your audience and drives results? Our video editing solution is here to help. With our platform, you can produce high-quality videos in just a few clicks.
Key Features:
- Intuitive Design: Our platform is easy to navigate, so you can start creating immediately.
- Pre-Built Themes: Choose from a variety of professionally designed themes to match your brand.
- Powerful Editing Tools: Enhance your videos with our state-of-the-art editing tools.
- Real-Time Feedback: Collaborate with your team and get instant feedback, ensuring your videos are always on-brand and effective.
These emails highlight the key features and benefits of the video editing solution, addressing the need for time-efficiency, ease of use, and professional-quality results. The language and tone in both variations are engaging and persuasive, encouraging the recipient to consider the solution for their video content needs.
> Finished chain.
[DEBUG]: [Marketing Strategist] Task output: Here are the two distinct variations of a cold email promoting our video editing solution:
Variation 1:
Subject: Elevate Your Video Content with Our Cutting-Edge Solution
Dear [Recipient's Name],
In today's digital landscape, video content is king. But creating high-quality, engaging videos can be time-consuming and complex. That's where we come in. Our video editing solution simplifies the process, allowing you to produce professional-grade videos in a fraction of the time.
Key Features:
- User-Friendly Interface: Our platform is designed for users of all skill levels, making video editing a breeze.
- Customizable Templates: Save time with our library of customizable templates, tailored to your brand.
- Advanced Editing Tools: Take your videos to the next level with our robust suite of editing tools.
- Seamless Collaboration: Work with your team in real-time, ensuring consistent branding and messaging.
Variation 2:
Subject: Transform Your Video Content in Minutes
Dear [Recipient's Name],
Are you looking to create stunning video content that captivates your audience and drives results? Our video editing solution is here to help. With our platform, you can produce high-quality videos in just a few clicks.
Key Features:
- Intuitive Design: Our platform is easy to navigate, so you can start creating immediately.
- Pre-Built Themes: Choose from a variety of professionally designed themes to match your brand.
- Powerful Editing Tools: Enhance your videos with our state-of-the-art editing tools.
- Real-Time Feedback: Collaborate with your team and get instant feedback, ensuring your videos are always on-brand and effective.
These emails highlight the key features and benefits of the video editing solution, addressing the need for time-efficiency, ease of use, and professional-quality results. The language and tone in both variations are engaging and persuasive, encouraging the recipient to consider the solution for their video content needs.
Else See : AutoGen + LangChian + SQLite + Function Schema = Super AI Chabot
Conclusion
To add memory to both an agent and its tools in Crewai, install the modules to get their dependencies and import libraries from them. After that, build the conversation memory, language model, tools, and the agent to add the memory.
The recommended method to add the memory is using the ReadOnlyMemory to the agent and its tools to store the chat history. The user can also use the conversational memory for both agents and tools. But, they get confused sometimes and change the chats in the memory.
where to find the requirements.txt?
in my GitHub