Newsletter Summarizer
Newsletter Summarizer GitHub Repository
Explore the complete source code for the Newsletter Summarizer project. This repository contains all the necessary files and scripts to set up and run the Newsletter Summarizer using Composio.
1
Install the required packages
1 pip install composio-crewai langchain-openai
Create a .env file and add your API keys.
2
Import base packages
Next, we’ll import the essential libraries for our project.
1 import os 2 from datetime import datetime, timedelta 3 4 from composio_crewai import App, ComposioToolSet 5 from crewai import Agent, Crew, Process, Task 6 from dotenv import load_dotenv 7 from langchain_openai import ChatOpenAI
3
Configure environments and tools
Set up the necessary configurations for our agents and tools.
1 load_dotenv() 2 3 # Initialize the language model 4 llm = ChatOpenAI(model="gpt-4o") 5 6 # Set up Composio tools 7 composio_toolset = ComposioToolSet() 8 9 # Get Gmail tools 10 gmail_tools = composio_toolset.get_tools(apps=[App.GMAIL]) 11 12 # Get today's date and the date from 7 days ago 13 today = datetime.today().strftime("%Y/%m/%d") 14 week_ago = (datetime.today() - timedelta(days=7)).strftime("%Y/%m/%d")
4
Define agents and tasks
Create the agents and tasks needed for summarizing newsletters.
1 # Define the Email Fetcher Agent 2 email_fetcher = Agent( 3 role="Email Fetcher", 4 goal="Fetch all newsletter emails", 5 backstory="""You are an Email Fetcher specialized in finding newsletter emails. 6 Your job is to identify and retrieve recent newsletter emails from the user's inbox.""", 7 verbose=True, 8 tools=gmail_tools, 9 llm=llm 10 ) 11 12 # Define the Email Summarizer Agent 13 summarizer = Agent( 14 role="Newsletter Summarizer", 15 goal="Create concise summaries of newsletter content", 16 backstory="""You are a Newsletter Summarizer who excels at distilling information. 17 You can read newsletter emails and create brief, informative summaries of their main points.""", 18 verbose=True, 19 llm=llm 20 ) 21 22 # Define the Email Sender Agent 23 email_sender = Agent( 24 role="Email Sender", 25 goal="Send email with newsletter summaries", 26 backstory="""You are an Email Sender who specializes in composing and sending emails. 27 You take summaries and convert them into well-formatted emails before sending them to recipients.""", 28 verbose=True, 29 tools=gmail_tools, 30 llm=llm 31 ) 32 33 # Define tasks for each agent 34 fetch_task = Task( 35 description=f"""Search for newsletter emails in the inbox from {week_ago} to {today}. 36 Return a list of emails with their subjects, senders, and content.""", 37 expected_output="A list of newsletter emails with their content", 38 agent=email_fetcher 39 ) 40 41 summarize_task = Task( 42 description="""Create a concise summary of each newsletter email. 43 Identify the key points, announcements, and insights from each newsletter. 44 Format your summaries neatly with bullet points.""", 45 expected_output="Summarized content from all newsletters", 46 agent=summarizer, 47 context=[fetch_task] 48 ) 49 50 send_task = Task( 51 description="""Compose an email with the newsletter summaries. 52 Send it to youremail@example.com with the subject 'Weekly Newsletter Digest'. 53 Format the email in a reader-friendly way with headings for each newsletter.""", 54 expected_output="Confirmation that the email was sent", 55 agent=email_sender, 56 context=[summarize_task] 57 )
5
Execute the workflow
Run the summarization process and handle the results.
1 # Create and run the crew 2 crew = Crew( 3 agents=[email_fetcher, summarizer, email_sender], 4 tasks=[fetch_task, summarize_task, send_task], 5 verbose=2, 6 process=Process.sequential 7 ) 8 9 # Execute the workflow 10 result = crew.kickoff() 11 print(f"Result: {result}")
Complete Code
1 import os 2 from datetime import datetime, timedelta 3 4 from composio_crewai import App, ComposioToolSet 5 from crewai import Agent, Crew, Process, Task 6 from dotenv import load_dotenv 7 from langchain_openai import ChatOpenAI 8 9 # Load environment variables 10 load_dotenv() 11 12 # Initialize the language model 13 llm = ChatOpenAI(model="gpt-4o") 14 15 # Set up Composio tools 16 composio_toolset = ComposioToolSet() 17 18 # Get Gmail tools 19 gmail_tools = composio_toolset.get_tools(apps=[App.GMAIL]) 20 21 # Get today's date and the date from 7 days ago 22 today = datetime.today().strftime("%Y/%m/%d") 23 week_ago = (datetime.today() - timedelta(days=7)).strftime("%Y/%m/%d") 24 25 # Define the Email Fetcher Agent 26 email_fetcher = Agent( 27 role="Email Fetcher", 28 goal="Fetch all newsletter emails", 29 backstory="""You are an Email Fetcher specialized in finding newsletter emails. 30 Your job is to identify and retrieve recent newsletter emails from the user's inbox.""", 31 verbose=True, 32 tools=gmail_tools, 33 llm=llm 34 ) 35 36 # Define the Email Summarizer Agent 37 summarizer = Agent( 38 role="Newsletter Summarizer", 39 goal="Create concise summaries of newsletter content", 40 backstory="""You are a Newsletter Summarizer who excels at distilling information. 41 You can read newsletter emails and create brief, informative summaries of their main points.""", 42 verbose=True, 43 llm=llm 44 ) 45 46 # Define the Email Sender Agent 47 email_sender = Agent( 48 role="Email Sender", 49 goal="Send email with newsletter summaries", 50 backstory="""You are an Email Sender who specializes in composing and sending emails. 51 You take summaries and convert them into well-formatted emails before sending them to recipients.""", 52 verbose=True, 53 tools=gmail_tools, 54 llm=llm 55 ) 56 57 # Define tasks for each agent 58 fetch_task = Task( 59 description=f"""Search for newsletter emails in the inbox from {week_ago} to {today}. 60 Return a list of emails with their subjects, senders, and content.""", 61 expected_output="A list of newsletter emails with their content", 62 agent=email_fetcher 63 ) 64 65 summarize_task = Task( 66 description="""Create a concise summary of each newsletter email. 67 Identify the key points, announcements, and insights from each newsletter. 68 Format your summaries neatly with bullet points.""", 69 expected_output="Summarized content from all newsletters", 70 agent=summarizer, 71 context=[fetch_task] 72 ) 73 74 send_task = Task( 75 description="""Compose an email with the newsletter summaries. 76 Send it to youremail@example.com with the subject 'Weekly Newsletter Digest'. 77 Format the email in a reader-friendly way with headings for each newsletter.""", 78 expected_output="Confirmation that the email was sent", 79 agent=email_sender, 80 context=[summarize_task] 81 ) 82 83 # Create and run the crew 84 crew = Crew( 85 agents=[email_fetcher, summarizer, email_sender], 86 tasks=[fetch_task, summarize_task, send_task], 87 verbose=2, 88 process=Process.sequential 89 ) 90 91 # Execute the workflow 92 result = crew.kickoff() 93 print(f"Result: {result}")