import os
import dotenv
from composio_langchain import Action, App, ComposioToolSet
from crewai import Agent, Crew, Process, Task
from langchain_groq import ChatGroq
from datetime import datetime
dotenv.load_dotenv()
toolset = ComposioToolSet()
gmail_tools = toolset.get_tools(apps=[App.GMAIL])
llm = ChatGroq(model="llama3.1-70b-versatile", stop_sequences=["\n\n"])
email_fetcher_agent = Agent(
role="Email Fetcher Agent",
goal="Fetch recent newsletter emails from the inbox.",
verbose=True,
memory=True,
backstory=f"You are an expert in retrieving and organizing email content. Today's date is {datetime.now().strftime('%B %d, %Y')}.",
llm=llm,
allow_delegation=False,
tools=gmail_tools,
)
summarizer_agent = Agent(
role="Summarizer Agent",
goal="Summarize the content of newsletter emails.",
verbose=True,
memory=True,
backstory=f"You are an expert in analyzing and summarizing complex information. Today's date is {datetime.now().strftime('%B %d, %Y')}.",
llm=llm,
allow_delegation=False,
tools=[],
)
email_sender_agent = Agent(
role="Email Sender Agent",
goal="Send the summarized newsletter content via email.",
verbose=True,
memory=True,
backstory=f"You are an expert in composing and sending emails. Today's date is {datetime.now().strftime('%B %d, %Y')}.",
llm=llm,
allow_delegation=False,
tools=gmail_tools,
)
fetch_emails_task = Task(
description="Fetch the most recent newsletter emails from the inbox.",
expected_output="A detailed list of recent newsletter emails with their content.",
tools=gmail_tools,
agent=email_fetcher_agent,
)
summarize_emails_task = Task(
description="Summarize the content of the fetched newsletter emails.",
expected_output="A comprehensive summary of the newsletter emails.",
agent=summarizer_agent,
context=[fetch_emails_task],
)
send_summary_task = Task(
description="Compose and send an email containing the summarized newsletter content.",
expected_output="Confirmation that the summary email has been sent.",
tools=gmail_tools,
agent=email_sender_agent,
context=[summarize_emails_task],
)
crew = Crew(
agents=[email_fetcher_agent, summarizer_agent, email_sender_agent],
tasks=[fetch_emails_task, summarize_emails_task, send_summary_task],
process=Process.sequential,
)
result = crew.kickoff()
print("Newsletter Summary Process Completed:")
print(result)