-
Notifications
You must be signed in to change notification settings - Fork 0
feat: enhance AI parsing and prompts with company details integration #83
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
1906196
df95e1d
5d45c3d
6480774
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -34,10 +34,27 @@ def get_system_prompt(document_schema: dict) -> str: | |
| {document_schema}""" | ||
|
|
||
|
|
||
| def get_user_prompt(document_type: str, document_data: str) -> str: | ||
| def get_user_prompt( | ||
| document_type: str, document_data: str, company_info: str = "" | ||
| ) -> str: | ||
| input_doc_type = INPUT_DOCUMENTS.get(document_type, "document") | ||
|
|
||
| return f"""Generate {document_type} for given {input_doc_type} according to above JSON schema. | ||
| company_context = "" | ||
| if company_info: | ||
| if document_type == "Sales Order": | ||
| role_hint = "Use this to correctly identify the company as the seller/vendor and the other party as the customer/buyer." | ||
| else: | ||
| role_hint = "Use this to correctly identify the company as the buyer/recipient and the other party as the vendor/supplier." | ||
|
Comment on lines
+44
to
+47
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The role hint currently branches on exactly Consider aligning this with the SELLER_DOCUMENT_TYPES = {"Sales Order"}
...
if document_type in SELLER_DOCUMENT_TYPES:
role_hint = "Use this to correctly identify the company as the seller/vendor and the other party as the customer/buyer."
else:
role_hint = "Use this to correctly identify the company as the buyer/recipient and the other party as the vendor/supplier." |
||
|
|
||
| company_context = f""" | ||
|
|
||
| This {input_doc_type} is received by the following company: | ||
| {company_info} | ||
|
|
||
| {role_hint} | ||
| """ | ||
|
|
||
| return f"""Generate {document_type} for the given {input_doc_type} according to above JSON schema.{company_context} | ||
| Document data is given below: | ||
| {document_data}""" | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -50,4 +50,4 @@ | |
| "sort_field": "modified", | ||
| "sort_order": "DESC", | ||
| "states": [] | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,6 +13,18 @@ def on_update(doc, method=None): | |
| if not (settings.enabled and settings.parse_incoming_emails): | ||
| return | ||
|
|
||
| matched_account = next( | ||
| ( | ||
| row | ||
| for row in settings.incoming_email_accounts | ||
| if row.to_email in doc.recipients | ||
| ), | ||
| None, | ||
| ) | ||
|
|
||
| if not matched_account: | ||
| return | ||
|
Comment on lines
+16
to
+26
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Moving the This is a meaningful breaking change for any installation that has If the intent is intentional (i.e. you always need a matched account to obtain # matched_account must be resolved before any processing path so we can
# derive the correct company for both the general and party-email flows.
matched_account = next(
(
row
for row in settings.incoming_email_accounts
if row.to_email in doc.recipients
),
None,
)
if not matched_account:
return |
||
|
|
||
| if settings.parse_party_emails: | ||
| matched_party_config = next( | ||
| (row for row in settings.party_emails if row.party_email == doc.sender), | ||
|
|
@@ -41,21 +53,10 @@ def on_update(doc, method=None): | |
| settings, | ||
| default_user, | ||
| matched_party_config.party, | ||
| matched_account.company, | ||
| ) | ||
| return | ||
|
|
||
| matched_account = next( | ||
| ( | ||
| row | ||
| for row in settings.incoming_email_accounts | ||
| if row.to_email in doc.recipients | ||
| ), | ||
| None, | ||
| ) | ||
|
|
||
| if not matched_account: | ||
| return | ||
|
|
||
| # Attachments are not available when the Communication doc is created. | ||
| # Next time the doc is updated, we will check for attachments, | ||
| # and update the flag `is_processed_by_transaction_parser` accordingly. | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
company_address_displaymay contain unresolved HTML entitiesstrip_htmlremoves HTML tags, but it does not decode HTML entities such as&, ,', etc. If the address stored in Frappe contains these entities (which is common for addresses stored as rich text), they will appear verbatim in the AI prompt, potentially confusing the model.Consider adding an HTML entity decode step: