(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
+123 -52
View File
@@ -10,7 +10,7 @@
OBJECTIVE:
To provide a structure to receive auth details of various SMS providers.
To provide a structure to normalize input to and output from a standardized LLM wrapper.
REFERENCES:
@@ -36,8 +36,8 @@ sys.path.append(".")
sys.path.append("..")
# For making data behaviour_models:
from pydantic import BaseModel, Field, field_validator, PastDatetime
from typing import Optional, Literal, Union
from pydantic import BaseModel, Field, field_validator, PastDatetime, AwareDatetime
from typing import Optional, Literal, Union, List
# My utils:
from utils_v2.string import regex
@@ -75,30 +75,15 @@ REGEX_SESSION_TOKEN = r"^[a-f0-9]{8}-[a-f0-9]{4}-[1-5][a-f0-9]{3}-[89ab][a-f0-9]
# *****************************************************************************************************************
class NimbusSMSIndiaAuth(BaseModel):
class LLMInputMessage(BaseModel):
entityId: str = Field(
description = "the entity id as registered with DLT",
min_length = 1,
role: Literal["system", "ai", "human"] = Field(
description = "the role of this message",
frozen = True
)
senderId: str = Field(
description = "the 6-char code that you see in your SMS inbox",
min_length = 1,
frozen = True,
examples = ["HDFCBK", "NSESMS", "ZRODHA"]
)
userId: str = Field(
description = "the 6-digit id that Nimbus has assigned to you",
min_length = 1,
frozen = True
)
apiKey: str = Field(
description = "the key generated through Nimbus's portal",
min_length = 1,
content: str = Field(
description = "the message sent by the 'role'",
frozen = True
)
@@ -114,23 +99,63 @@ class NimbusSMSIndiaAuth(BaseModel):
# ---------------------------------------------------------------------------------------------------------------------
class SavvyBulkSMSKenyaAuth(BaseModel):
class LLMInput(BaseModel):
apiKey: str = Field(
description = "the key generated through Savvy's portal",
min_length = 1,
messages: List[LLMInputMessage]
# ┏┓ ┏•
# ┃ ┏┓┏┓╋┓┏┓
# ┗┛┗┛┛┗┛┗┗┫
# ┛
class Config:
extra = "forbid"
# ┓┏ ┓• ┓ •
# ┃┃┏┓┃┓┏┫┏┓╋┓┏┓┏┓
# ┗┛┗┻┗┗┗┻┗┻┗┗┗┛┛┗
@field_validator("messages")
def validate_messages(cls, value):
# Maintain counter(s):
system_message_index = -1
system_message_count = 0
# Loop through the messages and check them:
for index, message in enumerate(value):
# For 'system' messages:
if message.role == "system":
system_message_index = index
system_message_count += 1
# Verify that there is AT MOST ONE 'system' message,
# and verify that the 'system' message is the first message:
if system_message_count > 1: raise ValueError(f"there can be at most 1 'system' message, found {system_message_count}")
if system_message_index > 0: raise ValueError(f"'system' message must always be at index 0, found it at index {system_message_index}")
# Done here:
return value
# ---------------------------------------------------------------------------------------------------------------------
class LLMUsageTokens(BaseModel):
input: int = Field(
description = "how many tokens were given in the input",
frozen = True
)
partnerId: str = Field(
description = "the key generated through Savvy's portal",
min_length = 1,
output: int = Field(
description = "how many tokens were generated as the output",
frozen = True
)
shortCode: str = Field(
description = "your short code with Savvy",
min_length = 1,
total: int = Field(
description = "the sum of the input and output tokens",
frozen = True
)
@@ -146,7 +171,54 @@ class SavvyBulkSMSKenyaAuth(BaseModel):
# ---------------------------------------------------------------------------------------------------------------------
class SMSAuthRequestHeaders(BaseModel):
class LLMOutput(BaseModel):
ts: AwareDatetime = Field(
description = "the time at which the llm was invoked",
default_factory = date_time.get_current_utc_date_time,
frozen = True
)
messages: List[LLMInputMessage] = Field(
description = "the messages that came in that invoked the llm",
frozen = True
)
output: str | None = Field(
description = "what the llm generated",
default = None,
frozen = True
)
client: Literal["openai"] = Field(
description = "the co./brand that was used to use an llm",
frozen = True
)
model: str = Field(
description = "to know which model used in the process",
frozen = True
)
tokens: LLMUsageTokens = Field(
description = "to know how many tokens were used in the process",
default = LLMUsageTokens(input = 0, output = 0, total = 0),
frozen = True
)
# ┏┓ ┏•
# ┃ ┏┓┏┓╋┓┏┓
# ┗┛┗┛┛┗┛┗┗┫
# ┛
class Config:
extra = "forbid"
# ---------------------------------------------------------------------------------------------------------------------
class LLMRequestHeaders(BaseModel):
sessionToken: str = Field(
description = "the session token of the user who is requesting the service",
@@ -167,23 +239,6 @@ class SMSAuthRequestHeaders(BaseModel):
return super().model_dump(*args, by_alias = True, **kwargs)
# ---------------------------------------------------------------------------------------------------------------------
class SMSAuthRequestData(BaseModel):
messageClient: Literal["nimbusSmsIndia", "savvyBulkSmsKenya"]
auth: Union[NimbusSMSIndiaAuth, SavvyBulkSMSKenyaAuth]
# ┏┓ ┏•
# ┃ ┏┓┏┓╋┓┏┓
# ┗┛┗┛┛┗┛┗┗┫
# ┛
class Config:
extra = "forbid"
# *****************************************************************************************************************
# ***** ****
# *** MAIN PROGRAM ***
@@ -193,4 +248,20 @@ class SMSAuthRequestData(BaseModel):
if __name__ == "__main__":
pass
llm_messages = [
{
"role": "system",
"content": "You are an office assistant."
},
{
"role": "ai",
"content": "Hello, sir. How may I help you today?"
},
{
"role": "human",
"content": "Please summarize this mail for me..."
}
]
llm_input = LLMInput(messages = llm_messages)
print(llm_input)