|
| 1 | +from datetime import date, datetime |
| 2 | + |
| 3 | +from langchain_core.prompts import ChatPromptTemplate |
| 4 | +from langchain_openai import ChatOpenAI |
| 5 | +from pydantic import BaseModel, Field, computed_field |
| 6 | + |
| 7 | + |
| 8 | +class NoticeEmailExtract(BaseModel): |
| 9 | + date_of_notice_str: str | None = Field( |
| 10 | + default=None, |
| 11 | + exclude=True, |
| 12 | + repr=False, |
| 13 | + description="""The date of the notice (if any) reformatted |
| 14 | + to match YYYY-mm-dd""", |
| 15 | + ) |
| 16 | + entity_name: str | None = Field( |
| 17 | + default=None, |
| 18 | + description="""The name of the entity sending the notice (if present |
| 19 | + in the message)""", |
| 20 | + ) |
| 21 | + entity_phone: str | None = Field( |
| 22 | + default=None, |
| 23 | + description="""The phone number of the entity sending the notice |
| 24 | + (if present in the message)""", |
| 25 | + ) |
| 26 | + entity_email: str | None = Field( |
| 27 | + default=None, |
| 28 | + description="""The email of the entity sending the notice |
| 29 | + (if present in the message)""", |
| 30 | + ) |
| 31 | + project_id: int | None = Field( |
| 32 | + default=None, |
| 33 | + description="""The project ID (if present in the message) - |
| 34 | + must be an integer""", |
| 35 | + ) |
| 36 | + site_location: str | None = Field( |
| 37 | + default=None, |
| 38 | + description="""The site location of the project (if present |
| 39 | + in the message). Use the full address if possible.""", |
| 40 | + ) |
| 41 | + violation_type: str | None = Field( |
| 42 | + default=None, |
| 43 | + description="""The type of violation (if present in the |
| 44 | + message)""", |
| 45 | + ) |
| 46 | + required_changes: str | None = Field( |
| 47 | + default=None, |
| 48 | + description="""The required changes specified by the entity |
| 49 | + (if present in the message)""", |
| 50 | + ) |
| 51 | + compliance_deadline_str: str | None = Field( |
| 52 | + default=None, |
| 53 | + exclude=True, |
| 54 | + repr=False, |
| 55 | + description="""The date that the company must comply (if any) |
| 56 | + reformatted to match YYYY-mm-dd""", |
| 57 | + ) |
| 58 | + max_potential_fine: float | None = Field( |
| 59 | + default=None, |
| 60 | + description="""The maximum potential fine |
| 61 | + (if any)""", |
| 62 | + ) |
| 63 | + |
| 64 | + @staticmethod |
| 65 | + def _convert_string_to_date(date_str: str | None) -> date | None: |
| 66 | + try: |
| 67 | + return datetime.strptime(date_str, "%Y-%m-%d").date() |
| 68 | + except Exception as e: |
| 69 | + print(e) |
| 70 | + return None |
| 71 | + |
| 72 | + @computed_field |
| 73 | + @property |
| 74 | + def date_of_notice(self) -> date | None: |
| 75 | + return self._convert_string_to_date(self.date_of_notice_str) |
| 76 | + |
| 77 | + @computed_field |
| 78 | + @property |
| 79 | + def compliance_deadline(self) -> date | None: |
| 80 | + return self._convert_string_to_date(self.compliance_deadline_str) |
| 81 | + |
| 82 | + |
| 83 | +info_parse_prompt = ChatPromptTemplate.from_messages( |
| 84 | + [ |
| 85 | + ( |
| 86 | + "system", |
| 87 | + """ |
| 88 | + Parse the date of notice, sending entity name, sending entity |
| 89 | + phone, sending entity email, project id, site location, violation |
| 90 | + type, required changes, compliance deadline, and maximum potential |
| 91 | + fine from the message. If any of the fields aren't present, don't |
| 92 | + populate them. Try to cast dates into the YYYY-mm-dd format. Don't |
| 93 | + populate fields if they're not present in the message. |
| 94 | + Here's the notice message: |
| 95 | + {message} |
| 96 | + """, |
| 97 | + ) |
| 98 | + ] |
| 99 | +) |
| 100 | + |
| 101 | +notice_parser_model = ChatOpenAI(model="gpt-4o-mini", temperature=0) |
| 102 | + |
| 103 | +NOTICE_PARSER_CHAIN = ( |
| 104 | + info_parse_prompt |
| 105 | + | notice_parser_model.with_structured_output(NoticeEmailExtract) |
| 106 | +) |
0 commit comments