Spaces:
Sleeping
Sleeping
import os | |
import time | |
from typing import List, Optional | |
from langchain_openai import ChatOpenAI | |
from langchain.chains import LLMChain | |
from langchain.prompts import PromptTemplate | |
from dotenv import load_dotenv | |
from langchain_core.messages import HumanMessage | |
# Load environment variables | |
load_dotenv() | |
class BaseAgent: | |
def __init__(self, tools: Optional[List] = None): | |
# Get API key | |
api_key = os.getenv("OPENAI_API_KEY") | |
if not api_key: | |
raise ValueError("OPENAI_API_KEY environment variable is not set") | |
self.model = ChatOpenAI( | |
model=os.getenv("MODEL", "gpt-3.5-turbo"), | |
openai_api_key=api_key, | |
max_tokens=4000, | |
temperature=0.0, | |
top_p=1 | |
) | |
class MHEvaluationAgent(BaseAgent): | |
def __init__(self, tools: Optional[List] = None): | |
super().__init__(tools) | |
# Define the evaluation prompt | |
self.evaluation_prompt = PromptTemplate( | |
input_variables=["job_description", "candidates_profile", "checkpoints", "answer_script"], | |
template=""" | |
You are an expert recruiter specializing in evaluating resumes against job descriptions. | |
Your task is to evaluate and assign a categorisation for the candidate's resume based on the "checkpoints" and "answer_script" provided to understand how well it aligns with the JD. Also provide a brief reasoning. You are required to take a pragmatic and holistic approach considering the context of the resume and understand the implied aspects as well. For example, if the JD specifies requirement of Graduation and the resume mentions post-graduation, it is implied that the person holds graduation and should be considered as such. | |
Think step by step and follow the instructions provided below: | |
*Input*: The input for this task will be "Checkpoints" and " Answer Script" and "Job Description". | |
*Output*: The output should be category of the resume and a summary of evidence and reasoning explaining the observation and the reasons for the categorisation. | |
### Steps: | |
Step1 : *Understand the Job Description along with checkpoints and answer script provided* | |
Step 2 : Analyse if the answers from the checkpoints and answer script satisfy the must-haves while considering the following aspects : | |
a) If there are any checkpoints related to years of experience, do not strictly focus on just the number of years in the literary sense. The number of years should be given due importance, but more importantly, considered holistically taking into account the context of the role, responsibilities handled by the candidate, etc. Minor deviations in number of years of experience should not lead to disqualification. | |
b) While considering the other checkpoints and answers, take a holistic and a pragmatic approach in understanding and give due importance to both the explicit and implied experiences and skills based on the role/responsibilities, context of the roles and past experiences of the individual. | |
C) If there are any checkpoints related to Education, consider the context while evaluating. For example, if the JD specifies requirement of Graduation and the resume mentions post-graduation, it is implied that the person qualifies for that checkpoint. | |
Step 3 : Based on the understanding from Step 1 and Step 2, categorise the resume following the criteria specified below : | |
a) Category I : JD does not explicitly or implicitly specify any must-haves or essentials for the resume to be considered for the role. | |
b) Category II: Satisfies all must-haves explicitly or implicitly. If there is slight uncertainty, give benefit of doubt to the candidate and place him/her in Category II. | |
c) Category III: Lacks one or more must-haves mentioned in the JD. | |
Step 4. *Provide factual evidence:* | |
Provide reasoning for the rating along with observations and explaining why the candidate has been assigned to a particular category. Include specific examples from the "checkpoints" and "Answer script" to support your categorisation. | |
### Output Format: | |
### Output Format: | |
**category**: category I/II/III based on the evidence. | |
**evidence**: Provide a concise justification for categorization in only 40-50 words explaining why the candidate's relevant skills and expertise does or does not align with the skills required for the role outlined in the job description. | |
Job Description: | |
{job_description} | |
Candidate's Resume: | |
{candidates_profile} | |
Checkpoints: | |
{checkpoints} | |
Answer Script: | |
{answer_script} | |
""" | |
) | |
def forward( | |
self, | |
job_description: Optional[str] = None, | |
profile: Optional[str] = None, | |
checkpoints: Optional[str] = None, | |
answer_script: Optional[str] = None, | |
retries: int = 5 | |
) -> Optional[str]: | |
if job_description: | |
for _ in range(retries): | |
try: | |
# Create the prompt | |
prompt_text = self.evaluation_prompt.format( | |
job_description=job_description, | |
candidates_profile=profile or "", | |
checkpoints=checkpoints or "", | |
answer_script=answer_script or "" | |
) | |
# Get response from the model | |
response = self.model.invoke([HumanMessage(content=prompt_text)]) | |
return response.content | |
except Exception as e: | |
print(e) | |
time.sleep(3) | |
return None | |
if __name__ == "__main__": | |
agent = MHEvaluationAgent() | |
ans = agent.forward(job_description="good_jd", profile="profile") | |
print("\n\nResult:", ans) |