letter-generator2 / LangChainConnect.py
jaothan's picture
Upload 3 files
cab5c74 verified
import os
from langchain_groq import ChatGroq
from langchain_core.prompts import PromptTemplate
from langchain_core.output_parsers import JsonOutputParser
from langchain_community.document_loaders import WebBaseLoader
from langchain_core.exceptions import OutputParserException
from dotenv import load_dotenv
load_dotenv()
class LangChainConnect:
def __init__(self):
self.llm=ChatGroq(
model="llama3-8b-8192",
temperature=0,
groq_api_key=os.getenv("GROQ_API_KEY"))
def extract_job_content(self, job_text:str)->dict:
"""extract the job content in a json format from the url that was passed in"""
prompt_extract = PromptTemplate.from_template(
"""
###SCRAPED TEXT FROM WEBSITE: {job_description}
###INSTRUCTION: The scarped text is a job posting. Extract the title and skills needed for this job in a JSON format, include company name from the link in the url as a field too.
###VALID JSON (NO PREAMBLE)""")
chain_news_extract = prompt_extract | self.llm
response = chain_news_extract.invoke(input={'job_description': job_text})
try:
json_parser = JsonOutputParser()
json_content = json_parser.parse(response.content)
except OutputParserException:
raise OutputParserException(f"content cannot be parsed {response.content}")
return json_content
def write_cover_letter(self,job_content:dict, profile:str, name:str, experience:str, job_link:str, pdf_resume:list)->str:
"""create the cover letter prompting with the job description, pdf resume and relevant information like name and experience"""
prompt_email = PromptTemplate.from_template("""
###JOB DESCRIPTION:
{job_description}
###INSTRUCTION:
Add a subject line for this email
You are {name} a Machine Learning Engineer with {experience} of experience looking for better advancements in the careers.
Pick the company name from the job link and use that in the text of the cover letter
You should add information from the provided resume in this letter {pdf_resume} that applies to the specific job description provided
You should include the email address, and phone number in the letter body itself.
Don't add anything that is not mentioned in the resume or in the inputs.
Make sure to thank the hiring manager as the last sentence of the letter.
Add a signature to the email with the github link, linkedin link.
###EMAIL (NO PREAMBLE)
Don't add footer for the email.
""")
chain_email = prompt_email | self.llm
response_email = chain_email.invoke({"job_description": str(job_content), "name":name,"experience":experience,"job_link":job_link,"pdf_resume":pdf_resume})
return response_email.content