File size: 9,889 Bytes
2f50c94
b6a2224
37185e0
 
cdbe688
0707373
 
2f50c94
37185e0
0707373
41f95b2
0707373
 
 
 
41f95b2
 
 
 
0707373
 
 
 
 
37185e0
b6a2224
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bd877a9
 
0707373
ab5ba21
 
 
 
 
0707373
bd877a9
 
ab5ba21
 
 
 
 
 
 
 
 
 
 
 
bd877a9
2f50c94
 
ab5ba21
 
ebcf536
0707373
2f50c94
 
 
 
 
 
 
 
 
ab5ba21
2f50c94
 
 
ab5ba21
2f50c94
ebcf536
4a2f000
bd877a9
0707373
 
ebcf536
2f50c94
0707373
 
41f95b2
ebcf536
 
 
 
41f95b2
ebcf536
41f95b2
ebcf536
 
41f95b2
 
 
 
 
 
 
0707373
 
 
 
ebcf536
 
 
 
0707373
 
 
7ea79b0
 
 
 
 
 
 
 
 
 
 
 
b6a2224
 
 
7ea79b0
2f50c94
b6a2224
 
7ea79b0
 
 
 
 
 
 
 
 
 
ebcf536
7ea79b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import requests
import os

# Load API keys securely from environment variables
proxycurl_api_key = os.getenv("PROXYCURL_API_KEY")  # Proxycurl API key
groq_api_key = os.getenv("GROQ_CLOUD_API_KEY")  # Groq Cloud API key
firecrawl_api_key = os.getenv("FIRECRAWL_API_KEY")  # Firecrawl API key

class EmailAgent:
    def __init__(self, linkedin_url, company_name, role, word_limit, user_name, email, phone, linkedin):
        self.linkedin_url = linkedin_url
        self.company_name = company_name
        self.role = role
        self.word_limit = word_limit
        self.user_name = user_name
        self.email = email
        self.phone = phone
        self.linkedin = linkedin
        self.bio = None
        self.skills = []
        self.experiences = []
        self.company_info = None
        self.role_description = None

    # Use the LLM to reason and reflect on the provided data
    def reason_with_llm(self):
        print("Reasoning: Using LLM to reason about available data...")
        
        # LLM reasoning prompt that evaluates the current data and reflects on next actions
        reasoning_prompt = f"""
        You are a reasoning agent tasked with generating a job application email. Here's what we have:
        
        1. Candidate's LinkedIn profile URL: {self.linkedin_url}
        2. Company Name: {self.company_name}
        3. Role: {self.role}
        4. Word Limit: {self.word_limit}
        5. Candidate's Name: {self.user_name}
        6. Candidate's Email: {self.email}
        7. Candidate's Phone: {self.phone}
        8. Candidate's LinkedIn: {self.linkedin}

        Candidate's Bio: {self.bio}
        Candidate's Skills: {', '.join(self.skills)}
        Candidate's Experiences: {', '.join([exp['title'] for exp in self.experiences])}

        Company Information: {self.company_info}
        Role Description: {self.role_description}

        Evaluate the completeness of the data. If some key data is missing, determine whether we should:
        - Scrape for more data (e.g., company info, role descriptions).
        - Proceed with the available information and generate the email using default logic.

        Reflect on whether we need more data or if the current information is sufficient to proceed.
        """
        
        # Send this reasoning prompt to the LLM
        url = "https://api.groq.com/openai/v1/chat/completions"
        headers = {
            "Authorization": f"Bearer {groq_api_key}",
            "Content-Type": "application/json",
        }
        
        data = {
            "messages": [{"role": "user", "content": reasoning_prompt}],
            "model": "llama3-8b-8192"
        }
        
        response = requests.post(url, headers=headers, json=data)
        if response.status_code == 200:
            reasoning_output = response.json()["choices"][0]["message"]["content"].strip()
            print("LLM Reasoning Output:", reasoning_output)
            return reasoning_output
        else:
            print(f"Error: {response.status_code}, {response.text}")
            return "Error: Unable to complete reasoning."

    # Action: Fetch LinkedIn data via Proxycurl (acting based on reasoning)
    def fetch_linkedin_data(self):
        if not self.linkedin_url:
            print("Action: No LinkedIn URL provided, using default bio.")
            self.bio = "A professional with diverse experience."
            self.skills = ["Adaptable", "Hardworking"]
            self.experiences = ["Worked across various industries"]
        else:
            print("Action: Fetching LinkedIn data via Proxycurl.")
            headers = {"Authorization": f"Bearer {proxycurl_api_key}"}
            url = f"https://nubela.co/proxycurl/api/v2/linkedin?url={self.linkedin_url}"
            response = requests.get(url, headers=headers)
            if response.status_code == 200:
                data = response.json()
                self.bio = data.get("summary", "No bio available")
                self.skills = data.get("skills", [])
                self.experiences = data.get("experiences", [])
            else:
                print("Error: Unable to fetch LinkedIn profile. Using default bio.")
                self.bio = "A professional with diverse experience."
                self.skills = ["Adaptable", "Hardworking"]
                self.experiences = ["Worked across various industries"]

    # Action: Fetch company information via Firecrawl API
    def fetch_company_info_with_firecrawl(self):
        if not self.company_name:
            print("Action: No company name provided, using default company info.")
            self.company_info = "A leading company in its field."
        else:
            print(f"Action: Fetching company info for {self.company_name} using Firecrawl.")
            headers = {"Authorization": f"Bearer {firecrawl_api_key}"}
            firecrawl_url = "https://api.firecrawl.dev/v1/scrape"
            data = {
                "url": f"https://{self.company_name}.com",
                "patterns": ["description", "about", "careers", "company overview"]
            }
            
            response = requests.post(firecrawl_url, json=data, headers=headers)
            if response.status_code == 200:
                firecrawl_data = response.json()
                self.company_info = firecrawl_data.get("description", "No detailed company info available.")
                print(f"Company info fetched: {self.company_info}")
            else:
                print(f"Error: Unable to fetch company info via Firecrawl. Using default info.")
                self.company_info = "A leading company in its field."

    # Final Action: Generate the email using Groq Cloud LLM based on gathered data
    def generate_email(self):
        print("Action: Generating the email with the gathered information.")
        
        # Dynamic LLM prompt
        prompt = f"""
        Write a professional email applying for the {self.role} position at {self.company_name}.
        
        Use the following information:
        - The candidate’s LinkedIn bio: {self.bio}.
        - The candidate’s most relevant skills: {', '.join(self.skills)}.
        - The candidate’s professional experience: {', '.join([exp['title'] for exp in self.experiences])}.
        
        Please research the company's public information. If no company-specific information is available, use general knowledge about the company's industry.
        
        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.

        End the email with this signature:
        Best regards,
        {self.user_name}
        Email: {self.email}
        Phone: {self.phone}
        LinkedIn: {self.linkedin}
        
        The email should not exceed {self.word_limit} words.
        """
        
        url = "https://api.groq.com/openai/v1/chat/completions"
        headers = {
            "Authorization": f"Bearer {groq_api_key}",
            "Content-Type": "application/json",
        }
        
        data = {
            "messages": [{"role": "user", "content": prompt}],
            "model": "llama3-8b-8192"
        }
        
        response = requests.post(url, headers=headers, json=data)
        if response.status_code == 200:
            return response.json()["choices"][0]["message"]["content"].strip()
        else:
            print(f"Error: {response.status_code}, {response.text}")
            return "Error generating email. Please check your API key or try again later."

    # Main loop following ReAct pattern
    def run(self):
        reasoning_output = self.reason_with_llm()  # LLM performs reasoning and reflection
        print("LLM Reflection:", reasoning_output)

        self.fetch_linkedin_data()  # Fetch LinkedIn data
        self.fetch_company_info_with_firecrawl()  # Fetch company data using Firecrawl
        
        return self.generate_email()  # Final action: generate email

# Define the Gradio interface and the main app logic
def gradio_ui():
    # Input fields
    name_input = gr.Textbox(label="Your Name", placeholder="Enter your name")
    company_input = gr.Textbox(label="Company Name or URL", placeholder="Enter the company name or website URL")
    role_input = gr.Textbox(label="Role Applying For", placeholder="Enter the role you are applying for")
    email_input = gr.Textbox(label="Your Email Address", placeholder="Enter your email address")
    phone_input = gr.Textbox(label="Your Phone Number", placeholder="Enter your phone number")
    linkedin_input = gr.Textbox(label="Your LinkedIn URL", placeholder="Enter your LinkedIn profile URL")
    word_limit_slider = gr.Slider(minimum=50, maximum=300, step=10, label="Email Word Limit", value=150)
    
    # Output field
    email_output = gr.Textbox(label="Generated Email", placeholder="Your generated email will appear here", lines=10)

    # Function to create and run the email agent
    def create_email(name, company_name, role, email, phone, linkedin_url, word_limit):
        agent = EmailAgent(linkedin_url, company_name, role, word_limit, name, email, phone, linkedin_url)
        return agent.run()

    # Gradio interface
    demo = gr.Interface(
        fn=create_email,
        inputs=[name_input, company_input, role_input, email_input, phone_input, linkedin_input, word_limit_slider],
        outputs=[email_output],
        title="Email Writing AI Agent with ReAct",
        description="Generate a professional email for a job application using LinkedIn data, company info, and role description.",
        allow_flagging="never"
    )
    
    # Launch the Gradio app
    demo.launch()

# Start the Gradio app when running the script
if __name__ == "__main__":
    gradio_ui()