agent-factory: generate agent auto-parse-email-content-and-extrac

This commit is contained in:
2026-04-08 10:41:11 +00:00
parent 58e98ad062
commit 0384b1d569
3 changed files with 50 additions and 26 deletions

View File

@@ -1,8 +1,6 @@
"""
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.
This module defines a LangGraph node function that parses email content
and extracts the sender's email address, subject, and body using an LLM.
"""
async def process(state: dict) -> dict:
@@ -12,30 +10,21 @@ async def process(state: dict) -> dict:
try:
email_raw = state.get("email_raw", "")
if not email_raw:
return {"error": "Missing 'email_raw' input", "phase": "failed"}
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),
SystemMessage(content="You are an assistant that extracts key information from raw email content."),
HumanMessage(content=f"Extract the sender's email, subject, and body from the following email:\n\n{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"}
# Assuming the response is structured as a JSON string with keys: sender_email, email_subject, email_body
parsed_data = response.content.strip()
return {
"sender_email": parsed_data.get("sender_email", ""),
"email_subject": parsed_data.get("email_subject", ""),
"email_body": parsed_data.get("email_body", ""),
"phase": "complete"
}
except Exception as exc:
return {"error": str(exc), "phase": "failed"}