(20241205) LLM endpoint active now.
This commit is contained in:
@@ -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
|
||||
)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user