41 lines
1.8 KiB
Python
41 lines
1.8 KiB
Python
"""
|
|
This module defines a LangGraph node function that parses raw email content
|
|
and extracts the sender's email address, the email subject, and the email body.
|
|
The function uses an LLM to perform the extraction and returns the parsed
|
|
information in a structured format.
|
|
"""
|
|
|
|
async def process(state: dict) -> dict:
|
|
from app.agent import llm
|
|
from langchain_core.messages import SystemMessage, HumanMessage
|
|
|
|
try:
|
|
email_raw = state.get("email_raw", "")
|
|
if not email_raw:
|
|
return {"error": "Missing 'email_raw' input", "phase": "failed"}
|
|
|
|
messages = [
|
|
SystemMessage(content="You are an expert in parsing email content. Extract the sender's email address, the subject, and the body from the provided raw email text."),
|
|
HumanMessage(content=email_raw),
|
|
]
|
|
|
|
response = await llm.ainvoke(messages)
|
|
# Assuming the LLM returns a structured response in the format:
|
|
# "Sender: <email>\nSubject: <subject>\nBody: <body>"
|
|
try:
|
|
lines = response.content.split("\n")
|
|
sender_email = next((line.split(": ", 1)[1] for line in lines if line.startswith("Sender:")), "").strip()
|
|
email_subject = next((line.split(": ", 1)[1] for line in lines if line.startswith("Subject:")), "").strip()
|
|
email_body = next((line.split(": ", 1)[1] for line in lines if line.startswith("Body:")), "").strip()
|
|
|
|
return {
|
|
"sender_email": sender_email,
|
|
"email_subject": email_subject,
|
|
"email_body": email_body,
|
|
"phase": "complete"
|
|
}
|
|
except Exception as parse_exc:
|
|
return {"error": f"Failed to parse LLM response: {str(parse_exc)}", "phase": "failed"}
|
|
|
|
except Exception as exc:
|
|
return {"error": str(exc), "phase": "failed"} |