-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
265 lines (228 loc) Β· 9.7 KB
/
main.py
File metadata and controls
265 lines (228 loc) Β· 9.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
"""
Main AI Email Agent - Orchestrates everything
"""
import os
from pyexpat.errors import messages
from typing import List, Dict, Any
from dotenv import load_dotenv
from pydantic_ai import Agent
from openai import OpenAI
from models import EmailContent, DraftResponse
from gmail_service import GmailService
import nest_asyncio
nest_asyncio.apply()
# Load environment variables
load_dotenv()
class EmailAIAgent:
"""
Main AI agent that reads emails and generates responses
"""
def __init__(self):
"""
Initialize the AI agent and Gmail service
"""
# Initialize OpenAI client
self.openai_client = OpenAI(
api_key=os.getenv('OPENAI_API_KEY')
)
# Initialize Gmail service
self.gmail_service = GmailService()
# Create PydanticAI agent
self.agent = Agent(
model='openai:gpt-4o-mini', # You can use gpt-3.5-turbo for cheaper testing
system_prompt="""You are an intelligent email assistant. Your tasks:
1. Analyze incoming emails
2. Decide if a reply is needed
3. Draft appropriate responses
4. Use professional but friendly tone
Guidelines:
- Only reply to emails that need responses (not spam, not notifications)
- Keep responses concise but helpful
- Ask clarifying questions if needed
- Never share sensitive information
- Be polite and professional"""
)
def process_emails(self, max_emails: int = 5) -> None:
"""
Main function: Fetch unread emails, process them, send replies
"""
print("π Fetching unread emails...")
# Step 1: Get unread emails
emails = self.gmail_service.get_unread_emails(max_results=max_emails)
if not emails:
print("No unread emails found.")
return
print(f"π§ Found {len(emails)} unread email(s)")
# Step 2: Process each email
for email in emails:
print(f"\n{'='*50}")
print(f"Processing email from: {email['from']}")
print(f"Subject: {email['subject'][:50]}...")
# Step 3: Use AI to analyze and draft response
response = self._generate_response(email)
# Step 4: Send reply if needed
if response.should_reply:
print(f"π€ AI decided to reply. Reason: {response.reason}")
# Extract email address from "Name <[email protected]>" format
to_email = email['from'].split('<')[-1].split('>')[0] if '<' in email['from'] else email['from']
# Send the reply
success = self.gmail_service.send_email(
to=to_email,
subject=response.reply_subject,
body=response.reply_body
)
if success:
print("β
Reply sent successfully!")
# Mark email as read
self.gmail_service.mark_as_read(email['id'])
else:
print("β Failed to send reply")
else:
print(f"βοΈ Skipping reply. Reason: {response.reason}")
# Still mark as read if it's spam/notification
if "notification" in response.reason.lower() or "spam" in response.reason.lower():
self.gmail_service.mark_as_read(email['id'])
def _generate_response(self, email: Dict[str, Any]) -> DraftResponse:
"""
Use AI to analyze email and generate response
"""
# Prepare the email content for AI
email_content = f"""
FROM: {email['from']}
SUBJECT: {email['subject']}
BODY: {email['body'][:1000]} # Limit to first 1000 chars
"""
try:
# Use the agent to generate response
result = self.agent.run_sync(
f"""Analyze this email and decide if we should reply:
{email_content}
Provide:
1. Should we reply? (yes/no with reason)
2. If yes, draft a response
3. Subject for reply (should reference original)
4. Tone to use
"""
)
# Parse the response into our Pydantic model
# Note: In a real app, you'd use structured output from the agent
# For simplicity, we'll use a simplified approach
# For now, let's use OpenAI directly for structured output
completion = self.openai_client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "system", "content": "You are an email assistant. Respond in JSON format."},
{"role": "user", "content": f"""Analyze this email and provide response in this exact JSON format:
{{
"should_reply": true/false,
"reason": "reason for decision",
"reply_subject": "Re: [Original Subject]",
"reply_body": "drafted response here",
"tone": "professional/friendly/formal/casual"
}}
Email to analyze:
{email_content}
Rules:
- Reply to important emails only
- Don't reply to spam, newsletters, or automated notifications
- Keep responses concise
"""}
],
response_format={"type": "json_object"}
)
# Parse JSON response
import json
ai_response = json.loads(completion.choices[0].message.content)
# Convert to Pydantic model
return DraftResponse(**ai_response)
except Exception as e:
print(f"Error generating response: {e}")
# Return a default response
return DraftResponse(
should_reply=False,
reason="Error processing email with AI",
reply_subject="",
reply_body="",
tone="professional"
)
'''
def test_send_email(self, to_email: str, subject: str, body: str) -> None:
"""
Test function to send an email directly
"""
print(f"\nπ€ Testing email send to: {to_email}")
success = self.gmail_service.send_email(to_email, subject, body)
if success:
print("β
Test email sent successfully!")
else:
print("β Failed to send test email")
'''
def main():
"""
Main execution function
"""
print("π Starting Email AI Agent...")
# Check for API key
if not os.getenv('OPENAI_API_KEY'):
print("β ERROR: OPENAI_API_KEY not found in .env file")
print("Please add your OpenAI API key to the .env file")
return
# Initialize agent
agent = EmailAIAgent()
try:
#limit = input("Max emails to process (default 5): ").strip()
#limit = int(limit) if limit else 5
limit = len(agent.gmail_service.get_unread_emails())
agent.process_emails(max_emails=limit)
except ValueError:
print("Invalid number, using default 5")
agent.process_emails()
'''
# Menu for different operations
while True:
print("\n" + "="*50)
print("EMAIL AI AGENT - MAIN MENU")
print("="*50)
print("1. Process unread emails (read and reply)")
print("2. Send test email")
print("3. Check unread emails (view only)")
print("4. Exit")
choice = input("\nSelect option (1-4): ").strip()
if choice == '1':
try:
limit = input("Max emails to process (default 5): ").strip()
limit = int(limit) if limit else 5
agent.process_emails(max_emails=limit)
except ValueError:
print("Invalid number, using default 5")
agent.process_emails()
elif choice == '2':
to_email = input("Recipient email: ").strip()
subject = input("Subject: ").strip()
body = input("Body (type 'AI' for AI-generated): ").strip()
if body.upper() == 'AI':
# Generate AI response
completion = agent.openai_client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "system", "content": "You are writing a test email."},
{"role": "user", "content": f"Write a brief test email about: {subject}"}
]
)
body = completion.choices[0].message.content
agent.test_send_email(to_email, subject, body)
elif choice == '3':
emails = agent.gmail_service.get_unread_emails(max_results=10)
print(f"\nFound {len(emails)} unread emails:")
for i, email in enumerate(emails, 1):
print(f"\n{i}. From: {email['from']}")
print(f" Subject: {email['subject']}")
print(f" Preview: {email['body'][:100]}...")
elif choice == '4':
print("π Goodbye!")
break
else:
print("β Invalid choice. Please select 1-4.")
'''
if __name__ == "__main__":
main()