xTwin / lisa_hr_agent.py
aamirhameed's picture
Upload 3 files
f8c4dcd verified
raw
history blame
4.13 kB
# lisa_hr_agent.py
from langchain_community.llms import Ollama
from datetime import datetime
from reportlab.lib.pagesizes import A4
from reportlab.pdfgen import canvas
from reportlab.lib.units import inch
import textwrap
class LISAHRAgent:
def __init__(self):
self.memory = {}
self.questions = [
("name", "What is the full name of the selected candidate?"),
("job_title", "What is the job title offered?"),
("salary", "What is the monthly salary offered (in Rs.)?"),
("joining_date", "What is the joining date? (e.g., 5 June 2025)"),
("probation_period", "What is the probation period? (e.g., 3 months)?"),
("location", "What is the job location?")
]
self.index = 0
def ask_next(self):
if self.index < len(self.questions):
return self.questions[self.index][1]
return None
def receive_answer(self, answer):
key = self.questions[self.index][0]
self.memory[key] = answer
self.index += 1
def is_complete(self):
return self.index >= len(self.questions)
def get_inputs(self):
return {
**self.memory,
"today": datetime.today().strftime("%d %B %Y")
}
def generate_letter_with_llm(data: dict) -> str:
llm = Ollama(model="phi") # You can change model if you like
prompt = f"""
You are a professional HR assistant. Write a detailed and formal appointment letter for a selected candidate with the following information:
Candidate Name: {data['name']}
Job Title: {data['job_title']}
Monthly Salary: Rs. {data['salary']}
Joining Date: {data['joining_date']}
Probation Period: {data['probation_period']}
Location: {data['location']}
Date of Letter: {data['today']}
Company Name: Amsaa
Founder: Amir Hameed
Instructions:
- This is an appointment letter, the candidate has already been selected.
- Include a letterhead title at the top: "Amsaa – Appointment Letter"
- Start with date, recipient name, and subject ("Appointment for the position of [Job Title]")
- Include details like reporting authority, job location, salary in Rs., joining date, probation period.
- Maintain a professional and polite tone throughout.
- Add a paragraph welcoming the candidate and emphasizing the company’s vision and values.
- Conclude with "Sincerely, Amir Hameed, Founder & CEO, Amsaa"
- Format should be clean and easy to read.
"""
response = llm.invoke(prompt)
return response.strip()
def save_letter_as_pdf(content: str, filename="appointment_letter.pdf"):
c = canvas.Canvas(filename, pagesize=A4)
width, height = A4
margin = 50
y_position = height - margin
# Header
c.setFont("Helvetica-Bold", 16)
c.drawString(margin, y_position, "Amsaa – Appointment Letter")
y_position -= 30
c.setFont("Helvetica", 12)
# Wrap and draw each line
for line in content.split('\n'):
if not line.strip():
y_position -= 12
continue
wrapped_lines = textwrap.wrap(line, width=95)
for wrap_line in wrapped_lines:
c.drawString(margin, y_position, wrap_line)
y_position -= 14
if y_position < 50: # Add new page if needed
c.showPage()
y_position = height - margin
c.setFont("Helvetica", 12)
c.save()
print(f"\n[✓] Appointment letter saved as: {filename}")
def main():
agent = LISAHRAgent()
print("\n👩‍💼 Welcome to LISA HR — AI HR Assistant (Powered by LLM)\n")
while not agent.is_complete():
question = agent.ask_next()
answer = input(f"{question} ")
agent.receive_answer(answer)
print("\n🧠 Generating Appointment Letter with LLM...\n")
collected_data = agent.get_inputs()
letter = generate_letter_with_llm(collected_data)
save_letter_as_pdf(letter)
print("\n📄 Preview:\n")
print(letter)
if __name__ == "__main__":
main()