From 0a336c0938e6b51f8bcaf933d681e7d8bd646ac9 Mon Sep 17 00:00:00 2001 From: khushal Date: Wed, 18 Dec 2024 14:09:14 +0530 Subject: [PATCH] (20241218) E-Mails now get a vendor/client tag when AI thinks the message is from one of those entities. --- controllers/api/mail.py | 39 +++++++++++++++++++++++++++++---------- models/core/ai/llm.py | 6 ++++++ 2 files changed, 35 insertions(+), 10 deletions(-) diff --git a/controllers/api/mail.py b/controllers/api/mail.py index 44b7867..14c1f21 100644 --- a/controllers/api/mail.py +++ b/controllers/api/mail.py @@ -125,9 +125,22 @@ class MailController: { "role": "system", "content": ( - "You're a mail summary expert that summarizes mails in 150 chars or less. " + "You're an expert mail summary program. " + "Provide the response in a structured JSON format with two fields: \"summary\" and \"senderType\". " + "The summary should be 150 chars or less. " "If available, show login info like username and OTPs in your summary." "If no login info is provided, please don't worry; just summarize what you see." + "\"senderType\" must be one of [\"Vendor\", \"Client\", null].\n" + "Example output 1: " + "{\"summary\":\"Sagar Supplies has shipped your materials. They are expected to reach by Thursday. " + "Use OTP 346780 when the delivery agent asks.\",\"senderType\":\"Vendor\"}\n" + "Example output 2: " + "{\"summary\":\"Mr. Mehta is enquiring about the submission of his tax filings, which are to be done " + "today.\",\"senderType\":\"Client\"}\n" + "Example output 3: " + "{\"summary\":\"JustDial's marketing message. They're offering a 35% discount to new accounts.\"," + "\"senderType\":null}\n" + "Remember to respond only with the JSON string, nothing else. Good luck :)" ) } ] @@ -381,7 +394,7 @@ class MailController: message = client_response.data, snippet = client_response.data["subject"], aiSnippet = None, - tags = ["email", "gmail"] + tags = ["Email", "Gmail"] ) # Give a quick indicator of whether this mail is an inbox mail or sent mail: @@ -391,13 +404,19 @@ class MailController: else: mail_message.isSent = True # Invoke the LLM: - ai_snippet = await self.summarize_mail_with_ai( - mongo_conn = mongo_conn, - user_info = user_info, - llm = llm, - message = mail_message - ) - mail_message.aiSnippet = ai_snippet.summary + try: + ai_snippet = await self.summarize_mail_with_ai( + mongo_conn = mongo_conn, + user_info = user_info, + llm = llm, + message = mail_message + ) + ai_json = ai_snippet.json + mail_message.aiSnippet = ai_snippet.summary + mail_message.aiSnippet["output"] = ai_json["summary"] + if ai_json["senderType"] is not None: mail_message.tags.append(ai_json["senderType"]) + except Exception as exception: + current_app.printer(exception) # Done here: sync_result.success = True @@ -540,7 +559,7 @@ class MailController: # ┻ ┗ ┗┗┛┗ ┻ ┗┛┛┗┗ ┛┗┛ # We first load the authorization tokens: - auth_token = await self.get_token( + auth_token = await self.get_token_from_key( mongo_conn = mongo_conn, token_key = token_key, ) diff --git a/models/core/ai/llm.py b/models/core/ai/llm.py index cc43755..247263f 100644 --- a/models/core/ai/llm.py +++ b/models/core/ai/llm.py @@ -40,6 +40,7 @@ from pydantic import BaseModel, Field, field_validator, PastDatetime, AwareDatet from typing import Optional, Literal, Union, List, Any # My utils: +from utils_v2.string import json from utils_v2.string import regex from utils_v2.date_time import date_time @@ -233,6 +234,11 @@ class LLMOutput(BaseModel): "invocationId": self.invocationId } + @property + def json(self) -> dict | list | None: + try: return json.from_string(self.output) + except: return None + # ***************************************************************************************************************** # ***** ****