thamani commited on
Commit
d04d842
·
verified ·
1 Parent(s): a2e6fdf

Create chains.py

Browse files
Files changed (1) hide show
  1. chains.py +98 -0
chains.py ADDED
@@ -0,0 +1,98 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from langchain_groq import ChatGroq
3
+ from langchain_core.prompts import PromptTemplate
4
+ from langchain_core.output_parsers import JsonOutputParser
5
+ from langchain_core.exceptions import OutputParserException
6
+ from dotenv import load_dotenv
7
+
8
+ load_dotenv()
9
+
10
+ class Chain:
11
+ def __init__(self):
12
+ self.llm = ChatGroq(temperature=0, groq_api_key=os.getenv("GROQ_API_KEY"), model_name="llama-3.3-70b-versatile")
13
+
14
+ def extract_jobs(self, cleaned_text):
15
+ prompt_extract = PromptTemplate.from_template(
16
+ """
17
+ ### SCRAPED TEXT FROM WEBSITE:
18
+ {page_data}
19
+ ### INSTRUCTION:
20
+ The scraped text is from the career's page of a website.
21
+ Your job is to extract the job postings and return them in JSON format containing the following keys: `role`, `experience`, `skills`, and `description`.
22
+ Only return the valid JSON.
23
+ ### VALID JSON (NO PREAMBLE):
24
+ """
25
+ )
26
+ chain_extract = prompt_extract | self.llm
27
+ res = chain_extract.invoke(input={"page_data": cleaned_text})
28
+ try:
29
+ json_parser = JsonOutputParser()
30
+ res = json_parser.parse(res.content)
31
+ except OutputParserException:
32
+ raise OutputParserException("Context too big. Unable to parse jobs.")
33
+ return res if isinstance(res, list) else [res]
34
+
35
+ def write_mail(self, job, links, username="Thamani", client_name="Hiring Manager", email_style="Formal"):
36
+ """Generates cold emails based on the selected style."""
37
+
38
+ style_instructions = {
39
+ "Formal": "Maintain a professional and polished tone. Focus on achievements and qualifications.",
40
+ "Casual": "Use a friendly, engaging tone. Keep it light while still showcasing strengths.",
41
+ "Persuasive": "Be compelling and assertive. Highlight why you are the perfect fit with strong language."
42
+ }
43
+
44
+ # Convert style name to lowercase for prompt clarity
45
+ email_style_lower = email_style.lower()
46
+
47
+ prompt_email = PromptTemplate.from_template(
48
+ """
49
+ ### JOB DETAILS:
50
+ - Role: {job_title}
51
+ - Company: {company_name}
52
+ - Experience Required: {experience}
53
+ - Skills: {skills}
54
+ - Description: {job_description}
55
+
56
+ ### INSTRUCTION:
57
+ You are {username}, a motivated MCA graduate with strong technical and analytical skills, seeking an opportunity to contribute to {company_name} as a {job_title}.
58
+ Craft a **{email_style} cold email** to {client_name} demonstrating your skills, projects, and value.
59
+
60
+ Style Instruction: {style_instruction}
61
+
62
+ Highlight relevant projects, certifications, or portfolio links: {link_list}.
63
+
64
+ ### EMAIL (NO PREAMBLE):
65
+ """
66
+ )
67
+
68
+ chain_email = prompt_email | self.llm
69
+
70
+ # Extract job details, handling missing fields
71
+ job_title = job.get("role", "the position")
72
+ company_name = job.get("company", "the company")
73
+ experience = job.get("experience", "not specified")
74
+ skills = ", ".join(job.get("skills", [])) or "not mentioned"
75
+ job_description = job.get("description", "No description provided.")
76
+
77
+ # Filter out empty links
78
+ valid_links = [link for link in links if link]
79
+ formatted_links = "\n".join(f"- {link}" for link in valid_links) if valid_links else "No portfolio links provided."
80
+
81
+ # Generate email
82
+ res = chain_email.invoke({
83
+ "job_title": job_title,
84
+ "company_name": company_name,
85
+ "experience": experience,
86
+ "skills": skills,
87
+ "job_description": job_description,
88
+ "link_list": formatted_links,
89
+ "username": username,
90
+ "client_name": client_name,
91
+ "email_style": email_style_lower, # Fixed Issue
92
+ "style_instruction": style_instructions[email_style]
93
+ })
94
+
95
+ return res.content
96
+
97
+ if __name__ == "__main__":
98
+ print(os.getenv("GROQ_API_KEY"))