(20241212) Reorganizing code to perform core actions in one place.

This commit is contained in:
2024-12-12 18:34:51 +05:30
parent 70c817b3b8
commit fb95e0b38c
50 changed files with 1259 additions and 3166 deletions
View File
+255
View File
@@ -0,0 +1,255 @@
"""
AUTHOR:
Khushal P Soonderji
DATE:
Thursday, 5th Dec., 2024.
OBJECTIVE:
To provide a structure to normalize input to and output from a standardized LLM wrapper.
REFERENCES:
N/A
DOWNLOADS:
N/A
"""
# *****************************************************************************************************************
# ***** ****
# *** IMPORT ***
# ***** ****
# *****************************************************************************************************************
# To make sibling directories accessible for imports:
import sys
sys.path.append(".")
sys.path.append("..")
# For making data behaviour_models:
from pydantic import BaseModel, Field, field_validator, PastDatetime, AwareDatetime
from typing import Optional, Literal, Union, List, Any
# My utils:
from utils_v2.string import regex
from utils_v2.date_time import date_time
# To work with date and time:
import datetime
# *****************************************************************************************************************
# ***** ****
# *** MACROS / ONE-TIME INIT ***
# ***** ****
# *****************************************************************************************************************
# RegEx Patterns:
REGEX_SESSION_TOKEN = r"^[a-f0-9]{8}-[a-f0-9]{4}-[1-5][a-f0-9]{3}-[89ab][a-f0-9]{3}-[a-f0-9]{12}$"
# *****************************************************************************************************************
# ***** ****
# *** VARIABLES ***
# ***** ****
# *****************************************************************************************************************
# --- Nothing Yet
# *****************************************************************************************************************
# ***** ****
# *** FUNCTIONS ***
# ***** ****
# *****************************************************************************************************************
class LLMInputMessage(BaseModel):
role: Literal["system", "ai", "human"] = Field(
description = "the role of this message",
frozen = True
)
content: str = Field(
description = "the message sent by the 'role'",
frozen = True
)
# ┏┓ ┏•
# ┃ ┏┓┏┓╋┓┏┓
# ┗┛┗┛┛┗┛┗┗┫
# ┛
class Config:
extra = "forbid"
# ---------------------------------------------------------------------------------------------------------------------
class LLMInput(BaseModel):
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
)
output: int = Field(
description = "how many tokens were generated as the output",
frozen = True
)
total: int = Field(
description = "the sum of the input and output tokens",
frozen = True
)
# ┏┓ ┏•
# ┃ ┏┓┏┓╋┓┏┓
# ┗┛┗┛┛┗┛┗┗┫
# ┛
class Config:
extra = "forbid"
# ---------------------------------------------------------------------------------------------------------------------
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
)
invocationId: Any | None = Field(
description = "the id of the document that notes this invocation; useful for reconciliation",
frozen = False,
default = None
)
# ┏┓ ┏•
# ┃ ┏┓┏┓╋┓┏┓
# ┗┛┗┛┛┗┛┗┗┫
# ┛
class Config:
extra = "forbid"
# *****************************************************************************************************************
# ***** ****
# *** MAIN PROGRAM ***
# ***** ****
# *****************************************************************************************************************
if __name__ == "__main__":
# 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.model_dump_json(indent = 4))
llm_output = LLMOutput(
messages=[LLMInputMessage(role='system', content="You are an office assistant. It's Christmas, so definitley respond like Santa Claus."), LLMInputMessage(role='ai', content='Hello, sir. How may I help you today?'), LLMInputMessage(role='human', content='Please summarize this mail for me...')],
client = "openai",
model = "o1"
)