Digital-twin / tools.py
SushmithaCh's picture
Update tools.py
415117d verified
Raw
History Blame Contribute Delete
5.82 kB
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import os
import requests
import json
gmail_address = os.getenv("GMAIL_ADDRESS")
gmail_app_password = os.getenv("GMAIL_APP_PASSWORD")
resend_api_key = os.getenv("RESEND_API_KEY")
notification_email = os.getenv("GMAIL_ADDRESS")
def send_recruiter_lead(recruiter_name, recruiter_company, recruiter_email, interest_summary):
response = requests.post(
"https://api.resend.com/emails",
headers={
"Authorization": f"Bearer {resend_api_key}",
"Content-Type": "application/json"
},
json={
"from": "onboarding@resend.dev",
"to": notification_email,
"subject": f"New Lead: {recruiter_name} from {recruiter_company}",
"text": f"""Your Digital Twin caught a recruiter lead!
Name: {recruiter_name}
Company: {recruiter_company}
Email: {recruiter_email}
Interest: {interest_summary}"""
}
)
print(f"Email status: {response.status_code}{response.text}")
pushover_user_key = os.getenv("PUSHOVER_USER_KEY")
pushover_api_token = os.getenv("PUSHOVER_API_KEY")
api_url = "https://api.pushover.net/1/messages.json"
def send_pushover_notification(message):
if pushover_user_key is None or pushover_api_token is None:
return "Pushover credentials not set. Cannot send notification."
payload = {
"token": pushover_api_token,
"user": pushover_user_key,
"message": message
}
response = requests.post(api_url, data=payload)
if response.status_code == 200:
print("Notification sent successfully!")
else:
print(f"Failed to send notification. Status code: {response.status_code}, Response: {response.text}")
return f"Notification sent {message}"
def add_tools():
tools = []
send_recruiter_lead_tool = {
"name": "send_recruiter_lead",
"description": "Notifies Sushmitha via email when a recruiter expresses hiring interest or shares their details. Use this to notify Sushmitha about recruiter leads. Trigger this when the recruiter mentions a role, company, or that they are looking to hire. Do not send an email without asking for at least the recruiter's name and company. If they don't provide any information, do not send an email.",
"parameters": {
"type": "object",
"properties": {
"recruiter_name": {
"type": "string",
"description": "Recruiter's name, or 'Unknown' if not mentioned"
},
"recruiter_company": {
"type": "string",
"description": "Recruiter's company, or 'Unknown' if not mentioned"
},
"recruiter_email": {
"type": "string",
"description": "Recruiter's email address if they shared it, ask for it if not provided, if they don't provide it after the request 'Not provided'"
},
"interest_summary": {
"type": "string",
"description": "One sentence summary of what they are looking for"
}
},
"required": ["recruiter_name", "recruiter_company","recruiter_email", "interest_summary"]
}
}
send_pushover_notification_tool = {
"name" : "send_pushover_notification",
"description" : "Sends an instant push notification to Sushmitha's phone via Pushover. MUST be called whenever you are uncertain or do not have enough information to answer a question about Sushmitha. Always let the user know you are uncertain when you do not have enough information. Also use when the user explicitly asks you to send a notification.",
"parameters" : {
"type" : "object",
"properties" : {
"message" : {
"type" : "string",
"description" : "The message to be sent in the notification"
}
},
"required" : ["message"]
}
}
tools.append({"type": "function", "function": send_recruiter_lead_tool})
tools.append({"type": "function", "function": send_pushover_notification_tool})
print(f"*******Tools added to the tools list {tools}*******")
return tools
def handle_tool_call(sorted_tcs, openai_msgs, full_content):
openai_msgs.append({
"role": "assistant",
"content": full_content or None,
"tool_calls": sorted_tcs,
})
for tc in sorted_tcs:
func_name = tc["function"]["name"]
try:
args = json.loads(tc["function"]["arguments"] or "{}")
except json.JSONDecodeError:
args = {}
if func_name == "send_pushover_notification":
try:
send_pushover_notification(args.get("message", ""))
tool_content = f"Notification sent: {args.get('message', '')}"
except Exception as e:
tool_content = f"Could not send notification: {e}"
elif func_name == "send_recruiter_lead":
try:
send_recruiter_lead(
args.get("recruiter_name", "Unknown"),
args.get("recruiter_company", "Unknown"),
args.get("recruiter_email", "Not provided"),
args.get("interest_summary", "")
)
tool_content = f"Lead captured for {args.get('recruiter_name', 'Unknown')}"
except Exception as e:
tool_content = f"Could not send email: {e}"
else:
tool_content = f"Unknown tool: {func_name}"
openai_msgs.append({
"role": "tool",
"content": tool_content,
"tool_call_id": tc["id"],
})