from backgroundAgent import Agents import EmailSender as es import json, sys import logging import sqlite3 from sqlite3 import Error logging.basicConfig(filename="logs/agent_script.log", level=logging.INFO) # logging.debug('your text goes here') # DB connection def create_connection(db_file): conn = None try: conn = sqlite3.connect(db_file) except Error as e: print(e) logging.error("DB connection error:",e) return conn # get command argument values conversation_id = sys.argv[1] user_id = sys.argv[2] content = sys.argv[3] # define database name database = r"db.sqlite3" alert_flag = False conn = create_connection(database) with conn: cur = conn.cursor() conversation_messages = "" cur.execute("SELECT first_name, last_name, email FROM user_customuser WHERE id = ?",(str(user_id))) user_row = cur.fetchone() cur.execute("SELECT * FROM chat_chat WHERE user_id = ? AND conversation_id = ?",(str(user_id), str(conversation_id))) rows = cur.fetchall() for row in rows: conversation_messages += "
User: " + row[1] + "
AI Counselor: " + row[2] conversation_messages += "
User: " + content email_body = "

User Details:
First Name: "+user_row[0]+"
Last Name: "+user_row[1] +"
Email: " + user_row[2] + "

" llm_reponse = Agents.openai_response(content) llm_reponse_json = json.loads(llm_reponse) email_body += "

Safety Agent Questions check: " for question, answer_obj in llm_reponse_json.items(): if answer_obj[0]["answer"] == "yes" or answer_obj[0]["answer"] == "Yes": alert_flag = True email_body += "
" + question.capitalize() + ": " email_body += "" + answer_obj[0]["answer"]+ ", " email_body += answer_obj[0]["reasoning"] email_body += "

" if alert_flag is True: # Moderation API moderation_message = "" moderation_response = Agents.moderation_response(content) if moderation_response.flagged == True: moderation_message = "

Content Moderation Match Found in Below Categories: " if moderation_response.categories.sexual == True: moderation_message += "
Sextual" if moderation_response.categories.hate == True: moderation_message += "
Hate" if moderation_response.categories.harassment == True: moderation_message += "
Harassment" if moderation_response.categories.self_harm == True: moderation_message += "
Self-harm" if moderation_response.categories.sexual_minors == True: moderation_message += "
Sexual/Minors" if moderation_response.categories.hate_threatening == True: moderation_message += "
Hate/Threatening" if moderation_response.categories.violence_graphic == True: moderation_message += "
Violence/Graphic" if moderation_response.categories.self_harm_intent == True: moderation_message += "
Self-harm/Intent" if moderation_response.categories.self_harm_instructions == True: moderation_message += "
Self-harm/Instructions" if moderation_response.categories.harassment_threatening == True: moderation_message += "
Harassment/Threatening" if moderation_response.categories.violence == True: moderation_message += "
Violence" moderation_message += "

" # Send safety alert email #TODO: prepare comple conversation message & get user information body = "This is an automated alert from the Safety System Agent." + email_body + moderation_message + "

Conversation Transcript: " + conversation_messages + "

" es.send_email(body) alert_flag = False logging.info("Email sent successfully!")