(20241219) SMS module fully re-organized.

This commit is contained in:
2024-12-19 17:26:18 +05:30
parent d89a88ab8e
commit 79b771b48c
28 changed files with 1086 additions and 827 deletions
+105 -8
View File
@@ -50,10 +50,11 @@ from controllers.base import BaseModel
from models.core.user import CoreUserInfoModel
from models.core.auth_token import CoreAuthTokenModel
from models.core.message import CoreMessageModel
from models.api.mail.sync import MailSyncOneResult, MailSyncManyResults
from models.api.mail.sync import MailSyncOneResult, MailSyncManyResults, MailSendOneResult
from models.api.mail.send import MailSendRequestData
# Mail Clients:
from utils_v2.goog.gmail.gmail_client import AsyncGMailClient
from utils_v2.goog.controllers.gmail.gmail_client import AsyncGMailClient
from utils_v2.goog.models.auth_tokens import GoogleAuthTokens
# To work with MongoDB:
@@ -121,7 +122,7 @@ class MailController:
# ┗┛┗┗┻┛┛ ┗┛┗┻┛ ┛
# For AI Magic through LLMs:
PROMPT_TEMPLATE = [
RECEIVED_MAIL_PROMPT_TEMPLATE = [
{
"role": "system",
"content": (
@@ -140,7 +141,17 @@ class MailController:
"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 :)"
"Remember to respond only with the raw JSON string, nothing else. Good luck :)"
)
}
]
SENT_MAIL_PROMPT_TEMPLATE = [
{
"role": "system",
"content": (
"You're an expert mail summary assistant. That summarizes sent mails in 150 chars or less. "
"The objective of your user is to be able to recollect what a mail they sent was about from a brief "
"summary. Reply in a simple string, no formatting is allowed except emojis. Good luck :)"
)
}
]
@@ -205,7 +216,8 @@ class MailController:
mongo_conn: AsyncMongo,
user_info: CoreUserInfoModel,
llm: CoreLLMController,
message: CoreMessageModel
message: CoreMessageModel,
prompt_template: List[dict]
) -> LLMOutput:
# Extract the text from the message here:
@@ -217,7 +229,7 @@ class MailController:
mongo_conn = mongo_conn,
user_info = user_info,
llm_input = LLMInput(
messages = self.PROMPT_TEMPLATE + [
messages = prompt_template + [
{
"role": "human",
"content": f"Please summarize this mail: \"\"\"{text}\"\"\""
@@ -409,7 +421,8 @@ class MailController:
mongo_conn = mongo_conn,
user_info = user_info,
llm = llm,
message = mail_message
message = mail_message,
prompt_template = self.PROMPT_TEMPLATE
)
ai_json = ai_snippet.json
mail_message.aiSnippet = ai_snippet.summary
@@ -566,7 +579,7 @@ class MailController:
# If we failed to load the authorization tokens:
if not auth_token:
sync_results.message = f"no such token key '{token_key}'"
sync_results.message = f"no such token key"
return sync_results
# ┏┓ ┏┓┳┳┓ •┓
@@ -596,6 +609,90 @@ class MailController:
sync_results.message = f"no such mail client '{auth_token.client}'"
return sync_results
# ┏┓ ┓ ┳┳┓
# ┗┓┏┓┏┓┏┫ ┃┃┃┏┓┏┏┏┓┏┓┏┓┏
# ┗┛┗ ┛┗┗┻ ┛ ┗┗ ┛┛┗┻┗┫┗ ┛
# ┛
async def __send_one_gmail(
self,
db_conn: AsyncMySQL,
mongo_conn: AsyncMongo,
user_info: CoreUserInfoModel,
token_key: ObjectId | str,
inbound_data: MailSendRequestData,
inbound_files: dict,
llm: CoreLLMController = None,
session_token: str = None
) -> MailSendOneResult:
# Start by assuming failure:
sync_results = MailSendOneResult()
# ┏┓ ┏┳┓┓ ┳┳┓
# ┃ ┏┓┏┓┏╋┏┓┓┏┓┏┏╋ ┃ ┣┓┏┓ ┃┃┃┏┓┏┏┏┓┏┓┏┓
# ┗┛┗┛┛┗┛┗┛ ┗┻┛┗┗┗ ┻ ┛┗┗ ┛ ┗┗ ┛┛┗┻┗┫┗
# ┛
gmail_message = None
async def send_one_mail(
self,
db_conn: AsyncMySQL,
mongo_conn: AsyncMongo,
user_info: CoreUserInfoModel,
token_key: ObjectId | str,
inbound_data: MailSendRequestData,
inbound_files: dict,
llm: CoreLLMController = None,
session_token: str = None
) -> MailSendOneResult:
# Start by assuming failure:
sync_results = MailSendOneResult()
# ┏┓ ┓ ┏┳┓ ┓
# ┣ ┏┓╋┏┣┓ ┃ ┏┓┃┏┏┓┏┓┏
# ┻ ┗ ┗┗┛┗ ┻ ┗┛┛┗┗ ┛┗┛
# We first load the authorization tokens:
auth_token = await self.get_token_from_key(
mongo_conn = mongo_conn,
token_key = token_key,
)
# If we failed to load the authorization tokens:
if not auth_token:
sync_results.message = f"no such token key"
return sync_results
# ┏┓ ┏┓┳┳┓ •┓
# ┣ ┏┓┏┓ ┃┓┃┃┃┏┓┓┃
# ┻ ┗┛┛ ┗┛┛ ┗┗┻┗┗
# if auth_token.client == "gmail":
# return await self.__send_one_gmail(
# db_conn = db_conn,
# mongo_conn = mongo_conn,
# user_info = user_info,
# auth_token = auth_token,
# mail_client = current_app.gmail_client,
# llm = llm,
# force_sync = force_sync,
# start_date = start_date,
# end_date = end_date,
# max_count = max_count,
# session_token = session_token,
# )
# ┳ ┓• ┓ ┏┓┓•
# ┃┏┓┓┏┏┓┃┓┏┫ ┃ ┃┓┏┓┏┓╋
# ┻┛┗┗┛┗┻┗┗┗┻ ┗┛┗┗┗ ┛┗┗
# If we haven't been able to sync mail due to not entering any 'if' condition:
sync_results.message = f"no such mail client '{auth_token.client}'"
return sync_results
# ┓ • ┏┓ ┏┓ ┳┳┓
# ┃ ┓┏╋ ┣╋ ┃┓┏┓╋ ┃┃┃┏┓┏┏┏┓┏┓┏┓┏
# ┗┛┗┛┗ ┗┻ ┗┛┗ ┗ ┛ ┗┗ ┛┛┗┻┗┫┗ ┛