30 lines
1.2 KiB
Python
30 lines
1.2 KiB
Python
"""
|
|
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:
|
|
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 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 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"} |