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 new file mode 100644 index 000000000..69c2b7e57 --- /dev/null +++ b/llm-server/routes/analytics/analytics_service.py @@ -0,0 +1,31 @@ +from shared.models.opencopilot_db.analytics import Analytics +from sqlalchemy.orm import sessionmaker +from shared.models.opencopilot_db import engine + +def upsert_analytics_record(chatbot_id: str, successful_operations: int, total_operations: int, logs: str = ""): + Session = sessionmaker(bind=engine) + session = Session() + + # Fetch the existing record, if any + existing_record = session.query(Analytics).filter_by(chatbot_id=chatbot_id).first() + + 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 2cdfaee18..32c05c42e 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, 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 new file mode 100644 index 000000000..1dfae5884 --- /dev/null +++ b/llm-server/shared/models/opencopilot_db/analytics.py @@ -0,0 +1,20 @@ +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(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, successful_operations, total_operations, logs=None): + self.chatbot_id = chatbot_id + self.successful_operations = successful_operations + self.total_operations = total_operations + self.logs = logs + +Base.metadata.create_all(engine) \ 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