You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I added a very descriptive title to this question.
I searched the LangChain documentation with the integrated search.
I used the GitHub search to find a similar question and didn't find it.
Commit to Help
I commit to help with one of those options 👆
Example Code
Ihavethefollowingmodel:
classEmailModel(BaseModel):
""" Example: Date: December 25, 2024 From: Mickey Mouse <mickey@mouse.com> """date_str: str|None=Field(
default=None,
exclude=True,
repr=False,
description="The date of the email reformatted to match YYYY-mm-dd. This is usually found in the \"Date:\" field in the email.",
)
name: str|None=Field(
default=None,
description="The name of the email sender. This is usually found in the \"From:\" field in the email formatted as \"name <email>\"",
)
phone: str|None=Field(
default=None,
description="The phone number of the email sender (if present in the message). This is usually found in the signature at the end of the email body.",
)
email: str|None=Field(
default=None,
description="The email addreess of the email sender (if present in the message). This is usually found in the same \"From:\" field in the email formatted as \"name <email>\"",
)
@staticmethoddef_convert_string_to_date(date_str: str|None) ->date|None:
try:
returndatetime.strptime(date_str, "%Y-%m-%d").date() ifdate_strelseNoneexceptExceptionase:
print(e)
returnNone@computed_field@propertydefdate_of_email(self) ->date|None:
returnself._convert_string_to_date(self.date_str)
The chain:
_email_parser_prompt = ChatPromptTemplate.from_messages(
[
(
"system",
"""
Parse the date of email, sender's name, sender's phone, sender's email, project id, site location,
violation type, required changes, compliance deadline, and maximum potential fine from the email.
If any of the fields aren't present, don't populate them.
Try to cast dates into the YYYY-mm-dd format. Don't populate fields if they're not present in the email.
Here's the email:
{email}
""",
),
("human", "{email}"),
#("placeholder", "{email}"), #should be a list of base messages
]
)
self._chainLLM = init_chat_model("llama3.2", model_provider="ollama", temperature=0)
self._email_parser_chain = (
self._email_parser_prompt
| self._chainLLM.with_structured_output(EmailModel)
)
The class method:
@dataclass
class EmailRAGState(TypedDict):
email: str
extract: EmailModel | None
async def ParseEmail(self, state: EmailRAGState, config: RunnableConfig) -> EmailRAGState:
"""
Extract structured fields from an email.
This should be used when the email message comes from
a regulatory body or auditor regarding a property or
construction site that the company works on.
"""
logging.info(f"\n=== {self.ParseEmail.__name__} ===")
state["extract"] = await self._email_parser_chain.with_config(config).ainvoke({"email": state["email"]})
print(f"Extract: {state['extract']}")
return state
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Checked other resources
Commit to Help
Example Code
The chain:
The class method:
The pytest:
The output:
Description
It fails to extract the name and phone :-(
System Info
Beta Was this translation helpful? Give feedback.
All reactions