siddhartharya commited on
Commit
e50fdf4
1 Parent(s): e7bb3db

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -40
app.py CHANGED
@@ -6,6 +6,7 @@ import os
6
  proxycurl_api_key = os.getenv("PROXYCURL_API_KEY") # Proxycurl API key
7
  groq_api_key = os.getenv("GROQ_CLOUD_API_KEY") # Groq Cloud API key
8
  firecrawl_api_key = os.getenv("FIRECRAWL_API_KEY") # Firecrawl API key
 
9
 
10
  class AutonomousEmailAgent:
11
  def __init__(self, linkedin_url, company_name, role, word_limit, user_name, email, phone, linkedin):
@@ -22,6 +23,7 @@ class AutonomousEmailAgent:
22
  self.experiences = []
23
  self.company_info = None
24
  self.role_description = None
 
25
 
26
  # Reason and Act via LLM: Let the LLM control reasoning and actions dynamically
27
  def autonomous_reasoning(self):
@@ -78,8 +80,10 @@ class AutonomousEmailAgent:
78
  instruction = reasoning_output.lower().strip()
79
 
80
  if "scrape" in instruction:
81
- # Action: Scrape the company's website for more information
82
- self.fetch_company_info_with_firecrawl()
 
 
83
  # Reflect again by invoking the LLM to reassess
84
  return self.autonomous_reasoning()
85
 
@@ -101,6 +105,24 @@ class AutonomousEmailAgent:
101
  print("Error: Unrecognized instruction from LLM. Proceeding with available data.")
102
  return self.generate_email()
103
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
104
  # Action: Fetch LinkedIn data via Proxycurl
105
  def fetch_linkedin_data(self):
106
  if not self.linkedin_url:
@@ -124,51 +146,47 @@ class AutonomousEmailAgent:
124
  self.skills = ["Adaptable", "Hardworking"]
125
  self.experiences = ["Worked across various industries"]
126
 
127
- # Action: Fetch company information via Firecrawl API
128
- def fetch_company_info_with_firecrawl(self):
129
- if not self.company_name:
130
- print("Action: No company name provided, using default company info.")
131
- self.company_info = "A leading company in its field."
 
 
 
 
 
 
 
 
 
 
132
  else:
133
- print(f"Action: Fetching company info for {self.company_name} using Firecrawl.")
134
- headers = {"Authorization": f"Bearer {firecrawl_api_key}"}
135
- firecrawl_url = "https://api.firecrawl.dev/v1/scrape"
136
- data = {
137
- "url": f"https://{self.company_name}.com",
138
- "patterns": ["description", "about", "careers", "company overview"]
139
- }
140
-
141
- response = requests.post(firecrawl_url, json=data, headers=headers)
142
- if response.status_code == 200:
143
- firecrawl_data = response.json()
144
- self.company_info = firecrawl_data.get("description", "No detailed company info available.")
145
- print(f"Company info fetched: {self.company_info}")
146
- else:
147
- print(f"Error: Unable to fetch company info via Firecrawl. Using default info.")
148
- self.company_info = "A leading company in its field."
149
 
150
- # Final Action: Generate the email using Groq Cloud LLM
151
  def generate_email(self):
152
  print("Action: Generating the email with the gathered information.")
153
-
154
  linkedin_text = f"Please find my LinkedIn profile at {self.linkedin}" if self.linkedin else ""
155
 
156
- # Dynamic LLM prompt
157
  prompt = f"""
158
- Write a professional email applying for the {self.role} position at {self.company_name}.
159
-
160
- Use the following information:
 
 
 
 
 
 
161
  - The candidate’s LinkedIn bio: {self.bio}.
162
  - The candidate’s most relevant skills: {', '.join(self.skills)}.
163
  - The candidate’s professional experience: {', '.join([exp['title'] for exp in self.experiences])}.
164
-
165
- Please research the company's public information. If no company-specific information is available, use general knowledge about the company's industry.
166
-
167
- Tailor the email dynamically to the role of **{self.role}** at {self.company_name}, aligning the candidate's skills and experiences with the expected responsibilities of the role and the company’s operations.
168
-
169
- {linkedin_text}
170
-
171
- Remove references to job posting sources unless provided. Use the LinkedIn URL for the candidate and do not include placeholders.
172
 
173
  End the email with this signature:
174
  Best regards,
@@ -179,18 +197,18 @@ class AutonomousEmailAgent:
179
 
180
  The email should not exceed {self.word_limit} words.
181
  """
182
-
183
  url = "https://api.groq.com/openai/v1/chat/completions"
184
  headers = {
185
  "Authorization": f"Bearer {groq_api_key}",
186
  "Content-Type": "application/json",
187
  }
188
-
189
  data = {
190
  "messages": [{"role": "user", "content": prompt}],
191
  "model": "llama3-8b-8192"
192
  }
193
-
194
  response = requests.post(url, headers=headers, json=data)
195
  if response.status_code == 200:
196
  return response.json()["choices"][0]["message"]["content"].strip()
 
6
  proxycurl_api_key = os.getenv("PROXYCURL_API_KEY") # Proxycurl API key
7
  groq_api_key = os.getenv("GROQ_CLOUD_API_KEY") # Groq Cloud API key
8
  firecrawl_api_key = os.getenv("FIRECRAWL_API_KEY") # Firecrawl API key
9
+ serp_api_key = os.getenv("SERP_API_KEY") # SERP API key for fetching company URL
10
 
11
  class AutonomousEmailAgent:
12
  def __init__(self, linkedin_url, company_name, role, word_limit, user_name, email, phone, linkedin):
 
23
  self.experiences = []
24
  self.company_info = None
25
  self.role_description = None
26
+ self.company_url = None # Add company URL for scraping
27
 
28
  # Reason and Act via LLM: Let the LLM control reasoning and actions dynamically
29
  def autonomous_reasoning(self):
 
80
  instruction = reasoning_output.lower().strip()
81
 
82
  if "scrape" in instruction:
83
+ # Action: Fetch company URL via SERP API before scraping
84
+ self.fetch_company_url()
85
+ if self.company_url:
86
+ self.fetch_company_info_with_firecrawl(self.company_url)
87
  # Reflect again by invoking the LLM to reassess
88
  return self.autonomous_reasoning()
89
 
 
105
  print("Error: Unrecognized instruction from LLM. Proceeding with available data.")
106
  return self.generate_email()
107
 
108
+ # Fetch company URL using SERP API
109
+ def fetch_company_url(self):
110
+ print(f"Fetching company URL for {self.company_name} using SERP API...")
111
+ serp_url = f"https://serpapi.com/search.json?q={self.company_name}&api_key={serp_api_key}&num=1"
112
+ response = requests.get(serp_url)
113
+
114
+ if response.status_code == 200:
115
+ serp_data = response.json()
116
+ if 'organic_results' in serp_data and len(serp_data['organic_results']) > 0:
117
+ self.company_url = serp_data['organic_results'][0]['link']
118
+ print(f"Found company URL: {self.company_url}")
119
+ else:
120
+ print("No URL found for the company via SERP API.")
121
+ self.company_url = None
122
+ else:
123
+ print(f"Error fetching company URL: {response.status_code}")
124
+ self.company_url = None
125
+
126
  # Action: Fetch LinkedIn data via Proxycurl
127
  def fetch_linkedin_data(self):
128
  if not self.linkedin_url:
 
146
  self.skills = ["Adaptable", "Hardworking"]
147
  self.experiences = ["Worked across various industries"]
148
 
149
+ # Action: Fetch company information via Firecrawl API using company URL
150
+ def fetch_company_info_with_firecrawl(self, company_url):
151
+ print(f"Fetching company info for {company_url} using Firecrawl.")
152
+ headers = {"Authorization": f"Bearer {firecrawl_api_key}"}
153
+ firecrawl_url = "https://api.firecrawl.dev/v1/scrape"
154
+ data = {
155
+ "url": company_url,
156
+ "patterns": ["description", "about", "careers", "company overview"]
157
+ }
158
+
159
+ response = requests.post(firecrawl_url, json=data, headers=headers)
160
+ if response.status_code == 200:
161
+ firecrawl_data = response.json()
162
+ self.company_info = firecrawl_data.get("description", "No detailed company info available.")
163
+ print(f"Company info fetched: {self.company_info}")
164
  else:
165
+ print(f"Error: Unable to fetch company info via Firecrawl. Using default info.")
166
+ self.company_info = "A leading company in its field."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
167
 
168
+ # Final Action: Generate the email using Groq Cloud LLM with "Start with Why" framework
169
  def generate_email(self):
170
  print("Action: Generating the email with the gathered information.")
171
+
172
  linkedin_text = f"Please find my LinkedIn profile at {self.linkedin}" if self.linkedin else ""
173
 
174
+ # Updated prompt to reflect Simon Sinek's "Start with Why" approach
175
  prompt = f"""
176
+ Write a professional job application email applying for the {self.role} position at {self.company_name}.
177
+
178
+ The email should follow the "Start with Why" approach:
179
+ 1. **Why**: Begin with the candidate’s **purpose** or **belief**—why they are passionate about this role and the company. What motivates them to apply for this role? Connect their personal mission to the company's values, mission, or goals.
180
+ 2. **How**: Explain how the candidate’s skills, experience, and approach align with both their "why" and the company’s mission. This should show how they are uniquely qualified to contribute to the company’s success.
181
+ 3. **What**: Provide concrete examples of the candidate’s past achievements that support their qualifications for this role. These examples should demonstrate the candidate’s ability to succeed based on their skills and experience.
182
+ 4. **Call to Action**: End with a polite request for a meeting or further discussion to explore how the candidate can contribute to the company's success.
183
+
184
+ Use the following information to craft the email:
185
  - The candidate’s LinkedIn bio: {self.bio}.
186
  - The candidate’s most relevant skills: {', '.join(self.skills)}.
187
  - The candidate’s professional experience: {', '.join([exp['title'] for exp in self.experiences])}.
188
+ - Company information: {self.company_info}.
189
+ - Role description: {self.role_description}.
 
 
 
 
 
 
190
 
191
  End the email with this signature:
192
  Best regards,
 
197
 
198
  The email should not exceed {self.word_limit} words.
199
  """
200
+
201
  url = "https://api.groq.com/openai/v1/chat/completions"
202
  headers = {
203
  "Authorization": f"Bearer {groq_api_key}",
204
  "Content-Type": "application/json",
205
  }
206
+
207
  data = {
208
  "messages": [{"role": "user", "content": prompt}],
209
  "model": "llama3-8b-8192"
210
  }
211
+
212
  response = requests.post(url, headers=headers, json=data)
213
  if response.status_code == 200:
214
  return response.json()["choices"][0]["message"]["content"].strip()