(20241205) LLM endpoint active now.

This commit is contained in:
2024-12-05 18:09:32 +05:30
parent 0ec57089b0
commit 81550898eb
10 changed files with 347 additions and 383 deletions
+30 -68
View File
@@ -10,7 +10,7 @@
OBJECTIVE:
To receive auth details for various SMS client APIs.
To use LLMs to perform activities like chat completion, text summarization, etc.
REFERENCES:
@@ -59,15 +59,11 @@ from utils_v2.api.async_quart import (
handle_cancelled_request
)
# SMS-related utils:
from utils_v2.sms.nimbus.async_nimbus import AsyncNimbusSMS
from utils_v2.sms.savvy_bulk_sms.async_savvy_bulk_sms import AsyncSavvyBulkSMS
# Common:
from shared import constants
# Data Models:
from models.data.sms.auth import SMSAuthRequestHeaders, SMSAuthRequestData
from models.data.ai.llm import LLMRequestHeaders, LLMInput
# For asynchronous activities:
import asyncio
@@ -81,7 +77,7 @@ import asyncio
# Related to Quart:
sms_auth_bp = Blueprint("sms_auth", __name__)
llm_invoke_bp = Blueprint("llm_invoke", __name__)
# *****************************************************************************************************************
@@ -101,7 +97,7 @@ sms_auth_bp = Blueprint("sms_auth", __name__)
# *****************************************************************************************************************
@sms_auth_bp.record_once
@llm_invoke_bp.record_once
def init(blueprint_setup_state):
# This gets called when the blueprint is registered.
@@ -112,7 +108,7 @@ def init(blueprint_setup_state):
# ---------------------------------------------------------------------------------------------------------------------
@sms_auth_bp.route("/auth", methods = ["POST"])
@llm_invoke_bp.route("/llm/invoke", methods = ["POST"])
@set_api_version(api_version = "1.0.0")
@read_input(sanitize_headers = False, sanitize_data = False)
@get_session_info(key = "X-Session-Token", session_coro = "get_session")
@@ -120,7 +116,7 @@ def init(blueprint_setup_state):
attr_name = "logs_mongo",
project = constants.PROJECT_NAME,
log_type = constants.MODULE_NAME,
operation = "smsAuthApi",
operation = "llmInvokeApi",
log_input = True,
log_output = True,
sensitive_keys = ["sessionToken", "X-Session-Token"]
@@ -128,19 +124,19 @@ def init(blueprint_setup_state):
@log_chain_to_mongo(attr_name = "logs_mongo")
@should_not_be_under_maintenance(attr_name = "is_under_maintenance")
@validate_input(
header_validator = lambda x: SMSAuthRequestHeaders(**x).model_dump(),
data_validator = lambda x: SMSAuthRequestData(**x)
header_validator = lambda x: LLMRequestHeaders(**x).model_dump(),
data_validator = lambda x: LLMInput(**x)
)
@handle_cancelled_request()
async def request_oauth_authorization_url(
inbound_headers: dict | SMSAuthRequestHeaders = None,
inbound_data: dict | SMSAuthRequestData = None,
async def invoke_llm(
inbound_headers: dict | LLMRequestHeaders = None,
inbound_data: dict | LLMInput = None,
inbound_files: dict = None,
**kwargs
):
"""
Use this when a user wants to register a third-party SMS client with your service.
Use this to invoke an LLM for text completion kind of activities.
:param inbound_headers: auto-extracted by the decorators.
:param inbound_data: auto-extracted by the decorators.
:param inbound_files: auto-extracted by the decorators.
@@ -160,54 +156,17 @@ async def request_oauth_authorization_url(
http_code = HttpCodes.UNAUTHORIZED
)
# Start by assuming failure:
token_id = None
# ┳ ┓
# ┃┏┓┓┏┏┓┃┏┏┓
# ┻┛┗┗┛┗┛┛┗┗
# ┏┓ ┳┓• ┓ ┏┓┳┳┓┏┓ ┳ ┓•
# ┣ ┏┓┏┓ ┃┃┓┏┳┓┣┓┓┏┏ ┗┓┃┃┃┗┓ ┃┏┓┏┫┓┏┓
# ┻ ┗┛┛ ┛┗┗┛┗┗┗┛┗┻┛ ┗┛┛ ┗┗┛ ┻┛┗┗┻┗┗┻
if inbound_data.messageClient == "nimbusSmsIndia":
token_id = await current_app.sms_auth_model.set(
db_conn = current_app.sql_writer,
mongo_conn = current_app.data_mongo,
user_info = kwargs["session_info"],
client_user_id = {
"userId": inbound_data.auth.userId,
"senderId": inbound_data.auth.senderId,
"entityId": inbound_data.auth.entityId
},
auth = inbound_data.auth.model_dump(),
token = None,
service_client = inbound_data.messageClient,
auth_type = "auth",
sync_freq = 300,
session_token = inbound_headers["X-Session-Token"]
)
# ┏┓ ┏┓ ┳┓ ┓┓ ┏┓┳┳┓┏┓ ┓┏┓
# ┣ ┏┓┏┓ ┗┓┏┓┓┏┓┏┓┏ ┣┫┓┏┃┃┏ ┗┓┃┃┃┗┓ ┃┫ ┏┓┏┓┓┏┏┓
# ┻ ┗┛┛ ┗┛┗┻┗┛┗┛┗┫ ┻┛┗┻┗┛┗ ┗┛┛ ┗┗┛ ┛┗┛┗ ┛┗┗┫┗┻
# ┛ ┛
if inbound_data.messageClient == "savvyBulkSmsKenya":
token_id = await current_app.sms_auth_model.set(
db_conn = current_app.sql_writer,
mongo_conn = current_app.data_mongo,
user_info = kwargs["session_info"],
client_user_id = {
"partnerId": inbound_data.auth.partnerId,
"shortCode": inbound_data.auth.shortCode
},
auth = inbound_data.auth.model_dump(),
token = None,
service_client = inbound_data.messageClient,
auth_type = "auth",
sync_freq = 300,
session_token = inbound_headers["X-Session-Token"]
)
# Call the LLM and see if its service worked or not:
llm_response = await current_app.llm.invoke(
mongo_conn = current_app.data_mongo,
user_info = kwargs["session_info"],
llm_input = inbound_data
)
success = False if llm_response.output is None else True
# ┳┓
# ┣┫┏┓┏┏┓┏┓┏┓┏┏┓
@@ -216,12 +175,15 @@ async def request_oauth_authorization_url(
# Done here:
return ResponseModel(
status_code = StatusCodes.OK if token_id else StatusCodes.FAILED,
http_code = HttpCodes.SUCCESS if token_id else HttpCodes.INTERNAL_SERVER_ERROR,
status_code = StatusCodes.OK if success else StatusCodes.FAILED,
http_code = HttpCodes.SUCCESS if success else HttpCodes.INTERNAL_SERVER_ERROR,
data = {
"messageClient": inbound_data.messageClient,
"authorized": True
}
"ts": llm_response.ts.isoformat(),
"client": llm_response.client,
"model": llm_response.model,
"output": llm_response.output,
"tokens": llm_response.tokens.model_dump(),
} if success else None
)
+1 -1
View File
@@ -247,7 +247,7 @@ async def handle_gmail_callback() -> render_template:
@log_chain_to_mongo(attr_name = "logs_mongo")
@should_not_be_under_maintenance(attr_name = "is_under_maintenance")
@handle_cancelled_request()
async def mail_callback(
async def mail_auth_callback(
mail_client: str = None,
inbound_headers: dict = None,
inbound_data: dict = None,
+4
View File
@@ -126,6 +126,7 @@ def init(blueprint_setup_state):
async def sync_mails(
user_info: dict,
mongo_conn: AsyncMongo,
llm: ChatOpenAI,
inbound_headers: dict,
@@ -145,6 +146,7 @@ async def sync_mails(
# Try to sync the mails:
return await current_app.mail_sync_model.sync(
session_token = inbound_headers["X-Session-Token"],
user_info = user_info,
mongo_conn = mongo_conn,
token_id = inbound_data.tokenId,
llm = llm,
@@ -213,6 +215,7 @@ async def sync_mail(
if mode in ["background", "bg"]:
current_app.add_background_task(
sync_mails,
user_info = kwargs["session_info"],
mongo_conn = current_app.data_mongo,
llm = current_app.llm,
inbound_headers = inbound_headers,
@@ -226,6 +229,7 @@ async def sync_mail(
# Otherwise we process it right here:
sync_results = await sync_mails(
user_info = kwargs["session_info"],
mongo_conn = current_app.data_mongo,
llm = current_app.llm,
inbound_headers = inbound_headers,
+3 -3
View File
@@ -180,7 +180,7 @@ async def request_oauth_authorization_url(
},
auth = inbound_data.auth.model_dump(),
token = None,
service_client = inbound_data.messageClient,
service_client = inbound_data.smsClient,
auth_type = "auth",
sync_freq = 300,
session_token = inbound_headers["X-Session-Token"]
@@ -203,7 +203,7 @@ async def request_oauth_authorization_url(
},
auth = inbound_data.auth.model_dump(),
token = None,
service_client = inbound_data.messageClient,
service_client = inbound_data.smsClient,
auth_type = "auth",
sync_freq = 300,
session_token = inbound_headers["X-Session-Token"]
@@ -219,7 +219,7 @@ async def request_oauth_authorization_url(
status_code = StatusCodes.OK if token_id else StatusCodes.FAILED,
http_code = HttpCodes.SUCCESS if token_id else HttpCodes.INTERNAL_SERVER_ERROR,
data = {
"messageClient": inbound_data.messageClient,
"smsClient": inbound_data.messageClient,
"authorized": True
}
)
+15 -7
View File
@@ -75,6 +75,7 @@ from models.behaviour.mail.oauth_v2 import MailOAuthModel
from models.behaviour.mail.sync_v2 import MailSyncModel
from models.behaviour.mail.retrieve import MailRetrieveModel
from models.behaviour.sms.auth import SMSAuthModel
from models.behaviour.ai.llm.open_ai import LLMOpenAI
# To make REST API calls:
import httpx
@@ -91,13 +92,11 @@ from api.blueprints.mail.retrieve import mail_retrieve_bp
from api.blueprints.sms.auth import sms_auth_bp
from api.blueprints.tech.chat_alerts import tech_chat_alert_bp
from api.blueprints.test.callback import test_callback_bp
from api.blueprints.ai.llm.invoke import llm_invoke_bp
# All the helpers:
from api.helpers.user import session
# To work with LLMs:
from langchain_openai import ChatOpenAI
# *****************************************************************************************************************
# ***** ****
@@ -129,6 +128,7 @@ app.register_blueprint(mail_retrieve_bp, url_prefix = f"/{MODULE_BASE}/mail")
app.register_blueprint(sms_auth_bp, url_prefix = f"/{MODULE_BASE}/sms")
app.register_blueprint(tech_chat_alert_bp, url_prefix = f"/{MODULE_BASE}/tech/alert")
app.register_blueprint(test_callback_bp, url_prefix = f"/{MODULE_BASE}/test")
app.register_blueprint(llm_invoke_bp, url_prefix = f"/{MODULE_BASE}/ai")
# *****************************************************************************************************************
@@ -200,7 +200,7 @@ async def app_startup(**kwargs):
pool = 120.0, # .... Time to wait for a free connection from the pool.
connect = 2.5, # ... Time to wait for establishing a connection to the server.
write = 10.0, # .... Time to wait for sending data.
read = 2.5 # ....... Time to wait for receiving data.
read = 9.9 # ....... Time to wait for receiving data.
)
)
@@ -374,9 +374,17 @@ async def app_startup(**kwargs):
# ┛
# For LLMs:
current_app.llm = ChatOpenAI(
model = script_cred["openAi"]["model"],
openai_api_key = script_cred["openAi"]["openai_api_key"]
current_app.llm = LLMOpenAI(
llm_creds = {
"model": script_cred["openAi"]["model"],
"openai_api_key": script_cred["openAi"]["openai_api_key"]
},
cache = current_app.module_cache,
alert_url = current_app.script_data["alerts"]["url"],
http_client = current_app.http_client,
debug = enable_debugging,
debug_prefix = "AI (LLM) | ",
debug_only_errors = True
)
current_app.printer("AI ready.")