basic backend

This commit is contained in:
Niklas Mueller 2024-06-23 12:57:53 +02:00
parent 16e5004228
commit 89ec0476ca
29 changed files with 2125 additions and 13 deletions

View file

@ -0,0 +1,102 @@
""" This module defines the logging component for the LLM interactions.
The data is displayed in the OpenSearch Dashboard.
"""
import os
import uuid
import logging
from opensearch_logger import OpenSearchHandler
from opensearchpy import RequestsHttpConnection
class DashboardLogger:
"""Logger instance for OpenSearch dashboard"""
def __init__(self, logger_name=None):
if logger_name is None:
# Generate a unique logger name
logger_name = "rag-logs-" + str(uuid.uuid4())
self.logger_instance = self._create_os_logger(logger_name=logger_name)
self.logger_info = {}
def add_information(self, label: str, value):
"""Here you can add information to a given process.
Each information consists of a label and its given value.
Args:
label (str): label
value (any): value
"""
self.logger_info[label] = value
def close_logging(self):
"""Close logging in the final process."""
self.logger_info = self._roll_out_json(original_json=self.logger_info)
if type(self.logger_info) == list:
for logger_json in self.logger_info:
self.logger_instance.info("Logging information", extra=logger_json)
else:
self.logger_instance.info("Logging information", extra=self.logger_info)
def _create_os_logger(self, logger_name):
"""Create a logger which logs on OpenSearch.
Args:
logger_name (str): use some logger name.
Returns:
logger: OpenSearch logger instance.
"""
logger = logging.getLogger(logger_name)
logger.setLevel(logging.INFO)
opensearch_host = os.getenv("VECTOR_STORE_ENDPOINT", "localhost")
opensearch_port = os.getenv("VECTOR_STORE_PORT", "9200")
handler = OpenSearchHandler(
index_name="rag-logs",
hosts=[f"http://{opensearch_host}:{opensearch_port}"],
http_auth=("admin", "admin"),
use_ssl=os.getenv("OPENSEARCH_USE_SSL", "False").lower()
in ["true", "1", "yes", "y"],
verify_certs=False,
connection_class=RequestsHttpConnection,
)
logger.addHandler(handler)
return logger
def _roll_out_json(self, original_json):
"""Roll out JSON with lists into individual JSONs with additional attributes.
Args:
original_json: The original JSON with lists in 'sources' and 'passages' attributes.
Return
rolled_out_jsons: List of rolled out JSONs.
"""
rolled_out_jsons = []
# Iterate through each item in the 'sources' and 'passages' lists
for rank, (source, passage) in enumerate(
zip(original_json["sources"], original_json["passages"]), start=1
):
# Create a new JSON for each pair of source and passage
new_json = {
"query": original_json["query"],
"source": source["source"],
"page": source["page"],
# NOTE we need to cut. Otherwise the visualization wont work with these many chars
"passage": passage[:250],
"answer": original_json["answer"][:250],
"language": original_json["language"],
"model": original_json["model"],
"rank": rank, # Assign the rank
}
rolled_out_jsons.append(new_json)
return rolled_out_jsons

View file

@ -0,0 +1,46 @@
""" This module defines the logging component."""
import logging
def create_logger(log_level: str, logger_name: str = "custom_logger"):
"""Create a logging based on logger.
Args:
log_level (str): Kind of logging
logger_name (str, optional): Name of logger
Returns:
logger: returns logger
"""
logger = logging.getLogger(logger_name)
logger.setLevel(logging.DEBUG) # Set the base logging level to the lowest (DEBUG)
# If logger already has handlers, don't add a new one
if logger.hasHandlers():
logger.handlers.clear()
# Create a console handler and set the level based on the input
console_handler = logging.StreamHandler()
if log_level == "DEBUG":
console_handler.setLevel(logging.DEBUG)
elif log_level == "INFO":
console_handler.setLevel(logging.INFO)
elif log_level == "WARNING":
console_handler.setLevel(logging.WARNING)
elif log_level == "ERROR":
console_handler.setLevel(logging.ERROR)
else:
raise ValueError("Invalid log level provided")
# Create a formatter and set it for the console handler
formatter = logging.Formatter(
"%(asctime)s - %(levelname)s [%(name)s] - %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
console_handler.setFormatter(formatter)
# Add the console handler to the logger
logger.addHandler(console_handler)
return logger