From 0663aaa4cb169c3305ebea48f8e3ee7e52e85384 Mon Sep 17 00:00:00 2001 From: shanurrahman Date: Fri, 8 Dec 2023 19:33:05 +0300 Subject: [PATCH 1/3] Adding analytics service to track chat behaviour --- .../routes/analytics/analytics_service.py | 25 +++++++++++++++++++ .../shared/models/opencopilot_db/analytics.py | 16 ++++++++++++ .../models/opencopilot_db/database_setup.py | 2 +- 3 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 llm-server/routes/analytics/analytics_service.py create mode 100644 llm-server/shared/models/opencopilot_db/analytics.py diff --git a/llm-server/routes/analytics/analytics_service.py b/llm-server/routes/analytics/analytics_service.py new file mode 100644 index 000000000..78669f853 --- /dev/null +++ b/llm-server/routes/analytics/analytics_service.py @@ -0,0 +1,25 @@ +from shared.models.opencopilot_db.analytics import Analytics +from sqlalchemy.orm import sessionmaker +from shared.models.opencopilot_db import engine +from sqlalchemy.dialects.mysql import insert + +def upsert_analytics_record(chatbot_id: str, successful_operations: int, total_operations: int, user_id: str): + Session = sessionmaker(bind=engine) + session = Session() + + insert_stmt = insert(Analytics).values( + chatbot_id=chatbot_id, + successful_operations=successful_operations, + total_operations=total_operations, + user_id=user_id + ) + + session.execute(insert_stmt) + session.commit() + + if session.query(Analytics).filter_by(chatbot_id=chatbot_id).count() > 0: + print("Analytics record updated for chatbot_id:", chatbot_id) + else: + print("Analytics record inserted for chatbot_id:", chatbot_id) + + session.close() \ No newline at end of file diff --git a/llm-server/shared/models/opencopilot_db/analytics.py b/llm-server/shared/models/opencopilot_db/analytics.py new file mode 100644 index 000000000..7d4883147 --- /dev/null +++ b/llm-server/shared/models/opencopilot_db/analytics.py @@ -0,0 +1,16 @@ +from sqlalchemy import Column, Integer, String +from sqlalchemy.ext.declarative import declarative_base + +Base = declarative_base() + +class Analytics(Base): + __tablename__ = 'analytics' + + chatbot_id = Column(String, primary_key=True) + successful_operations = Column(Integer) + total_operations = Column(Integer) + user_id = Column(String) + + def __init__(self, chatbot_id, user_id): + self.chatbot_id = chatbot_id + self.user_id = user_id \ No newline at end of file diff --git a/llm-server/shared/models/opencopilot_db/database_setup.py b/llm-server/shared/models/opencopilot_db/database_setup.py index ef6f01d8d..f62492b7b 100644 --- a/llm-server/shared/models/opencopilot_db/database_setup.py +++ b/llm-server/shared/models/opencopilot_db/database_setup.py @@ -16,7 +16,7 @@ def connection_creator(): # Create an SQLAlchemy engine with the connection pool engine = create_engine(db_url, poolclass=QueuePool, pool_logging_name="worker_pool") -pool = QueuePool(creator=connection_creator, pool_size=5) +pool = QueuePool(creator=connection_creator, pool_size=20) def create_database_schema(): Base.metadata.create_all(engine) \ No newline at end of file From 3289cfd04cca090e5d274ff76989657da1e2c3b9 Mon Sep 17 00:00:00 2001 From: shanurrahman Date: Fri, 8 Dec 2023 19:59:35 +0300 Subject: [PATCH 2/3] binding response to analytics --- llm-server/routes/analytics/analytics_service.py | 3 +-- llm-server/routes/chat/chat_controller.py | 6 +++++- llm-server/shared/models/opencopilot_db/analytics.py | 1 - 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/llm-server/routes/analytics/analytics_service.py b/llm-server/routes/analytics/analytics_service.py index 78669f853..249d03342 100644 --- a/llm-server/routes/analytics/analytics_service.py +++ b/llm-server/routes/analytics/analytics_service.py @@ -3,7 +3,7 @@ from shared.models.opencopilot_db import engine from sqlalchemy.dialects.mysql import insert -def upsert_analytics_record(chatbot_id: str, successful_operations: int, total_operations: int, user_id: str): +def upsert_analytics_record(chatbot_id: str, successful_operations: int, total_operations: int): Session = sessionmaker(bind=engine) session = Session() @@ -11,7 +11,6 @@ def upsert_analytics_record(chatbot_id: str, successful_operations: int, total_o chatbot_id=chatbot_id, successful_operations=successful_operations, total_operations=total_operations, - user_id=user_id ) session.execute(insert_stmt) diff --git a/llm-server/routes/chat/chat_controller.py b/llm-server/routes/chat/chat_controller.py index 2cdfaee18..7030085f7 100644 --- a/llm-server/routes/chat/chat_controller.py +++ b/llm-server/routes/chat/chat_controller.py @@ -1,6 +1,7 @@ from typing import cast from flask import jsonify, Blueprint, request, Response, abort, Request +from routes.analytics.analytics_service import upsert_analytics_record from routes.chat.chat_dto import ChatInput from utils.get_logger import CustomLogger from utils.llm_consts import X_App_Name @@ -160,13 +161,16 @@ async def send_chat(): ) if response_data["response"]: + upsert_analytics_record(chatbot_id=str(bot.id), successful_operations=1, total_operations=1) create_chat_history(str(bot.id), session_id, True, message) create_chat_history( str(bot.id), session_id, False, - response_data["response"] or response_data["error"], + response_data["response"] or response_data["error"] or "", ) + elif response_data["error"]: + upsert_analytics_record(chatbot_id=str(bot.id), successful_operations=0, total_operations=1) return jsonify( {"type": "text", "response": {"text": response_data["response"]}} diff --git a/llm-server/shared/models/opencopilot_db/analytics.py b/llm-server/shared/models/opencopilot_db/analytics.py index 7d4883147..358631ed0 100644 --- a/llm-server/shared/models/opencopilot_db/analytics.py +++ b/llm-server/shared/models/opencopilot_db/analytics.py @@ -9,7 +9,6 @@ class Analytics(Base): chatbot_id = Column(String, primary_key=True) successful_operations = Column(Integer) total_operations = Column(Integer) - user_id = Column(String) def __init__(self, chatbot_id, user_id): self.chatbot_id = chatbot_id From a6bc7ec79b298bd80196a2efe76cc6b45f58e609 Mon Sep 17 00:00:00 2001 From: shanurrahman Date: Fri, 8 Dec 2023 21:34:28 +0300 Subject: [PATCH 3/3] upsert operation complete --- llm-server/Dockerfile | 2 +- .../routes/analytics/analytics_service.py | 29 ++++++++++++------- llm-server/routes/chat/chat_controller.py | 2 +- .../shared/models/opencopilot_db/analytics.py | 21 +++++++++----- 4 files changed, 33 insertions(+), 21 deletions(-) diff --git a/llm-server/Dockerfile b/llm-server/Dockerfile index fe7cf7fe9..b1c1bafc3 100644 --- a/llm-server/Dockerfile +++ b/llm-server/Dockerfile @@ -2,7 +2,7 @@ FROM python:3.9-slim AS common WORKDIR /app COPY requirements.txt /app/ -RUN pip install --no-cache-dir -r requirements.txt +RUN pip install -r requirements.txt COPY . /app/ # Development stage diff --git a/llm-server/routes/analytics/analytics_service.py b/llm-server/routes/analytics/analytics_service.py index 249d03342..69c2b7e57 100644 --- a/llm-server/routes/analytics/analytics_service.py +++ b/llm-server/routes/analytics/analytics_service.py @@ -1,24 +1,31 @@ from shared.models.opencopilot_db.analytics import Analytics from sqlalchemy.orm import sessionmaker from shared.models.opencopilot_db import engine -from sqlalchemy.dialects.mysql import insert -def upsert_analytics_record(chatbot_id: str, successful_operations: int, total_operations: int): +def upsert_analytics_record(chatbot_id: str, successful_operations: int, total_operations: int, logs: str = ""): Session = sessionmaker(bind=engine) session = Session() - insert_stmt = insert(Analytics).values( - chatbot_id=chatbot_id, - successful_operations=successful_operations, - total_operations=total_operations, - ) + # Fetch the existing record, if any + existing_record = session.query(Analytics).filter_by(chatbot_id=chatbot_id).first() - session.execute(insert_stmt) - session.commit() - - if session.query(Analytics).filter_by(chatbot_id=chatbot_id).count() > 0: + if existing_record: + # Increment existing values + existing_record.successful_operations += successful_operations + existing_record.total_operations += total_operations + if logs: + existing_record.logs = logs print("Analytics record updated for chatbot_id:", chatbot_id) else: + # Create a new record + record_to_upsert = Analytics( + chatbot_id=chatbot_id, + successful_operations=successful_operations, + total_operations=total_operations, + logs=logs + ) + session.add(record_to_upsert) print("Analytics record inserted for chatbot_id:", chatbot_id) + session.commit() session.close() \ No newline at end of file diff --git a/llm-server/routes/chat/chat_controller.py b/llm-server/routes/chat/chat_controller.py index 7030085f7..32c05c42e 100644 --- a/llm-server/routes/chat/chat_controller.py +++ b/llm-server/routes/chat/chat_controller.py @@ -170,7 +170,7 @@ async def send_chat(): response_data["response"] or response_data["error"] or "", ) elif response_data["error"]: - upsert_analytics_record(chatbot_id=str(bot.id), successful_operations=0, total_operations=1) + upsert_analytics_record(chatbot_id=str(bot.id), successful_operations=0, total_operations=1, logs=response_data["error"]) return jsonify( {"type": "text", "response": {"text": response_data["response"]}} diff --git a/llm-server/shared/models/opencopilot_db/analytics.py b/llm-server/shared/models/opencopilot_db/analytics.py index 358631ed0..1dfae5884 100644 --- a/llm-server/shared/models/opencopilot_db/analytics.py +++ b/llm-server/shared/models/opencopilot_db/analytics.py @@ -1,15 +1,20 @@ -from sqlalchemy import Column, Integer, String -from sqlalchemy.ext.declarative import declarative_base - -Base = declarative_base() - +from sqlalchemy import Column, Integer, String, ForeignKey +from .get_declarative_base import Base +from .database_setup import engine +from .chatbot import Chatbot class Analytics(Base): __tablename__ = 'analytics' - chatbot_id = Column(String, primary_key=True) + chatbot_id = Column(String(36), primary_key=True) + chatbot_id_fk = Column(String(36), ForeignKey(Chatbot.id), nullable=True) successful_operations = Column(Integer) total_operations = Column(Integer) + logs= Column(String(4096), nullable=True) - def __init__(self, chatbot_id, user_id): + def __init__(self, chatbot_id, successful_operations, total_operations, logs=None): self.chatbot_id = chatbot_id - self.user_id = user_id \ No newline at end of file + self.successful_operations = successful_operations + self.total_operations = total_operations + self.logs = logs + +Base.metadata.create_all(engine) \ No newline at end of file