|
| 1 | +import time |
| 2 | +import logging |
| 3 | +from langchain_core.messages import AIMessage |
| 4 | +from langchain_core.tools import tool |
| 5 | +from langgraph.prebuilt import ToolNode |
| 6 | +from langchain_openai import ChatOpenAI |
| 7 | +from langgraph.graph import StateGraph, MessagesState, START, END |
| 8 | +from chains.notice_extraction import NoticeEmailExtract |
| 9 | +from graphs.notice_extraction import NOTICE_EXTRACTION_GRAPH |
| 10 | + |
| 11 | +logging.basicConfig( |
| 12 | + level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s" |
| 13 | +) |
| 14 | +LOGGER = logging.getLogger(__name__) |
| 15 | + |
| 16 | + |
| 17 | +@tool |
| 18 | +def forward_email(email_message: str, send_to_email: str) -> bool: |
| 19 | + """ |
| 20 | + Forward an email_message to the address of sent_to_email. Returns |
| 21 | + true if the email was successful otherwise it wil return false. Note |
| 22 | + that this tool only forwards the email to an internal department - |
| 23 | + it does not reply to the sender. |
| 24 | + """ |
| 25 | + |
| 26 | + LOGGER.info(f"Forwarding the email to {send_to_email}...") |
| 27 | + time.sleep(2) |
| 28 | + LOGGER.info("Email forwarded!") |
| 29 | + |
| 30 | + return True |
| 31 | + |
| 32 | + |
| 33 | +@tool |
| 34 | +def send_wrong_email_notification_to_sender( |
| 35 | + sender_email: str, correct_department: str |
| 36 | +): |
| 37 | + """ |
| 38 | + Send an email back to the sender informing them that |
| 39 | + they have the wrong address. The email should be sent |
| 40 | + to the correct_department. |
| 41 | + """ |
| 42 | + |
| 43 | + LOGGER.info(f"Sending wrong email notification to {sender_email}...") |
| 44 | + time.sleep(2) |
| 45 | + LOGGER.info("Email sent!") |
| 46 | + |
| 47 | + return True |
| 48 | + |
| 49 | + |
| 50 | +@tool |
| 51 | +def extract_notice_data( |
| 52 | + email: str, escalation_criteria: str |
| 53 | +) -> NoticeEmailExtract: |
| 54 | + """ |
| 55 | + Extract structured fields from a regulatory notice. |
| 56 | + This should be used when the email message comes from |
| 57 | + a regulatory body or auditor regarding a property or |
| 58 | + construction site that the company works on. |
| 59 | + escalation_criteria is a description of which kinds of |
| 60 | + notices require immediate escalation. |
| 61 | + After calling this tool, you don't need to call any others. |
| 62 | + """ |
| 63 | + |
| 64 | + LOGGER.info("Calling the email notice extraction graph...") |
| 65 | + |
| 66 | + initial_state = { |
| 67 | + "notice_message": email, |
| 68 | + "notice_email_extract": None, |
| 69 | + "critical_fields_missing": False, |
| 70 | + "escalation_text_criteria": escalation_criteria, |
| 71 | + "escalation_dollar_criteria": 100_000, |
| 72 | + "requires_escalation": False, |
| 73 | + "escalation_emails": ["brog@abc.com", "bigceo@company.com"], |
| 74 | + } |
| 75 | + |
| 76 | + results = NOTICE_EXTRACTION_GRAPH.invoke(initial_state) |
| 77 | + return results["notice_email_extract"] |
| 78 | + |
| 79 | + |
| 80 | +@tool |
| 81 | +def determine_email_action(email: str) -> str: |
| 82 | + """ |
| 83 | + Call to determine which action should be taken |
| 84 | + for an email. Only use when the other tools don't seem |
| 85 | + relevant for the email task. Do not call this tool if |
| 86 | + you've already called extract_notice_data. |
| 87 | + """ |
| 88 | + |
| 89 | + instructions = """ |
| 90 | + If the email appears to be an invoice of any kind or related to |
| 91 | + billing, forward the email to the billing and invoices team: |
| 92 | + billing@company.com |
| 93 | + and send a wrong email notice back to the sender. The correct department is |
| 94 | + billing@company.com. |
| 95 | + If the email appears to be from a customer, forward to support@company.com, |
| 96 | + cdetuma@company.com, and ctu@abc.com. Be sure to forward it to all three |
| 97 | + emails listed. |
| 98 | + Send a wrong email notice back to the |
| 99 | + customer and let them know the correct department is support@company.com. |
| 100 | + For any other emails, please send a wrong email notification and try to |
| 101 | + infer the correct department from one of billing@company.com, |
| 102 | + support@company.com, |
| 103 | + humanresources@company.com, and it@company.com. |
| 104 | + """ |
| 105 | + |
| 106 | + return instructions |
| 107 | + |
| 108 | + |
| 109 | +tools = [ |
| 110 | + determine_email_action, |
| 111 | + forward_email, |
| 112 | + send_wrong_email_notification_to_sender, |
| 113 | + extract_notice_data, |
| 114 | +] |
| 115 | +tool_node = ToolNode(tools) |
| 116 | + |
| 117 | +EMAIL_AGENT_MODEL = ChatOpenAI(model="gpt-4o-mini", temperature=0).bind_tools( |
| 118 | + tools |
| 119 | +) |
| 120 | + |
| 121 | + |
| 122 | +def call_agent_model_node(state: MessagesState) -> dict[str, list[AIMessage]]: |
| 123 | + """Node to call the email agent model""" |
| 124 | + messages = state["messages"] |
| 125 | + response = EMAIL_AGENT_MODEL.invoke(messages) |
| 126 | + return {"messages": [response]} |
| 127 | + |
| 128 | + |
| 129 | +def route_agent_graph_edge(state: MessagesState) -> str: |
| 130 | + """Determine whether to call more tools or exit the graph""" |
| 131 | + last_message = state["messages"][-1] |
| 132 | + if last_message.tool_calls: |
| 133 | + return "email_tools" |
| 134 | + return END |
| 135 | + |
| 136 | + |
| 137 | +workflow = StateGraph(MessagesState) |
| 138 | + |
| 139 | +workflow.add_node("email_agent", call_agent_model_node) |
| 140 | +workflow.add_node("email_tools", tool_node) |
| 141 | + |
| 142 | +workflow.add_edge(START, "email_agent") |
| 143 | +workflow.add_conditional_edges( |
| 144 | + "email_agent", route_agent_graph_edge, ["email_tools", END] |
| 145 | +) |
| 146 | +workflow.add_edge("email_tools", "email_agent") |
| 147 | + |
| 148 | +email_agent_graph = workflow.compile() |
0 commit comments